Pagini recente » Cod sursa (job #2246837) | Cod sursa (job #1658807) | Cod sursa (job #610310) | Cod sursa (job #2831412) | Cod sursa (job #2700958)
#include <bits/stdc++.h>
#define ll long long
#define fin cin
#define sz(x) (int)(x).size()
#define debug(v,n) for (int i = 1; i <= (n); ++i) cout << v[i] << " ";
#define next cout << '\n'
#define LSB(a) (a & (-a))
using namespace std;
const int N = 100005;
int AIB[N], n, q, x;
void update(int posAct, int val) {
while(posAct <= n) {
AIB[posAct] += val;
posAct += LSB(posAct);
}
}
int sum(int posAct) {
int sum = 0;
while(posAct > 0) {
sum += AIB[posAct];
posAct -= LSB(posAct);
}
return sum;
}
int main() {
//ifstream fin("date.in.txt");
ifstream fin("aib.in");
ofstream fout("aib.out");
fin >> n >> q;
for (int i = 1; i <= n; ++i) {
fin >> x;
update(i, x);
}
while(q--) {
int t, a, b;
fin >> t;
if(t == 0) {
fin >> a >> b;
update(a, b);
}
else if(t == 1) {
fin >> a >> b;
fout << sum(b) - sum(a - 1) << '\n';
}
else {
fin >> a;
int l = 1, r = n;
while(l < r) {
int m = (l + r) / 2;
if(sum(m) >= a)
r = m;
else
l = m + 1;
}
if(sum(l) == a)
fout << l << '\n';
else
fout << -1 << '\n';
}
}
return 0;
}