http://www.lydsy.com/JudgeOnline/problem.php?id=3282

复习了下lct,发现两个问题。。

1:一开始我以为splay那里直接全部rot(x)就好了,然后改了好几题lct的题,都过了且速度和原版一样。。然后怀疑了下。。。。。。后来请教神犇,他说这样不行。。(这是单旋了?时间复杂度不保证,,但是我还不知道反例)

2:findroot操作里不要使用makeroot后再找root。。。。。。。。。。。。。。。。。。。。。。多么的sb啊。。。。。。。。

然后就是裸的lct。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=300005;
struct node *null;
struct node {
int v, rev, w;
node *ch[2], *fa;
node(const int _v=0) : v(_v), rev(0), w(0) { ch[0]=ch[1]=fa=null; }
bool d() { return fa->ch[1]==this; }
bool check() { return fa->ch[0]!=this && fa->ch[1]!=this; }
void setc(node* c, int d) { ch[d]=c; c->fa=this; }
void pushup() { w=ch[0]->w^ch[1]->w^v; }
void pushdown() {
if(rev) {
ch[0]->rev^=1;
ch[1]->rev^=1;
swap(ch[0], ch[1]);
rev=0;
}
}
}*t[N];
void rot(node* x) {
node* fa=x->fa; bool d=x->d();
fa->pushdown(); x->pushdown();
if(!fa->check()) fa->fa->setc(x, fa->d());
else x->fa=fa->fa;
fa->setc(x->ch[!d], d);
x->setc(fa, !d);
fa->pushup();
}
void fix(node* x) {
if(!x->check()) fix(x->fa);
x->pushdown();
}
void splay(node* x) {
fix(x);
while(!x->check())
if(x->fa->check()) rot(x);
else x->d()==x->fa->d()?(rot(x->fa), rot(x)):(rot(x), rot(x));
x->pushup();
}
node* access(node* x) {
node* y=null;
for(; x!=null; y=x, x=x->fa) {
splay(x);
x->ch[1]=y;
}
return y;
}
void mkroot(node* x) { access(x)->rev^=1; splay(x); }
void link(node* x, node* y) { mkroot(x); x->fa=y; }
void cut(node* x, node* y) {
mkroot(x); access(y); splay(y);
y->ch[0]->fa=null; y->ch[0]=null;
}
node* findrt(node* x) {
access(x); splay(x);
while(x->ch[0]!=null) x=x->ch[0];
return x;
}
void init() { null=new node; null->ch[0]=null->ch[1]=null->fa=null; }
int n, m; int main() {
init();
read(n); read(m);
for1(i, 1, n) t[i]=new node(getint());
rep(i, m) {
int c=getint(), x=getint(), y=getint();
if(c==0) { mkroot(t[x]); access(t[y]); splay(t[y]); printf("%d\n", t[y]->w); }
else if(c==1) { if(findrt(t[x])!=findrt(t[y])) link(t[x], t[y]); }
else if(c==2) { if(findrt(t[x])==findrt(t[y])) cut(t[x], t[y]); }
else if(c==3) { mkroot(t[x]); t[x]->v=y; t[x]->pushup(); }
}
return 0;
}

Description

给定N个点以及每个点的权值,要你处理接下来的M个操作。操作有4种。操作从0到3编号。点从1到N编号。

0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和。保证x到y是联通的。

1:后接两个整数(x,y),代表连接x到y,若x到Y已经联通则无需连接。

2:后接两个整数(x,y),代表删除边(x,y),不保证边(x,y)存在。

3:后接两个整数(x,y),代表将点X上的权值变成Y。

Input

第1行两个整数,分别为N和M,代表点数和操作数。

第2行到第N+1行,每行一个整数,整数在[1,10^9]内,代表每个点的权值。

第N+2行到第N+M+1行,每行三个整数,分别代表操作类型和操作所需的量。

Output

对于每一个0号操作,你须输出X到Y的路径上点权的Xor和。

Sample Input

3 3
1
2
3
1 1 2
0 1 2
0 1 1

Sample Output

3
1

HINT

1<=N,M<=300000

Source

