这道题我错在了细节上
如果无法通过下面这组测试样例,那么你需要注意一下我在代码中注释的部分
样例如下:
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.
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<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;
}