Codeforces Round #218 (Div. 2)——D. Vessels

151 阅读3分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第4天,点击查看活动详情

Codeforces Round #218 (Div. 2)——D. Vessels

Problem - 371D - Codeforces

There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is a**i liters.

img

Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the i-th vessel goes to the (i + 1)-th one. The liquid that overflows from the n-th vessel spills on the floor.

Your task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries:

  1. Add x i liters of water to the p i-th vessel;
  2. Print the number of liters of water in the k i-th vessel.

When you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels.

Input

The first line contains integer n — the number of vessels (1 ≤ n ≤ 2·10^5). The second line contains n integers a1, a2, ..., a n — the vessels' capacities (1 ≤ a i ≤ 109). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer m — the number of queries (1 ≤ m ≤ 2·105). Each of the next m lines contains the description of one query. The query of the first type is represented as "1 p i x i", the query of the second type is represented as "2 k i" (1 ≤ p**i ≤ n, 1 ≤ x i ≤ 109, 1 ≤ k i ≤ n).

Output

For each query, print on a single line the number of liters of water in the corresponding vessel.

Examples

input

3
5 10 8
6
1 1 12
2 2
1 1 6
1 3 2
2 2
2 3

output

7
10
5

问题解析

题目是说给你n个碗,从上到下依次排列,每个碗能装ai份的水,如果装满了,就会流到下一层的碗里,有m次操作,操作1是把第i个碗装上k份水,操作2是问第i个碗里有多少水。

可以想到,假如第一个碗和第五个碗没有装满水,而第2、3、4个碗都装满了,此时要是第一个碗也装满水了,水最后会流到第五个碗里。所以,对于装满了的碗,我们可以直接略过他,而是指向它下面的第一个没满的碗。

这里我们就想到了可以利用并查集的做法,给第i个碗加水就等于给fa[i]加水,初始fa[i]=i,即给自己加水,如果当前加满了,fa[i]就指向fa[i+1],如果fa[i+1]也装满了,则fa[i+1]=fa[i+2],以此类推,直到水流到地上或者不能装满当前碗为止。

AC代码

const int N = 2e5 + 50, MOD = 1e9 + 7;
 
int n, m, fa[N], a[N], b[N];
int find(int x)
{
    if (x != fa[x])
        fa[x] = find(fa[x]);
    return fa[x];
}
void solve() 
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        fa[i] = i;
    }
    cin >> m;
    int op, idx, num;
    while (m--)
    {
        cin >> op >> idx;
        if (op == 1)
        {
            cin >> num;
            int x = find(idx);
            b[x] += num;
            while (a[x] != 0 && b[x] > a[x])
            {
                int y = find(x + 1);
                b[y] += b[x] - a[x];
                b[x] = a[x];
                fa[x] = y;
                x = y;
            }
        }
        else
        {
            cout << b[idx] << endl;
        }
    }
}