C++ || Segment Tree || With Comments ||
\n#define ll int\n#define vll vector<int>\nclass NumArray{\n    public:\n    \n        vll vp;//given array which we are going to use..we will copy it in constr
```\n#define ll int\n#define vll vector<int>\nclass NumArray{\n    public:\n    \n        vll vp;//given array which we are going to use..we will copy it in constructor\n        //v is node number tl and tr are node range...node range means \n        //range that falls under subtree of node v\n        //tl and tr are basically array ranges\n    \n        vll segarr;\n        ll n;\n    \n        ll combine(ll a,ll b){\n            //you have to combine acc to query type like add,min,max..etc;\n            return a + b;\n        }\n    \n        void build(ll v,ll tl,ll tr){\n            if(tl == tr){\n                segarr[v] = vp[tl];\n                return;\n            }\n    \n            ll tm = (tl + tr) / 2;\n            //building tree in 2 seperate parts\n            build(2*v,tl,tm);\n            build(2*v + 1,tm + 1,tr);\n    \n            segarr[v] = combine(segarr[2*v],segarr[2*v + 1]);\n        }\n    \n    \n        void upd(ll v,ll tl,ll tr,ll ind,ll val){\n            //that update point itself\n            if(tl == ind && tr == ind){\n                segarr[v] = val;\n                return;\n            }\n    \n            //we are in wrong region we dont need to update anthing here\n            //zero overlap\n            if(ind > tr || ind < tl){\n                return;\n            }\n    \n            ll tm = (tl + tr) / 2;\n            // now we will update both regions and after updating them we will combine them\n    \n            upd(2*v,tl,tm,ind,val);\n            upd(2*v + 1,tm + 1,tr,ind,val);\n    \n            segarr[v] = combine(segarr[2*v],segarr[2*v + 1]);\n        }   \n    \n        void update(int index, int val) {\n            upd(1,0,n-1,index,val);\n        }\n        //here tl and tr are node ranges and l and r are query ranges which will be given to us\n        ll query(ll v,ll l,ll r,ll tl,ll tr){\n            \n            //out of range i.e. invalid case\n            if(tl > r || l > tr){\n                return 0;//for sum its 0...for min it should be inf..i.e. it should not affect on ans;\n            }\n    \n            //fully within\n            if(l<=tl && r>=tr){\n                return segarr[v];\n            }\n    \n            //partial overlap\n            ll tm = (tl + tr) / 2;\n    \n            //perform 2 queries and combine both ans\n            ll val1 = query(2*v,l,r,tl,tm);\n            ll val2 = query(2*v + 1,l,r,tm + 1,tr);\n    \n            return combine(val1,val2);\n        }\n    \n        int sumRange(int left, int right) {\n            return query(1,left,right,0,n-1);\n        }\n    \n        //0 based indexing segment tree ..value of v always starts from 1\n        NumArray(vector<int> &a){\n            n = a.size();\n            vp.resize(n);\n            vp = a;\n            segarr.resize(4*n);\n            build(1,0,n - 1);\n        }\n};\n\n```
['Tree']