• luogu 3919


    主席树

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #include <string>
    
    using namespace std;
    
    #define LL long long
    
    #define gc getchar()
    inline int read() {int x = 0, f = 1; char c = gc; 
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = gc;};
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc; return x * f;}
    inline LL read_LL() {LL x = 0; char c = gc; while(c < '0' || c > '9') c = gc;
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc; return x;}
    #undef gc
    
    const int N = 1e6 + 10;
    
    int W[N * 30 * 2], Lson[N * 30 * 2], Rson[N * 30 * 2];
    int Root[N];
    int Hjt;
    int n, q;
    
    void Build_tree(int l, int r, int &rt) {
        rt = ++ Hjt;
        if(l == r) {
            W[rt] = read();
            return ;
        }
        int mid = (l + r) >> 1;
        Build_tree(l, mid, Lson[rt]), Build_tree(mid + 1, r, Rson[rt]);
    }
    
    void Fill(int x, int y) {
        W[x] = W[y], Lson[x] = Lson[y], Rson[x] = Rson[y];
    }
    
    void Poi_G(int &rt, int l, int r, int x, int val) {
        Fill(++ Hjt, rt);
        rt = Hjt;
        if(l == r) {
            W[rt] = val;
            return ;
        }
        int mid = (l + r) >> 1;
        if(x <= mid) Poi_G(Lson[rt], l, mid, x, val);
        else Poi_G(Rson[rt], mid + 1, r, x, val);
    }
    
    int Ans;
    
    void Poi_A(int rt, int l, int r, int x) {
        if(l == r) {
            Ans = W[rt];
            return ;
        }
        int mid = (l + r) >> 1;
        if(x <= mid) Poi_A(Lson[rt], l, mid, x);
        else Poi_A(Rson[rt], mid + 1, r, x);
    }
    
    int main() {
        n = read(), q = read();
        Build_tree(1, n, Root[0]);
        for(int i = 1; i <= q; i ++) {
            int v = read(), opt = read(), loc = read(), val;
            if(opt == 1) val = read();
            if(opt == 1) {
                Root[i] = Root[v];
                Poi_G(Root[i], 1, n, loc, val);
            } else {
                Poi_A(Root[v], 1, n, loc);
                Root[i] = Root[v];
                cout << Ans << "
    ";
            }
        }
        return 0;
    }
  • 相关阅读:
    2016.8.16上午纪中初中部NOIP普及组比赛
    Laravel之Eloquent ORM
    Laravel基础
    sql
    PHP面向对象编程
    PHP判断远程文件是否存在
    专业术语之------耦合?依赖?耦合和依赖的关系?耦合就是依赖
    门禁系统socket通讯编程
    PHP设计模式:类自动载入、PSR-0规范、链式操作、11种面向对象设计模式实现和使用、OOP的基本原则和自动加载配置
    PHP 真正多线程的使用
  • 原文地址:https://www.cnblogs.com/shandongs1/p/9588640.html
Copyright © 2020-2023  润新知