cf D. Vessels

时间:2023-11-23 16:34:14

http://codeforces.com/contest/371/problem/D

第一遍写的超时了,然后看了别人的代码,思路都是找一个点的根,在往里面加水的时候碗中的水满的时候建立联系。查询的时候直接查询就行。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define LL __int64
#define maxn 500000
using namespace std; int f[maxn];
LL a[maxn],cap[maxn]; int find1(int x)
{
if(f[x]==x)
{
return x;
}
return f[x]=find1(f[x]);
} void deal(int pos,LL y)
{
while(y>)
{
int c=find1(pos);
a[c]+=y;
y=a[c]-cap[c];
if(y<=) return;
a[c]=cap[c];
if(a[c]==cap[c]) f[c]=f[c+];
}
} int main()
{
int n;
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%I64d",&cap[i]);
f[i]=i;
}
f[n+]=n+;
cap[n+]=200000000000000LL;
int m;
scanf("%d",&m);
while(m--)
{
int xx,p;
LL x;
scanf("%d",&xx);
if(xx==)
{
scanf("%d%I64d",&p,&x);
deal(p,x);
}
else
{
scanf("%d\n",&p);
printf("%I64d\n",a[p]);
}
}
return ;
}