C. Producing Snow
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Alice likes snow a lot! Unfortunately, this year’s winter is already over, and she can’t expect to have any more of it. Bob has thus bought her a gift — a large snow maker. He plans to make some amount of snow every day. On day i he will make a pile of snow of volume Vi and put it in her garden.
Each day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is Ti, each pile will reduce its volume by Ti. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other.
Note that the pile made on day i already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day.
You are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day.
Input
The first line contains a single integer N (1 ≤ N ≤ 105) — the number of days.
The second line contains N integers V1, V2, …, VN (0 ≤ Vi ≤ 109), where Vi is the initial size of a snow pile made on the day i.
The third line contains N integers T1, T2, …, TN (0 ≤ Ti ≤ 109), where Ti is the temperature on the day i.
Output
Output a single line with N integers, where the i-th integer represents the total volume of snow melted on day i.
Examples
inputCopy
3
10 10 5
5 7 2
output
5 12 4
inputCopy
5
30 25 20 15 10
9 10 12 4 13
output
9 20 35 11 25
Note
In the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day.
比赛的时候不会差分数组,强行用线段树模板。。。
代码如下:
#include <bits/stdc++.h>
using namespace std;
#define M INT_MAX
#define N (int)1e5+10
typedef long long ll;
ll jt[N], res[N];
ll arr[N], melt[N];
#define lc root<<1
#define rc root<<1|1
struct seg{
int l, r;
ll he, lazy, tag;
}t[N<<2];
void build(int root, int l, int r)
{
t[root].l = l, t[root].r = r;
t[root].lazy = 0;
t[root].tag = -1;
if (l == r)
{
t[root].he = 0;
return;
}
int m = (l + r) >> 1;
build(lc, l, m);
build(rc, m+1, r);
t[root].he = t[lc].he + t[rc].he;
}
void imptag(int root, ll change)
{
t[root].tag = change;
t[root].he = (t[root].r - t[root].l + 1) * change;
t[root].lazy = 0;
}
void implazy(int root, ll change)
{
t[root].lazy += change;
t[root].he += (t[root].r - t[root].l + 1) * change;
}
void pushdown(int root)
{
ll temp1 = t[root].tag;
if (temp1 != -1)
{
imptag(lc, temp1);
imptag(rc, temp1);
t[root].tag = -1;
}
ll temp2 = t[root].lazy;
if (temp2)
{
implazy(lc, temp2);
implazy(rc, temp2);
t[root].lazy = 0;
}
}
void assignment(int root, int l, int r, ll change)
{
if (r < t[root].l || l > t[root].r) return;
if (l <= t[root].l && t[root].r <= r)
{
imptag(root, change);
return;
}
pushdown(root);
assignment(rc, l, r, change);
assignment(lc, l, r, change);
t[root].he = t[lc].he + t[rc].he;
}
void update(int root, int l, int r, ll change)
{
if (r < t[root].l || l > t[root].r) return;
if (l <= t[root].l && t[root].r <= r)
{
implazy(root, change);
return;
}
pushdown(root);
update(rc, l, r, change);
update(lc, l, r, change);
t[root].he = t[lc].he + t[rc].he;
}
ll query(int root, int l, int r)
{
if (r < t[root].l || l > t[root].r) return 0;
if (l <= t[root].l && t[root].r <= r)
{
return t[root].he;
}
pushdown(root);
return query(rc, l, r) + query(lc, l, r);
}
int lower(int l, int r, int n)
{
int mid;
int q = l;
while (l <= r)
{
mid = (l + r) >> 1;
if (n < melt[mid] - melt[q-1])
{
r = mid - 1;
}
else if (n > melt[mid] - melt[q-1])
{
l = mid + 1;
}
else return mid;
}
if (n < melt[r] - melt[q-1]) return r;
else return l;
}
int main(void)
{
int i;
int n;
ll temp, tt;
scanf("%d", &n);
build(1, 1, n);
for (i = 1; i <= n; i++)
{
scanf("%lld", &arr[i]);
}
temp = 0;
for (i = 1; i <= n; i++)
{
scanf("%lld", &jt[i]);;
temp += jt[i];
melt[i] = temp;
}
for (i = 1; i <= n; i++)
{
int pos = lower(i, n, arr[i]);
if (pos == i) res[i] += arr[i];
else
{
if (pos == n + 1) update(1, i, n, 1);
else if (arr[i] == melt[pos])
{
update(1, i, pos, 1);
}
else
{
update(1, i, pos-1, 1);
res[pos] += arr[i] - melt[pos-1] + melt[i-1];
}
}
}
for (i = 1; i <= n; i++)
{
res[i] += query(1, i, i) * jt[i];
printf("%lld ", res[i]);
}
}
学长教了我差分数组后,重新写了个版本:
差分数组+二分+前缀和:
#include <bits/stdc++.h>
using namespace std;
#define N (int)1e5+10
typedef long long ll;
ll arr[N], melt[N], d[N], sum, res[N];
int main(void)
{
int i, n;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
scanf("%lld", &arr[i]);
}
for (i = 1; i <= n; i++)
{
scanf("%lld", &melt[i]);
melt[i] += melt[i-1];
}
for (i = 1; i <= n; i++)
{
arr[i] += melt[i-1];
int pos = lower_bound(melt+i, melt+n+1, arr[i]) - melt;
if (pos > i)
{
d[i] += 1;
d[pos] -= 1;
res[pos] += arr[i] - melt[pos-1];
}
else res[i] += arr[i] - melt[i-1];
}
for (i = 1; i <= n; i++)
{
sum += d[i];
printf("%lld ", sum * (melt[i] - melt[i-1]) + res[i]);
}
}