Pagini recente » Cod sursa (job #2638193) | Cod sursa (job #1460500) | Cod sursa (job #2516755) | Cod sursa (job #3240659) | Cod sursa (job #2363706)
#include <stdio.h>
#define nmax 100000
#define dim 10000
using namespace std;
FILE *fin=fopen("aib.in","r");
FILE *fout=fopen("aib.out","w");
int n,m,aib[nmax+5],poz;
char buffer[dim];
void read(int &x)
{
x=0;
while(buffer[poz]<'0' || buffer[poz]>'9')
if(++poz==dim)
fread(buffer,1,dim,fin),poz=0;
while(buffer[poz]>='0' && buffer[poz]<='9')
{
x=x*10+buffer[poz]-'0';
if(++poz==dim)
fread(buffer,1,dim,fin),poz=0;
}
}
void update(int poz,int val)
{
while(poz<=n)
{
aib[poz]+=val;
poz+=poz&(-poz);
}
}
int querry(int poz)
{
int s=0;
while(poz>0)
{
s+=aib[poz];
poz-=poz&(-poz);
}
return s;
}
int kth(int s)
{
int p=1;
while(p<n)p<<=1;
for(int i=0;p>0;p>>=1)
if(i+p<=n)
{
if(s>=aib[i+p])
{
s-=aib[i+p];
i+=p;
if(!s)
return i;
}
}
return -1;
}
int main()
{
int x,y,z;
read(n);
read(m);
for(int i=1;i<=n;i++)
{
read(x);
update(i,x);
}
for(int i=1;i<=m;i++)
{
read(z);
switch(z)
{
case 0:
read(x);
read(y);
update(x,y);
break;
case 1:
read(x);
read(y);
fprintf(fout,"%d\n",querry(y)-querry(x-1));
break;
case 2:
read(x);
fprintf(fout,"%d\n",kth(x));
}
}
return 0;
}