Description
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
Input
3
10 10 5
5 7 2
Output
5 12 4
Input
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.
题意:
有台造雪机器,每天可以造a[i]的雪,然后每天有太阳,会融化掉down[i]的雪,给出你每天造的雪的数量和每条的温度,让求出每天融化的雪的数量。
思路:
一看就知道暴力会超时,然后一直在想怎么优化,因为刚做完树状数组的专题,就想到了树状数组,然后更新的时候没有办法弄,就想了别的。
要求的是当前的温度可以融化掉前面的多少雪,但我一想,我知道所有的数据,我就可以判断当前天数的雪可以为后面几天提供完整的雪,找到一个可以完整提供雪的区间[a,b]。
定义一个数组diff,表示当前的天数的温度被完整用了几次。然后把diff[a]++,diff[b+1]–;把最后一个不能完整提供温度的可以融化的数量求出来,放到ans数组中。
最后用当前天的温度乘上被完整用的次数,加上未被完整利用的值。
代码
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<stack>
#include<map>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const double pi=acos(-1.0);
const int N=2100000;
const int mod=1e9+7;
ll arr[N];
ll pre[N];
ll down[N];
ll ans[N];//当前温度未被完整利用的值
ll num[N];//区间的更新,利用差分,当前天的温度被完整利用的次数 的差分数组
int main()
{
ios::sync_with_stdio(false);
ll n;
cin>>n;
ll rel=0;
for(ll i=1; i<=n; i++)
cin>>arr[i];
for(ll i=1; i<=n; i++)
{
cin>>down[i];
pre[i]=pre[i-1]+down[i];
}
// for(ll i=1;i<=n;i++)
// cout<<pre[i]<<" ";
// cout<<endl;
for(ll i=1; i<=n; i++)
{
if(arr[i]<down[i])
{
ans[i]+=arr[i];//当前的温度比当前的雪还大,直接放入ans中
continue;
}
ll l=i,r=n;
ll index=-1;
while(l<=r)
{
ll mid=(l+r)/2;
if(arr[i]<pre[mid]-pre[i-1])
{
index=mid;
r=mid-1;
}
else
l=mid+1;
}
if(index==-1)
{
num[i]++;
num[n+1]--;
}
else
{
num[i]++;
num[index]--;
ans[index]+=arr[i]-(pre[index-1]-pre[i-1]);
}
// printf("%d %d\n",i,index);
}
for(ll i=1; i<=n; i++)
{
num[i]+=num[i-1];
ans[i]+=num[i]*down[i];
cout<<ans[i]<<" ";
}
cout<<endl;
return 0;
}