Problem: https://cses.fi/alon/task/1651
#include <iostream>
#include <vector>
using namespace std;
int n;
int q;
int type;
long long arr[200002];
vector<long long> tree;
void change(int k, long long x) {
k += n + 1;
tree[k] += x;
for (k /= 2; k >= 1; k /= 2) {
tree[k] = tree[2 * k] + tree[2 * k + 1];
}
}
long long query(int l, int r) {
l += n + 1;
r += n + 1;
long long sum = 0;
while (l <= r) {
if (l % 2 == 1) sum += tree[l++];
if (r % 2 == 0) sum += tree[r--];
l /= 2;
r /= 2;
}
return sum;
}
void build() {
for (int i = 1; i <= n; i++) {
change(i, arr[i] - arr[i - 1]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
tree.resize(2 * (n + 1), 0);
build();
int a, b, k;
long long u;
while (q--) {
cin >> type;
if (type == 1) {
cin >> a >> b >> u;
change(a, u);
change(b + 1, -u);
} else {
cin >> k;
cout << query(1, k) << '\n';
}
}
return 0;
}
Top comments (0)