zjnu1726 STOGOVI (lca)
Description
Mirko is playing with stacks. In the beginning of the game, he has an empty stack denoted with number 0. In the
ith step of the game he will choose an existing stack denoted with v, copy it and do one of the following actions:
a. place number i on top of the new stack
b. remove the number from the top of the new stack
c. choose another stack denoted with w and count how many different numbers exist that are in the new stack
and in the stack denoted with w
The newly created stack is denoted with i.
Mirko doesn’t like to work with stacks so he wants you to write a programme that will do it for him. For each
operation of type b output the number removed from stack and for each operation of type c count the required
numbers and output how many of them there are.
Input
The first line of input contains the integer N (1 <= N <= 300000), the number of steps in Mirko’s game.
The steps of the game are chronologically denoted with the first N integers.
The ith of the following N lines contains the description of the ith step of the game in one of the following three
forms:
"a v" for operation of type a.
"b v" for operation of type b.
"c v w" for operation of type c.
The first character in the line denotes the type of operation and the following one or two denote the accompanying
stack labels that will always be integers from the interval [0,i-1].
For each operation of type b, the stack we’re removing the element from will not be empty.
Output
For each operation type b or c output the required number, each in their own line, in the order the operations
were given in the input.
Sample Input
a 0
a 1
b 2
c 2 3
b 4
11
a 0
a 1
a 2
a 3
a 2
c 4 5
a 5
a 6
c 8 7
b 8
b 8
Sample Output
1
2
2
2
8
8
Hint
In the beginning, we have the stack S0 = {}. In the first step, we copy S0 and place
number 1 on top, so S1 = {1}. In the second step, we copy S1 and place 2 on top of it, S2 = {1,2}. In the third step we
copy S2 and remove number 2 from it, S3 = {1}. In the fourth step we copy S2 and denote the copy with S4, then count
the numbers appearing in the newly created stack S4 and stack S3, the only such number is number 1 so the solution is 1.
In the fifth step we copy S4 and remove number 2 from it, S5 = {1}.
题意:一开始给你一个空栈,有3个操作。1.a v:先把编号为v的栈复制,然后在栈顶上放i 2.b v:先把编号为v的栈复制,然后去掉栈顶元素 3.c v w:先把编号为v的栈复制,然后数出同时存在于v,w栈的数的个数。开一个二叉树,如果是a操作,那么加入新的节点,如果是b操作,那么找到v的父节点,如果是c操作,那么全部输入读入后,求一个lca。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 300005
vector<int>vec[maxn];
vector<int>::iterator it;
struct node{
int x,y;
}c[maxn];
int jd[maxn],fa[maxn],f[maxn][25],dep[maxn],ans[maxn],daibiao[maxn],vis[maxn],siz[maxn];
int lca(int x,int y){
int i;
if(dep[x]<dep[y]){
swap(x,y);
}
for(i=20;i>=0;i--){
if(dep[f[x][i] ]>=dep[y]){
x=f[x][i];
}
}
if(x==y)return x;
for(i=20;i>=0;i--){
if(f[x][i]!=f[y][i]){
x=f[x][i];y=f[y][i];
}
}
return f[x][0];
}
int main()
{
int n,m,i,j,x,jiedian,jiedian1,y,k;
char s[10];
while(scanf("%d",&m)!=EOF)
{
jd[1]=1;
for(i=1;i<=m+1;i++)vec[i].clear();
int t=1;
daibiao[1]=1;
dep[1]=1;
for(i=2;i<=m+1;i++){
scanf("%s%d",s,&x);x++;
jiedian=jd[x];
if(s[0]=='a'){
t++;
dep[t]=dep[jiedian]+1;
jd[i]=t;
f[t][0]=jiedian;
daibiao[t]=i;
c[i].x=c[i].y=-2;
}
else if(s[0]=='b'){
ans[i]=daibiao[jiedian];
jiedian1=f[jiedian][0];
jd[i]=jiedian1;
c[i].x=c[i].y=-1;
}
else if(s[0]=='c'){
scanf("%d",&y);y++;
jd[i]=jiedian;
c[i].x=jd[i];c[i].y=jd[y];
}
}
for(k=1;k<=20;k++){
for(i=1;i<=m+1;i++){
f[i][k]=f[f[i][k-1]][k-1];
}
}
for(i=2;i<=m+1;i++){
if(c[i].x==-2)continue;
if(c[i].x==-1){
printf("%d\n",ans[i]-1);
}
else{
int gong=lca(c[i].x,c[i].y );
printf("%d\n",dep[gong]-1);
}
}
}
return 0;
}
zjnu1726 STOGOVI (lca)的更多相关文章
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]
3626: [LNOI2014]LCA Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2050 Solved: 817[Submit][Status ...
- [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)
Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...
- [bzoj2588][count on a tree] (主席树+lca)
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
- [板子]倍增LCA
倍增LCA板子,没有压行,可读性应该还可以.转载请随意. #include <cstdio> #include <cstring> #include <algorithm ...
- poj3417 LCA + 树形dp
Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4478 Accepted: 1292 Descripti ...
- [bzoj3626][LNOI2014]LCA
Description 给出一个$n$个节点的有根树(编号为$0$到$n-1$,根节点为$0$). 一个点的深度定义为这个节点到根的距离$+1$. 设$dep[i]$表示点$i$的深度,$lca(i, ...
- (RMQ版)LCA注意要点
inline int lca(int x,int y){ if(x>y) swap(x,y); ]][x]]<h[rmq[log[y-x+]][y-near[y-x+]+]])? rmq[ ...
- bzoj3631: [JLOI2014]松鼠的新家(LCA+差分)
题目大意:一棵树,以一定顺序走完n个点,求每个点经过多少遍 可以树链剖分,也可以直接在树上做差分序列的标记 后者打起来更舒适一点.. 具体实现: 先求x,y的lca,且dep[x]<dep[y] ...
随机推荐
- go mod 拉取私有仓库
前言 如果代码中依赖了本地的包, 这个包是托管在内网 Gitlab 中, 而且不是 HTTPS 服务,那么应该怎样使用 go mod 拉取代码呢? 本文会给你我的答案 正文 首先我们要知道, 如果本地 ...
- VRay for SketchUp渲染图黑原因及解决方案
很多人都遇到用Vray for SketchUp云渲染的时候,渲染出来的图片是全黑或者是局部是黑色, 这是什么原因呢? 1.有一种情况是,SketchUp的文件储存机制和其他的软件有些不同,它是把模型 ...
- Spring中的@Valid 和 @Validated注解你用对了吗
1.概述 本文我们将重点介绍Spring中 @Valid和@Validated注解的区别 . 验证用户输入是否正确是我们应用程序中的常见功能.Spring提供了@Valid和@Validated两个注 ...
- Python运维自动化psutil 模块详解(超级详细)
psutil 模块 参考官方文档:https://pypi.org/project/psutil/ 一.psutil简介 psutil是一个开源且跨平台(http://code.google.com/ ...
- 【计算机基础】常用的快捷键和DOS命令
常用的快捷键和DOS命令 DOS命令 使用Linux比较酷 cool
- 【Oracle】归档日志的删除操作
[root@sha3 oracle]# rman target / Recovery Manager: Release 10.2.0.4.0 - Production on Tue Jan 20 01 ...
- File Inclusion - Pikachu
概述: 文件包含,是一个功能.在各种开发语言中都提供了内置的文件包含函数,其可以使开发人员在一个代码文件中直接包含(引入)另外一个代码文件. 比如 在PHP中,提供了: include(),inclu ...
- 开发进阶:Dotnet Core多路径异步终止
今天用一个简单例子说说异步的多路径终止.我尽可能写得容易理解吧,但今天的内容需要有一定的编程能力. 今天这个话题,来自于最近对gRPC的一些技术研究. 话题本身跟gRPC没有太大关系.应用中,我用 ...
- 用SAP浏览网页
在SAP里,通过两个类就可以做一个简单的,嵌入sap里的网页.这两个类就是 1. cl_gui_custom_container 这个类是自定义屏幕里用得,也就是画一个container,在这个容器中 ...
- 【源码解读】js原生消息提示插件
效果如下: 关闭message后前后message的衔接非常丝滑,这部分是我比较感兴趣的.带着这个问题先了解下DOM结构,顺便整理下作者的思路. 从DOM里我们可以看到所有的message都在一个容器 ...