【题目分析】

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的更多相关文章

  1. POJ 题目3237 Tree(Link Cut Tree边权变相反数,求两点最大值)

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 6131   Accepted: 1682 Description ...

  2. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  3. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

  4. LG3690 【模板】Link Cut Tree (动态树)

    题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...

  5. 脑洞大开加偏执人格——可持久化treap版的Link Cut Tree

    一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现 ...

  6. LG3690 【模板】Link Cut Tree 和 SDOI2008 洞穴勘测

    UPD:更新了写法. [模板]Link Cut Tree 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 后接两个整数(x,y),代表询问从x到y ...

  7. (RE) luogu P3690 【模板】Link Cut Tree

    二次联通门 : luogu P3690 [模板]Link Cut Tree 莫名RE第8个点....如果有dalao帮忙查错的话万分感激 #include <cstdio> #includ ...

  8. [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)

    [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...

  9. [BJOI2014]大融合(Link Cut Tree)

    [BJOI2014]大融合(Link Cut Tree) 题面 给出一棵树,动态加边,动态查询通过每条边的简单路径数量. 分析 通过每条边的简单路径数量显然等于边两侧节点x,y子树大小的乘积. 我们知 ...

  10. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

随机推荐

  1. C# for循环的嵌套 作用域

    for() {   循环体可以套无数个for循环 } 比如:for() { for() { for() {... ...这里面可以镶嵌无数个for循环} } } 也可以这样 for() { for() ...

  2. MySQL 导出一句话

    听说是很老的东西了,学习的时候发现还是很好用的,故学习转载过来,留备学习. mysql 导出一句话 方法1:网上流行的方法 流程:(1)建表--->(2)插入数据--->(3)select ...

  3. Alpha-beta pruning

    function alphabeta(node, depth, α, β, maximizingPlayer) or node is a terminal node return the heuris ...

  4. 宠溺旧习,win10清单-配置与软件

    从win98到win7塑就的旧“习 不是一两天能随了win10的任性 输入法反win X的头疼与苦恼 开机总要输密码的麻烦与滋扰 还有着一些莫名其妙的问题, 在过往与如今的交织间错乱. -序 好吧,其 ...

  5. jni 开发

    创建android工程 -> 添加native 函数 添加库之后: 1. 用javah 生成c语言.h头文件时, 在cmd 窗口中cd 到bin/classes 目录下执行下代码无效: java ...

  6. Codeforces Round #277.5 (Div. 2)-A. SwapSort

    http://codeforces.com/problemset/problem/489/A A. SwapSort time limit per test 1 second memory limit ...

  7. 【Codeforces Rockethon 2014】Solutions

    转载请注明出处:http://www.cnblogs.com/Delostik/p/3553114.html 目前已有[A B C D E] 例行吐槽:趴桌子上睡着了 [A. Genetic Engi ...

  8. [BZOJ3307]:雨天的尾巴(LCA+树上差分+权值线段树)

    题目传送门 题目描述: N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多的是哪种物品. 输入格式: 第一 ...

  9. 洛谷 P2921 在农场万圣节

    https://www.luogu.org/problemnew/show/P2921 开始感觉这题30行代码就可以搞定,还是太菜啦,还是乖乖地写了tarjan. 对图进行缩点,那么这个强联通分量中的 ...

  10. POJ-3669-流星雨

    这题的话,卡了有两个小时左右,首先更新地图的时候越界了,我们进行更新的时候,要判断一下是不是小于零了,越界就会Runtime Error. 然后bfs 的时候,我没有允许它搜出300以外的范围,然后就 ...