Pagini recente » Cod sursa (job #157804) | Cod sursa (job #2352097) | Cod sursa (job #735525) | Cod sursa (job #1570242) | Cod sursa (job #2294551)
#include <bits/stdc++.h>
using namespace std;
ifstream in("aib.in");
ofstream out("aib.out");
vector< int > BIT;
int getParent(int index) {
return index - (index & -index);
}
int getNext(int index) {
return index + (index & -index);
}
void updateTree(int value, int index) {
while(index < (int)BIT.size()) {
BIT[index] += value;
index = getNext(index);
}
}
int getSum(int index) {
int sum = 0;
while(index > 0) {
sum += BIT[index];
index = getParent(index);
}
return sum;
}
int getPosition(int 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;
BIT.resize(n + 1);
for(int i = 1; i <= n; ++i) {
int value; in >> value;
updateTree(value, i);
}
for(int i = 1; i <= m; ++i) {
int type; in >> type;
if(type < 2) {
int a, b; in >> a >> b;
if(type == 0) {
updateTree(b, a);
} else {
out << getSum(b) - getSum(a - 1) << "\n";
}
} else {
int a; in >> a;
out << getPosition(a) << "\n";
}
}
in.close(); out.close();
return 0;
}