Vessels

查看原题点这里^_^

D. Vessels
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  1. Add xi liters of water to the pi-th vessel;
  2. 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 "pi xi", the query of the second type is represented as "ki" (1 ≤ pi ≤ n1 ≤ xi ≤ 1091 ≤ 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
题意:给了1、2两种选择,如果是1,往第p个容器中倒水,如果满了则往下流,最后一个容器满了后,流出的水作废。

思路:此题关键在于用并查集化简,直接看代码吧。

#include<iostream>
#include<cstdio>
const int maxn=2e5+10;
using namespace std;

int vol[maxn], remain[maxn], to[maxn];//分别表示容量、剩余空间、流向。

int main(){
	int n;
	cin>>n;
	for(int i=1; i<=n; i++)
	{
		cin>>vol[i];
		remain[i]=vol[i];
	}
	for(int i=1; i<=n; i++)
	{
		to[i]=i+1;
	}
	int m;
	cin>>m;
	while(m--)
	{
		int q, p, x, flag=0;
		cin>>q;
		if(q==1)
		{
			flag=0;
			cin>>p>>x;
			int r = p, j;
			while(x-remain[r]>=0 && r<=n)
			{
				x-=remain[r];
				remain[r]=0;
				r=to[r];
				flag=1;
			}
			while(flag && to[p]!=r)//这个while是并查集的优化。一定要立一个flag,只有溢出才有必要优化。开始我就忽略了。
			{
				j=to[p];
				to[p]=r;
				p=j;
			}
			if(p<=n) remain[r]-=x;
		}
		else
		{
			int k;
			cin>>k;
			cout<<vol[k]-remain[k]<<endl;
		}
	}
	return 0;
}


本人语拙,若有不懂,尽请留言,若有错误,不吝赐教。 喜欢博主的点波关注, 谢谢^_^

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值