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 ...
随机推荐
- kafka6 编写使用自定义分区的生产者
一 客户端 在上一篇博客创建的简单生产者的基础上,进行两个修改操作: 1.新建SimplePartitioner.java,修改返回分区为1. SimplePartitioner.java代码如下 p ...
- Font Awesome 最简单应用例子
简介: Font Awesome为您提供可缩放的矢量图标,您可以使用CSS所提供的所有特性对它们进行更改,包括:大小.颜色.阴影或者其它任何支持的效果. 使用方法: 引入<link rel=&q ...
- Build a Basic CRUD App with Vue.js and nodejs
https://developer.okta.com/blog/2018/02/15/build-crud-app-vuejs-node#add-authentication-with-okta I’ ...
- [django]django model的查询和更新
再分享Django系列的另外几篇文章: Django model select的各种用法详解:https://mp.weixin.qq.com/s/JVh4UnS2Tql9gUVaBSoGuA Dja ...
- [vue]组件篇
slot&子组件通过computed修改父组件数据 <div id="app"> <modal type="primary"> ...
- Day7 初识面向对象,面向对象之继承、多态和封装
一.面向对象引言 一.面向对象的程序设计的由来 详述见:http://www.cnblogs.com/linhaifeng/articles/6428835.html 二.面向对象引子 写一个简单程序 ...
- gerrit设置默认rebase以及禁用分支
1.设置默认rebase 在工程配置中设置: 2.禁用分支 Refrance:refs/for/refs/heads/分支名 禁用权限:push “Block” (这样就禁止了开发者往这个分支上 ...
- mac xcode 常见配置
1.报错:There are no schemes in workspace "..." 设置scheme共享,方法: 2.Build 文件夹是中间文件的保存地方,如何设置在工程目 ...
- svn回复历史版本的操作方法
svn恢复历史版本的操作方法svn update 保证最新svn log login.php 获得版本号svn diff -r 11026:11027 login.php 发现11027出问题svn ...
- glibc源码下载
https://www.gnu.org/software/libc/ Download sources Releases are available by source branch checkout ...