【二分】Producing Snow @Codeforces Round #470 Div.2 C
time limit per test: 1 second
memory limit per test: 256 megabytes
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.
http://codeforces.com/contest/948/problem/C
題意是第i天會給你一堆V[i]單位的雪,每天你的每個雪堆都會融化掉T[i]單位(包括當天的雪堆),如果一堆雪化完了就會消失,求每天融化掉的雪的量。
先處理出T[i]的字首和ext[i],遍歷每堆雪,二分V[i]+ext[i-1]在ext陣列中的位置並存入day[i],即每堆雪會在哪一天消失。複雜度是
然後給day陣列排序。
最後遍歷每一天,對於第i天,可以二分day陣列找到第i天之前消失的雪堆數量num, 當天融化的雪量就是(i-num)*T[i]+remain[i];
#define IN_LB() freopen("F:\\in.txt","r",stdin)
#define IN_PC() freopen("C:\\Users\\hz\\Desktop\\in.txt","r",stdin)
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
typedef long long ll;
ll v[maxn],t[maxn],ext[maxn];
ll remain[maxn],dday[maxn];
struct node{
int ind;
int day;
}nd[maxn];
bool cmp(node a,node b){
return a.day<b.day;
}
int main() {
// IN_LB();
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++) {
scanf("%d",v+i);
}
for(int i=1; i<=n; i++) {
scanf("%d",t+i);
}
ext[1] = t[1];
for(int i=2; i<=n; i++) {
ext[i]=ext[i-1]+t[i];
}
for(int i=1; i<=n; i++) {
nd[i].ind = i;
nd[i].day = (int)(lower_bound(ext+1,ext+n+1,v[i]+ext[i-1])-ext);
remain[nd[i].day] += v[i]-ext[nd[i].day-1]+ext[i-1];
}
sort(nd+1,nd+n+1,cmp);
for(int i=1; i<=n; i++) {
dday[i] = nd[i].day;
}
for(int i=1;i<=n;i++){
int num = (int)(upper_bound(dday+1,dday+n+1,i)-dday)-1;
printf("%lld%s",(i-num)*t[i]+remain[i],i==n?"\n":" ");
}
return 0;
}