描述

给一个空数列,有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) - 可持久化线段树的更多相关文章

  1. [Codeforces 464E] The Classic Problem(可持久化线段树)

    [Codeforces 464E] The Classic Problem(可持久化线段树) 题面 给出一个带权无向图,每条边的边权是\(2^{x_i}(x_i<10^5)\),求s到t的最短路 ...

  2. [BZOJ 3218] A + B Problem 【可持久化线段树 + 网络流】

    题目连接:BZOJ - 3218 题目分析 题目要求将 n 个点染成黑色或白色,那么我们可以转化为一个最小割模型. 我们规定一个点 i 最后属于 S 集表示染成黑色,属于 T 集表示染成白色,那么对于 ...

  3. luogu P4137 Rmq Problem / mex(可持久化线段树)

    一开始想的是莫队,然后维护几个bitset,然后瞎搞.脑子里想了想实现,发现并不好写. 还是主席树好写.我们维护一个权值的线段树,记录每一个权值的最后一次出现的位置下标.我们查询的时候要在前\(r\) ...

  4. 【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 ...

  5. 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex

    题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...

  6. UOJ#77. A+B Problem [可持久化线段树优化建边 最小割]

    UOJ#77. A+B Problem 题意:自己看 接触过线段树优化建图后思路不难想,细节要处理好 乱建图无果后想到最小割 白色和黑色只能选一个,割掉一个就行了 之前选白色必须额外割掉一个p[i], ...

  7. 【BZOJ-3218】a+b Problem 最小割 + 可持久化线段树

    3218: a + b Problem Time Limit: 20 Sec  Memory Limit: 40 MBSubmit: 1320  Solved: 498[Submit][Status] ...

  8. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  9. 【BZOJ 3218】 3218: a + b Problem(最小割+可持久化线段树)

    3218: a + b Problem Time Limit: 20 Sec  Memory Limit: 40 MBSubmit: 1440  Solved: 545 Description Inp ...

随机推荐

  1. setUp和tearDown及setUpClass和tearDownClass的用法及区别

    ① setup():每个测试函数运行前运行 ② teardown():每个测试函数运行完后执行 ③ setUpClass():必须使用@classmethod 装饰器,所有test运行前运行一次 ④ ...

  2. 纯css瀑布流布局

    由于公司的项目需要才用到瀑布流布局 因为后台返回的json直接循环出来的,所以不能做左右浮动的那种,所以才用到了瀑布流布局 16年之前的瀑布流布局基本都时基于js或者直接用jq插件的,但是随着技术的进 ...

  3. ruby中的alias和alias_method

    ruby中的alias和alias_method都可以重命名一个方法,它们的区别如下: 1.alias是ruby的一个关键字,因此使用的时候是alias :newname :oldname alias ...

  4. PKCS#1

    ASN.1 syntax,octet string是一个8 bytes sequence string. RSA中涉及到的Data conversion: 1)I2OSP,Integer to Oct ...

  5. win10系统进入BIOS

    按住shift+重启,在重启过程中界面会出现“疑难解答”,点击后,在新的界面点击“高级选项”,之后在新界面上点击“UEFI固件设置”,最后点击重启,重启过程中点击Delete键,就进入了BIOS界面了 ...

  6. JavaScript循环和数组常用操作

    while循环 语法: do while循环 语法:do{循环体}while(条件表达式); 特点:do while循环不管条件是否成立,无论如何循环体都会执行一次. 使用场合:用户输入密码,如果密码 ...

  7. 随机模拟(MCMC)

    http://cos.name/2013/01/lda-math-mcmc-and-gibbs-sampling/ http://blog.csdn.net/lin360580306/article/ ...

  8. JavaScript--常用的输出方式

       1.alert("要输出的内容"); 在浏览器中弹出一个对话框,然后把要输出的内容展示出来    2.document.write("要输出的内容");  ...

  9. js函数集

    js函数集·字符串(String) 1.声明 var myString = new String("Every good boy does fine."); var myStrin ...

  10. 【Redis学习之五】Redis数据类型:列表和散列

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 redis-2.8.18 一.列表 基于Linked Lis ...