Vessels (Codeforces 371D)
Tags: Implement, dsu

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 ai liters.  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:  Add xi liters of water to the pi-th vessel;  Print the number of liters of water in the ki-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·105). The second line contains n integers a1, a2, ..., an — the vessels' capacities (1 ≤ ai ≤ 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 pi xi", the query of the second type is represented as "2 ki" (1 ≤ pi ≤ n, 1 ≤ xi ≤ 109, 1 ≤ ki ≤ n).

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


Sample Inputs:
2
5 10
6
1 1 4
2 1
1 2 5
1 1 4
2 1
2 2

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

Sample Outputs:
4
5
8

7
10
5

Solution (Python):
from sys import stdin, stdout
n = int(stdin.readline()) + 1
a = list(map(int, stdin.readline().split())) + [1 << 50]
(l, p, r) = ([0] * n, list(range(n)), [])
for i in range(int(stdin.readline())):
    t = list(map(int, stdin.readline().split()))
    if t[0] == 2:
        r.append(l[t[1] - 1])
    else:
        x = t[1] - 1
        (s, d) = ([x], t[2])
        while True:
            if p[x] != x:
                x = p[x]
                s.append(x)
                continue
            if l[x] + d < a[x]:
                l[x] += d
                break
            d -= a[x] - l[x]
            l[x] = a[x]
            x += 1
            s.append(x)
        for j in s:
            p[j] = x
stdout.write('\n'.join(map(str, r)))

Solution (Python):
from collections import defaultdict
n = int(input())
vessel_cap = list(map(int, input().split()))
vessel_cap = [0] + vessel_cap
vessel_has = [0] * (n + 1)
vessel_link = [i + 1 for i in range(n + 1)]
n_query = int(input())
for _ in range(n_query):
    query = list(map(int, input().split()))
    relink = []
    if query[0] == 1:
        (pi, x) = (query[1], query[2])
        while pi <= n and x > 0:
            if x >= vessel_cap[pi] - vessel_has[pi]:
                x -= vessel_cap[pi] - vessel_has[pi]
                vessel_has[pi] = vessel_cap[pi]
                relink.append(pi)
                pi = vessel_link[pi]
            else:
                vessel_has[pi] += x
                x = 0
        for xr in relink:
            vessel_link[xr] = pi
    else:
        print(vessel_has[query[1]])

Solution (Python):
n = int(input())
nxt = [0] * (n + 5)
v = [0] * (n + 5)
for i in range(1, n + 1):
    nxt[i] = i + 1
a = [0] + list(map(int, input().split()))
T = int(input())
for pp in range(T):
    x = list(map(int, input().split()))
    if x[0] == 2:
        print(v[x[1]])
    else:
        u = x[1]
        w = x[2]
        now = u
        while now <= n and w + v[now] >= a[now]:
            w -= a[now] - v[now]
            v[now] = a[now]
            now = nxt[now]
        if now <= n:
            v[now] += w
        while u != now:
            t = u
            u = nxt[u]
            nxt[t] = now