主席树模板题,注意空间\((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;
}

最新文章

  1. [No00008B]远程桌面发送“Ctrl+Alt+Delete”组合键调用任务管理器
  2. thinkphp3.2与phpexcel解析
  3. 《C与指针》第十章练习
  4. 一些鲜为人知却非常实用的数据结构 - Haippy
  5. JavaScript之canvas
  6. 使用WM_COPYDATA跨进程发送数据
  7. Arcgis for javascript map操作addLayer具体解释
  8. Java面试题精选(三) JSP/Servlet Java面试逻辑题
  9. Android:What is ART?
  10. WebApi Help Pages
  11. 2017-2018-1 1623 bug终结者 冲刺004
  12. 模块(相当于Java里的包)
  13. 2018-2019-2 网络对抗技术 20165228 Exp1 PC平台逆向破解
  14. swoole 安装方法
  15. [SDOI2008]洞穴勘测
  16. redis PUB/SUB(发布/订阅)
  17. 通过ANT实现jmeter批量执行脚本、生成报告、发送邮件全套build.xml文件
  18. LeetCode CombinationSum II
  19. plsql developer连接oracle 12.2报错 ora-28040 No matching authentication protocol
  20. 健身VS不健身,完全是两种不同的人生!

热门文章

  1. Python汉诺塔求解
  2. 关于『进击的Markdown』:第三弹
  3. String、StringBuilder、StringBuffer——JavaSE基础
  4. 在 Traefik Proxy 2.5 中使用/开发私有插件(Traefik 官方博客)
  5. pytorch基础常识
  6. 4. Docker自定义镜像
  7. App自动化之dom结构和元素定位方式(包含滑动列表定位)
  8. 开启网易邮箱客户端授权码-POP/SMTP/IMAP
  9. Linux远程连接工具和运行级别
  10. 爬虫(6) - 网页数据解析(2) | BeautifulSoup4在爬虫中的使用