[COGS2554][SYZOJ247][福利]可持久化线段树
思路:
主席树模板。
注意内存的分配,原始的线段树有$2n$个结点,每次更新时最多增加$log(n)$个结点,总共有$q$次询问,所以存储结点的数组大小为$2N+q log(n)$。
#include<cstdio> #include<cctype> #include<vector> #include<algorithm> inline int getint() { char ch; while(!isdigit(ch=getchar())); int x=ch^'0'; while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0'); return x; } const int N=10001,Q=100001,logN=100; class FotileTree { private: unsigned int left[(N<<1)+Q*logN],right[(N<<1)+Q*logN]; std::vector<int> val; void push_up(const int p) { val[p]=std::max(val[left[p]],val[right[p]]); } public: unsigned int root[Q]; unsigned int newnode() { val.push_back(0); return val.size()-1; } void build(const int p,const int b,const int e) { if(b==e) { val[p]=getint(); return; } int mid=(b+e)>>1; build(left[p]=newnode(),b,mid); build(right[p]=newnode(),mid+1,e); push_up(p); } unsigned int modify(const int p,const int b,const int e,const int x,const int y) { unsigned int new_p=newnode(); if(b==e) { val[new_p]=y; return new_p; } int mid=(b+e)>>1; if(x<=mid) left[new_p]=modify(left[p],b,mid,x,y),right[new_p]=right[p]; if(x>mid) right[new_p]=modify(right[p],mid+1,e,x,y),left[new_p]=left[p]; push_up(new_p); return new_p; } int query(const int p,const int b,const int e,const int l,const int r) { if((b==l)&&(e==r)) return val[p]; int mid=(b+e)>>1; int ans=0; if(l<=mid) ans=std::max(ans,query(left[p],b,mid,l,std::min(mid,r))); if(r>mid) ans=std::max(ans,query(right[p],mid+1,e,std::max(mid+1,l),r)); return ans; } }; FotileTree t; int main() { freopen("longterm_segtree.in","r+",stdin); freopen("longterm_segtree.out","w+",stdout); int n=getint(),q=getint(),ver=0; t.build(t.root[++ver]=t.newnode(),1,n); while(q--) { int op=getint(),k=getint(),x=getint(),y=getint(); if(!op) printf("%d\n",t.query(t.root[k],1,n,x,y)); if(op) t.root[++ver]=t.modify(t.root[k],1,n,x,y); } fclose(stdin),fclose(stdout); return 0; }
- COGS 2554. [福利]可持久化线段树
- AC日记——[福利]可持久化线段树 cogs 2554
- cogs 2554. [福利]可持久化线段树
- cogs2554 [福利]可持久化线段树
- 入坑 可持久化线段树——主席树
- 【UOJ #218. 【UNR #1】】火车管理 可持久化线段树
- 【bzoj3514】Codechef MARCH14 GERALD07加强版 LCT+可持久化线段树
- bzoj 3524 可持久化线段树(统计区间数值出现次数
- BZOJ 2733 [HNOI2012]永无乡 可持久化线段树合并
- bzoj 2653: middle 可持久化线段树+二分答案
- 【BZOJ3673/3674】可持久化并查集/可持久化并查集加强版 可持久化线段树
- hdu2665 主席树(可持久化线段树)
- 可持久化线段树(主席树)【数组】
- 主席树(可持久化线段树)学习笔记
- BZOJ 3932 [CQOI2015]任务查询系统 可持久化线段树
- [BZOJ 3207] 花神的嘲讽计划Ⅰ【Hash + 可持久化线段树】
- 【cogs247】售票系统【线段树】
- POJ 2104 K-th Number (可持久化线段树)
- poj 2104 可持久化线段树 区间K大 不修改
- COGS 2638. 数列操作ψ 线段树