zoukankan      html  css  js  c++  java
  • Luogu3919 【模板】可持久化数组(主席树)

    主席树模板题,注意空间((n+m) log(n))

    #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 = 20000007;
    struct Chairman{
        // space complexity : (n + m) * log(n)
        int rt[1000007], T[N], L[N], R[N];
        int treeIndex;
        inline int Build(int l, int r){
        	int root = ++treeIndex;
        	if(l == r){
        		io >> T[root];
        		return root;
        	}
        	int mid = (l + r) >> 1;
        	L[root] = Build(l, mid);
        	R[root] = Build(mid + 1, r);
        	return root;
        }
        inline int Updata(int rt, int l, int r, int x, int w){
        	int root = ++treeIndex;
        	if(l == r){
        		T[root] = w;
        		return root;
        	}
        	L[root] = L[rt], R[root] = R[rt];
        	int mid = (l + r) >> 1;
        	if(x <= mid) L[root] = Updata(L[rt], l, mid, x ,w);
        	else R[root] = Updata(R[rt], mid + 1, r, x, w);
        	return root;
        }
        inline int Query(int rt, int l, int r, int x){
        	if(l == r) return T[rt];
        	int mid = (l + r) >> 1;
        	if(x <= mid) return Query(L[rt], l, mid, x);
        	else return Query(R[rt], mid + 1, r, x);
        }
    }t;
    int main(){
        t.treeIndex=0;
        int n,m;
        io >> n >> m;
        
        t.Build(1,n);
        t.rt[0]=1;
        
        R(i,1,m){
        	int edition,opt;
            io >> edition >> opt;
            if(opt==1){
            	int pos, newValue;
                io >> pos >> newValue;
                t.rt[i] = t.Updata(t.rt[edition], 1, n, pos, newValue);
            }
            if(opt==2){
            	int pos;
            	io >> pos;
                printf("%d
    ", t.Query(t.rt[edition], 1, n, pos));
                t.rt[i] = t.rt[edition];
            }
        }
        
        return 0;
    }
    

  • 相关阅读:
    A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。
    Fliptile 开关问题 poj 3279
    Face The Right Way 一道不错的尺取法和标记法题目。 poj 3276
    Aggressive cows 二分不仅仅是查找
    Cable master(二分题 注意精度)
    B. Pasha and String
    Intervals poj 1201 差分约束系统
    UITextField的快速基本使用代码块
    将UIImage转换成圆形图片image
    color转成image对象
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11222508.html
Copyright © 2011-2022 走看看