Pagini recente » Cod sursa (job #1262089) | Cod sursa (job #377305) | Cod sursa (job #340437) | Cod sursa (job #281815) | Cod sursa (job #370398)
Cod sursa(job #370398)
#include <fstream>
using namespace std;
ifstream fi("aib.in");
ofstream fo("aib.out");
int N,M,aib[100010];
inline int bit(int x){ return (x&(x-1))^x; }
void update(int poz,int s){
for (;poz<=N;poz+=bit(poz)) aib[poz]+=s;
}
int sum(int poz){
int rez=0;
for (;poz;poz-=bit(poz)) rez+=aib[poz];
return rez;
}
int pozitie(int suma){
int ref=0,pow=1,rez=N+1,cop=suma;
while (pow<=N) pow*=2;
pow/=2;
while (pow) {
if ((ref+pow)<=N) if (aib[ref+pow]>=suma){ rez=ref+pow; } else {suma-=aib[ref+pow];ref+=pow; }
pow/=2;
}
if (rez==N+1 || sum(rez)!=cop) return -1; else return rez;
}
int main(){
fi>>N>>M;
int x,y,z;
for (int i=1;i<=N;++i){
fi>>x;
update(i,x);
}
for (int i=1;i<=M;++i){
fi>>x;
if (x!=2){
fi>>y>>z;
if (x==0) update(y,z); else fo<<sum(z)-sum(y-1)<<"\n";
} else {
fi>>z;
fo<<pozitie(z)<<"\n";
}
}
fi.close();fo.close();
return 0;
}