zoukankan      html  css  js  c++  java
  • 主席树维护--可持久化数组

    题意:https://www.luogu.org/problem/P3919

      1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
      2 #include <cstdio>//sprintf islower isupper
      3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
      4 #include <iostream>//pair
      5 #include <fstream>//freopen("C:\Users\13606\Desktop\草稿.txt","r",stdin);
      6 #include <bitset>
      7 //#include <map>
      8 //#include<unordered_map>
      9 #include <vector>
     10 #include <stack>
     11 #include <set>
     12 #include <string.h>//strstr substr
     13 #include <string>
     14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
     15 #include <cmath>
     16 #include <deque>
     17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
     18 #include <vector>//emplace_back
     19 //#include <math.h>
     20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
     21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
     22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
     23 //******************
     24 int abss(int a);
     25 int lowbit(int n);
     26 int Del_bit_1(int n);
     27 int maxx(int a,int b);
     28 int minn(int a,int b);
     29 double fabss(double a);
     30 void swapp(int &a,int &b);
     31 clock_t __STRAT,__END;
     32 double __TOTALTIME;
     33 void _MS(){__STRAT=clock();}
     34 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
     35 //***********************
     36 #define rint register int
     37 #define fo(a,b,c) for(rint a=b;a<=c;++a)
     38 #define fr(a,b,c) for(rint a=b;a>=c;--a)
     39 #define mem(a,b) memset(a,b,sizeof(a))
     40 #define pr printf
     41 #define sc scanf
     42 #define ls rt<<1
     43 #define rs rt<<1|1
     44 typedef long long ll;
     45 const double E=2.718281828;
     46 const double PI=acos(-1.0);
     47 //const ll INF=(1LL<<60);
     48 const int inf=(1<<30);
     49 const double ESP=1e-9;
     50 const int mod=(int)1e9+7;
     51 const int N=(int)1e6+10;
     52 int read(){
     53     int x=0,f=1;char ch=getchar();
     54     for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
     55     for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
     56     return x*f;
     57 }
     58 
     59 int tot;
     60 int root[N],lson[N*20],rson[N*20];
     61 int a[N],ans[N*20];
     62 
     63 void build(int &now,int l,int r)
     64 {
     65     now=++tot;
     66     if(l==r)
     67     {
     68         ans[now]=a[l];
     69         return;
     70     }
     71     int mid=(l+r)>>1;
     72     build(lson[now],l,mid);
     73     build(rson[now],mid+1,r);
     74 }
     75 void update(int &now,int pre,int l,int r,int pos,int val)
     76 {
     77     now=++tot;
     78     lson[now]=lson[pre];
     79     rson[now]=rson[pre];
     80     if(l==r)
     81     {
     82         ans[now]=val;
     83         return;
     84     }
     85     int mid=(l+r)>>1;
     86     if(pos<=mid)update(lson[now],lson[pre],l,mid,pos,val);
     87     else update(rson[now],rson[pre],mid+1,r,pos,val);
     88 }
     89 int Query(int l,int r,int pos,int rt)
     90 {
     91     if(l==r)
     92         return ans[rt];
     93     int mid=(l+r)>>1;
     94     if(pos<=mid)return Query(l,mid,pos,lson[rt]);
     95     else return Query(mid+1,r,pos,rson[rt]);
     96 }
     97 
     98 int main()
     99 {
    100     int n=read(),m=read();
    101     for(int i=1;i<=n;++i)a[i]=read();
    102     tot=-1;
    103     build(root[0],1,n);
    104     int version,op,pos,val;
    105     for(int i=1;i<=m;++i)
    106     {
    107         version=read();op=read();pos=read();
    108         if(op==1)
    109             val=read(),update(root[i],root[version],1,n,pos,val);
    110         else
    111             pr("%d
    ",Query(1,n,pos,root[version])),root[i]=root[version];
    112     }
    113     return 0;
    114 }
    115 
    116 /**************************************************************************************/
    117 
    118 int maxx(int a,int b)
    119 {
    120     return a>b?a:b;
    121 }
    122 
    123 void swapp(int &a,int &b)
    124 {
    125     a^=b^=a^=b;
    126 }
    127 
    128 int lowbit(int n)
    129 {
    130     return n&(-n);
    131 }
    132 
    133 int Del_bit_1(int n)
    134 {
    135     return n&(n-1);
    136 }
    137 
    138 int abss(int a)
    139 {
    140     return a>0?a:-a;
    141 }
    142 
    143 double fabss(double a)
    144 {
    145     return a>0?a:-a;
    146 }
    147 
    148 int minn(int a,int b)
    149 {
    150     return a<b?a:b;
    151 }
  • 相关阅读:
    从 mian 函数开始一步一步分析 nginx 执行流程(四)
    从 mian 函数开始一步一步分析 nginx 执行流程(三)
    从 mian 函数开始一步一步分析 nginx 执行流程(二)
    从 mian 函数开始一步一步分析 nginx 执行流程(一)
    QQ游戏百万人同时在线服务器架构实现
    HTTP协议详解
    centos7用yum搭建LAMP环境
    msfvenom 木马生成(常用)
    用Digispark制作BadUSB+msf植入病毒木马
    kvm管理工具Webvirtmgr安装
  • 原文地址:https://www.cnblogs.com/--HPY-7m/p/11703553.html
Copyright © 2011-2022 走看看