zoukankan      html  css  js  c++  java
  • 线段树板子(刘汝佳)

    https://www.cnblogs.com/tsw123/p/4374728.html

    点修改

    Update(x,v):  把A[x]修改为v

    Query(L,R): 计算区间[L,R] 最小值.

    // Dynamic RMQ
    // Rujia Liu
    // 输入格式:
    // n m    数组范围是a[1]~a[n],初始化为0。操作有m个
    // 1 p v  表示设a[p]=v
    // 2 L R  查询a[L]~a[R]的min
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    const int INF = 1000000000;
    const int maxnode = 1<<17;
    
    int op, qL, qR, p, v;  //qL和qR为全局变量,询问区间[qL,qR];
    
    struct IntervalTree {
      int minv[maxnode];
    
      void update(int o, int L, int R) {
        int M = L + (R-L)/2;
        if(L == R) minv[o] = v; // 叶结点,直接更新minv
        else {
          // 先递归更新左子树或右子树
          if(p <= M) update(o*2, L, M); else update(o*2+1, M+1, R);
          // 然后计算本结点的minv
          minv[o] = min(minv[o*2], minv[o*2+1]);
        }
      }
    
      int query(int o, int L, int R) {
        int M = L + (R-L)/2, ans = INF;
        if(qL <= L && R <= qR) return minv[o]; // 当前结点完全包含在查询区间内
        if(qL <= M) ans = min(ans, query(o*2, L, M)); // 往左走
        if(M < qR) ans = min(ans, query(o*2+1, M+1, R)); // 往右走
        return ans;
      }
    };
    
    
    IntervalTree tree;
    
    int main() {
      int n, m;
      while(scanf("%d%d", &n, &m) == 2) {
        memset(&tree, 0, sizeof(tree));
        while(m--) {
          scanf("%d", &op);
          if(op == 1) {
            scanf("%d%d", &p, &v);
            tree.update(1, 1, n);  // 修改树节点,或者是建树的过程
          } else {
            scanf("%d%d", &qL, &qR);  //修改询问区间
            printf("%d
    ", tree.query(1, 1, n));
          }
        }
      }
      return 0;
    }
    View Code

    区间修改

    一段区间加上一个数求最大值、最小值、和

    // Fast Sequence Operations I
    // Rujia Liu
    // 输入格式:
    // n m     数组范围是a[1]~a[n],初始化为0。操作有m个
    // 1 L R v 表示设a[L]+=v, a[L+1]+v, ..., a[R]+=v
    // 2 L R   查询a[L]~a[R]的sum, min和max
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    const int maxnode = 1<<17;
    
    int _sum, _min, _max, op, qL, qR, v; //<span style="color:#ff0000;">_sum为全局变量</span>
    
    struct IntervalTree {
      int sumv[maxnode], minv[maxnode], maxv[maxnode], addv[maxnode];
    
      // 维护信息
      void maintain(int o, int L, int R) {
        int lc = o*2, rc = o*2+1;
        sumv[o] = minv[o] = maxv[o] = 0;
        if(R > L) {
          sumv[o] = sumv[lc] + sumv[rc];
          minv[o] = min(minv[lc], minv[rc]);
          maxv[o] = max(maxv[lc], maxv[rc]);
        }
        if(addv[o]) { minv[o] += addv[o]; maxv[o] += addv[o]; sumv[o] += addv[o] * (R-L+1); }
      }
    
      void update(int o, int L, int R) {
        int lc = o*2, rc = o*2+1;
        if(qL <= L && qR >= R) { // 递归边界
          addv[o] += v; // 累加边界的add值
        } else {
          int M = L + (R-L)/2;
          if(qL <= M) update(lc, L, M);
          if(qR > M) update(rc, M+1, R);
        }
        maintain(o, L, R); // 递归结束前重新计算本结点的附加信息
      }
    
      void query(int o, int L, int R, int add) {
        if(qL <= L && qR >= R) { // 递归边界:用边界区间的附加信息更新答案
          _sum += sumv[o] + add * (R-L+1);
          _min = min(_min, minv[o] + add);
          _max = max(_max, maxv[o] + add);
        } else { // 递归统计,累加参数add
          int M = L + (R-L)/2;
          if(qL <= M) query(o*2, L, M, add + addv[o]);
          if(qR > M) query(o*2+1, M+1, R, add + addv[o]);
        }
      }
    };
    
    const int INF = 1000000000;
    
    IntervalTree tree;
    
    int main() {
      int n, m;
      while(scanf("%d%d", &n, &m) == 2) {
        memset(&tree, 0, sizeof(tree));
        while(m--) {
          scanf("%d%d%d", &op, &qL, &qR);
          if(op == 1) {
            scanf("%d", &v);
            tree.update(1, 1, n);
          } else {
            _sum = 0; _min = INF; _max = -INF;
            tree.query(1, 1, n, 0);
            printf("%d %d %d
    ", _sum, _min, _max);
          }
        }
      }
      return 0;
    }
    View Code

    修改区间的值

    求区间的和,最大值,最小值

    // Fast Sequence Operations II
    // Rujia Liu
    // 输入格式:
    // n m     数组范围是a[1]~a[n],初始化为0。操作有m个
    // 1 L R v 表示设a[L]=a[L+1]=...=a[R] = v。其中v > 0
    // 2 L R  查询a[L]~a[R]的sum, min和max
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    const int maxnode = 1<<17;
    
    int _sum, _min, _max, op, qL, qR, v;
    
    struct IntervalTree {
      int sumv[maxnode], minv[maxnode], maxv[maxnode], setv[maxnode];
    
      // 维护信息
      void maintain(int o, int L, int R) {
        int lc = o*2, rc = o*2+1;
        if(R > L) {
          sumv[o] = sumv[lc] + sumv[rc];
          minv[o] = min(minv[lc], minv[rc]);
          maxv[o] = max(maxv[lc], maxv[rc]);
        }
        if(setv[o] >= 0) { minv[o] = maxv[o] = setv[o]; sumv[o] = setv[o] * (R-L+1); }
      }
    
      // 标记传递
      void pushdown(int o) {
        int lc = o*2, rc = o*2+1;
        if(setv[o] >= 0) { //本结点有标记才传递。注意本题中set值非负,所以-1代表没有标记
          setv[lc] = setv[rc] = setv[o];
          setv[o] = -1; // 清除本结点标记
        }
      }
    
      void update(int o, int L, int R) {
        int lc = o*2, rc = o*2+1;
        if(qL <= L && qR >= R) { // 标记修改
          setv[o] = v;
        } else {
          pushdown(o);
          int M = L + (R-L)/2;
          if(qL <= M) update(lc, L, M); else maintain(lc, L, M);
          if(qR > M) update(rc, M+1, R); else maintain(rc, M+1, R);
        }
        maintain(o, L, R);
      }
    
      void query(int o, int L, int R) {
        if(setv[o] >= 0) { // 递归边界1:有set标记
          _sum += setv[o] * (min(R,qR)-max(L,qL)+1);
          _min = min(_min, setv[o]);
          _max = max(_max, setv[o]);
        } else if(qL <= L && qR >= R) { // 递归边界2:边界区间
          _sum += sumv[o]; // 此边界区间没有被任何set操作影响
          _min = min(_min, minv[o]);
          _max = max(_max, maxv[o]);
        } else { // 递归统计
          int M = L + (R-L)/2;
          if(qL <= M) query(o*2, L, M);
          if(qR > M) query(o*2+1, M+1, R);
        }
      }
    };
    
    const int INF = 1000000000;
    
    IntervalTree tree;
    
    int main() {
      int n, m;
      while(scanf("%d%d", &n, &m) == 2) {
        memset(&tree, 0, sizeof(tree));
        memset(tree.setv, -1, sizeof(tree.setv));
        tree.setv[1] = 0;
        while(m--) {
          scanf("%d%d%d", &op, &qL, &qR);
          if(op == 1) {
            scanf("%d", &v);
            tree.update(1, 1, n);
          } else {
            _sum = 0; _min = INF; _max = -INF;
            tree.query(1, 1, n);
            printf("%d %d %d
    ", _sum, _min, _max);
          }
        }
      }
      return 0;
    }
    View Code

    最后附上蒟蒻自己写的板子,可能有不对的地方,欢迎指正。

    #include<bits/stdc++.h>
    #define _for(i,a,b) for(int i=a;i<=b;i++)
    using namespace std;
    typedef long long ll;
    const int mod =1e6+7;
    double esp=1e-6;
    int INF =0x3f3f3f3f;
    const int inf = 1<<28;
    const int MAXN=1e5+5;
    struct ST
    {
        int num,_max,_min,_sum,l,r,lz;
        ST()
        {
            lz=0;
        }
    }tree[MAXN*4];
    void build(int l,int r,int id)
    {
        tree[id].l=l;
        tree[id].r=r;
        tree[id].lz=0;
        if(l==r)
        {
            scanf("%d",&tree[id].num);
            tree[id]._sum=tree[id].num;
            tree[id]._max=tree[id].num;
            tree[id]._min=tree[id].num;
            //printf("%d:%d
    ",id,tree[id].num);
            return ;
        }
        int mid=(l+r)>>1;
        build(l,mid,id*2);
        build(mid+1,r,id*2+1);
        tree[id]._sum=tree[id*2]._sum+tree[id*2+1]._sum;
        //printf("%d:%d
    ",id,tree[id].)
        tree[id]._max=max(tree[id*2]._max,tree[id*2+1]._max);
        tree[id]._min=min(tree[id*2]._min,tree[id*2+1]._min);
        //printf("%d:%d %d
    ",id,tree[id]._max,tree[id]._min);
    }
    void update(int id,int k,int num)//单点修改
    {
        if(tree[id].l==tree[id].r)
        {
            //printf("**%d
    ",k);
            tree[id].num=num;
            return ;
        }
        if(k<=tree[id*2].r)update(id*2,k,num);
        else update(id*2+1,k,num);
        tree[id]._sum=tree[id*2]._sum+tree[id*2+1]._sum;
        tree[id]._max=max(tree[id*2]._max,tree[id*2+1]._max);
        tree[id]._min=min(tree[id*2]._min,tree[id*2+1]._min);
    }
    int Seach(int id,int k)//单点查询
    {
        if(tree[id].l==tree[id].r)
            return tree[id].num;
        if(k<=tree[2*id].r)
            Seach(id*2,k);
        else
            Seach(id*2+1,k);
    
    }
    void push_down(int id)//下放lz标记
    {
        if(tree[id].lz!=0)
        {
            tree[id*2].lz+=tree[id].lz;
            tree[id*2+1].lz+=tree[id].lz;
            int mid=(tree[id].l+tree[id].r)>>1;
            tree[id*2]._sum+=tree[id*2].lz*(mid-tree[id].l+1);
            tree[id*2+1]._sum+=tree[id*2].lz*(tree[id].r-mid);
            tree[id*2]._max+=tree[id].lz;
            tree[id*2+1]._max+=tree[id].lz;
            tree[id*2]._min+=tree[id].lz;
            tree[id*2+1]._min+=tree[id].lz;
            tree[id].lz=0;
        }
        return ;
    }
    void updata_Qu(int l,int r,int id,int num)//l-r区间加上num
    {
        if(tree[id].r<=r&&tree[id].l>=l)
        {
            tree[id]._sum+=num*(tree[id].r-tree[id].l+1);
            tree[id]._max=max(tree[id].r+num,tree[id].l+num);
            tree[id]._min=min(tree[id].r+num,tree[id].l+num);
            tree[id].lz+=num;
            return ;
        }
        push_down(id);
        if(tree[id*2].r>=l)
            updata_Qu(l,r,id*2,num);
        if(tree[id*2+1].l<=r)
            updata_Qu(l,r,id*2+1,num);
        tree[id]._sum=tree[id*2]._sum+tree[id*2+1]._sum;
        tree[id]._max=max(tree[id*2]._max,tree[id*2+1]._max);
        tree[id]._min=min(tree[id*2]._min,tree[id*2+1]._min);
        return ;
    }
    int Ma,Mi;
    int Seach_Qu(int l,int r,int id)//区间查找
    {
        if(tree[id].l>=l&&tree[id].r<=r)
        {
            Ma=max(tree[id]._max,Ma);
            Mi=min(tree[id]._min,Mi);
            return tree[id]._sum;
        }
        push_down(id);
        int s=0;
        if(tree[2*id].r>=l){s+=Seach_Qu(l,r,id*2);}//Mi=min(Mi,tree[id*2]._min);Ma=max(Ma,tree[id*2]._max);}
        if(tree[2*id+1].l<=r){s+=Seach_Qu(l,r,id*2+1);}//Mi=min(Mi,tree[id*2+1]._min);Ma=max(Ma,tree[id*2+1]._max);}
        return s;
    }
    int main()
    {
        int n;
        scanf("%d",&n);
        build(1,n,1);
        updata_Qu(1,n,1,4);//1-n区间加上4
        Ma=-INF,Mi=INF;
        int mm=Seach_Qu(2,5,1);//查找[2-5]修改完后的和,最大值,最小值
        printf("%d %d %d
    ",mm,Ma,Mi);
    //单点修改
    //    update(1,4,0);
    //    printf("%d
    ",Seach(1,4));
        return 0;
    }
    View Code
  • 相关阅读:
    ajax 发送请求无法重定向问题
    网页中转跳转的几种方式
    后台返回的HTML整个页面代码打开方法
    Json对象与Json字符串的转化、JSON字符串与Java对象的转换
    SpringMVC ModelAndView跳转失效
    springMVC中前台ajax传json数据后台controller接受对象为null
    $.ajax 中的contentType
    springMVC--请求的跳转和传值
    Windows NT WinLogon Notify
    虚拟机检测技术剖析
  • 原文地址:https://www.cnblogs.com/kayiko/p/12301264.html
Copyright © 2011-2022 走看看