1. 程式人生 > >Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)C. Producing Snow

Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)C. Producing Snow

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.

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.

題意是給定長度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("\n")
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;
}