G. The Declaration of Independence
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

In 1776, a Committee of Five was chosen to draft the Declaration of Independence of the USA, among them John Adams, Thomas Jefferson and Benjamin Franklin. From June 11 to July 5 they worked tirelessly to write such important document.

It wasn't written correctly in the first attempt, and many many changes had to be made. Since in that time there weren't such great and time-saving utilities like git and version control, they had a very simple but time consuming way to keep old versions of the document.

Suppose Thomas Jefferson wanted to add a line to a version i of the document. Instead of changing the actual document in version i, he would use a letter copying press, a machine just invented by James Watt just around that time, to copy the version i to a new sheet of paper, and then would modify this new sheet of paper. This paper would then be stored so it could be copied later. As each modification creates a new version of the document, the kth modification will create the version k of the document. You can assume the version 0 of the document is an empty piece of paper.

As everything was written in ink and the committee doesn't like to contradict itself, each modification could only add some lines to the end of the document, or erase some lines from the beginning of the document.

Your task is to simulate the making of the Declaration of Independence. Each sentence is represented as an integer number. You need to process the following queries:

  • E v x — Copy the document with version v, then add sentence x to the end of the copied document
  • D v — Copy the document with version v, then remove the first sentence of the copied document

Queries are numbered from one. The document with version 0 is empty.

Input

In the first line an integer Q, the number of queries. Each of the next Q lines has a query, in the format given in the statement.

Limits

  • 1 ≤ Q ≤ 105
  • In the ith query it is guaranteed that 0 ≤ v < i
  • For each query of type E, x will fit in a 32-bit signed integer
  • For each query of type D, it is guaranteed the document will not be empty during the removal of the first sentence
Output

For each query of type D, print the sentence removed.

Example
input
8
E 0 10
E 0 -10
D 2
D 1
E 2 5
D 5
D 6
D 2
output
-10
10
-10
5
-10

题意:q个操作,你需要在i是一个队列;

   E v x 表示在第v个队列插入x,形成第i个队列;

   D v 表示去掉v的对头,形成第i个队列;

思路:主席树,利用线段树第i个点,存储队列第(i-对头的位置)个元素的大小;

   利用L,R数组表示这个队列的左端在L[i],右端在R[i]的位置;

  

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=1e9+;
struct SGT
{
int ls[N*],rs[N*],rt[N*],L[N*],R[N*],num[N*];
int tot;
void init()
{
tot=;
memset(num,,sizeof(num));
memset(ls,,sizeof(ls));
memset(rs,,sizeof(rs));
memset(rt,,sizeof(rt));
memset(L,,sizeof(L));
memset(R,,sizeof(R));
}
void build(int l,int r,int &pos)
{
pos=++tot;
if(l==r)return;
int mid=(l+r)>>;
build(l,mid,ls[pos]);
build(mid+,r,rs[pos]);
}
void update(int rt,int p,int c,int l,int r,int &pos)
{
pos=++tot;
ls[pos]=ls[rt];
rs[pos]=rs[rt];
if(l==r)
{
num[pos]=c;
return;
}
int mid=(l+r)>>;
if(p<=mid)update(ls[rt],p,c,l,mid,ls[pos]);
else update(rs[rt],p,c,mid+,r,rs[pos]);
}
int query(int p,int l,int r,int pos)
{
if(l==r) return num[pos];
int mid=(l+r)>>;
if(p<=mid)return query(p,l,mid,ls[pos]);
else return query(p,mid+,r,rs[pos]);
}
}tree;
char a[];
int main()
{ int q;
scanf("%d",&q);
//tree.init();
tree.build(,q,tree.rt[]);
tree.L[]=;
tree.R[]=;
for(int i=;i<=q;i++)
{
scanf("%s",a);
if(a[]=='E')
{
int v,x;
scanf("%d%d",&v,&x);
tree.L[i]=tree.L[v];
tree.R[i]=tree.R[v];
tree.update(tree.rt[v],++tree.R[i],x,,q,tree.rt[i]=tree.rt[v]);
}
else
{
int v;
scanf("%d",&v);
tree.L[i]=tree.L[v];
tree.R[i]=tree.R[v];
//cout<<tree.L[v]<<" "<<tree.R[v]<<endl;
printf("%d\n",tree.query(tree.L[i]++,,q,tree.rt[i]=tree.rt[v]));
}
}
return ;
}
/* 8
E 0 10
E 0 -10
D 2
D 1
E 2 5
D 5
D 6
D 2 */

