Pagini recente » Cod sursa (job #201245) | Cod sursa (job #479958) | Cod sursa (job #419880) | Cod sursa (job #2346871) | Cod sursa (job #963664)
Cod sursa(job #963664)
#include <iostream>
#include <fstream>
using namespace std;
#define lsb(X) (X&(X^(X-1)))
#define LE 100666
#define ll long long
ifstream f("aib.in");
ofstream g("aib.out");
int n,m;
ll tree[LE],vv;
int i,typ;
void update(int pos,ll value) {
for(; pos<=n; pos+=lsb(pos))
tree[pos]+=value;
}
ll query(int pos) {
ll result=0;
for(; pos>0; pos-=lsb(pos))
result+=tree[pos];
return result;
}
int sch(ll smax) {
ll step,pos,total=0;
for(step=1; step<=n; step<<=1);
for(pos=0; step; step>>=1)
if (pos+step<=n&&total+tree[pos+step]<=smax) {
pos+=step;
total+=tree[pos];
}
return ((total==smax&&pos==0)?pos:-1);
}
int main() {
f>>n>>m;
for(i=1; i<=n; ++i) {
f>>vv;
update(i,vv);
}
for(i=1; i<=m; ++i) {
f>>typ;
ll aa,bb;
if (typ==0) {
f>>aa>>bb;
update (aa,bb);
}
if (typ==1) {
f>>aa>>bb;
g<<query(bb)-query(aa-1)<<'\n';
}
if (typ==2) {
f>>aa;
g<<sch(aa)<<'\n';
}
}
f.close();
g.close();
return 0;
}