【BZOJ】3282: Tree(lct)的更多相关文章

  1. 【BZOJ】1468: Tree(POJ1741) 点分治

    [题意]给定带边权树,求两点距离<=k的点对数.n<=40000. [算法]点分治 [题解]对于一个区域,选择其重心x作为根,则划分出来的每棵子树都是子区域,可以证明至多划分log n次( ...

  2. 3282: Tree(LCT)

    3282: Tree Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 2249  Solved: 1042[Submit][Status][Discus ...

  3. 【转】Device Tree(三):代码分析

    原文网址:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html 一.前言 Device Tree总共有三篇,分别是: 1.为何要引入De ...

  4. 【转】Device Tree(二):基本概念

    原文网址:http://www.wowotech.net/linux_kenrel/dt_basic_concept.html 一.前言 一些背景知识(例如:为何要引入Device Tree,这个机制 ...

  5. 【3】Decision tree(决策树)

    前言 Decision tree is one of the most popular classification tools 它用一个训练数据集学到一个映射,该映射以未知类别的新实例作为输入,输出 ...

  6. 【BZOJ】1468: Tree(点分治)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1468 分治真是一门高大上的东西... 好神... 树分治最好资料是:qzc的<分治算法在树的路 ...

  7. 【BZOJ】2959: 长跑(lct+缩点)(暂时弃坑)

    题目 传送门:QWQ 分析 看起来就是一个支持link的东西. 但有环,考虑缩点...... 但疯狂Tle.大概是常数卡不过去. 行走的大常数noble_ 代码 #include <bits/s ...

  8. 【Luogu】P2173网络(LCT)

    题目链接 这次坑我的是与或的结合顺序…… 开十个LCT记录一下即可.以上. #include<cstdio> #include<cstring> #include<cst ...

  9. 3754. 【NOI2014】魔法森林(LCT)

    Problem 给定一个\(n\)个结点,\(m\)条边的的无向图,每条边有两个权值\(ai,bi\). 现在从\(1\)出发,要到达\(n\),每次只能沿着\(ai\le A\)且\(bi\le B ...

随机推荐

  1. Pandas三个数据结构

    系列(Series) 数据帧(DataFrame) 面板(Panel) 这些数据结构构建在Numpy数组之上,这意味着它们很快. 考虑这些数据结构的最好方法是,较高维数据结构是其较低维数据结构的容器. ...

  2. 关于安装 rst2pdf 时遇到的 setuptools过老的问题

    由于想把flask里面的.rst文档 转一些成pdf方便查看和翻阅(.rst文档都非常新一直在维护) 所以查到一个模块rst2pdf 安装好之后却发现警告里面提示我的setuptools版本过老无法运 ...

  3. XE2 运行时 item not found的解决办法

    .net类库的原因. 将C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG下面的 machine.config.default 改名为machin ...

  4. selenium之安装和登陆操作举例

    安装selenium: python -m pip install selenium-3.4.3-py2.py3-none-any.whl 下载对应浏览器版本的驱动,且在环境变量PATH中指定驱动程序 ...

  5. python之类的继承

    # 类的的操作实例 # 子类ECar继承父类Car,并将实例Battery用作属性 class Car(): def __init__(self, name, model, year): self.n ...

  6. Lodop导出图片,导出单页内容的图片

    用如下语句设置图片的格式,设置导出的图片初识的默认名称,Lodop可以把打印内容导出成图片. LODOP.SET_SAVE_MODE("SAVEAS_IMGFILE_EXENAME" ...

  7. JavaScript——AJAX

    AJAX技术是网页构建的必备技能之一,本文希望能帮助大家轻松的学习这项技术 一.什么是ajax? ajax(异步javascript xml) 能够刷新局部网页数据而不是重新加载整个网页. 二.如何使 ...

  8. BZOJ1828[USACO 2010 Mar Gold 2.Barn Allocation]——贪心+线段树

    题目描述 输入 第1行:两个用空格隔开的整数:N和M * 第2行到N+1行:第i+1行表示一个整数C_i * 第N+2到N+M+1行: 第i+N+1行表示2个整数 A_i和B_i 输出 * 第一行: ...

  9. BZOJ3597 SCOI2014方伯伯运椰子(分数规划+spfa)

    即在总流量不变的情况下调整每条边的流量.显然先二分答案变为求最小费用.容易想到直接流量清空跑费用流,但复杂度略有些高. 首先需要知道(不知道也行?)一种平时基本不用的求最小费用流的算法——消圈法.算法 ...

  10. POJ3259(Wormholes) 判断负环

    题意: 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W个 虫洞组成,FJ想从一块土地开始,经过若干条路和虫洞 ...