zoukankan      html  css  js  c++  java
  • 线段树

    the easy xds code is follow:

    #include <cstdio>
    #include <iostream>
    #define int long long int
    using namespace std;
    
    const int N = 1e5+10;
    
    int n, m;
    int a[N], b[N<<2], d[N<<2];
    
    inline void build (int s, int t, int p) {
       if (s == t) {d[p] = a[s]; return;}
       int m = (s+t)>>1;
       build (s, m, p<<1), build (m+1, t, (p<<1)|1);
       d[p] = d[p<<1] + d[(p<<1)|1];
    }
    
    inline void update (int l, int r, int c, int s, int t, int p) {
       if (l <= s && t <= r) {
          d[p] += (t-s+1)*c;
          b[p] += c;
          return;
       }
       int m = (s+t)>>1;
       if (b[p]) {
          d[p<<1] += (m-s+1)*b[p], d[(p<<1)|1] += (t-m)*b[p];
          b[p<<1] += b[p], b[(p<<1)|1] += b[p];
          b[p] = 0;
       }
       if (l <= m) update (l, r, c, s, m, p<<1);
       if (r > m) update (l, r, c, m+1, t, (p<<1)|1);
       d[p] = d[p<<1] + d[(p<<1)|1];
       return;
    }
    
    inline int getsum (int l, int r, int s, int t, int p) {
       if (l <= s && t <= r) return d[p];
       int m = (s+t)>>1, sum = 0;
       if (b[p]) {
          d[p<<1] += (m-s+1)*b[p], d[(p<<1)|1] += (t-m)*b[p];
          b[p<<1] += b[p], b[(p<<1)|1] += b[p];
          b[p] = 0;
       }
       if (l <= m) sum += getsum (l, r, s, m, p<<1);
       if (r > m) sum += getsum (l, r, m+1, t, (p<<1)|1);
       return sum;
    }
    
    main () {
       cin >> n >> m;
       for (int i =1 ; i <= n; ++i) cin >> a[i];
       build (1, n, 1);
       for (int i = 1; i <= m; ++ i) {
          int opt, x, y, k;
          cin >> opt >> x >> y;
          if (opt == 1) cin >> k, update (x, y, k, 1, n, 1);
          else cout << getsum (x, y, 1, n, 1) << '
    ';
       }
       return 0;
    }
    

    and the code with the 'mul' is follow:

    #include <cstdio>
    #include <iostream>
    #define int long long int
    using namespace std;
    
    const int N = 1e5+10;
    
    int n, m, mod;
    int a[N], b[N<<2], d[N<<2], y[N<<2];
    
    inline void pd (int p, int s, int t) {
       int l = p<<1, r = (p<<1)|1, m = (s+t)>>1;
       if (y[p] != 1) {
          y[l] *= y[p], y[r] *= y[p];
          b[l] *= y[p], b[r] *= y[p];
          d[l] *= y[p], d[r] *= y[p];
          y[l] %= mod, y[r] %= mod;
          b[l] %= mod, b[r] %= mod;
          d[l] %= mod, d[r] %= mod;
          y[p] = 1;
       }
       if (b[p] != 0) {
          d[l] += (m-s+1)*b[p], d[r] += (t-m)*b[p];
          b[l] += b[p], b[r] += b[p];
          d[l] %= mod, d[r] %= mod;
          b[l] %= mod, b[r] %= mod;
          b[p] = 0;
       }
       return;
    }
    
    inline void build (int s, int t, int p) {
       y[p] = 1;
       if (s == t) {d[p] = a[s]%mod; return;}
       int m = (s+t)>>1;
       build (s, m, p<<1), build (m+1, t, (p<<1)|1);
       d[p] = (d[p<<1] + d[(p<<1)|1])%mod;
    }
    
    inline void chenge (int l, int r, int c, int s, int t, int p) {
       if (l <= s && t <= r) {
          d[p] *= c, d[p] %= mod; 
          b[p] *= c, b[p] %= mod;
          y[p] *= c, y[p] %= mod;
          return;
       }
       int m = (s+t)>>1;
       pd (p, s, t);
       if (l <= m) chenge (l, r, c, s, m, p<<1);
       if (r > m) chenge (l, r, c, m+1, t, (p<<1)|1);
       d[p] = (d[p<<1] + d[(p<<1)|1])%mod;
    }
    
    inline void add (int l, int r, int c, int s, int t, int p) {
       if (l <= s && t <= r) {
          d[p] += (t-s+1)*c, d[p] %= mod;
          b[p] += c, b[p] %= mod;
          return;
       }
       int m = (s+t)>>1;
       pd (p, s, t);
       if (l <= m) add (l, r, c, s, m, p<<1);
       if (r > m) add (l, r, c, m+1, t, (p<<1)|1);
       d[p] = (d[p<<1] + d[(p<<1)|1])%mod;	
    }
    
    inline int getsum (int l, int r, int s, int t, int p) {
       if (l <= s && t <= r) return d[p]%mod;
       int m = (s+t)>>1, sum = 0;
       pd (p, s, t);
       if (l <= m) sum += getsum (l, r, s, m, p<<1)%mod;
       if (r > m) sum += getsum (l, r, m+1, t, (p<<1)|1)%mod;
       return sum;
    }
    
    main () {
       cin >> n >> m >> mod;
       for (int i = 1; i <= n; ++ i) cin >> a[i];
       build (1, n, 1);
       for (int i = 1; i <= m; ++ i) {
          int opt, x, y, k;
          cin >> opt >> x >> y;
          if (opt == 1) cin >> k, chenge (x, y, k, 1, n, 1);
          if (opt == 2) cin >> k, add (x, y, k, 1, n, 1);
          if (opt == 3) cout << getsum (x, y, 1, n, 1)%mod << '
    ';
       }
       return 0;
    }
    

    the thought is easy

    but there is some details

    PS:

    1.if must with the '(return)'

    2.'<<' and '>>'

    3.'update' and 'add' and 'chenge' need
    (d[p] = d[p<<1] + d[(p<<1)|1])

    4.(y[p]) need put on the first of the (build)

    5.(int) && (long long)

  • 相关阅读:
    iOS开发之--隐藏状态栏
    iOS开发之--iPhone X 适配:MJRefresh上拉加载适配
    iOS开发之--为UITextField监听数值变化的三种方法
    ios开发之--为父view上的子view添加阴影
    iOS开发之--在UIWindow上展示/移除一个View
    iOS开发之--Masonry多个平均布局
    CocoaPods更新过程中出现的坑及解决方法
    那些已成定局的人和事
    两个陌生人的对话
    好好写代码吧,没事别瞎B去创业!
  • 原文地址:https://www.cnblogs.com/yszhyhm/p/13365594.html
Copyright © 2011-2022 走看看