Pagini recente » Cod sursa (job #2844155) | Cod sursa (job #160791) | Cod sursa (job #29485) | Cod sursa (job #1176016) | Cod sursa (job #3272569)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
void update(int position, int value, int n, vector<long long> &AIB) {
for(int i=position; i<=n; i+= (i & (-i))) {
AIB[i] += value;
}
}
long long partial_sum(int position, int n, vector<long long> &AIB) {
long long sum = 0;
for(int i=position; i>=1; i-= (i & (-i))) {
sum += AIB[i];
}
return sum;
}
long long query(int l, int r, int n, vector<long long> &AIB) {
return 1LL*(partial_sum(r, n, AIB) - partial_sum(l-1, n, AIB));
}
int Binary_Search(int value, int n, vector<long long> &AIB) {
int position = -1;
int left = 1, right = n;
while(left <= right) {
int mid = (left + right) / 2;
int ans = query(1, mid, n, AIB);
if(ans == value) {
return mid;
}
else if(ans > value)
right = mid-1;
else
left = mid+1;
}
return -1;
}
int main() {
int n,q;
fin >> n >> q;
vector<long long> AIB(n+1, 0);
for(int i=1; i<=n; i++) {
int x;
fin >> x;
update(i, x, n, AIB);
}
while(q--) {
int type, value, left, right;
fin >> type;
if(type == 2) {
fin >> value;
fout << Binary_Search(value, n, AIB) << '\n';
continue;
}
fin >> left >> right;
if(type == 0)
update(left, right, n, AIB);
else
fout << query(left, right, n, AIB) << '\n';
}
return 0;
}