【题目分析】

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. SQL根据出生日期精确计算年龄、获取日期中的年份、月份

    第一种: 一张人员信息表里有一人生日(Birthday)列,跟据这个列,算出该人员的年龄 datediff(year,birthday,getdate()) 例:birthday = '2003-3- ...

  2. 连接MongoDB数据库的配置说明

  3. 树形dp——覆盖所有边的最少费用(Protecting Zonk)

    一.问题描述 有一个n(n<=10000)个节点的无根树.有两种装置A,B,每种都有无限多个. 1.在某个节点X使用A装置需要C1(C1<=1000)的花费,并且此时与节点X相连的边都被覆 ...

  4. WPF中的TextBlock处理长字符串

    Xaml: <StackPanel> <TextBlock Margin="10" Foreground="Red"> This is ...

  5. 创建一个文件夹用于写入UTF-8编码的文件

    实现效果: 知识运用: File类的CreateText方法 StreamWriter类的WriteLine方法 实现代码: private void button2_Click(object sen ...

  6. ubuntu frp 自编译。本文不能按顺序来 请自己理解

    go run:go run 编译并直接运行程序,它会产生一个临时文件(但不会生成 .exe 文件),直接在命令行输出程序执行结果,方便用户调试. go build:go build 用于测试编译包,主 ...

  7. CSS - position属性小结

    Relative: 属于文档流,针对自身进行偏移: Absolute: 脱离文档流,针对最近的定位元素进行偏移,如果没有,则针对根元素,即body标签尽心偏移: Fixed: 和absolute基本一 ...

  8. C# 使用Epplus导出Excel [2]:导出动态列数据

    C# 使用Epplus导出Excel [1]:导出固定列数据 C# 使用Epplus导出Excel [2]:导出动态列数据 C# 使用Epplus导出Excel [3]:合并列连续相同数据 C# 使用 ...

  9. 【meet in middle】poj1840Eqs

    震惊!map的常数居然如此之大 Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43 ...

  10. 【动态规划】poj2353Ministry

    拓扑序……好些玄妙 Description Mr. F. wants to get a document be signed by a minister. A minister signs a doc ...