Pagini recente » Cod sursa (job #1928923) | Cod sursa (job #1430529) | Cod sursa (job #2527583) | Cod sursa (job #964186) | Cod sursa (job #1959501)
#include <fstream>
using namespace std;
int n,m,t,arb[100001],c,s,k,x,y;
void update(int,int);
int query(int);
int Search(int);
int main()
{
freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
scanf("%d%d", &n, &m);
for(int i=1; i<=n; i++)
{
scanf("%d", &t);
update(i,t);
}
for(int i=1; i<=m; i++)
{
scanf("%d", &k);
if(k<2)
{
scanf("%d%d", &x, &y);
if(!k)
update(x,y);
else
printf("%d\n", query(y)-query(x-1));
}
else
{
scanf("%d", &x);
printf("%d\n", Search(x));
}
}
}
int Search(int sum)
{
int poz=n+1,b=n,st=0,dr=n+1;
s=query(b);
if(s==sum)
poz=n;
while(b)
{
b=(st+dr)/2;
s=query(b);
if(s>sum)
{
if(dr>b)
dr=b;
b--;
}
else
if(s==sum)
{poz=min(poz,b);
dr=b;
b--;}
else
{
if(st<b)
st=b;
b++;
}
if(b<=st)
break;
if(b>=dr)
break;
}
if(poz==n+1)
return -1;
return poz;
}
void update(int poz, int val)
{
c=0;
while(poz<=n)
{
arb[poz]+=val;
while(!(poz & (1<<c)))
c++;
poz+=(1<<c);
c++;
}
}
int query(int poz)
{
c=0, s=0;
while(poz>0)
{
s+=arb[poz];
while(!(poz & (1<<c)))
c++;
poz-=(1<<c);
c++;
}
return s;
}