Pagini recente » Cod sursa (job #1962841) | Cod sursa (job #1304821) | Cod sursa (job #2000654) | Cod sursa (job #3285627) | Cod sursa (job #2933084)
#include <bits/stdc++.h>
using namespace std;
#define nmax 100001
ifstream f("aib.in");
ofstream g("aib.out");
int n,arb[nmax];
void update(int poz,int val){
int i=0;
while(poz<=n){
arb[poz]+=val;
while(!(poz & (1<<i)))i++;
poz+=(1<<i);
i++;
}
}
int query(int poz){
int i=0,s=0;
while(poz>0){
s+=arb[poz];
while(!(poz & (1<<i)))i++;
poz-=(1<<i);
i++;
}
return s;
}
int cauta(int sum){
int poz=n+1;
int limst=0,limdr=n+1;
int n1=n;
int s=query(n1);
if(s==sum)poz=n;
while(n1){
n1=(limst+limdr)>>1;
s=query(n1);
if(s>sum){
if(limdr>n1)limdr=n1;
n1--;
}
else if(s==sum)poz=min(poz,n1),limdr=n1,n1--;
else{
if(limst<n1)limst=n1;
n1++;
}
if(n1<=limst)break;
if(n1>=limdr)break;
}
if(poz==n+1)return -1;
return poz;
}
int main()
{
int m,x,y,cer;
f>>n>>m;
for(int i=1;i<=n;++i)
f>>x,update(i,x);
for(int i=1;i<=m;++i)
{
f>>cer;
if(cer==0){
f>>x>>y;
update(x,y);
}
else if(cer==1){
f>>x>>y;
int ans=query(y)-query(x-1);
g<<ans<<"\n";
}
else if(cer==2){
f>>x;
int ans=cauta(x);
g<<ans<<"\n";
}
}
}