Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void build_tree(vector<int> &t, int v, int l, int r) {
  6.     if (r-l == -1) {
  7.         t[v] = a[l];
  8.         return;
  9.     }
  10.     int m=(l+r)/2;
  11.     build_tree(t, 2*v, l, m);
  12.     build_tree(t, 2*v+1, m, r);
  13.     t[v] = t[2*v] + t[2*v+1];
  14. }
  15.  
  16. void change(vector<int> &t, int v, int n, int pos, int x) {
  17.     if (l>pos || r<=pos)
  18.         return;
  19.     if (r-l == 1) {
  20.         t[v] = x;
  21.         return;
  22.     }
  23.     int m=(l+r)/2;
  24.     change(t, 2*v, l, m, pos, x);
  25.     change(t, 2*v+1, m, r, pos, x);
  26.     t[v] = t[2*v] + t[2*v+1];
  27. }
  28.  
  29. int get_sum(vector<int> &t, vector<int> &val, int v, int l, int r, int lt, int rt) {
  30.     if (lt>=r|| l>=rt)
  31.         return 0;
  32.     if (lt<=l && r<=rt)
  33.         return t[v];
  34.     push(t,val, v, l, r);
  35.     int m=(l+r)/2;
  36.     int lsum=get_sum(t, 2*v, l, m, lt, rt);
  37.     int rsum=get_sem(t, 2*v+1, m, r, lt, rt);
  38.     t[v] = t[2*v] + t[2*v+1];
  39.     return lsum + rsum;
  40. }
  41.  
  42. void add_val(vector<int> &t, vector<int> &val, int v, int l, int r, int x) {
  43.     t[v]=(r-l)*x;
  44.     val[v]+=x;
  45. }
  46.  
  47. void push (vector<int> &t, vector<int> &val, int v, int l, int r) {
  48.     if (r-l == -1)
  49.         return;
  50.     int m=(l+r)/2;
  51.     add_val(t, 2*v, l, m, val[v]);
  52.     add_val(t, 2*v+1, m, r, val[v]);
  53.     val[v] = 0;
  54. }
  55.  
  56. void add_seg(vector<int> &t, vector<int> &val, int v, int l, int r, int lt, int rt, int x) {
  57.     if (lt>=r || l>=rt)
  58.         return;
  59.     if (lt<=l && r<=rt) {
  60.         add_val(t, val, v, l, r, x)
  61.         return;
  62.     }
  63.     push(t, val, v, l, r);
  64.     int m=(l+r)/2;
  65.     add_seg(t, val, 2*v, v, l, m, lt, rt, x);
  66.     add_seg(t, val, 2*v+1, v, m, r, lt, rt, x);
  67.     t[v] = t[2*v] + t[2*v+1];
  68. }
  69.  
  70. int main() {
  71.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  72.     int N=0; cin >> N;
  73.     vector<int> a(N), t1(N), t2(N), val(N);
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement