Pagini recente » Cod sursa (job #2557434) | Cod sursa (job #3247185) | Cod sursa (job #20835) | Cod sursa (job #1582477) | Cod sursa (job #2947103)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <deque>
#include <queue>
#include <stack>
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
typedef long long ll;
//We assume elements are indexed from 0 to n-1
vector<ll> initializeFenwickTree(const vector<ll>& elements) {
int n = elements.size();
vector<ll> tree(n + 1), sum(n + 1);
for (int i = 1; i <= n; ++i) {
sum[i] = sum[i - 1] + elements[i - 1];
}
for (int i = 1; i <= n; ++i) {
int x = 1;
while (!(i & x)) {
x = x << 1;
}
tree[i] = sum[i] - sum[i - x];
}
return tree;
}
ll sum(const vector<ll>& tree,int position) {
ll sum = 0;
while (position >= 1) {
sum += tree[position];
position -= position & -position;
}
return sum;
}
void add(vector<ll>& tree, int position, ll value) {
int n = tree.size() - 1;
while (position <= n) {
tree[position] += value;
position += position & -position;
}
}
//find the position k such that sum(1,k)=value
//return the position found,or -1 if it doesn't exist
int index(const vector<ll>& tree, int value) {
int left, right, middle, n = tree.size() - 1;
ll s;
left = 1;
right = n + 1;
while (left < right) {
middle = (left + right) / 2;
s = sum(tree, middle);
if (s > value) {
right = middle;
}
else {
left = middle + 1;
}
}
if (right > 1 && sum(tree, right - 1) == value) {
--right;
}
else {
right = -1;
}
return right;
}
void Solve() {
int n, m;
fin >> n >> m;
vector<ll> elements(n);
for (int i = 0; i < n; ++i) {
fin >> elements[i];
}
auto tree = initializeFenwickTree(elements);
for (int i = 1; i <= m; ++i) {
ll t, a, b;
fin >> t;
if (t < 2) {
fin >> a >> b;
}
else {
fin >> a;
}
if (t == 0) {
add(tree, a, b);
}
else if (t == 1) {
fout << sum(tree, b) - sum(tree, a - 1) << '\n';
}
else {
fout << index(tree, a) << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(false);
Solve();
return 0;
}