|
| 1 | + |
| 2 | +//------------------------------------------------------------------------------ |
| 3 | +#include <iostream> |
| 4 | +#include <vector> |
| 5 | +// #include <bits/stdc++.h> |
| 6 | +// #include <cmath> |
| 7 | +// #include <algorithm> |
| 8 | +// #include <unordered_map> |
| 9 | +// #include <map> |
| 10 | +// #include <set> |
| 11 | +// #include <unordered_set> |
| 12 | +//------------------------------------------------------------------------------ |
| 13 | +using namespace std; |
| 14 | +//------------------------------------------------------------------------------ |
| 15 | +#define FastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); |
| 16 | +#define v(Type) vector<Type> |
| 17 | +#define w(T) \ |
| 18 | + int T; \ |
| 19 | + cin >> T; \ |
| 20 | + while (T--) |
| 21 | +#define int long long int |
| 22 | +#define mod 1000000007ll |
| 23 | +#define endl "\n" |
| 24 | +//------------------------------------------------------------------------------ |
| 25 | +// Any fucntion can be called using Math.function_name(); |
| 26 | +//------------------------------------------------------------------------------ |
| 27 | +class Math |
| 28 | +{ |
| 29 | +public: |
| 30 | + //Returns gcd of two numbers |
| 31 | + int gcd(int a, int b) |
| 32 | + { |
| 33 | + return (a % b == 0) ? b : gcd(b, a % b); |
| 34 | + } |
| 35 | + |
| 36 | + //Returns lcm of two numbers |
| 37 | + int lcm(int a, int b) |
| 38 | + { |
| 39 | + return a * (b / gcd(a, b)); |
| 40 | + } |
| 41 | + |
| 42 | + // Returns flag array isPrime |
| 43 | + // isPrime[i] = true (if i is Prime) |
| 44 | + // isPrime[i] = false (if i is not Prime) |
| 45 | + vector<bool> *seiveOfEratosthenes(const int N) |
| 46 | + { |
| 47 | + vector<bool> *isPrime = new vector<bool>(N + 1, true); |
| 48 | + (*isPrime)[0] = (*isPrime)[1] = false; |
| 49 | + for (int i = 2; i * i <= N; ++i) |
| 50 | + if ((*isPrime)[i]) |
| 51 | + for (int j = i * i; j <= N; j += i) |
| 52 | + (*isPrime)[j] = false; |
| 53 | + |
| 54 | + return isPrime; |
| 55 | + } |
| 56 | + |
| 57 | + //Returns (x ^ n) |
| 58 | + int pow(const int &x, int n) |
| 59 | + { |
| 60 | + if (n == 0) |
| 61 | + return 1; |
| 62 | + int h = pow(x, n / 2); |
| 63 | + return (n & 1) ? h * h * x : h * h; |
| 64 | + } |
| 65 | + |
| 66 | + //Returns (x ^ n) % M |
| 67 | + int pow(const int &x, int n, const int &M) |
| 68 | + { |
| 69 | + if (n == 0) |
| 70 | + return 1; |
| 71 | + int h = pow(x, n / 2) % M; |
| 72 | + return (n & 1) ? (h * h * x) % M : (h * h) % M; |
| 73 | + } |
| 74 | + |
| 75 | + //Returns all Primes <= N |
| 76 | + vector<int> *primesUptoN(const int N) |
| 77 | + { |
| 78 | + vector<bool> *isPrime = seiveOfEratosthenes(N); |
| 79 | + vector<int> *Primes = new vector<int>; |
| 80 | + if (2 <= N) |
| 81 | + (*Primes).push_back(2); |
| 82 | + for (int i = 3; i <= N; i += 2) |
| 83 | + if ((*isPrime)[i]) |
| 84 | + (*Primes).push_back(i); |
| 85 | + return Primes; |
| 86 | + } |
| 87 | + |
| 88 | +} Math; |
| 89 | +//------------------------------------------------------------------------------ |
| 90 | + |
| 91 | +struct SegTree |
| 92 | +{ |
| 93 | + v(int) t; |
| 94 | + int size; |
| 95 | + |
| 96 | + int init = 0; |
| 97 | + |
| 98 | + int f(int &a, int &b) |
| 99 | + { |
| 100 | + return a + b; |
| 101 | + } |
| 102 | + |
| 103 | + SegTree(v(int) & A) |
| 104 | + { |
| 105 | + size = 1; |
| 106 | + int n = A.size(); |
| 107 | + while (size < n) |
| 108 | + size *= 2; |
| 109 | + t.assign(2 * size, INT32_MAX); |
| 110 | + build(A, 0, 0, size); |
| 111 | + } |
| 112 | + |
| 113 | + void build(v(int) & A, int x, int lx, int rx) |
| 114 | + { |
| 115 | + if (rx - lx == 1) |
| 116 | + { |
| 117 | + if (lx < A.size()) |
| 118 | + t[x] = A[lx]; |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + int m = (lx + rx) >> 1; |
| 123 | + |
| 124 | + build(A, 2 * x + 1, lx, m); |
| 125 | + build(A, 2 * x + 2, m, rx); |
| 126 | + |
| 127 | + t[x] = f(t[2 * x + 1], t[2 * x + 2]); |
| 128 | + } |
| 129 | + |
| 130 | + void set(int &i, int &v, int x, int lx, int rx) |
| 131 | + { |
| 132 | + if (rx - lx == 1) |
| 133 | + { |
| 134 | + t[x] = v; |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + int m = (lx + rx) >> 1; |
| 139 | + |
| 140 | + if (i < m) |
| 141 | + set(i, v, 2 * x + 1, lx, m); |
| 142 | + else |
| 143 | + set(i, v, 2 * x + 2, m, rx); |
| 144 | + |
| 145 | + t[x] = f(t[2 * x + 1], t[2 * x + 2]); |
| 146 | + } |
| 147 | + |
| 148 | + int query(int &l, int &r, int x, int lx, int rx) |
| 149 | + { |
| 150 | + if (lx >= r or l >= rx) |
| 151 | + return init; |
| 152 | + |
| 153 | + if (l <= lx and rx <= r) |
| 154 | + return t[x]; |
| 155 | + |
| 156 | + int m = (lx + rx) >> 1; |
| 157 | + |
| 158 | + int ans1 = query(l, r, 2 * x + 1, lx, m); |
| 159 | + int ans2 = query(l, r, 2 * x + 2, m, rx); |
| 160 | + |
| 161 | + return f(ans1, ans2); |
| 162 | + } |
| 163 | + |
| 164 | + void set(int &i, int &v) |
| 165 | + { |
| 166 | + set(i, v, 0, 0, size); |
| 167 | + } |
| 168 | + |
| 169 | + int query(int &l, int &r) |
| 170 | + { |
| 171 | + return query(l, r, 0, 0, size); |
| 172 | + } |
| 173 | +}; |
| 174 | + |
| 175 | +void solve() |
| 176 | +{ |
| 177 | + int n, q; |
| 178 | + cin >> n >> q; |
| 179 | + |
| 180 | + v(int) A(n); |
| 181 | + for (int &x : A) |
| 182 | + cin >> x; |
| 183 | + |
| 184 | + SegTree st(A); |
| 185 | + |
| 186 | + while (q--) |
| 187 | + { |
| 188 | + int op; |
| 189 | + cin >> op; |
| 190 | + |
| 191 | + if (op == 1) |
| 192 | + { |
| 193 | + int i, v; |
| 194 | + cin >> i >> v; |
| 195 | + st.set(i, v); |
| 196 | + } |
| 197 | + else |
| 198 | + { |
| 199 | + int l, r; |
| 200 | + cin >> l >> r; |
| 201 | + cout << st.query(l, r) << endl; |
| 202 | + } |
| 203 | + } |
| 204 | +} |
| 205 | +//------------------------------------------------------------------------------ |
| 206 | +int32_t main() |
| 207 | +{ |
| 208 | + FastIO; |
| 209 | + |
| 210 | + // w(T) |
| 211 | + solve(); |
| 212 | + |
| 213 | + return 0; |
| 214 | +} |
| 215 | +//------------------------------------------------------------------------------ |
0 commit comments