Pagini recente » Cod sursa (job #2341633) | Cod sursa (job #2523086) | Cod sursa (job #1590881) | Cod sursa (job #1266368) | Cod sursa (job #2647293)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
const int NMAX=1e5;
int a[NMAX], n, m;
int bit[NMAX];
void update(int poz, int x){
for(;poz<=n; poz+=((~poz)+1)&poz){
bit[poz]+=x;
}
}
int sum(int poz){
int ans=0;
// for(;poz>0;poz=poz&(poz-1)){
for(;poz>0;poz-=((~poz+1)&poz)){
ans+=bit[poz];
}
return ans;
}
int search(int x, int poz=1, int s=0){
return -1;
auto op=[](int poz){
int ans=poz+((~poz+1)&poz);
return ans;
};
static int fpoz=0;
if(fpoz==0){
for(fpoz=1; fpoz<n; fpoz<<=1)
;
cout<<"X : "<<x<<"\n\n\n";
}
// int ipoz=poz;
for(; op(poz)<=fpoz; poz=op(poz)){
int npoz=op(poz);
if(s+bit[poz]==x) return poz;
if(s+bit[npoz]==x) return npoz;
if(s+bit[npoz]>x){
int ans= search(x, poz+1, s+bit[poz]);
if(ans!=-1)
return ans;
}
}
return -1;
}
int main() {
fin>>n>>m;
for(int i=1;i<=n;++i)
fin>>a[i], update(i,a[i]);
for(int i=1;i<=m;++i){
int k, x,y;
fin>>k>>x;
if(k<=1){
fin>>y;
if(k==0){
update(x,y);
}
else {
fout<<sum(y)-sum(x-1);
}
}
else{
fout<<search(x);
}
if(k==0) goto asd;
fout<<"\n";
asd:;
}
for(int i=1;i<=n;++i)
cout<<bit[i]<<" ";
}