Codeforces Round #167 (Div. 2) C. Dima and Staircase(线段树·成段更新,繁琐)

C. Dima and Staircase
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Dima's got a staircase that consists of n stairs. The first stair is at height a1, the second one is at a2, the last one is at an (1 ≤ a1 ≤ a2 ≤ ... ≤ an).

Dima decided to play with the staircase, so he is throwing rectangular boxes at the staircase from above. The i-th box has width wi and height hi. Dima throws each box vertically down on the first wi stairs of the staircase, that is, the box covers stairs with numbers 1, 2, ..., wi. Each thrown box flies vertically down until at least one of the two following events happen:

  • the bottom of the box touches the top of a stair;
  • the bottom of the box touches the top of a box, thrown earlier.

We only consider touching of the horizontal sides of stairs and boxes, at that touching with the corners isn't taken into consideration. Specifically, that implies that a box with width wi cannot touch the stair number wi + 1.

You are given the description of the staircase and the sequence in which Dima threw the boxes at it. For each box, determine how high the bottom of the box after landing will be. Consider a box to fall after the previous one lands.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of stairs in the staircase. The second line contains a non-decreasing sequence, consisting of n integers, a1, a2, ..., an (1 ≤ ai ≤ 109ai ≤ ai + 1).

The next line contains integer m (1 ≤ m ≤ 105) — the number of boxes. Each of the following m lines contains a pair of integers wi, hi (1 ≤ wi ≤ n; 1 ≤ hi ≤ 109) — the size of the i-th thrown box.

The numbers in the lines are separated by spaces.

Output

Print m integers — for each box the height, where the bottom of the box will be after landing. Print the answers for the boxes in the order, in which the boxes are given in the input.

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.

Sample test(s)
Input
5
1 2 3 6 6
4
1 1
3 1
1 1
4 3
Output
1
3
4
6
Input
3
1 2 3
2
1 1
3 1
Output
1
3
Input
1
1
5
1 2
1 10
1 10
1 10
1 10
Output
1
3
13
23
33
Note

The first sample are shown on the picture.

经典的线段树,用到成段更新。

AC Code:

  1 #include <iostream>
  2 #include <string>
  3 #include <set>
  4 #include <map>
  5 #include <vector>
  6 #include <stack>
  7 #include <queue>
  8 #include <cmath>
  9 #include <cstdio>
 10 #include <cstring>
 11 #include <algorithm>
 12 using namespace std;
 13 #define LL long long
 14 #define cti const int
 15 #define dg(i) cout << "*" << i << endl;
 16 
 17 cti MAXN = 100005;
 18 int n, m;
 19 struct Box
 20 {
 21     int w;
 22     LL h;
 23     LL height;  //height是box放置位置的高度
 24 }box[MAXN];
 25 struct Interval
 26 {
 27     int l, r;
 28     LL h;  //该区间中最高的一级阶梯的高度
 29     int id; //最上方的box的编号
 30     bool tag;  //成段更新标记
 31 }in[MAXN<<2];
 32 
 33 LL Max(LL x, LL y)  //返回位置较高的box的高度
 34 {
 35     return x > y ? x : y;
 36 }
 37 
 38 int MaxId(int i, int j)  //返回位置较高的box的编号
 39 {
 40     return box[in[i].id].height + box[in[i].id].h > box[in[j].id].height + box[in[j].id].h ? in[i].id : in[j].id;
 41 }
 42 
 43 void Build(int left, int right, int root)
 44 {
 45     in[root].l = left;
 46     in[root].r = right;
 47     in[root].id = 0;
 48     in[root].tag = false;
 49     if(left == right)
 50     {
 51         scanf("%I64d", &in[root].h);
 52         return ;
 53     }
 54     int mid = (left + right) >> 1;
 55     Build(left, mid, root << 1);  //中点归到左树
 56     Build(mid + 1, right, root << 1 | 1);
 57     in[root].h = Max(in[root<<1].h, in[root<<1|1].h);
 58 }
 59 
 60 void Update(int from, int to, int root, int idx)
 61 {
 62     if(from <= in[root].l && to >= in[root].r)
 63     {
 64         in[root].tag = true;
 65         if(!in[root].id) box[idx].height = Max(box[idx].height, in[root].h);
 66         // !in[root].id则此区间的阶梯上没有box
 67         else box[idx].height = Max(box[idx].height, box[in[root].id].height + box[in[root].id].h);
 68         in[root].id = idx;  //后面扔到此阶梯上的box必然在最上面
 69         return ;
 70     }
 71     int mid = (in[root].l + in[root].r) >> 1;
 72     //==========================================
 73     //PushDown: 往下传递更新
 74     if(in[root].tag)
 75     {
 76         in[root<<1].id = in[root<<1|1].id = in[root].id;
 77         //整个区间被编号为in[root].id的box覆盖
 78         in[root<<1].tag = in[root<<1|1].tag = true;
 79         in[root].tag = false;
 80     }
 81     //==========================================
 82     if(from <= mid) Update(from, to, root<<1, idx);
 83     //建立线段树时,mid归到左子树,故此处==mid时归到左子树
 84     if(to > mid) Update(from, to, root<<1|1, idx);
 85     int maxId = MaxId(root<<1, root<<1|1);
 86     //获得区间in[root]中最高的box的id,注意此box有可能不及in[root]的
 87     //高度高,故仍需要下式:
 88     in[root].id = in[root].h > box[maxId].height + box[maxId].h ? 0 : maxId;
 89 }
 90 
 91 int main()
 92 {
 93     while(scanf("%d", &n) != EOF)
 94     {
 95         Build(1, n, 1);
 96         scanf("%d", &m);
 97         box[0].height = box[0].h = 0;
 98         for(int i = 1; i <= m; i++)
 99         {
100             scanf("%d %I64d", &box[i].w, &box[i].h);
101             box[i].height = 0;
102             Update(1, box[i].w, 1, i);
103         }
104         for(int i = 1; i <= m; i++)
105             printf("%I64d\n", box[i].height);
106     }
107     return 0;
108 }
原文地址:https://www.cnblogs.com/cszlg/p/2933754.html