sleepy_coder

Segment_Tree_C++

Jan 2nd, 2019
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.73 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/tree_policy.hpp>
  3. #include <ext/pb_ds/assoc_container.hpp>
  4. #include <ext/pb_ds/detail/standard_policies.hpp>
  5. using namespace   std;
  6. using namespace __gnu_cxx;
  7. using namespace __gnu_pbds;
  8.  
  9. typedef pair<int, int> pii; typedef long long ll; typedef pair<ll, ll> pll;
  10. typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vii; typedef vector<pll> vll;
  11. typedef map<ll, ll> mii; typedef map<string, ll> msi; typedef vector< vi > vvi;
  12.  
  13. #define     sz                 size()
  14. #define     pb                 push_back
  15. #define     inf                (1<<30)
  16. #define     mod                (1000000007)
  17. #define     pi                 (acos(-1.0))
  18. #define     mem(a,b)           memset((a), (b), sizeof(a));
  19. #define     fast_io            ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  20. #define     what_is(x)         cerr << #x << " is " << x << endl;
  21. #define     rep(i, n)          for(__typeof(n) i=0; i<(n); i++)
  22. #define     foab(i, a, b)      for(__typeof(b) i=(a); i<=(b); i++)
  23. #define     foba(i, a, b)      for(__typeof(b) i=(b); i>=(a); i--)
  24. #define     foch(it, l)        for(__typeof((l).begin()) it = begin(l);  it != end(l); it++)
  25. #define     d_value(x)         cout<<fixed<<setprecision(x);
  26. #define     file_io            {                                        \
  27.                                     freopen("input.txt", "r", stdin);   \
  28.                                     freopen("output.txt", "w", stdout); \
  29.                                }
  30. #define error(args...) {                                                            \
  31.         string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');                 \
  32.         stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args);    \
  33.         }
  34.  
  35. void err(istream_iterator<string> it) {}                 template<typename T, typename... Args>
  36. void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); }
  37.  
  38. /**Define BitWise operation**/
  39. #define     checkBit(n, pos)            (n & (1<<(pos)))
  40. #define     bitOn(n, pos)               (n | (1<<(pos)))
  41. #define     bitOff(n, pos)              (n & ~(1<<(pos)))
  42. inline void printBits(int n){ if(n >= 2)printBits(n/2); cout<<(n&1); }
  43. inline int countOnes(int n) { int r=0; while(n && ++r) n -= n&(-n); return r; }
  44. inline int bigMod(ll a, ll b, int M) { int r = (int)(a % M), v = 1; while(b){if(b % 2 == 1)v = (v * r) % M; r = (r * r) % M; b/=2;} return v; }
  45.  
  46. template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  47. template <typename T>
  48. string toString(T n) { stringstream ss; ss << n; return ss.str(); }
  49. template< typename T >
  50. T e_gcd(T a, T b, T& x, T& y) {
  51.     if(!a){ x = 0, y = 1; return b; }
  52.     T x1, y1, gcd = e_gcd<T>(b%a, a, x1, y1);
  53.     x = y1 - (b/a)*x1, y = x1;
  54.     return gcd;
  55. }
  56. template< typename T >
  57. inline T modInverse(T a, T m){ T x, y; T g = e_gcd<T>(a, m, x, y); if(g != 1){ return -1; } else{ return (x%m + m)%m; } }
  58. template<typename T> ostream&
  59. operator<<(ostream& os, const vector<T> &t) { ll n = t.size(); rep(i, n) os<<t[i]<<" "; return os; }
  60. template<typename T,typename TT>
  61. ostream& operator<<(ostream &os, const pair<T,TT> &t) { return os<<"("<<t.first<<","<<t.second<<")"; }
  62.  
  63. enum COLOR{ white=1 , grey=2 , black=3, red = 4, green = 5, blue = 6 };
  64.  
  65. vii dir_4 {{1,0}, {0,1}, {-1,0}, {0,-1}};
  66. vii dir_8 {{0,1}, {1,1}, {1,0}, {1,-1}, {0,-1}, {-1,-1}, {-1,0}, {-1,1}};
  67. vii dir_knight {{2,1}, {1,2}, {-1,2}, {-2,1}, {-2,-1}, {-1,-2}, {1,-2}, {2,-1}};
  68. //-------------- ---------------- ----------------- --------------- ------------ ----------
  69.  
  70.  
  71. //This is a Segment Tree Data structure for Range Query, Point Update
  72. class SegmentTree{
  73.     int* ara, *tree;
  74. public:
  75.  
  76.     explicit SegmentTree(int n, const int _ara[]){
  77.         ara = new int[n+1], tree = new int[4*n+1];
  78.         foab(i, 1, n){
  79.             ara[i] = _ara[i-1];
  80.         }
  81.     }
  82.  
  83.     void init(const int& pos, const int& begin, const int& end){
  84.         if(begin == end){//leaf node
  85.             tree[pos] = ara[end];
  86.         }else{//internal node
  87.             init(pos<<1, begin, (begin+end)>>1);
  88.             init((pos<<1)+1, ((begin+end)>>1)+1, end);
  89.             tree[pos] = tree[pos<<1] + tree[(pos<<1)+1];
  90.         }
  91.     }
  92.  
  93.     void update(const int& pos, const int& begin, const int& end, const int& indx, const int& newVal){
  94.         if(end < indx || indx < begin){//out of boundary
  95.             return;
  96.         }else if(indx<=begin && indx>=end){//relevant segment(index)
  97.             tree[pos] = newVal;
  98.         }else{//partial overlap
  99.             update(pos<<1, begin, (begin+end)>>1, indx, newVal);
  100.             update((pos<<1)+1, ((begin+end)>>1)+1, end, indx, newVal);
  101.             tree[pos] = tree[pos<<1] + tree[(pos<<1)+1];
  102.         }
  103.     }
  104.  
  105.     int query(const int& pos, const int& begin, const int& end, const int& i, const int& j){
  106.         if(i<=begin && end<=j){//relevant segment
  107.             return tree[pos];
  108.         }else if(j<begin || i>end){//out of boundary
  109.             return 0;
  110.         }else{//partial overlap
  111.             return query(pos<<1, begin, (begin+end)>>1, i, j) + query((pos<<1)+1, ((begin+end)>>1)+1, end, i, j);
  112.         }
  113.     }
  114.  
  115.     virtual ~SegmentTree(){
  116.         delete [] ara;
  117.         delete [] tree;
  118.         ara = tree = nullptr;
  119.     }
  120. };
  121.  
  122.  
  123. int main(int argc, char** argv) {
  124.     //d_value(20);
  125.     fast_io
  126.  
  127.     int n = 4, ara[] = {1, 0, 3, 4};
  128.     SegmentTree segmentTree(n, ara);
  129.     segmentTree.init(1, 1, 4);
  130.     segmentTree.print();
  131.     cout<<segmentTree.query(1, 1, 4, 1, 4)<<endl;
  132.     segmentTree.update(1, 1, 4, 2, 2);
  133.     segmentTree.print();
  134.     cout<<segmentTree.query(1, 1, 4, 1, 4)<<endl;
  135.  
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment