zoukankan      html  css  js  c++  java
  • HDU 1698 Just a Hook 线段树

    区间更新中 lazy标记的pushdown操作 风格还是傻崽的...感觉很好用...

    /********************* Template ************************/
    #include <set>
    #include <map>
    #include <list>
    #include <cmath>
    #include <ctime>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <bitset>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cassert>
    #include <cstdlib>
    #include <cstring>
    #include <sstream>
    #include <fstream>
    #include <numeric>
    #include <iomanip>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    using namespace std;
    #define lson        l,m,rt<<1
    #define rson        m+1,r,rt<<1|1
    #define BUG         cout<<" BUG! "<<endl;
    #define LINE        cout<<" ------------------ "<<endl;
    #define FIN         freopen("in.txt","r",stdin);
    #define FOUT        freopen("out.txt","w",stdout);
    #define mem(a,b)    memset(a,b,sizeof(a))
    #define FOR(i,a,b)  for(int i = a ; i < b ; i++)
    #define read(a)         scanf("%d",&a)
    #define read2(a,b)      scanf("%d%d",&a,&b)
    #define read3(a,b,c)    scanf("%d%d%d",&a,&b,&c)
    #define write(a)        printf("%d
    ",a)
    #define write2(a,b)     printf("%d %d
    ",a,b)
    #define write3(a,b,c)   printf("%d %d %d
    ",a,b,c)
    #pragma comment         (linker,"/STACK:102400000,102400000")
    template<class T> inline T L(T a)       {return (a << 1);}
    template<class T> inline T R(T a)       {return (a << 1 | 1);}
    template<class T> inline T lowbit(T a)  {return (a & -a);}
    template<class T> inline T Mid(T a,T b) {return ((a + b) >> 1);}
    template<class T> inline T gcd(T a,T b) {return b ? gcd(b,a%b) : a;}
    template<class T> inline T lcm(T a,T b) {return a / gcd(a,b) * b;}
    template<class T> inline T Min(T a,T b) {return a < b ? a : b;}
    template<class T> inline T Max(T a,T b) {return a > b ? a : b;}
    template<class T> inline T Min(T a,T b,T c)     {return min(min(a,b),c);}
    template<class T> inline T Max(T a,T b,T c)     {return max(max(a,b),c);}
    template<class T> inline T Min(T a,T b,T c,T d) {return min(min(a,b),min(c,d));}
    template<class T> inline T Max(T a,T b,T c,T d) {return max(max(a,b),max(c,d));}
    template<class T> inline T exGCD(T a, T b, T &x, T &y){
        if(!b) return x = 1,y = 0,a;
        T res = exGCD(b,a%b,x,y),tmp = x;
        x = y,y = tmp - (a / b) * y;
        return res;
    }
    typedef long long LL;    typedef unsigned long long ULL;
    //typedef __int64 LL;      typedef unsigned __int64 ULL;
    const LL LINF       = 1LL << 60;
    const int MOD       = 1000000007;
    const int INF       = 0x7fffffff;
    const int MAXN      = 105000;
    const double EPS    = 1e-8;
    const double DINF   = 1e15;
    const double PI     = acos(-1.0);
    /*********************   By  F   *********************/
    int sum[MAXN*4];
    int lazy[MAXN*4];
    void pushup(int rt){
        sum[rt] = sum[L(rt)] + sum[R(rt)];
    }
    void pushdown(int rt,int val){
        if(lazy[rt]){
            lazy[L(rt)] = lazy[R(rt)] = lazy[rt];
            sum[L(rt)] = (val - (val>>1)) * lazy[rt];
            sum[R(rt)] = (val>>1) * lazy[rt];
            lazy[rt] = 0;
        }
    }
    void build(int l,int r,int rt){
        lazy[rt] = 0;
        sum[rt] = 1;
        if(l == r) return;
        int m = Mid(l,r);
        build(lson);
        build(rson);
        pushup(rt);
    }
    void update(int L,int R,int l,int r,int rt,int val){
        if(L <= l && R >= r){
            lazy[rt] = val;
            sum[rt] = val * (r-l+1);
            return;
        }
        pushdown(rt,r-l+1);
        int m = Mid(l,r);
        if(L <= m) update(L,R,lson,val);
        if(R > m) update(L,R,rson,val);
        pushup(rt);
    }
    int main(){
        //FIN;
        //FOUT;
        int T,n,m,x,y,z;
        read(T);
        for(int cas = 1 ; cas <= T ; cas++){
            read2(n,m);
            build(1,n,1);
            while(m--){
                read3(x,y,z);
                update(x,y,1,n,1,z);
            }
            printf("Case %d: The total value of the hook is %d.
    ",cas,sum[1]);
        }
        return 0;
    }
  • 相关阅读:
    Overloaded的方法是否可以改变返回值的类型
    parseXXX的用法
    java的类型转换问题。int a = 123456;short b = (short)a;System.out.println(b);为什么结果是-7616?
    UVA 10405 Longest Common Subsequence(简单DP)
    POJ 1001 Exponentiation(大数处理)
    POJ 2318 TOYS(计算几何)(二分)
    POJ 1265 Area (计算几何)(Pick定理)
    POJ 3371 Flesch Reading Ease (模拟题)
    POJ 3687 Labeling Balls(拓扑序列)
    POJ 1094 Sorting It All Out(拓扑序列)
  • 原文地址:https://www.cnblogs.com/Felix-F/p/3337545.html
Copyright © 2011-2022 走看看