http://codeforces.com/contest/948/problem/C
C. Producing Snow
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.
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 a single line with N integers, where the i-th integer represents the total volume of snow melted on day i.
3
10 10 5
5 7 2
5 12 4
5
30 25 20 15 10
9 10 12 4 13
9 20 35 11 25
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.
题意是给定长度n,第一行第i项是第i天造出来的雪的体积Vi,第二行第i项是第i天气温能够融化掉雪的体积Ti,求第i天当天总共融了多少雪。因此对Ti求前缀和可以得到总共融雪体积,对于每一项Vi可以二分前缀和数组得到在第Q天,这堆雪全部融化完,即在[i,Q-1]范围内这堆雪每天融化Ti,用树状数组对它进行区间更新;第Q天融掉这堆雪剩下的量,直接计入答案数组。最后只需要在答案数组中将树状数组单点的值和当天的Ti相乘加进去就可以了。
有两个地方要注意,一个是如果查询到的Q是最后一天,需要特判融化的量应该是Ti和雪剩余量中的较小值;还有一个是查询到的Q如果比当前天i要小不计入计算。
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <string> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #define INF 0x3f3f3f3f #define lowbit(x) (x&(-x)) #define eps 0.00000001 #define pn printf(" ") using namespace std; typedef long long ll; const int maxn = 1e6+5; int n; ll v[maxn], t[maxn], to[maxn]; ll ans[maxn]; ll c[maxn]; void update(int pos,int val) { while(pos <= maxn) { c[pos] += val; pos += lowbit(pos); } } int query(int pos) { int ret = 0; while(pos > 0) { ret += c[pos]; pos -= lowbit(pos); } return ret; } int lower(ll x) { int l = 1, r = n, mid; while(l < r) { mid = (l + r) >> 1; if(to[mid] >= x) r = mid; else l = mid + 1; } return l; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%I64d", v+i); for(int i=1;i<=n;i++) { scanf("%I64d", t+i); to[i] = to[i-1] + t[i]; } for(int i=1;i<=n;i++) { ll tp = v[i] + to[i-1]; int pos = lower(tp); if(i < pos) { update(i, 1); update(pos, -1); } if(i <= pos) { if(tp > to[pos]) ans[pos] += t[pos]; else ans[pos] += v[i] - (to[pos-1] - to[i-1]); } } for(int i=1;i<=n;i++) ans[i] += 1LL * t[i] * query(i); for(int i=1;i<=n;i++) { if(i != 1) printf(" "); printf("%I64d", ans[i]); } pn; }