codeforces Vessels(并查集)
2017-07-21 12:21
323 查看
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.
Example
Input
Output
Input
Output
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.
Example
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
并查集做,根节点为i个杯子后第一个未满的杯子编号
#include<cstdio> using namespace std; const int M = 2e5 + 5; int pre[M]; struct Node { int l; int f; }a[M]; int Find(int x) { int r = x; while(pre[r]!=r) r = pre[r]; int i = x; int j; while(pre[i]!=r) { j = pre[i]; pre[i] = r; j = i; } return r; } void join(int x, int y) { int tx = Find(x); int ty = Find(y); if(tx!=ty) { if(tx<ty) pre[tx] = ty; else pre[ty] = tx; } } int main() { int n; scanf("%d", &n); for(int i=1;i<=n;i++) { scanf("%d", &a[i].f); a[i].l = a[i].f; pre[i] = i; } pre[n+1] = n+1; int m; scanf("%d", &m); int t, p, x, tmp, r; for(int i=1;i<=m;i++) { scanf("%d", &t); if(t==1) { scanf("%d%d", &p, &x); r = Find(p); while(x>0&&r<=n) { if(a[r].l>=x) { tmp = x; a[r].l -= tmp; } else { tmp = a[r].l; a[r].l = 0; join(r, r+1); } x -= tmp; r = Find(r); } } else if(t==2) { scanf("%d", &p); printf("%d\n", a[p].f - a[p].l); } /* for(int i=1;i<=n;i++) printf("cup ====== %d\n", a[i].f-a[i].l);*/ } return 0; }
相关文章推荐
- Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 开关--并查集
- Educational Codeforces Round 31 E. Binary Matrix 并查集
- A - Union of Doubly Linked Lists CodeForces - 847A(并查集)
- CodeForces Roads not only in Berland(并查集)
- Educational Codeforces Round 1 D. Igor In the Museum bfs 并查集
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 高维并查集,STL连招
- Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 开关--并查集
- Educational Codeforces Round 31 E. Binary Matrix 并查集
- CodeForces Roads not only in Berland(并查集)
- Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary(带权并查集)
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集
- Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 开关--并查集
- Educational Codeforces Round 31 E. Binary Matrix 并查集
- Codeforces Round #170 (Div. 2)---C. Learning Languages(并查集)
- 我的并查集——Codeforces Contets#134 div2 Problem C
- Codeforces Gym 101173 H. Hangar Hurdles (LCA + 并查集)
- codeforces—Royal Questions(并查集维护最大基环外向树)
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C 并查集
- Codeforces Round #360 (Div. 1) D Dividing Kingdom II(并查集+暴力)
- Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 2-SAT、并查集