SPOJ QTREE3 Query on a tree again! ——Link-Cut Tree
【题目分析】
QTREE2,一看是倍增算法,太懒了,不写了。( ̄_, ̄ )
QTREE3,树链剖分可以做,发现链上的问题LCT也很好做。
要是子树问题貌似可以DFS序。
然后就成LCT模板题了。
考前背板模式开启了。
【代码】
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define maxn 100005 struct LCT{
int ch[maxn][2],fa[maxn],sta[maxn],top,rev[maxn],mx[maxn],col[maxn];
bool isroot(int x)
{return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
void update(int x)
{
if (~mx[ch[x][0]]) mx[x]=mx[ch[x][0]];
else if (col[x]) mx[x]=x;
else if (~mx[ch[x][1]]) mx[x]=mx[ch[x][1]];
else mx[x]=-1;
}
void pushdown(int x)
{
if (rev[x])
{
rev[x]^=1;
rev[ch[x][0]]^=1;
rev[ch[x][1]]^=1;
swap(ch[x][0],ch[x][1]);
}
}
void rot(int x)
{
int y=fa[x],z=fa[y],l,r;
if (ch[y][0]==x) l=0; else l=1;
r=l^1;
if (!isroot(y))
{
if (ch[z][0]==y) ch[z][0]=x;
else ch[z][1]=x;
}
fa[x]=z; fa[y]=x; fa[ch[x][r]]=y;
ch[y][l]=ch[x][r]; ch[x][r]=y;
update(y);update(x);
}
void splay(int x)
{
top=0;sta[++top]=x;
for (int i=x;!isroot(i);i=fa[i]) sta[++top]=fa[i];
while (top) pushdown(sta[top--]); while (!isroot(x))
{
int y=fa[x],z=fa[y];
if (!isroot(y))
{
if (ch[z][0]==y^ch[y][0]==z) rot(y);
else rot(x);
}
rot(x);
}
}
void access(int x)
{for(int t=0;x;t=x,x=fa[x])splay(x),ch[x][1]=t,update(x);}
void makeroot(int x)
{access(x);splay(x);rev[x]^=1;}
int find(int x)
{access(x);splay(x);while(ch[x][0])x=ch[x][0];return x;}
void link(int x,int y)
{makeroot(x);fa[x]=y;}
void cut(int x,int y)
{makeroot(x);access(y);splay(y);if (ch[y][0]==x)fa[x]=0;}
int query(int x,int y)
{makeroot(x);access(y);splay(y);return mx[y];}
void init()
{memset(mx,-1,sizeof mx);}
void modify(int x)
{access(x);splay(x);col[x]^=1;update(x);}
}lct; int n,q; int main()
{
freopen("in.txt","r",stdin);
scanf("%d%d",&n,&q);
lct.init();
for (int i=1;i<n;++i)
{
int x,y;
scanf("%d%d",&x,&y);
lct.link(x,y);
}
while (q--)
{
int op,v;
scanf("%d%d",&op,&v);
if (op==0) lct.modify(v);
else printf("%d\n",lct.query(1,v));
}
}
SPOJ QTREE3 Query on a tree again! ——Link-Cut Tree的更多相关文章
- POJ 题目3237 Tree(Link Cut Tree边权变相反数,求两点最大值)
Tree Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 6131 Accepted: 1682 Description ...
- link cut tree 入门
鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...
- Link Cut Tree学习笔记
从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...
- LG3690 【模板】Link Cut Tree (动态树)
题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...
- 脑洞大开加偏执人格——可持久化treap版的Link Cut Tree
一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现 ...
- LG3690 【模板】Link Cut Tree 和 SDOI2008 洞穴勘测
UPD:更新了写法. [模板]Link Cut Tree 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 后接两个整数(x,y),代表询问从x到y ...
- (RE) luogu P3690 【模板】Link Cut Tree
二次联通门 : luogu P3690 [模板]Link Cut Tree 莫名RE第8个点....如果有dalao帮忙查错的话万分感激 #include <cstdio> #includ ...
- [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)
[BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...
- [BJOI2014]大融合(Link Cut Tree)
[BJOI2014]大融合(Link Cut Tree) 题面 给出一棵树,动态加边,动态查询通过每条边的简单路径数量. 分析 通过每条边的简单路径数量显然等于边两侧节点x,y子树大小的乘积. 我们知 ...
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题
A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...
随机推荐
- sql service 查询分析数据库
--学会通配符 https://blog.csdn.net/blackfwhite/article/details/80382849 --学会变量中的变量 https://www.cnblogs.co ...
- COGS 133. [USACO Mar08] 牛跑步
★★★ 输入文件:cowjog.in 输出文件:cowjog.out 简单对比时间限制:1 s 内存限制:128 MB Bessie准备用从牛棚跑到池塘的方法来锻炼. 但是因为她懒,她 ...
- codevs 3344 迷宫
时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 Description 小刚在迷宫内,他需要从A点出发,按顺序经过B,C,D……,到达最后一个点,再回到A ...
- (十一)maven之安装nexus私服
安装nexus私服 前面的文章中对项目引入jar依赖包的时候,maven一般先是在本地仓库找对应版本的jar依赖包,如果在本地仓库中找不到,就上中央仓库中下载到本地仓库. 然而maven默认提供的中央 ...
- nginx 编译某个模板的问题./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library stati
root@hett-PowerEdge-T30:/usr/local/src/nginx-1.9.8# ./configure --prefix=/usr/local/nginx --add-mod ...
- iOS图片目录批量复制到android图片目录
复制shell脚本 #!/bin/bash for i in `ls` do for imgname in `ls $i | grep '^WM.*'` do echo $imgname cp $i/ ...
- 数据库_4_SQL介绍
SQL SQL:Structured Query Language,结构化查询语言(数据已查询为主:99%是在进行查询操作) what型语言,而非how型的语言. SQL分为三个部分: DDL: ...
- 树状数组 简单题 cf 961E
题目链接 : https://codeforces.com/problemset/problem/961/E One day Polycarp decided to rewatch his absol ...
- Jordan 标准型的推论
将学习到什么 从 Jordan 标准型出发,能够获得非常有用的信息. Jordan 矩阵的构造 Jordan 矩阵 \begin{align} J=\begin{bmatrix} J_{n_1}( ...
- shell脚本,一个字符一个字符输出。
[root@localhost wyb]# cat file abc def abc 789de f567 [root@localhost wyb]# cat fffile.sh #!/bin/bas ...