题目描述 题目
模板模板
线段与它的历史版本
新的版本只要在历史版本上修改一条路就好了
继续打指针
指针真的好用
代码(可持久化线段树)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int N=1000010;
int n,m,a[N],cnt;
struct node{node* ch[2];int v;};node* T[N];
void build(node* &o,int l,int r)
{
if(l > r) return ;
o=new node();
if(l == r) {o->v=a[l];return ;}
int mid=(l+r)>>1;
build(o->ch[0],l,mid);
build(o->ch[1],mid+1,r);
}
void updata(node* pre,node* &o,int l,int r,int x,int v)
{
if(l > r) return ;
o=new node();o->ch[0]=pre->ch[0];o->ch[1]=pre->ch[1];
if(l == r) {o->v=v;return ;}
int mid=(l+r)>>1;
if(x <= mid) updata(pre->ch[0],o->ch[0],l,mid,x,v);
else updata(pre->ch[1],o->ch[1],mid+1,r,x,v);
}
int query(node* o,int l,int r,int x)
{
if(l > r) return 0;
if(l == r) return o->v;
int mid=(l+r)>>1;
if(x <= mid) return query(o->ch[0],l,mid,x);
return query(o->ch[1],mid+1,r,x);
}
int read(){
int out=0,f=1;char c=getchar();while(c > '9' || c < '0') {if(c == '-') f=-1;c=getchar();}
while(c <= '9' && c >= '0') {out=(out<<1)+(out<<3)+c-'0';c=getchar();}return out*f;
}
void solve()
{
n=read(),m=read();
for(int i=1;i<=n;i++) a[i]=read();
build(T[0],1,n);
for(int i=1;i<=m;i++)
{
int x=read(),opt=read(),loc=read();
if(opt == 1)
{
int y=read();
updata(T[x],T[++cnt],1,n,loc,y);
}
if(opt == 2) {T[++cnt]=T[x];printf("%d\n",query(T[x],1,n,loc));}
}
}
int main()
{
solve();
return 0;
}