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
题意:现在有n天,每一天增加一堆雪v,每天对应一个温度t,每堆雪在当天温度下都会融化t,问你每天的融化量是多少。
思路:由于本人太弱只想到了二分+线段树的方法,预处理将温度v[i]求出前缀和,具体为两步。
1:假设当天是l,新增的雪为v,那么我们找到v能存在的最长天数的位置r,在气温t[i]里二分第一个大于v的位置就是r。那么[l,r]这个区间内每天直接加倍就可以,如果到r时为0,那么刚好融化尽,如果有剩余,那么一定能被r+1这个位置融化尽,这个剩余额外保存就可以。
2:接下来我们要维护这个[l,r]区间,将其加倍,我们只维护倍数,比如[2,5]两倍:1 2 2 2 2,我们只维护倍数,此时是简单的区间更新,最后输出答案时,用当前的v乘倍数+可能有的额外值即为答案。
整体思路就这两步,还是比较好想的,代码实现还是比较麻烦的,时间复杂度O(n*(logn+logn))。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#define ll long long
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
using namespace std;
ll sum[888888],ans[100010],v[100010],t[100010],vis[888888],n;
void push_up(ll root){sum[root]+=sum[root<<1]+sum[root<<1|1];}
void push_down(ll l,ll r,ll root)
{
if(vis[root])
{
ll mid=(l+r)>>1;
vis[root<<1]+=vis[root];
vis[root<<1|1]+=vis[root];
sum[root<<1]+=(mid-l+1)*vis[root];
sum[root<<1|1]+=(r-mid)*vis[root];
vis[root]=0;
}
}
void update_seg(ll L,ll R,ll x,ll l,ll r,ll root) //区间更新,只维护需要加倍的倍数
{
if(L<=l&&r<=R)
{
vis[root]=vis[root]+x;
sum[root]+=(r-l+1)*x;
return ;
}
push_down(l,r,root);
ll mid=(l+r)>>1;
if(L<=mid)update_seg(L,R,x,lson);
if(R>mid)update_seg(L,R,x,rson);
push_up(root);
}
ll query(ll L,ll R,ll l,ll r,ll root)
{
if(L<=l&&r<=R)return sum[root];
push_down(l,r,root);
ll mid=(l+r)>>1;
ll ans=0;
if(L<=mid)ans+=query(L,R,lson);
if(R>mid)ans+=query(L,R,rson);
return ans;
}
int main()
{
while(~scanf("%lld",&n))
{
t[0]=0;
for(ll i=1;i<=n;i++)scanf("%lld",&v[i]);
for(ll i=1;i<=n;i++)
{
scanf("%lld",&t[i]);
t[i]+=t[i-1]; //求前缀和
}
memset(sum,0,sizeof(sum));
memset(vis,0,sizeof(vis));
memset(ans,0,sizeof(ans)); //ans保存二分之后的剩余值,只需要在输出答案时加上就可以了
for(ll i=1;i<=n;i++)
{
if(v[i]<=(t[i]-t[i-1])){ans[i]+=v[i];continue;} //如果一天就耗尽,直接特判求解,区间根本用不到
ll pos=upper_bound(t+i,t+n+1,v[i]+t[i-1])-t; //二分上界
ll tmp=v[i]+t[i-1]-t[pos-1]; //求出剩余值
ans[pos]+=tmp; //扔给ans保存
update_seg(i,pos-1,1,1,n,1); //区间更新开始
}
for(ll i=1;i<=n;i++)
{
ll tmp=query(i,i,1,n,1); //查询当天温度所需要乘的倍数
printf("%lld ",tmp*(t[i]-t[i-1])+ans[i]);
}
}
return 0;
}