bzoj1180 tree
link cut tree
模板题
link cut tree不都是模板题嘛?(雾
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define rre(i,r,l) for(int i=(r);i>=(l);i--)
#define re(i,l,r) for(int i=(l);i<=(r);i++)
#define Clear(a,b) memset(a,b,sizeof(a))
#define inout(x) printf("%d",(x))
#define douin(x) scanf("%lf",&x)
#define strin(x) scanf("%s",(x))
#define LLin(x) scanf("%lld",&x)
#define op operator
#define CSC main
typedef unsigned long long ULL;
typedef const int cint;
typedef long long LL;
using namespace std;
void inin(int &ret)
{
ret=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=;ch=getchar();}
while(ch>=''&&ch<='')ret*=,ret+=ch-'',ch=getchar();
ret=f?-ret:ret;
}
int n,q,w[],ch[][],fa[],sum[],rev[];
bool isroot(int x){return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;}
void maintain(int x){if(x)sum[x]=w[x]+sum[ch[x][]]+sum[ch[x][]];}
void rotate(int x)
{
int y=fa[x],z=fa[y];
if(!isroot(y))ch[z][ch[z][]==y]=x;
fa[x]=z,fa[y]=x;
int d=ch[y][]==x;
fa[ch[x][d^]]=y;
ch[y][d]=ch[x][d^];
ch[x][d^]=y;
maintain(y);maintain(x);
}
void down(int x)
{
if(rev[x])
{
swap(ch[x][],ch[x][]);
rev[ch[x][]]^=;
rev[ch[x][]]^=;
rev[x]=;
}
}
int sta[],top;
void splay(int x)
{
top=;int xx=x;sta[++top]=xx;
while(!isroot(xx))sta[++top]=fa[xx],xx=fa[xx];
while(top)down(sta[top--]);
while(!isroot(x))
{
int y=fa[x],z=fa[y];
if(!isroot(y))
if((ch[y][]==x)^(ch[z][]==y))rotate(x);
else rotate(y);else ;
rotate(x);
}
}
void access(int x)
{
int temp=;
while(x)
{
splay(x);
ch[x][]=temp;maintain(x);
temp=x,x=fa[x];
}
}
void reverse(int x)
{
access(x),splay(x),rev[x]^=;
}
void link(int x,int y)
{
reverse(x),fa[x]=y;
}
void cut(int x,int y)
{
reverse(x),access(y),fa[x]=ch[y][]=;maintain(y);
}
int find(int x)
{
access(x);splay(x);
while(ch[x][])x=ch[x][];
return x;
}
int main()
{
inin(n);char ss[];
re(i,,n)inin(w[i]),sum[i]=w[i];
inin(q);
re(i,,q)
{
strin(ss);int a,b;inin(a),inin(b);
if(ss[]=='b')
{
if(find(a)==find(b))cout<<"no\n";
else
{
cout<<"yes\n";
link(a,b);
}
}
else if(ss[]=='p')
{
splay(a),w[a]=b,maintain(a);
}
else if(ss[]=='e')
{
if(find(a)!=find(b)){cout<<"impossible\n";continue;}
reverse(a),access(b),splay(b);
printf("%d\n",sum[b]);
}
}
return ;
}
bzoj1180 tree的更多相关文章
- [bzoj2843&&bzoj1180]极地旅行社 (lct)
双倍经验双倍的幸福... 所以另一道是300大洋的世界T_T...虽然题目是一样的,不过2843数据范围小了一点... 都是lct基本操作 #include<cstdio> #includ ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- Centos7之Systemd风格
Unit(单元) /usr/lib/systemd/system 每个服务最主要的启动脚本设置,类似于之前的/etc/init.d目录 关键特性 1.基于socket的激活机制,socke ...
- 002-读书笔记-企业IT架构转型之道-阿里巴巴中台战略思想与架构实战-第二章 构建业务中台的基础-共享服务体系简介
2.1.回归SOA的本质-服务重用 SOA理念的核心价值:松耦合的服务带来业务的复用,通过服务的编排助力业务的快速响应和创新. 现有模式多是烟囱式结合 ESB 企业总线打通不同系统间的交互. 2.2. ...
- React篇-报错信息:warning: Can't call setState (or forceUpdate) on an unmounted component.
报错信息是: Warning: Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but ...
- CGAL4.10 / CGAL4.13编译
环境:Window10,VS2013 ,QT5.6.2 64bit 1.按装boost_1_64_0 boost_1_64_0-msvc-12.0-64.exe 2.zlib-1.2.11.tar.g ...
- [py][mx]django处理登录逻辑
浏览器同源策略(same-origin policy) csrf攻击防御核心点总结 django的cookie和session操作-7天免登录 flask操作cookie&django的see ...
- MySql利用mysqldump导出/导入数据库表数据
备份 在源主机上,先使用mysqldump命令备份,导出sql脚本文件 mysqldump -uroot -p tel_dev > /opt/tel_dev.sql 也可指定编码 mysqldu ...
- 安装OpenSSL中出现的问题及解决
1.报错:install-record.txt --single-version-externally-managed --compile" failed with error code 1 ...
- Element-ui的表单中怎么添加正则校验
1. 以中国大陆手机号验证为例 // 这是组价的代码 <el-form-item prop="mobile"> <el-input type="text ...
- [LeetCode] 1. Two Sum_Easy
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- JAVA比较两个List集合的方法
import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.H ...