Vessels
来源:互联网 发布:mac os 启动管理界面 编辑:程序博客网 时间:2024/06/13 07:32
这道题我错在了细节上
如果无法通过下面这组测试样例,那么你需要注意一下我在代码中注释的部分
样例如下:
2
5 10
2
1 1 20
2 2
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.
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.
25 1061 1 42 11 2 51 1 42 12 2
458
35 10 861 1 122 21 1 61 3 22 22 3
7105
#include<cstdio> #include<string.h>void prv(int a);void mer(int a,int b);int find(int a);int pre[200010]={0},an[200010]={0},q[200010]={0};int main(){int n,i,m;scanf("%d",&n);prv(n+1); //如果不是n+1的话,会陷入死循环 for(i=1;i<=n;i++){scanf("%d",&an[i]);}scanf("%d",&m);for(i=0;i<m;i++){int num,x,p,cnt;cnt=0;scanf("%d",&num);if(num==1){int x,p;scanf("%d%d",&p,&x);while(x){p=find(p);if(p==n+1) //注意 break;if(x==an[p]-q[p]){x-=an[p]-q[p];q[p]=an[p];}else if(x>an[p]-q[p]){x-=an[p]-q[p];q[p]=an[p];mer(p,p+1);p=p+1;p=find(p);}else if(x<an[p]-q[p]){q[p]+=x;x=0;}}}else{int k;scanf("%d",&k);printf("%d\n",q[k]);}}}void prv(int a){int i;for(i=1;i<=a;i++) {pre[i]=i;}} void mer(int a,int b){int fa,fb;fa=find(a);fb=find(b);if(fa!=fb){pre[fa]=fb;}}int find(int a){int r;r=a;while(r!=pre[r])r=pre[r];while(a!=pre[a]){int z;z=a;a=pre[a];pre[z]=r;}return r;}
- Vessels
- Vessels
- Vessels
- D. Vessels
- D. Vessels
- Vessels CodeForces
- CodeForces 371D. Vessels
- CodeForces 371D Vessels
- Codeforces Round #218 (Div. 2) D. Vessels
- CF 371D Vessels 【并查集】
- Codeforces 371D. Vessels【并查集】
- codeforces Vessels(并查集)
- CF317D. Vessels(并查集)
- Codeforces Round #218 (Div. 2)---D. Vessels(并查集)
- Codeforces 341D Vessels 线段树 OR Set
- 129. Empty vessels make the greatest sound. 实磨无声空磨响,满瓶不动半瓶摇
- CodeForces Round 218 D. Vessels(学姐我没用线段树系列)
- 论文笔记 Ensemble of Deep Convolutional Neural Networks for Learning to Detect Retinal Vessels in Fundus
- es6环境中,export与import使用方法
- Join有关的几种连接查询
- HDU
- form表单
- 去除input为date的小三角方法 -webkit-appearance: none;
- Vessels
- 线程进行定时操作
- iOS音频——AudioToolbox
- iOS:一句代码实现文本输入的限制
- Hadoop2.8.0 单机搭建和eclipse开发应用配置 新手笔记
- 面向对象练习题
- SweetSheet的Java.lang.IllegalStateException: Cannot start this animator on a detached view!异常
- linux 截取文件名字
- JSP学习---MVC开发模式