• [模板] 可持久化数组


    [题目链接]

             https://www.luogu.org/problemnew/show/P3919

    [算法]

           可持久化线段树模板

           详见这篇文章 : https://www.cnblogs.com/RabbitHu/p/segtree.html

    [代码]

            

    #include<bits/stdc++.h>
    using namespace std;
    const int MAXN = 2e6 + 10;
    
    int n , m , version , idx;
    int a[MAXN] , sum[MAXN << 4] , root[MAXN << 4] , lson[MAXN << 4] , rson[MAXN << 4];
    
    template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
    template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
    template <typename T> inline void read(T &x)
    {
        T f = 1; x = 0;
        char c = getchar();
        for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
        for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
        x *= f;
    }
    inline void build(int &k , int l , int r)
    {
            k = ++idx;
            if (l == r) 
            {
                    sum[k] = a[l];
                    return;        
            }
            int mid = (l + r) >> 1;
            build(lson[k] , l , mid);
            build(rson[k] , mid + 1 , r);
            sum[k] = sum[lson[k]] + sum[rson[k]];
    }
    inline void modify(int &k , int old , int l , int r , int pos , int value)
    {
            k = ++idx;
            lson[k] = lson[old] , rson[k] = rson[old];
            if (l == r) 
            {
                    sum[k] = value;
                    return;
            }
            int mid = (l + r) >> 1;
            if (mid >= pos) modify(lson[k] , lson[k] , l , mid , pos , value);
            else modify(rson[k] , rson[k] , mid + 1 , r , pos , value);    
            sum[k] = sum[lson[k]] + sum[rson[k]];  
    }
    inline int query(int &k , int old , int l , int r , int pos)
    {
            k = ++idx;
            lson[k] = lson[old] , rson[k] = rson[old];
            sum[k] = sum[old];
            if (l == r) return sum[k];
            int mid = (l + r) >> 1;
            if (mid >= pos) return query(lson[k] , lson[k] , l , mid , pos);
            else return query(rson[k] , rson[k] , mid + 1 , r , pos);
    }
    
    int main()
    {
            
            read(n); read(m);
            for (int i = 1; i <= n; i++) read(a[i]);
            build(root[version = 0] , 1 , n);
            for (int i = 1; i <= m; i++)
            {
                    int vi , type , loc;
                    read(vi); read(type); read(loc);
                    if (type == 2)
                    {
                            printf("%d
    " , query(root[i] , root[vi] , 1 , n , loc));    
                    } else
                    {
                            int value;
                            read(value);
                            modify(root[i] , root[vi] , 1 , n , loc , value);
                    }            
            }
            
            return 0;
        
    }
  • 相关阅读:
    HTML5结构
    HTML5新增的非主体元素header元素、footer元素、hgroup元素、adress元素
    CF GYM 100703G Game of numbers
    CF GYM 100703I Endeavor for perfection
    CF GYM 100703K Word order
    CF GYM 100703L Many questions
    CF GYM 100703M It's complicate
    HDU 5313 Bipartite Graph
    CF 560e Gerald and Giant Chess
    POJ 2479 Maximum sum
  • 原文地址:https://www.cnblogs.com/evenbao/p/9971287.html
Copyright © 2020-2023  润新知