题目链接:http://codeforces.com/problemset/problem/371/D点击打开链接
每个位置满了之后跳转到下一个不满的位置 用pre储存 这样就不用一次遍历而t
#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <stack>
#include <algorithm>
#include <queue>
#include <string.h>
#include <set>
using namespace std;
int n=0;
struct xjy
{
int t;
int v;
}a[222222];
int pre[222222];
int findx(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;
i=j;
}
return r;
}
void join (int x,int y)
{
int p1=findx(x);
int p2=findx(y);
if(p1!=p2)
{
if(p1>p2)
pre[p2]=p1;
else
pre[p1]=p2;
}
}
void add(int num,int addv)
{
int r=findx(num);
while(addv>0&&r<=n)
{
if((a[r].v-a[r].t)>=addv)
{
a[r].t+=addv;
addv=0;
}
else
{
addv-=(a[r].v-a[r].t);
a[r].t=a[r].v;
join(r,r+1);
}
r=findx(r);
}
}
int main()
{
for(int i=0;i<=200001;i++)
pre[i]=i;
scanf("%d",&n);
xjy mid;
for(int i=1;i<=n;i++)
{
scanf("%d",&mid.v);
mid.t=0;
a[i]=mid;
}
int nn=0;
scanf("%d",&nn);
for(int i=0;i<nn;i++)
{
int midmid;
scanf("%d",&midmid);
if(midmid==1)
{
int num;int addv;
scanf("%d%d",&num,&addv);
add(num,addv);
}
else
{
int num;
scanf("%d",&num);
printf("%d\n",a[num].t);
}
}
}