Pagini recente » Cod sursa (job #2103869) | Cod sursa (job #2187987) | Cod sursa (job #1750764) | Cod sursa (job #1992331) | Cod sursa (job #2294543)
#include <bits/stdc++.h>
using namespace std;
ifstream in("aib.in");
ofstream out("aib.out");
typedef long long ll;
int getParent(int index) {
return index - (index & -index);
}
int getNext(int index) {
return index + (index & -index);
}
void updateTree(vector< ll > &BIT, ll value, int index) {
while(index < (int)BIT.size()) {
BIT[index] += value;
index = getNext(index);
}
}
ll getSum(const vector< ll >&BIT, int index) {
ll sum = 0;
while(index > 0) {
sum += BIT[index];
index = getParent(index);
}
return sum;
}
vector< ll > createTree(const vector< ll > &v) {
vector< ll > BIT((int)v.size() + 1);
for(int i = 1; i <= (int)v.size(); ++i) {
updateTree(BIT, v[i - 1], i);
}
return BIT;
}
int getPosition(const vector< ll > &BIT, ll targetSum) {
int index = 1;
while(index < (int)BIT.size()) {
if(BIT[index] == targetSum) {
return index;
}
index = getNext(index);
}
return -1;
}
int main() {
ios::sync_with_stdio(false); in.tie(0); out.tie(0);
int n, m; in >> n >> m;
vector< ll > v(n);
for(auto &it: v){
in >> it;
}
vector< ll > BIT = createTree(v);
for(int i = 1; i <= m; ++i) {
ll type, a; in >> type >> a;
if(type == 0) {
ll b; in >> b;
updateTree(BIT, b, a);
}
if(type == 1) {
ll b; in >> b;
out << getSum(BIT, b) - getSum(BIT, a - 1) << "\n";
}
if(type == 2) {
out << getPosition(BIT, a) << "\n";
}
}
in.close(); out.close();
return 0;
}