zoukankan      html  css  js  c++  java
  • HDU

    题意:n个棍子,初始值全为1,给定Q个区间,分别赋值,问n个棍子的总值。

    分析:lazy标记主要体现在update上。

    当l <= L && R <= r时,该结点的子结点值不再更新,取而代之的是给该结点一个lazy值,以记录下来该结点的子结点并没有更新。

    当赋值的区间落在子结点上时,才将lazy标记传递,同时更新子结点相应的sum值。

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define lowbit(x) (x & (-x))
    const double eps = 1e-8;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 100000 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    int sum[MAXN << 2];
    int lazy[MAXN << 2];
    void build(int id, int L, int R, int v){
        if(L == R){
            sum[id] = v;
        }
        else{
            int mid = L + (R - L) / 2;
            build(id << 1, L, mid, v);
            build(id << 1 | 1, mid + 1, R, v);
            sum[id] = sum[id << 1] + sum[id << 1 | 1];
        }
    }
    void pushdown(int id, int L, int R){
        if(lazy[id]){
            lazy[id << 1] = lazy[id << 1 | 1] = lazy[id];
            int mid = L + (R - L) / 2;
            sum[id << 1] = (mid - L + 1) * lazy[id];
            sum[id << 1 | 1] = (R - mid) * lazy[id];
            lazy[id] = 0;
        }
    }
    void update(int l, int r, int id, int L, int R, int v){
        if(l <= L && R <= r){
            sum[id] = (R - L + 1) * v;
            lazy[id] = v;
        }
        else{
            pushdown(id, L, R);
            int mid = L + (R - L) / 2;
            if(l <= mid) update(l, r, id << 1, L, mid, v);
            if(r > mid) update(l, r, id << 1 | 1, mid + 1, R, v);
            sum[id] = sum[id << 1] + sum[id << 1 | 1];
        }
    }
    int main(){
        int T;
        scanf("%d", &T);
        int kase = 0;
        while(T--){
            memset(sum, 0, sizeof sum);
            memset(lazy, 0, sizeof lazy);
            int N;
            scanf("%d", &N);
            build(1, 1, N, 1);
            int Q;
            scanf("%d", &Q);
            while(Q--){
                int X, Y, Z;
                scanf("%d%d%d", &X, &Y, &Z);
                update(X, Y, 1, 1, N, Z);
            }
            printf("Case %d: The total value of the hook is %d.
    ", ++kase, sum[1]);
        }
        return 0;
    }
    

      

  • 相关阅读:
    Java 多线程同步的五种方法
    MySQL中的内连接、左连接、右连接、全连接、交叉连接
    java中线程安全和非线程安全的集合
    hashCode和equal
    MySQL中char、varchar和nvarchar的区别
    MySQL存储引擎
    String在内存中如何存储(Java)
    String、StringBuffer、StringBuilder区别
    MySQL中的事务
    gbk、utf-8、utf8mb4区别
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/7273838.html
Copyright © 2011-2022 走看看