zoukankan      html  css  js  c++  java
  • HDU6315 Naive Operations 线段树

    (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦

    Catalog

    Problem:Portal传送门

     原题目描述在最下面。

    Solution:

     每个节点用一个变量储存它所覆盖区间最少需要加多少次答案能加1.
     如果这次更新不能使答案加1,则更新到lazy标记就可以了,如果能让答案加1,就更新到叶子节点,具体看代码。

    AC_Code:

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<set>
    #include<stack>
    #include<vector>
    #include<cctype>
    #include<string>
    #include<cmath>
    #define lson rt<<1
    #define rson rt<<1|1
    #define all(x) (x).begin(),(x).end()
    #define mme(a,b) memset((a),(b),sizeof((a)))
    #define fuck(x) cout<<"* "<<x<<"
    "
    #define iis std::ios::sync_with_stdio(false)
    using namespace std;
    typedef long long LL;
    typedef pair<int,int> pii;
    const int MXN = 2e5 + 7;
    const int MXE = 1e6 + 7;
    const int INF = 0x3f3f3f3f;
    int n, m;
    int ar[MXN], br[MXN];
    struct lp{
      int l, r;
      int nd, sum, lazy;
    }cw[MXN<<2];
    void push_up(int rt){
      cw[rt].nd = min(cw[lson].nd, cw[rson].nd);
      cw[rt].sum = cw[lson].sum + cw[rson].sum;
    }
    void build(int l,int r,int rt){
      cw[rt].l = l; cw[rt].r = r;
      cw[rt].sum = cw[rt].lazy = 0;
      if(l == r){
        cw[rt].nd = br[l];
        return;
      }
      int mid = (l + r)>>1;
      build(l,mid,lson);build(mid+1,r,rson);
      push_up(rt);
    }
    void push_down(int rt){
      if(cw[rt].lazy){
        cw[lson].lazy += cw[rt].lazy;
        cw[rson].lazy += cw[rt].lazy;
        cw[lson].nd -= cw[rt].lazy;
        cw[rson].nd -= cw[rt].lazy;
        cw[rt].lazy = 0;
      }
    }
    void update(int L,int R,int l,int r,int rt){
      if(L<=l && r<= R){
        if(cw[rt].nd <= 1){
          if(l == r){
            --cw[rt].nd;
            if(cw[rt].nd <= 0){
              cw[rt].nd = br[l];
              cw[rt].sum++;
            }
            return;
          }
        }else{
          --cw[rt].nd;
          ++cw[rt].lazy;
          return;
        }
      }
      if(l == r)return;
      push_down(rt);
      int mid = (l+r)>>1;
      if(L > mid)update(L,R,mid+1,r,rson);
      else if(R <= mid)update(L,R,l,mid,lson);
      else {
        update(L,mid,l,mid,lson);
        update(mid+1,R,mid+1,r,rson);
      }
      push_up(rt);
    }
    int query(int L, int R,int l,int r,int rt){
      if(L <= l&&r <= R){
        return cw[rt].sum;
      }
      if(l == r)return 0;
      push_down(rt);
      int mid = (l+r)>>1, ans = 0;
      if(L > mid)ans = query(L,R,mid+1,r,rson);
      else if(R <= mid)ans = query(L,R,l,mid,lson);
      else {
        ans = query(L,mid,l,mid,lson);
        ans += query(mid+1,R,mid+1,r,rson);
      }
      return ans;
    }
    int main(int argc, char const *argv[]){
      while(~scanf("%d%d", &n, &m)){
        for(int i = 1; i <= n; ++i){
          scanf("%d", &br[i]);
        }
        char op[10];
        int a, b;
        build(1,n,1);
        while(m--){
          scanf("%s%d%d", op, &a, &b);
          if(op[0] == 'a'){
            update(a, b, 1, n, 1);
          }else{
            printf("%d
    ", query(a, b, 1, n, 1));
          }
        }
      }
      return 0;
    }
    

    ####Problem Description: ![这里写图片描述](https://img-blog.csdn.net/20180910205114800)
  • 相关阅读:
    第 8 章 容器网络
    第 8 章 容器网络
    第 8 章 容器网络
    第 8 章 容器网络
    第 8 章 容器网络
    第 8 章 容器网络
    第 8 章 容器网络
    第 8 章 容器网络
    第 8 章 容器网络
    第 8 章 容器网络
  • 原文地址:https://www.cnblogs.com/Cwolf9/p/9622842.html
Copyright © 2011-2022 走看看