Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <set>
- #include <cmath>
- #include <map>
- using namespace std;
- const int maxn = 2e5 + 10;
- typedef long long ll;
- int a[maxn];
- ll segment_tree[3 * maxn];
- void build_tree(int L, int R, int position) {
- if(L == R) {
- segment_tree[position] = a[L];
- }
- else {
- int mid = (L + R) / 2;
- build_tree(L, mid, 2 * position);
- build_tree(mid + 1, R, 2 * position + 1);
- segment_tree[position] = segment_tree[2 * position] + segment_tree[2 * position + 1];
- }
- }
- // L R i L R j L R
- ll query(int L, int R, int position, int i, int j) {
- if(R < i or j < L) {
- return 0;
- }
- if(i <= L and R <= j) {
- return segment_tree[position];
- }
- int mid = (L + R) / 2;
- return query(L, mid, 2 * position, i, j) + query(mid + 1, R, 2 * position + 1, i, j);
- }
- void update(int L, int R, int position, int idx, int new_value) {
- if(L == R) {
- segment_tree[position] = new_value;
- return;
- }
- int mid = (L + R) / 2;
- if(idx <= mid) {
- update(L, mid, 2 * position, idx, new_value);
- }
- else {
- update(mid + 1, R, 2 * position + 1, idx, new_value);
- }
- segment_tree[position] = segment_tree[2 * position] + segment_tree[2 * position + 1];
- }
- int main() {
- ios_base::sync_with_stdio(false);
- int n, q;
- cin >> n >> q;
- for(int i = 0; i < n; i++) {
- cin >> a[i];
- }
- build_tree(0, n - 1, 1); // build the segment tree
- for(int i = 0; i < q; i++) {
- int type;
- cin >> type;
- if(type == 1) {
- int u, k;
- cin >> u >> k;
- u--;
- update(0, n - 1, 1, u, k);
- }
- else {
- int x, y;
- cin >> x >> y;
- x--;
- y--;
- cout << query(0, n - 1, 1, x, y) << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement