1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include <bits/stdc++.h>
#define debug freopen("in.txt","r",stdin); freopen("out.txt","w",stdout) #define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) using namespace std; typedef long long ll; typedef unsigned long long ull; const int MAXN=1e6+100; const int MOD=1e9+7; const int INF=0x3f3f3f3f; const int SUB=-0x3f3f3f3f; const double eps=1e-4; const double E=exp(1); const double pi=acos(-1); struct tree{ int ls,rs,val; }hjt[MAXN*32]; int arr[MAXN],rt[MAXN]; int n,m,tot,cnt; int build(int l,int r){ int root=++tot; if(l==r){ hjt[root].val=arr[l]; return root; } int mid=l+r>>1; hjt[root].ls=build(l,mid); hjt[root].rs=build(mid+1,r); return root; } int insert(int pre,int pos,int val,int l,int r){ int root=++tot; hjt[root]=hjt[pre]; if(l==r){ hjt[root].val=val; return root; } int mid=l+r>>1; if(pos<=mid) hjt[root].ls=insert(hjt[pre].ls,pos,val,l,mid); else hjt[root].rs=insert(hjt[pre].rs,pos,val,mid+1,r); return root; } int query(int l,int r,int x,int pos){ if(l==r) return hjt[x].val; int mid=l+r>>1; if(pos<=mid) return query(l,mid,hjt[x].ls,pos); else return query(mid+1,r,hjt[x].rs,pos); } int main(){ ios; cin>>n>>m; for(int i=1;i<=n;i++) cin>>arr[i]; rt[cnt]=build(1,n); while(m--){ int k,op,pos,val; cin>>k>>op; if(op==1){ cin>>pos>>val; rt[++cnt]=insert(rt[k],pos,val,1,n); } else{ cin>>pos; cout<<query(1,n,rt[k],pos)<<'\n'; rt[++cnt]=rt[k]; } } return 0; }
|