luogu 3919
主席树
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string> using namespace std; #define LL long long #define gc getchar()
inline int read() {int x = , f = ; char c = gc;
while(c < '' || c > '') {if(c == '-') f = -; c = gc;};
while(c >= '' && c <= '') x = x * + c - '', c = gc; return x * f;}
inline LL read_LL() {LL x = ; char c = gc; while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc; return x;}
#undef gc const int N = 1e6 + ; int W[N * * ], Lson[N * * ], Rson[N * * ];
int Root[N];
int Hjt;
int n, q; void Build_tree(int l, int r, int &rt) {
rt = ++ Hjt;
if(l == r) {
W[rt] = read();
return ;
}
int mid = (l + r) >> ;
Build_tree(l, mid, Lson[rt]), Build_tree(mid + , r, Rson[rt]);
} void Fill(int x, int y) {
W[x] = W[y], Lson[x] = Lson[y], Rson[x] = Rson[y];
} void Poi_G(int &rt, int l, int r, int x, int val) {
Fill(++ Hjt, rt);
rt = Hjt;
if(l == r) {
W[rt] = val;
return ;
}
int mid = (l + r) >> ;
if(x <= mid) Poi_G(Lson[rt], l, mid, x, val);
else Poi_G(Rson[rt], mid + , r, x, val);
} int Ans; void Poi_A(int rt, int l, int r, int x) {
if(l == r) {
Ans = W[rt];
return ;
}
int mid = (l + r) >> ;
if(x <= mid) Poi_A(Lson[rt], l, mid, x);
else Poi_A(Rson[rt], mid + , r, x);
} int main() {
n = read(), q = read();
Build_tree(, n, Root[]);
for(int i = ; i <= q; i ++) {
int v = read(), opt = read(), loc = read(), val;
if(opt == ) val = read();
if(opt == ) {
Root[i] = Root[v];
Poi_G(Root[i], , n, loc, val);
} else {
Poi_A(Root[v], , n, loc);
Root[i] = Root[v];
cout << Ans << "\n";
}
}
return ;
}
luogu 3919的更多相关文章
- [Luogu 3919]【模板】可持久化数组(可持久化线段树/平衡树)
Description 如题,你需要维护这样的一个长度为 N 的数组,支持如下几种操作 在某个历史版本上修改某一个位置上的值 访问某个历史版本上的某一位置的值 此外,每进行一次操作(对于操作2,即为生 ...
- [LUOGU]3919 【模板】可持久化数组
用可持久化线段树维护可持久化数组.可持久化线段树见之前发的主席树模板 #include <iostream> #include <cstdio> #include <cs ...
- Luogu 魔法学院杯-第二弹(萌新的第一法blog)
虽然有点久远 还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题 沉迷游戏,伤感情 #include <queue> ...
- luogu p1268 树的重量——构造,真正考验编程能力
题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...
- [luogu P2170] 选学霸(并查集+dp)
题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...
- [luogu P2647] 最大收益(贪心+dp)
题目传送门:https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,--,n.你可以在这当中任意选择任意多个物品. ...
- Luogu 考前模拟Round. 1
A.情书 题目:http://www.luogu.org/problem/show?pid=2264 赛中:sb题,直接暴力匹配就行了,注意一下读入和最后一句话的分句 赛后:卧槽 怎么只有40 B.小 ...
- luogu P2580 于是他错误的点名开始了
luogu P2580 于是他错误的点名开始了 https://www.luogu.org/problem/show?pid=2580 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边 ...
- CJOJ 1331 【HNOI2011】数学作业 / Luogu 3216 【HNOI2011】数学作业 / HYSBZ 2326 数学作业(递推,矩阵)
CJOJ 1331 [HNOI2011]数学作业 / Luogu 3216 [HNOI2011]数学作业 / HYSBZ 2326 数学作业(递推,矩阵) Description 小 C 数学成绩优异 ...
随机推荐
- C++ 数组和vector的基本操作
1.静态数组的基本操作 int a[5] = {0, 3, 4, 6, 2}; 1.1 数组的遍历 1.1.1 传统的for循环遍历 int size = sizeof(a) / sizeof(*a) ...
- go select 使得一个 goroutine 在多个通讯操作上等待。
select 语句使得一个 goroutine 在多个通讯操作上等待. select 会阻塞,直到条件分支中的某个可以继续执行,这时就会执行那个条件分支.当多个都准备好的时候,会随机选择一个. pac ...
- Oracle和SQL Server 用当前日期减去 '0001-01-01' 得出的天数不一致,相差2天,谁知道原因?
Oracle和SQL Server 用当前日期减去 '0001-01-01' 得出的天数不一致,相差2天.求大佬科普
- python 流程判断
import getpass# print("hello word") ## name= 'pangxiao'# mix_name=name# print(mix_name,nam ...
- UOJ #7 NOI2014购票(点分治+cdq分治+斜率优化+动态规划)
重写一遍很久以前写过的题. 考虑链上的问题.容易想到设f[i]为i到1的最少购票费用,转移有f[i]=min{f[j]+(dep[i]-dep[j])*p[i]+q[i]} (dep[i]-dep[j ...
- TreeListView排序不对
winForm控件TreeListView按照一定顺序后添加项,后发觉排序顺序自己变了,解决办法: TreeListViewItem viewItem = new TreeListViewItem() ...
- Python Web 程序使用 uWSGI 部署
Python Web 程序使用 uWSGI 部署 WSGI是什么? WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway ...
- 【转载】 C#使用String.Format拼接字符串
在C#程序开发过程中,很多时候会使用字符串拼接,最简单的字符串拼接操作就是所有的字符串使用加号+相加连接起来,但这种代码形式非常不适合代码维护阅读,尤其是拼接字符串语句比较复杂的时候,如拼接SQL语句 ...
- 【转载】C#使用FirstOrDefault方法快速查找List集合中符合条件的第一个实体
在C#的List集合的操作中,有时候我们需要根据相关条件快速从List集合中获取到第一个符合条件的实体对象,例如有个全校班级的List集合,我们需要根据班级代码快速从List集合中查找出班级信息.可以 ...
- MM-自制件改外购件
自制件改外购件 https://wenku.baidu.com/view/fbb182c6bb4cf7ec4afed081.html