3282. Tree【LCT】
Description
Input
Output
对于每一个0号操作,你须输出X到Y的路径上点权的Xor和。
Sample Input
1
2
3
1 1 2
0 1 2
0 1 1
Sample Output
1
#include<iostream>
#include<cstring>
#include<cstdio>
#define N (300000+100)
using namespace std;
int Father[N],Son[N][],Rev[N],Val[N],Xor[N];
int n,m; void Update(int x){Xor[x]=Val[x]^Xor[Son[x][]]^Xor[Son[x][]];}
int Get(int x) {return Son[Father[x]][]==x;}
int Is_root(int x) {return Son[Father[x]][]!=x && Son[Father[x]][]!=x;} void Rotate(int x)
{
int wh=Get(x);
int fa=Father[x],fafa=Father[fa];
if (!Is_root(fa)) Son[fafa][Son[fafa][]==fa]=x;
Father[fa]=x;
Son[fa][wh]=Son[x][wh^];
if (Son[fa][wh]) Father[Son[fa][wh]]=fa;
Father[x]=fafa;
Son[x][wh^]=fa;
Update(fa);
Update(x);
} void Pushdown(int x)
{
if (Rev[x] && x)
{
if (Son[x][]) Rev[Son[x][]]^=;
if (Son[x][]) Rev[Son[x][]]^=;
swap(Son[x][],Son[x][]);
Rev[x]=;
}
} int Push(int x)
{
if (!Is_root(x)) Push(Father[x]);
Pushdown(x);
} void Splay(int x)
{
Push(x);
for (int fa; !Is_root(x); Rotate(x))
if (!Is_root(fa=Father[x]))
Rotate(Get(fa)==Get(x)?fa:x);
} void Access(int x) {for (int y=;x;y=x,x=Father[x]) Splay(x),Son[x][]=y,Update(x);}
int Find_root(int x) {Access(x); Splay(x); while (Son[x][]) x=Son[x][]; return x;}
void Make_root(int x) {Access(x); Splay(x); Rev[x]^=;}
void Link(int x,int y) {Make_root(x); Father[x]=y;}
void Cut(int x,int y) {Make_root(x); Access(y); Splay(y); Son[y][]=Father[x]=;}
void Query(int x,int y){Make_root(x);Access(y); Splay(y); printf("%d\n",Xor[y]);} int main()
{
int opt,x,y;
scanf("%d%d",&n,&m);
for (int i=; i<=n; ++i)
scanf("%d",&Val[i]),Xor[i]=Val[i];
for (int i=; i<=m; ++i)
{
scanf("%d%d%d",&opt,&x,&y);
if (opt==) Query(x,y);
if (opt== && Find_root(x)!=Find_root(y)) Link(x,y);
if (opt== && Find_root(x)==Find_root(y)) Cut(x,y);
if (opt==) Access(x),Splay(x),Val[x]=y,Update(x);
}
}
3282. Tree【LCT】的更多相关文章
- [BZOJ 3282] Tree 【LCT】
题目链接:BZOJ - 3282 题目分析 这道题是裸的LCT,包含 Link , Cut 和询问两点之间的路径信息. 写代码时出现的错误:Access(x) 的循环中应该切断的是原来的 Son[x] ...
- BZOJ2631 tree 【LCT】
题目 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的边( ...
- [BZOJ - 2631] tree 【LCT】
题目链接:BZOJ - 2631 题目分析 LCT,像线段树区间乘,区间加那样打标记. 这道题我调了一下午. 提交之后TLE了,我一直以为是写错了导致了死循环. 于是一直在排查错误.直到.. 直到我看 ...
- 606. Construct String from Binary Tree 【easy】
606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...
- 【HDU4010】【LCT】Query on The Trees
Problem Description We have met so many problems on the tree, so today we will have a query problem ...
- 【BZOJ2049】【LCT】Cave 洞穴勘测
Description 辉 辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通 道组成,并且每条通道连接了 ...
- 543. Diameter of Binary Tree【Easy】【二叉树的直径】
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
- 3282: Tree(LCT)
3282: Tree Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 2249 Solved: 1042[Submit][Status][Discus ...
- 【BZOJ】3282: Tree(lct)
http://www.lydsy.com/JudgeOnline/problem.php?id=3282 复习了下lct,发现两个问题.. 1:一开始我以为splay那里直接全部rot(x)就好了,然 ...
随机推荐
- 【模板】埃拉托色尼筛法 && 欧拉筛法 && 积性函数
埃拉托色尼筛法 朴素算法 1 vis[1]=1; 2 for (int i=2;i<=n;i++) 3 if (!vis[i]) 4 { 5 pri[++tot]=i; 6 for (int j ...
- WPF绑定之索引器值变化通知
背景 在某些应用中,需要在界面上绑定到索引器,并在值发生变化时实时更新. 解决方案 只要将包含索引器的类实现INotifyPropertyChanged接口,并在索引值更改时引发PropertyCha ...
- 【原】spring redis 缓存注解使用
由于最近新上的项目很多模块没有做数据缓存,大量的请求都会到数据库去查询,为了减轻数据库的压力以及提高网站响应速度,所以在这里采用了spring 提供的注解+redis实现对数据的缓存,主要针对非热点数 ...
- python 软件管理规范
一.背景 软件开发是一个系统工程,当然编码实现是其中尤其重要的一个环节,关乎到功能需求的实现好坏.这个环节中除了编码这一硬功之外,与之相关的编码风格这一柔道,虽然没有直接决定功能的实现与否,但却在很大 ...
- HDU3533(KB2-D)
Escape Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- CSS样式之a标签(原文网址http://www.divcss5.com/shili/s57.shtml)
这是个人在做网站的时候整理的关于a标签的使用方法,整理一下,方便下次使用. 一.a超链接的代码 <a href="http://www.baidu.com" target=& ...
- ID3决策树算法实现(Python版)
# -*- coding:utf-8 -*- from numpy import * import numpy as np import pandas as pd from math import l ...
- 安装部署OpenPAI + VSCode 提交
========================================================== 安装openpai请参考这篇 https://www.cnblogs.com/ji ...
- CVE-2017-17215 - 华为HG532命令注入漏洞分析
前言 前面几天国外有个公司发布了该漏洞的详情.入手的二手 hg532 到货了,分析测试一下. 固件地址:https://ia601506.us.archive.org/22/items/RouterH ...
- 初识oracle重做日志文件
转自 http://blog.csdn.net/indexman/article/details/7746948 以下易容翻译自oracle dba官方文档,不足之处还望指出. 管理重做日志文件 学习 ...