Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#define _GLIBCXX_DEBUG
- //#include <bits/stdc++.h>
- //dd_definitions("-D_GLIBCXX_DEBUG")
- #include <iostream>
- #include <sstream>
- #include <algorithm>
- #include <vector>
- #include <deque>
- #include <queue>
- #include <string>
- #include <set>
- #include <cmath>
- #include <tuple>
- #include <map>
- #include <random>
- #define int long long
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- const int inf = 1e9 + 7;
- const int mod = 1e9 + 7;
- vector <int> tree, a;
- int f(int x, int y){
- return x + y;
- }
- int n, k, sz=1;
- void build(int i = 0, int l = 0, int r = sz){
- if (r == l + 1) tree[i] = a[l];
- else{
- int m = (r + l) / 2;
- build(2 * i + 1, l, m);
- build(2 * i + 2, m, r);
- tree[i] = f(tree[2 * i + 1], tree[2 * i + 2]);
- }
- }
- int get(int ql, int qr, int v = 0, int vl = 0, int vr = sz){
- if(ql <= vl && vr <= qr) return tree[v];
- if(qr <= vl || vr <= ql) return 0;
- int m = (vr + vl) / 2;
- int v1 = get(ql, qr, 2* v + 1, vl, m);
- int v2 = get(ql, qr, 2* v + 2, m, vr);
- return f(v1, v2);
- }
- void update(int q_i, int val, int v = 0, int vl = 0, int vr = sz){
- if(q_i < vl || q_i >= vr) return;
- if(vr == vl + 1) tree[v] = val;
- else{
- int m = (vr + vl) / 2;
- update(q_i, val, 2 * v + 1, vl, m);
- update(q_i, val, 2 * v + 2, m, vr);
- tree[v] = f(tree[2 * v + 1], tree[2 * v + 2]);
- }
- }
- signed main(){
- cin >> n;
- a.resize(n, 0);
- while (sz < n){
- sz = 2 * sz;
- }
- a.resize(sz, 0);
- tree.resize(2 * sz - 1, 0);
- for (int i = 0; i < n ;i++){
- cin >> a[i];
- }
- build();
- cout << get(1, 3) <<'\n';
- update(2, 100);
- cout << get(2, 3) <<'\n';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement