BZOJ-3282 Tree Link-Cut-Tree(似乎树链剖分亦可)
蛋蛋用链剖A的,我写的LCT
3282: Tree
Time Limit: 30 Sec Memory Limit: 512 MB
Submit: 1241 Solved: 542
[Submit][Status][Discuss]
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
动态树
Link-Cut-Tree模版啊。。话不多说直接干
code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int read()
{
int x=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define N 300100
int fa[N],sum[N],tmp[N],val[N],son[N][2];
bool rev[N];
int n,m;
bool isroot(int x)
{
return !fa[x]||son[fa[x]][0]!=x&&son[fa[x]][1]!=x;
}
void rev1(int x)
{
if (!x) return;
swap(son[x][0],son[x][1]);
rev[x]^=1;
}
void pb(int x)
{
if (rev[x])
rev1(son[x][0]),rev1(son[x][1]),rev[x]=0;
}
void up(int x)
{
sum[x]=sum[son[x][0]]^sum[son[x][1]]^val[x];
}
void rotate(int x)
{
int y=fa[x],w=son[y][1]==x;
son[y][w]=son[x][w^1];
if(son[x][w^1]) fa[son[x][w^1]]=y;
if(fa[y])
{
int z=fa[y];
if(son[z][0]==y) son[z][0]=x;
else if(son[z][1]==y) son[z][1]=x;
}
fa[x]=fa[y]; fa[y]=x;
son[x][w^1]=y; up(y);
}
void splay(int x)
{
int s=1,i=x,y;tmp[1]=i;
while (!isroot(i)) tmp[++s]=i=fa[i];
while (s) pb(tmp[s--]);
while (!isroot(x))
{
y=fa[x];
if(!isroot(y))
{
if((son[fa[y]][0]==y)^(son[y][0]==x))
rotate(x); else rotate(y);
}
rotate(x);
}
up(x);
}
void access(int x)
{
for (int y=0; x; y=x,x=fa[x])
splay(x),son[x][1]=y,up(x);
}
void makeroot(int x)
{
access(x);splay(x);rev1(x);
}
void link(int x,int y)
{
makeroot(x); fa[x]=y; access(x);
}
void cutf(int x)
{
access(x);splay(x);fa[son[x][0]]=0; son[x][0]=0; up(x);
}
void cut(int x,int y)
{
makeroot(x);cutf(y);
}
int find(int x)
{
access(x);splay(x);
int y=x;
while (son[y][0]) y=son[y][0];
return y;
}
int main()
{
n=read(),m=read();
for (int i=1; i<=n; i++)
{
int x=read();
sum[i]=x; val[i]=x;
}
for (int i=1; i<=m; i++)
{
int com=read();
int x=read(),y=read();
switch (com)
{
case 0: makeroot(x); access(y); splay(y); printf("%d\n",sum[y]); break;
case 1: if (find(x)!=find(y)) link(x,y); break;
case 2: if (find(x)==find(y)) cut(x,y); break;
case 3: access(x); splay(x); val[x]=y; up(x); break;
}
}
return 0;
}
BZOJ-3282 Tree Link-Cut-Tree(似乎树链剖分亦可)的更多相关文章
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 )
BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树+树链剖分+线段树
E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...
- D. Happy Tree Party CodeForces 593D【树链剖分,树边权转点权】
Codeforces Round #329 (Div. 2) D. Happy Tree Party time limit per test 3 seconds memory limit per te ...
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge (最小生成树+树链剖分)
题目链接:http://codeforces.com/contest/609/problem/E 给你n个点,m条边. 问枚举每条边,问你加这条边的前提下组成生成树的权值最小的树的权值和是多少. 先求 ...
- bzoj 3626 [LNOI2014]LCA(离线处理+树链剖分,线段树)
3626: [LNOI2014]LCA Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1272 Solved: 451[Submit][Status ...
- bzoj 4326: NOIP2015 运输计划(二分+树链剖分)
传送门 题解: 树链剖分快速求解任意两点间的路径的权值和: 然后,二分答案: 此题的难点是如何快速求解重合路径? 差分数组可以否??? 在此之前先介绍一下相关变量: int fa[maxn]; int ...
- BZOJ 2402 陶陶的难题II (树链剖分、线段树、凸包、分数规划)
毒瘤,毒瘤,毒瘤-- \(30000\)这个数据范围,看上去就是要搞事的啊... 题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2402 ...
- BZOJ 3631: [JLOI2014]松鼠的新家( 树链剖分 )
裸树链剖分... ------------------------------------------------------------------- #include<bits/stdc++ ...
- bzoj 3631 松鼠的新家 (树链剖分)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3631 思路: 直接用树链剖分求每一次运动,因为这道题只需要区间增添,单点求值,没必要用线段 ...
随机推荐
- android studio 使用入门 (快捷键等收集)
1. 解决 android studio cannot resolve symbol 1) file->import proj->create proj from exit proj .. ...
- Chrome 开发工具 Javascript 调试技巧
http://www.w3cplus.com/tools/dev-tips.html 一.Sources 面板介绍: Sources 面板分为左中右 3 部分左:Sources 当前页面加载的资源列表 ...
- 使用EasyUI要引入哪些文件
使用EasyUI,一般需要导入如下文件 <link rel="stylesheet" type="text/css" href="../reso ...
- 【转】【WPF】WPF 登录窗口关闭时打开主窗口
在WPF中设计登录窗口关闭时打开主窗口,自动生成的App.xaml不能满足要求, 1.把App.xaml的属性窗口中的生成操作设定为 无 2.添加Program类 static class Progr ...
- 未能解析此远程名称:'nuget.org' 的解决方法
今天用Nuget下一个程序包时,发现Nuget挂了: 未能解析此远程名称:'nuget.org' . 浏览器打开 http://nuget.org 失败. 使用cmd命令 输入nslookup n ...
- Linux内核启动
Linux内核启动过程概述 Linux的启动代码真的挺大,从汇编到C,从Makefile到LDS文件,需要理解的东西很多.毕竟Linux内核是由很多人,花费了巨大的时间和精力写出来的.而且直到现在,这 ...
- Git基础 - git blame
当想知道一段代码历史上有哪些人修改时,可以使用git blame查看,正如其名,当你看到那段让你抓狂的代码时,一定想找出是谁写的来一顿blame吧 : ) 使用方法 icebug@localhost: ...
- select、poll、epoll程序实例
三个函数的基本用法如下: select 创建 fd_set rset , allset; FD_ZERO(&allset); FD_SET(listenfd, &allset); 监听 ...
- Android连接网络打印机,jSocket连接网络打印机
老大写的一个打印工具类,记录一下. package com.Ieasy.Tool; import android.annotation.SuppressLint; import java.io.IOE ...
- [CareerCup] 3.6 Sort Stack 栈排序
3.6 Write a program to sort a stack in ascending order (with biggest items on top). You may use at m ...