Pagini recente » Cod sursa (job #2857306) | Cod sursa (job #2000426) | Cod sursa (job #1222249) | Istoria paginii runda/simulare-cartita-23/clasament | Cod sursa (job #2578487)
#include <fstream>
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
int aib[100005], n, m, t, a, b;
void update(int poz, int x) {
while(poz <= n) {
aib[poz] += x;
poz += (poz&-poz);
}
}
int query(int poz) {
int s = 0;
while(poz > 0) {
s += aib[poz];
poz -= (poz&-poz);
}
return s;
}
int cb(int val) {
int st = 1, dr = n;
while(st <= dr) {
int m = (st+dr)/2;
int q = query(m);
if(q == val && query(m-1) < val)
return m;
else if(q >= val)
dr = m-1;
else
st = m+1;
}
return -1;
}
int main() {
fin >> n >> m;
for(int i = 1; i <= n; i++) {
fin >> a;
update(i, a);
}
while(m--) {
fin >> t;
if(t == 0) {
fin >> a >> b;
update(a, b);
} else if(t == 1) {
fin >> a >> b;
fout << query(b) - query(a-1) << '\n';
} else {
fin >> a;
fout << cb(a) << '\n';
}
}
}