OpenJudge cdqz/Data Structure Challenge 2 (Problem 5822) - 可持久化线段树
描述
给一个空数列,有M次操作,每次操作是以下三种之一:
(1)在数列后加一个数
(2)求数列中某位置的值
(3)撤销掉最后进行的若干次操作(1和3)
输入
第一行一个正整数M。 接下来M行,每行开头是一个字符,若该字符为'A',则表示一个加数操作,接下来一个整数x,表示在数列后加一个整数x;若该字符为'Q',则表示一个询问操作,接下来一个整数x,表示求x位置的值;若该字符为'U',则表示一个撤销操作,接下来一个整数x,表示撤销掉最后进行的若干次操作。
输出
对每一个询问操作单独输出一行,表示答案。
样例输入
9
A 1
A 2
A 3
Q 3
U 1
A 4
Q 3
U 2
Q 3
样例输出
3
4
3
提示
1<=M<=10^5,输入保证合法,且所有整数可用带符号32位整型存储。
可持久化线段树不解释。
Code
/**
* OpenJudge
* Problem#5822
* Accepted
* Time: 846ms
* Memory: 144000k
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <queue>
using namespace std; const int segsize = ; typedef class SegTreeNode {
public:
int val;
SegTreeNode *l, *r; SegTreeNode():val() { }
}SegTreeNode; SegTreeNode pool[];
SegTreeNode *top = pool; SegTreeNode* newnode() {
return top++;
} typedef class SegTree {
public:
SegTreeNode** rts; SegTree():rts(NULL) { }
SegTree(int n) {
rts = new SegTreeNode*[(n + )];
build(rts[], , n);
} void build(SegTreeNode*& node, int l, int r) {
node = newnode();
if(l == r) return;
int mid = (l + r) >> ;
build(node->l, l, mid);
build(node->r, mid + , r);
} void update(SegTreeNode*& newv, SegTreeNode*& oldv, int l, int r, int idx, int val) {
newv = newnode();
*newv = *oldv;
if(l == r) {
newv->val = val;
return;
}
int mid = (l + r) >> ;
if(idx <= mid) update(newv->l, oldv->l, l, mid, idx, val);
else update(newv->r, oldv->r, mid + , r, idx, val);
} int query(SegTreeNode*& node, int l, int r, int idx) {
if(l == r) return node->val;
int mid = (l + r) >> ;
if(idx <= mid) return query(node->l, l, mid, idx);
return query(node->r, mid + , r, idx);
} SegTreeNode*& operator [] (int pos) {
return rts[pos];
}
}SegTree; int n;
int length[];
SegTree st;
char s[]; inline void solve() {
scanf("%d", &n);
st = SegTree(n);
length[] = ;
for(int opt = , v = , x; opt <= n; opt++) {
scanf("%s%d", s, &x);
switch(s[]) {
case 'A':
length[v] = length[v - ] + ;
st.update(st[v], st[v - ], , n, length[v], x), v++;
break;
case 'Q':
printf("%d\n", st.query(st[v - ], , n, x));
break;
case 'U':
length[v] = length[v - x - ];
st[v] = st[v - x - ], v++;
break;
}
}
} int main() {
solve();
return ;
}
OpenJudge cdqz/Data Structure Challenge 2 (Problem 5822) - 可持久化线段树的更多相关文章
- [Codeforces 464E] The Classic Problem(可持久化线段树)
[Codeforces 464E] The Classic Problem(可持久化线段树) 题面 给出一个带权无向图,每条边的边权是\(2^{x_i}(x_i<10^5)\),求s到t的最短路 ...
- [BZOJ 3218] A + B Problem 【可持久化线段树 + 网络流】
题目连接:BZOJ - 3218 题目分析 题目要求将 n 个点染成黑色或白色,那么我们可以转化为一个最小割模型. 我们规定一个点 i 最后属于 S 集表示染成黑色,属于 T 集表示染成白色,那么对于 ...
- luogu P4137 Rmq Problem / mex(可持久化线段树)
一开始想的是莫队,然后维护几个bitset,然后瞎搞.脑子里想了想实现,发现并不好写. 还是主席树好写.我们维护一个权值的线段树,记录每一个权值的最后一次出现的位置下标.我们查询的时候要在前\(r\) ...
- 【Data Structure】-NO.117.DS.1 -【Tree-23树】
[Data Structure]-NO.117.DS.1 -[Tree-23树] Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total ...
- 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex
题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...
- UOJ#77. A+B Problem [可持久化线段树优化建边 最小割]
UOJ#77. A+B Problem 题意:自己看 接触过线段树优化建图后思路不难想,细节要处理好 乱建图无果后想到最小割 白色和黑色只能选一个,割掉一个就行了 之前选白色必须额外割掉一个p[i], ...
- 【BZOJ-3218】a+b Problem 最小割 + 可持久化线段树
3218: a + b Problem Time Limit: 20 Sec Memory Limit: 40 MBSubmit: 1320 Solved: 498[Submit][Status] ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- 【BZOJ 3218】 3218: a + b Problem(最小割+可持久化线段树)
3218: a + b Problem Time Limit: 20 Sec Memory Limit: 40 MBSubmit: 1440 Solved: 545 Description Inp ...
随机推荐
- “无效数字” ;java.lang.Integer cannot be cast to java.lang.String
今天页面上突然查询不出数据,大致的sql语句是 select xx ,xxx from table a where a.lrmb in ( 6101060033, 61010503300, 61016 ...
- composer----------composer基本命令和遇到一些问题解决方案
1.composer跟xdebug有冲突,每次用composer命令的时候都要报xdebug的错误,去php的配置文件里面将xdebug注释掉就可以了,但是我注释掉了以后还是不行.找了半天才看到,我用 ...
- Koa中设置中文Cookie值
默认情况下, 如果 ctx.cookies.set('user', '杨过', { domain: 'xxxx', path: 'xxxx', maxAge: 24 * 60 * 60 * 1000, ...
- Eclipse 在Debug调试中用到的快捷键
作用域 功能 快捷键 全局 单步返回 F7 全局 单步跳过 F6 全局 单步跳入 F5 全局 单步跳入选择 Ctrl+F5 全局 调试上次启动 F11 全局 继续 F8 全局 使用过滤器单步执行 Sh ...
- report源码分析——宏的执行
uvm_info,uvm_error其实是对uvm_report_info,uvm_report_error的封装. 其中warning,error,fatal,macros默认都是定义为UVM_NO ...
- Java多线程-----创建线程的几种方式
1.继承Thread类创建线程 定义Thread类的子类,并重写该类的run()方法,该方法的方法体就是线程需要完成的任务,run()方法也称为线程执行体 创建Thread子类的实例,也就是创建 ...
- K-Means算法的Java实现
K-means算法是硬聚类算法,是典型的基于原型的目标函数聚类方法的代表,它是数据点到原型的某种距离作为优化的目标函数,利用函数求极值的方法得到迭代运算的调整规则.K-means算法以欧式距离作为相似 ...
- 设计模式之Mediator(中介者)(转)
Mediator定义: 用一个中介对象来封装一系列关于对象交互行为. 为何使用Mediator? 各个对象之间的交互操作非常多;每个对象的行为操作都依赖彼此对方,修改一个对象的行为,同时会涉及到修改很 ...
- 使用Hive读取ElasticSearch中的数据
本文将介绍如何通过Hive来读取ElasticSearch中的数据,然后我们可以像操作其他正常Hive表一样,使用Hive来直接操作ElasticSearch中的数据,将极大的方便开发人员.本文使用的 ...
- sqlyog下载
sqlyog下载(附注册码):http://www.onlinedown.net/soft/24926.htm