Disjoint-set violence

Training 4 - I题

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.

Examples

Input

2
5 10
6
1 1 4
2 1
1 2 5
1 1 4
2 1
2 2

Output

4
5
8

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

#pragma warning (disable:4996)
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5 + 5;

int f[maxn];//连续的满的容器

int get(int x)
{
	if (f[x] == x)
		return x;
	return f[x] = get(f[x]);
}

void merge(int x, int y)
{
	f[get(x)] = get(y);
}

int a[maxn];
int l[maxn];

int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		f[i] = i;
		scanf("%d", &a[i]);
	}
	int m;
	scanf("%d", &m);
	while (m--)
	{
		int oper;
		scanf("%d", &oper);
		if (oper == 1)
		{
			int p, x;
			scanf("%d%d", &p, &x);
			int tmp = p;
			while (l[tmp] + x > a[tmp])
			{
				if (tmp > n || tmp <= 0)
					break;
				x -= (a[tmp] - l[tmp]);
				l[tmp] = a[tmp];
				merge(tmp, tmp + 1);
				tmp = get(tmp + 1);
			}
			if (tmp <= n && tmp > 0)
				l[tmp] += x;
		}
		if (oper == 2)
		{
			int k;
			scanf("%d", &k);
			printf("%d\n", l[k]);
		}
	}
	return 0;
}

Ideas:
Simple pouring water issues, in order to optimize save time, with disjoint-set record continuous water-filled container, pour the next time continuous across the full container down the next full container.

Published 28 original articles · won praise 0 · Views 323

Guess you like

Origin blog.csdn.net/xukeke12138/article/details/104741221