用可持久化线段树维护可持久化数组。可持久化线段树见之前发的主席树模板

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n,m;
const int N=22000001;
int rt[N],l[N],r[N],t[N],cnt;
int build(int L,int R) {
int root=++cnt;
if(L==R) {
scanf("%d",&t[root]);
return root;
}
l[root]=build(L,L+R>>1);
r[root]=build(1+(L+R>>1),R);
return root;
}
int update(int pre,int L,int R,int c,int x) {
int root=++cnt;
if(L==R) {
t[root]=c;
return root;
}
l[root]=l[pre];
r[root]=r[pre];
int mid=L+R>>1;
if(x<=mid) l[root]=update(l[pre],L,mid,c,x);
else r[root]=update(r[pre],mid+1,R,c,x);
return root;
}
int query(int pre,int L,int R,int c) {
if(L==R) {
return t[pre];
}
int mid=L+R>>1;
if(c<=mid) return query(l[pre],L,mid,c);
else return query(r[pre],mid+1,R,c);
}
int main() {
scanf("%d%d",&n,&m);
build(1,n);
rt[0]=1;
int opt,a,b,c;
for(int i=1; i<=m; i++) {
scanf("%d%d",&a,&opt);
if(opt==1) {
scanf("%d%d",&b,&c);
rt[i]=update(rt[a],1,n,c,b);
} else {
scanf("%d",&b);
rt[i]=rt[a];
printf("%d\n",query(rt[a],1,n,b));
}
}
}

最新文章

  1. PHP面向对象讲解
  2. 利用opencv训练样本分类
  3. UVA 11464 偶数矩阵
  4. 自然语言6_treebank句子解析
  5. ubunut 14.04 将Caps Lock设置为Control
  6. demo07
  7. [ios2]iOS 使用subversion管理iOS源代码 【转】
  8. Django 学习笔记(五)模板标签
  9. 扩展Microsoft Graph数据结构(开放扩展)
  10. iOS - IM 即时通讯
  11. JAVA_SE基础——24.面向对象的内存分析
  12. SpringBoot 2 要不要升级
  13. EasyUI的textbox的disable ,readonly 用法
  14. rem 自适应适配方法
  15. jquery的div局部刷新
  16. Android Studio 安装与设置
  17. BZOJ2795&amp;2890&amp;3647[Poi2012]A Horrible Poem——hash
  18. hdu3072 Intelligence System (最小树形图?)
  19. Postman教程
  20. Semaphore wait has lasted &gt; 600 seconds

热门文章

  1. Javascript解析URL
  2. 3.1、Ansible命令简要说明及初步使用
  3. maven 依赖的传递性
  4. oracle删除非空的表空间
  5. Android学习总结(3)——Handler深入详解
  6. redis helloworld
  7. VMware虚拟机无法识别U盘解决方式
  8. 2015.04.27,外语,读书笔记-《Word Power Made Easy》 12 “如何奉承朋友” SESSION 35
  9. 希尔加密算法(湖南师范大学第六届大学生计算机程序设计竞赛)hnuoj11552
  10. Thread.setDaemon设置说明