• HDU4348 To the moon (主席树)


    标记永久化,除非想(MLE)
    忽然感到主席树不过是函数式的树套树

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
    #define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
    #define Max(a,b) ((a) > (b) ? (a) : (b))
    #define Min(a,b) ((a) < (b) ? (a) : (b))
    #define Fill(a,b) memset(a, b, sizeof(a))
    #define Abs(a) ((a) < 0 ? -(a) : (a))
    #define Swap(a,b) a^=b^=a^=b
    #define ll long long
    
    //#define ON_DEBUG
    
    #ifdef ON_DEBUG
    
    #define D_e_Line printf("
    
    ----------
    
    ")
    #define D_e(x)  cout << #x << " = " << x << endl
    #define Pause() system("pause")
    #define FileOpen() freopen("in.txt","r",stdin);
    
    #else
    
    #define D_e_Line ;
    #define D_e(x)  ;
    #define Pause() ;
    #define FileOpen() ;
    
    #endif
    
    struct ios{
        template<typename ATP>ios& operator >> (ATP &x){
            x = 0; int f = 1; char c;
            for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
            while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
            x*= f;
            return *this;
        }
    }io;
    using namespace std;
    
    const int N = 100007;
    
    struct ChairmanTree{
    	int l, r;
    	long long sum, tag;
    }t[N * 40];
    int treeIndex, T[N];
    
    inline void Pushup(int &rt, int &l, int &r){
    	t[rt].sum = t[t[rt].l].sum + t[t[rt].r].sum + 1ll * (r - l + 1) * t[rt].tag;
    }
    inline void Build(int &rt, int l, int r){
    	rt = ++treeIndex;
    	if(l == r){
    		io >> t[rt].sum;
    		return;
    	}
    	int mid = (l + r) >> 1;
    	Build(t[rt].l, l, mid), Build(t[rt].r, mid + 1, r);
    	Pushup(rt, l, r);
    }
    inline void Updata(int &rt, int pre, int l, int r, int L, int R, long long w) {
        rt = ++treeIndex;
        t[rt] = t[pre]; // it need to be initialed here
        if(L <= l && r <= R){
            t[rt].tag += w;
            t[rt].sum += 1ll * (r - l + 1) * w;
            return;
        }
        // t[rt] = t[pre]; // not here !
        int mid = (l + r) >> 1;
        if(L <= mid)
    		Updata(t[rt].l, t[pre].l, l, mid, L, R, w);
        if(R > mid)
    		Updata(t[rt].r, t[pre].r, mid + 1, r, L, R, w);
        Pushup(rt, l, r);
    }
    inline long long Query(int rt, int l, int r, int L, int R, long long tot){
        if(L <= l && r <= R) return t[rt].sum + 1ll * (r - l + 1) * tot;
        tot += t[rt].tag;
        int mid = (l + r) >> 1;
        long long sum = 0;
        if(L <= mid)
    		sum += Query(t[rt].l, l, mid, L, R, tot);
        if(R >= mid+1)
    		sum += Query(t[rt].r, mid+1, r, L, R, tot);
        return sum;
    }
    
    int tim;
    int main(){
    	FileOpen();
    	int n, m;
        while(~scanf("%d%d", &n, &m)){
            treeIndex = tim = 0;
            Build(T[tim], 1, n);
            while (m--) {
           		char opt;
                for(opt = getchar(); opt != 'C' && opt != 'Q' && opt != 'H' && opt != 'B'; opt = getchar());
                if(opt == 'C'){
                    int l, r, w;
                    io >> l >> r >> w;
                    ++tim;
                    Updata(T[tim], T[tim - 1], 1, n, l, r, w);
                }
                else if(opt == 'Q'){
                    int l, r;
                    io >> l >> r;
                    printf("%lld
    ", Query(T[tim], 1, n, l, r, 0));
                }
                else if(opt == 'H'){
                    int l, r, edition;
                    io >> l >> r >> edition;
                    printf("%lld
    ", Query(T[edition], 1, n, l, r, 0));
                }
                else if(opt == 'B'){
                    int newTime;
                    io >> newTime;
                    tim = newTime;
                }
            }
        }
        return 0;
    }
    

  • 相关阅读:
    有了这些开源动效项目,设计和开发不再相杀只剩相爱
    优雅地使用 C++ 制作表格:tabulate
    编写 Django 应用单元测试
    Django Haystack 全文检索与关键词高亮
    JAVA格物致知开篇:凡事预则立不预则废
    前端见微知著流程篇:前端开发流程总结
    前端见微知著工具篇:Bower组件管控
    前端见微知著工具篇:Grunt实现自动化
    前端见微知著AngularJS备忘篇:温故而知新,可以为师矣
    前端见微知著JavaScript基础篇:this or that ?
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11224425.html
Copyright © 2020-2023  润新知