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 ...
随机推荐
- RN导航栏使用
import PropTypes from 'prop-types'; import React, { Component } from 'react'; import { NavigatorIOS, ...
- Linux命令实例功能笔记
ls命令 ls对文件mtime时间进行排序 降序: ls -lt | grep '^-' 升序: ls -ltr | grep '^-' seq命令 求1000以内所有偶数的和 ech ...
- ORACLE环境变量定义.md
export在linux的bash中可以理解为设置环境变量.设置后能够被当前的shell及子shell使用.这些变量的含义有一些有意义,可以查看相应的文档,我给你解释一些我知道的:ORACLE_HOM ...
- sap gui 配置
1: sap gui 安全配置 ,在左下角搜索sap gui configuration.
- centos7部署fabric-ca错误解决
1.fabric-ca 编译错误:ltdl.h: no such file 在fabric-ca目录中使用make编译时,会出现如下错误: 解决方案: 如果在ubunt操作系统中,只需安装:apt i ...
- centos 打包报错License for package Android SDK Build-Tools 25.0.3 not accepted
报错如下: 提示没有25.0.3的安卓环境,那么,接下来就需要安装这个环境 1.android list sdk -a 会显示需要更新 类似如下(截图只是一部分,前后还有一部分): 2.android ...
- iOS 聊天表情键盘
具体思路 通过UIKeyboardWillChangeFrameNotification通知,监听键盘的改变,同时可以得到键盘的Frame和动画的持续时间, 新建键盘顶部工具条YSComposeToo ...
- iOS UI基础-13.0 数据存储
应用沙盒 每个iOS应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其他文件系统隔离.应用必须待在自己的沙盒里,其他应用不能访问该沙盒 应用沙盒的文件系统目录,如下图所示(假设应用的名称叫Lay ...
- Django Form(表单)
前台用 get 或 post 方法向后台提交一些数据. GET方法: html示例(保存在templates文件夹中): <!DOCTYPE html> <html> < ...
- node.js初识09
1.node_module文件夹 如果你的require中没有写./,那么Node.js将该文件视为node_modules目录下的一个文件. 2.package.json文件 如果使用文件夹来统筹管 ...