• 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;
    }
    

  • 相关阅读:
    TSQL 常用日期格式
    TSQL数据类型研究_tinyint
    设置数据库状态
    判断存储过程、触发器、视图是否存在并删除
    TSQL日期函数
    TSQL常用日期函数
    TSQL单双引号分隔符相关
    动态存储过程
    TSQL类型转换函数
    modelsim保存仿真结果,以及打开保存的仿真结果的波形wlf文件(经过实验,真实可用)
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11222508.html
Copyright © 2020-2023  润新知