GYM 101064 2016 USP Try-outs G. The Declaration of Independence 主席树的更多相关文章

  1. gym 101064 G.The Declaration of Independence (主席树)

    题目链接: 题意: n个操作,有两种操作: E p  c    在序号为p的队列尾部插入c得到新的队列,序号为i D p   查询并删除序号为p的队列顶部的元素,得到序号为i的新队列 思路: 需要查询 ...

  2. Gym 101064 D Black Hills golden jewels (二分)

    题目链接:http://codeforces.com/gym/101064/problem/D 问你两个数组合相加的第k大数是多少. 先sort数组,二分答案,然后判断其正确性(判断过程是枚举每个数然 ...

  3. 2016年省赛 G Triple Nim

    2016年省赛 G Triple Nimnim游戏,要求开始局面为先手必败,也就是异或和为0.如果n为奇数,二进制下最后一位只有两种可能1,1,1和1,0,0,显然异或和为1,所以方案数为0如果n为偶 ...

  4. 数据结构(主席树):HZOI 2016 采花

    [题目描述] 给定一个长度为n,包含c种颜色的序列,有m个询问,每次给出两个数l,r,表示询问区间[l,r]中有多少种颜色的出现次数不少于2次. 本题强制在线,对输入的l,r进行了加密,解密方法为: ...

  5. codeforces gym #101161E - ACM Tax(lca+主席树)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: 给出节点数为$n$的树 有$q$次询问,输出$a$节点到$b$节点路程中,经过的边的中位数 ...

  6. bzoj4408 [Fjoi 2016]神秘数 & bzoj4299 Codechef FRBSUM 主席树+二分+贪心

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4299 https://lydsy.com/JudgeOnline/problem.php?id ...

  7. 牛客多校第三场 G Removing Stones(分治+线段树)

    牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...

  8. codeforces Gym 100735 D、E、G、H、I

    http://codeforces.com/gym/100735 D题 直接暴力枚举 感觉这道题数据有点问题 为什么要先排下序才能过?不懂.. #include <stdio.h> #in ...

  9. 2016年省赛G题, Parenthesis

    Problem G: Parenthesis Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 398  Solved: 75[Submit][Status ...

随机推荐

  1. 前端框架VUE----vue的使用

    一.安装 对于新手来说,强烈建议大家使用<script>引入 二. 引入vue.js文件 我们能发现,引入vue.js文件之后,Vue被注册为一个全局的变量,它是一个构造函数. 三.使用V ...

  2. Python2的一些问题及解决办法

    1. 无法注释中文的解决办法 # -*- coding:utf8 -*- # 添加这一行就行了 from django.contrib import admin from myapp.models i ...

  3. 火车时刻表WebApp

    关键词 :Ajax 跨域访问 php 同源策略 JQueryMobile 前言 在面试的过程中,兄弟连的徐老师提出要求我用JQuery Mobile(前端框架)来实现一个具有“火车时刻表”功能的Web ...

  4. nmap扫描内网存活机器脚本

    nmap扫描内网存活机器并保存在指定文件中. host.sh #/usr/bin/bash read -p "Please input scan host or network:" ...

  5. opencv学习之路(15)、形态学其他操作(开、闭、顶帽、黑帽、形态学梯度)

    一.形态学其他操作(用的不多,如果忘了也可以通过膨胀腐蚀得到相同效果) 1.开运算 2.闭运算 3.形态学梯度 4.顶帽 5.黑帽 #include "opencv2/opencv.hpp& ...

  6. [c/c++] programming之路(6)、ASCII码,数据类型、随机数、字符转换及拼接等

    一.变量 #include<stdio.h> #include<stdlib.h> void main0(){ //数据使用必须在范围内,否则产生溢出 unsigned +;/ ...

  7. form 表单中 button 按钮用 return false 阻止默认刷新踩过的一个小坑

    今天在写一个button按钮的事件时,明明在点击事件的最后写了 return false,可是点击按钮页面仍然会自动刷新 最后看到刷新前 控制台有一抹红色一闪而过,由于速度很快,之前一直都注意到 后来 ...

  8. 如何利用好github的问题

    github对我来说真的是一个超好的平台,不过之前只是把它仓库来使用, 后来在大佬告诉我应该怎么使用github,今天就来总结下如何利用好github,让它发挥最大的威力. 1.把github当做百科 ...

  9. Codeforces 839C Journey - 树形动态规划 - 数学期望

    There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can r ...

  10. 【python005-数据类型】

    数据类型 一.字符串的相加是拼接,数字的相加是求和 二.python的数值类型:整形,浮点型,e记法,布尔类型 >>> 1.2e412000.0>>> 1.2e-4 ...