Pagini recente » Cod sursa (job #2761368) | Cod sursa (job #1545220) | Cod sursa (job #2510667) | Borderou de evaluare (job #2116701) | Cod sursa (job #1426021)
#include <cstdio>
using namespace std;
int n;
int aib[100005];
void update(int poz,int val)
{
for( ;poz<=n;poz+=poz&-poz)
aib[poz]+=val;
}
int query(int poz)
{
int s=0;
for( ;poz!=0;poz-=poz&-poz)
s=s+aib[poz];
return s;
}
int main()
{
freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
int m,i,tip,x,y,st,dr,med,last;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%d",&x);
update(i,x);
}
for(i=1;i<=m;i++)
{
scanf("%d",&tip);
if(tip==0)
{
scanf("%d%d",&x,&y);
update(x,y);
}
else
{
if(tip==1)
{
scanf("%d%d",&x,&y);
printf("%d\n",query(y)-query(x-1));
}
else
{
if(tip==2)
{
scanf("%d",&x);
st=1;
dr=n;
last=-1;
while(st<=dr)
{
med=(st+dr)>>1;
if(query(med)>=x)
{
dr=med-1;
last=med;
}
else
st=med+1;
}
printf("%d\n",last);
}
}
}
}
return 0;
}