Luogu 3919(主席树)
来源:互联网 发布:联合国安理会知乎 编辑:程序博客网 时间:2024/05/20 11:19
传送门
其实就是可持久化线段树,连pushup都不用写,可作为模板使用。
#include<bits/stdc++.h>using namespace std;#define lson lc[rt],l,mid#define rson rc[rt],mid+1,rconst int MAXN=1e6+2;int n,m;int tim=0,root[MAXN],lc[MAXN*22],rc[MAXN*22],sum[MAXN*22];inline int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar(); return x*f;}void build(int &rt,int l,int r) { rt=++tim; if (l==r) {sum[rt]=read();return ;} int mid=(l+r)>>1; build(lson), build(rson);}void insert(int pre,int &rt,int l,int r,int pos,int val) { rt=++tim; if (l==r) {sum[rt]=val;return ;} lc[rt]=lc[pre],rc[rt]=rc[pre]; int mid=(l+r)>>1; if (pos<=mid) insert(lc[pre],lson,pos,val); else insert(rc[pre],rson,pos,val);}int query(int rt,int l,int r,int pos) { if (l==r) return sum[rt]; int mid=(l+r)>>1; if (pos<=mid) return query(lson,pos); else return query(rson,pos);}int main() {// freopen("P3919.in","r",stdin);// printf("%d\n",sizeof(lc)*3+sizeof(root)); n=read(),m=read(); build(root[0],1,n); for (register int i=1;i<=m;++i) { int r=read(),opt=read(); if (opt&1) { int pos=read(),val=read(); insert(root[r],root[i],1,n,pos,val); } else { int pos=read(); printf("%d\n",query(root[i]=root[r],1,n,pos)); } } return 0;}
阅读全文
0 0
- Luogu 3919(主席树)
- Luogu 1972(主席树)
- Luogu-3834 (主席树模板)
- hdu4417(主席树)
- HDU4417(主席树)
- 主席树(hdu2665)
- 主席树(1)
- 主席、树、主席树!
- poj-2104(主席树)
- poj 2104 (主席树)
- zoj 2112 (主席树)
- bzoj 1803(主席树)
- POJ 2104(主席树)
- HDU3727 Jewel (主席树)
- poj 2104(主席树)
- POJ-2104(主席树)
- HDU 2665 (主席树)
- POJ 2104 (主席树)
- 机器学习入门笔记(三)----Octave简单使用
- 几种排序以及其时间复杂度
- 数据结构与算法_KMP算法
- 字符设备驱动
- Spring Cloud Eureka+Ribbon+Hystrix
- Luogu 3919(主席树)
- python 微信 itchat
- CF808C
- 空了一天,今天第二十四天
- NOI2017题解(挖坑待填)
- (十三)中介者模式详解(玄幻版)
- Mysql 优化——分析表读写和sql效率问题
- ajax基础介绍
- Linux(64位)下OpenBabel 2.4.1、python2.7和Ipython实战(一)