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.
![](https://i-blog.csdnimg.cn/blog_migrate/0f4f896ca26a5fe58cd18dc36bcb5986.png)
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.
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).
For each query, print on a single line the number of liters of water in the corresponding vessel.
2 5 10 6 1 1 4 2 1 1 2 5 1 1 4 2 1 2 2
4 5 8
3 5 10 8 6 1 1 12 2 2 1 1 6 1 3 2 2 2 2 3
7 10 5
一开始真没想到要用并查集来写,还傻乎乎的去暴力了几次,后来朋友提示了我才想到。每一次倒水的时候,如果一个容器满了,我们就可以把它相邻的连在一起,将下一个没有满的容器作为它的父亲,这样每次我们都可以很快找到那一个没满的容器
#include<bits/stdc++.h>
using namespace std;
int vessels[200010];
int tree[200010];
int num[200010];
int find(int x){
return x==tree[x]?x:tree[x]=find(tree[x]);
}
void merge(int x,int y){
int a=find(x);
int b=find(y);
if(a!=b) tree[a]=b;
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
scanf("%d",&vessels[i]);
num[i]=vessels[i];
}
for(int i=1;i<=n;i++) tree[i]=i;
int op;
cin>>op;
while(op--){
int type;
cin>>type;
if(type==1){
int x,water;
cin>>x>>water;
int father=find(x);
while(water>vessels[father]&&father<=n){
water-=vessels[father];
if(father-x) merge(father-1,father);
vessels[father++]=0;
}
vessels[father]-=water;
}
else{
int que;
cin>>que;
cout<<num[que]-vessels[que]<<endl;
}
}
}