Pagini recente » Cod sursa (job #1699915) | Cod sursa (job #2903699) | Cod sursa (job #1537218) | Cod sursa (job #416102) | Cod sursa (job #2759335)
#include <fstream>
#include <vector>
using namespace std;
void update(vector<int>& AIB, int node, const int& value, const int& N){
while(node <= N){
AIB[node] += value;
node += node & (-node);
}
}
int sum(const vector<int>& AIB, int node){
int sum = 0;
while(node > 0){
sum += AIB[node];
node -= node & (-node);
}
return sum;
}
int bsearch(int low, int high, const vector<int>& AIB, const int& target){
int pos = -1;
while(low <= high){
int mid = (low + high) / 2;
int currsum = sum(AIB, mid);
if(currsum < target)
low = mid + 1;
else if(currsum > target)
high = mid - 1;
else{
pos = mid;
high = mid - 1;
}
}
return pos;
}
int main(){
ifstream cin("aib.in");
ofstream cout("aib.out");
int N, M;
cin >> N >> M;
vector<int> AIB(N + 1, 0);
for(int i = 0, x; i < N; ++i){
cin >> x;
update(AIB, i + 1, x, N);
}
for(int op, x, y; M > 0; --M){
cin >> op;
switch(op){
case 0:
cin >> x >> y;
update(AIB, x, y, N);
break;
case 1:
cin >> x >> y;
cout << sum(AIB, y) - sum(AIB, x - 1) << "\n";
break;
case 2:
cin >> x;
cout << bsearch(1, N, AIB, x) << "\n";
break;
default:
break;
}
}
cin.close();
cout.close();
return 0;
}