主席树模板题,注意空间\((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("\n\n----------\n\n")
#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\n", t.Query(t.rt[edition], 1, n, pos));
t.rt[i] = t.rt[edition];
}
}
return 0;
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章