Codeforces 500E New Year Domino

E. New Year Domino
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Celebrating the new year, many people post videos of falling dominoes; Here's a list of them:https://www.youtube.com/results?search_query=New+Years+Dominos

User ainta, who lives in a 2D world, is going to post a video as well.

There are n dominoes on a 2D Cartesian plane. i-th domino (1 ≤ i ≤ n) can be represented as a line segment which is parallel to the y-axis and whose length is li. The lower point of the domino is on the x-axis. Let's denote the x-coordinate of the i-th domino as pi. Dominoes are placed one after another, so p1 < p2 < ... < pn - 1 < pn holds.

User ainta wants to take a video of falling dominoes. To make dominoes fall, he can push a single domino to the right. Then, the domino will fall down drawing a circle-shaped orbit until the line segment totally overlaps with the x-axis.

Codeforces 500E New Year Domino_第1张图片

Also, if the s-th domino touches the t-th domino while falling down, the t-th domino will also fall down towards the right, following the same procedure above. Domino s touches domino t if and only if the segment representing s and t intersects.

Codeforces 500E New Year Domino_第2张图片

See the picture above. If he pushes the leftmost domino to the right, it falls down, touching dominoes (A), (B) and (C). As a result, dominoes (A), (B), (C) will also fall towards the right. However, domino (D) won't be affected by pushing the leftmost domino, but eventually it will fall because it is touched by domino (C) for the first time.

Codeforces 500E New Year Domino_第3张图片

The picture above is an example of falling dominoes. Each red circle denotes a touch of two dominoes.

User ainta has q plans of posting the video. j-th of them starts with pushing the xj-th domino, and lasts until the yj-th domino falls. But sometimes, it could be impossible to achieve such plan, so he has to lengthen some dominoes. It costs one dollar to increase the length of a single domino by 1. User ainta wants to know, for each plan, the minimum cost needed to achieve it. Plans are processed independently, i. e. if domino's length is increased in some plan, it doesn't affect its length in other plans. Set of dominos that will fall except xj-th domino and yj-th domino doesn't matter, but the initial push should be on domino xj.

Input

The first line contains an integer n (2 ≤ n ≤ 2 × 105)— the number of dominoes.

Next n lines describe the dominoes. The i-th line (1 ≤ i ≤ n) contains two space-separated integers pili (1 ≤ pi, li ≤ 109)— the x-coordinate and the length of the i-th domino. It is guaranteed that p1 < p2 < ... < pn - 1 < pn.

The next line contains an integer q (1 ≤ q ≤ 2 × 105) — the number of plans.

Next q lines describe the plans. The j-th line (1 ≤ j ≤ q) contains two space-separated integers xjyj (1 ≤ xj < yj ≤ n). It means the j-th plan is, to push the xj-th domino, and shoot a video until the yj-th domino falls.

Output

For each plan, print a line containing the minimum cost needed to achieve it. If no cost is needed, print 0.

Sample test(s)
input
6
1 5
3 3
4 4
9 2
10 1
12 1
4
1 2
2 4
2 5
2 6
output
0
1
1
2
Note

Consider the example. The dominoes are set like the picture below.

Codeforces 500E New Year Domino_第4张图片

Let's take a look at the 4th plan. To make the 6th domino fall by pushing the 2nd domino, the length of the 3rd domino (whose x-coordinate is 4) should be increased by 1, and the 5th domino (whose x-coordinate is 9) should be increased by 1 (other option is to increase 4th domino instead of 5th also by 1). Then, the dominoes will fall like in the picture below. Each cross denotes a touch between two dominoes.

Codeforces 500E New Year Domino_第5张图片
Codeforces 500E New Year Domino_第6张图片
Codeforces 500E New Year Domino_第7张图片
Codeforces 500E New Year Domino_第8张图片

Codeforces 500E New Year Domino_第9张图片


题意:给你一系列的多米诺骨牌,你已知的是每个多米诺骨牌的位置和长度,再给你一些的询问,每次询问两个数l,r问你如果推倒第l个多米诺,那么还要至少多少代价使得l,r区间所有多米诺骨牌都倒下。你可以花费一个单位的代价使得任一一个多米诺骨牌长度增加1。询问的个数和多米诺的个数都达到了10^5的数量级,时间限制为2s。

收获:这是一个能力之内,却没有想出来的题目。后面看别人的题解的时候暴露了自己在数据结构方面的巨大不足以及缺陷。(比如写个线段树懒操作都问题多多。。。。) 用离线处理+线段树懒操作区间更新+单调栈的方法。

题解:先把所有数据读入,然后把多米诺骨牌从前往后扫一遍。
现定义一个单调栈:当推到第k个骨牌时,栈里存放一些骨牌编号,从栈顶到栈底在保证编号有序的情况下保证p+l(即长度+坐标)从小到大有序。
假设已经推倒了前1,,k-1个多米诺骨牌,然后现在要推第k个骨牌。
假设当前栈顶元素的p+l不小于第k个骨牌的p。那么毫无疑问,前1..k-1到第k个骨牌不需要再增加任何代价(仔细体会这个再)。
假设当前栈顶元素的p+l小于第k个骨牌的p,那么毫无疑问,栈顶第二个和栈顶之间的多米诺编号都要增加代价为pk-s.top().p-s.top().l(用线段树懒操作更新)。
然后再用线段树查询求出所有以当前第k个结尾的询问。
给出核心代码:
void init()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d %d",&a[i],&b[i]);
    build(1,n,1);
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        int c,d;
        scanf("%d %d",&c,&d);
        p[d].push_back(make_pair(i,c));
    }
}
stacks;

void solve()
{
    for(int k=2;k<=n;k++)
    {
        while(!s.empty())
        {
            int id=s.top();
            if(a[k-1]+b[k-1]>=a[id]+b[id])s.pop();
            else break;
        }
        s.push(k-1);
        while(!s.empty())
        {
            int id=s.top();
            if(a[id]+b[id]>=a[k])break;
            s.pop();
            int cmp=a[k]-a[id]-b[id];
            int tmp=0;
            if(!s.empty())tmp=s.top();
            update(tmp+1,id,cmp,1);
        }
        for(int i=0;i

你可能感兴趣的:(codeforces)