Pagini recente » Cod sursa (job #2795975) | Istoria paginii runda/arfibinesaintratilaasta | Autentificare | Istoria paginii runda/becreative17/clasament | Cod sursa (job #2915956)
#include <bits/stdc++.h>
#include <vector>
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
int n, m;
vector<int> v(100002), w(100002);
void add(int pos, int val) {
v[pos] += val;
while (pos <= n) {
w[pos] += val;
pos += (pos & -pos);
}
}
int sum(int pos) {
int sum = 0;
while (pos >= 1) {
sum += w[pos];
pos -= (pos & -pos);
}
return sum;
}
int main() {
fin >> n >> m;
for (int i = 1; i <= n; i++) {
fin >> v[i];
w[i] = v[i];
}
// populate finwick
for (int i = 1; i <= n; i++) {
int p = i + (i & -i);
if (p <= n) {
w[p] += w[i];
}
}
// answer queries
while (m--) {
int c; fin >> c;
if (c == 0) {
int a, b; fin >> a >> b;
add(a, b);
}
if (c == 1) {
int a, b; fin >> a >> b;
fout << sum(b) - sum(a - 1) << '\n';
}
if (c == 2) {
int a; fin >> a;
int s = 0, i = 0;
for (; s != a; s += v[i++]);
fout << i - 1 << '\n';
}
}
}