diff --git a/C++/segmentTree.cpp b/C++/segmentTree.cpp deleted file mode 100644 index 2ae9fc0..0000000 --- a/C++/segmentTree.cpp +++ /dev/null @@ -1,78 +0,0 @@ -class Node -{ -public: - -}; - - -Node merge(Node a, Node b) -{ - -} - -// 0 based indexing -template -class SegmentTree -{ -public: - v tree; - T identity; - int N; // size of the given array - - SegmentTree(int n, T identity) - { - N = n; - this->identity = identity; - tree.assign(4 * N, identity); - } - - // update a[qIdx] = val - void update(int qIdx, T val) - { - function f = [&](int tIdx, int tL, int tR) - { - if(tL == tR) - { - tree[tIdx] = val; - return; - } - - int tM = (tL + tR) / 2; - if(tL <= qIdx and qIdx <= tM) - { - f(2*tIdx + 1, tL, tM); - } - else - { - f(2*tIdx + 2, tM + 1, tR); - } - - // leftChild: 2*tIdx + 1, tL, tM - // rightChild: 2*tIdx + 2, tM + 1, tR - tree[tIdx] = merge(tree[2*tIdx + 1], tree[2*tIdx + 2]); - }; - f(0, 0, N - 1); - } - - T query(int l, int r) - { - function f = [&](int tIdx, int tL, int tR) - { - // no overlap - if(l > tR or r < tL) - { - return identity; - } - // complete overlap - if(l <= tL and tR <= r) - { - return tree[tIdx]; - } - - // partial overlap - int tM = (tL + tR) / 2; - return merge(f(2*tIdx+1, tL, tM), f(2*tIdx+2, tM+1, tR)); - }; - return f(0, 0, N - 1); - } -}; \ No newline at end of file diff --git a/C++/segmentTreeForSum.cpp b/C++/segmentTreeForSum.cpp new file mode 100644 index 0000000..f32752f --- /dev/null +++ b/C++/segmentTreeForSum.cpp @@ -0,0 +1,132 @@ +#include +using namespace std; + +#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) +#define MOD 1000000007 +#define MOD1 998244353 +#define INF 1e18 +#define endl "\n" +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define mem(a, b) memset(a, b, sizeof(a)) +#define int long long int + + +typedef long long ll; +typedef unsigned long long ull; +typedef long double lld; + +// segmentTree + +class segmentTree{ +public: + vector tree; + int identity = 0; + int N; + + int merge(int a, int b){ + return a + b; + } + segmentTree(int size, int identity){ + N = size; + this->identity = identity; + tree.assign(4 * N, identity); + } + + + // update query + + void update(int qIdx, int val){ + + + // lambda function + function f = [&](int tIdx, int tL, int tR){ + + // base case + if(tL == tR){ + tree[tIdx] = val; + return; + } + + int tM = tL + (tR - tL) / 2; + + // conditions + + if(tL <= qIdx && qIdx <= tM){ + // go left + f(2 * tIdx + 1, tL, tM); + } + else{ + // go right + f(2 * tIdx + 2, tM + 1, tR); + } + + tree[tIdx] = merge(tree[2 * tIdx + 1], tree[2 * tIdx + 2]); + }; + + f(0, 0, N - 1); + } + + + int query(int l, int r){ + + function f = [&](int tIdx, int tL, int tR){ + + // no overlap + + if(tR < l || r < tL){ + return identity; + } + + // complete overlap + + if(l <= tL && tR <= r ){ + return tree[tIdx]; + } + + // partial overlap + int tM = tL + (tR - tL) / 2; + return merge(f(2 * tIdx + 1, tL, tM), f(2 * tIdx + 2, tM + 1, tR)); + }; + + return f(0, 0, N - 1); + } +}; +void solve() { + int n, q; + cin >> n >> q; + + segmentTree st(n, 0); + + for(int i = 0; i < n; i++){ + int x; cin >> x; + st.update(i, x); + } + while(q--){ + int t, l, r; + cin >> t >> l >> r; + if(t == 1){ + st.update(l, r); + } + else + cout << st.query(l, r - 1) << endl; + } + + +} + +signed main() { + fastio(); + int t = 1; + while (t--) { + solve(); + } + return 0; +}