题目链接

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的更多相关文章

  1. [bzoj2843&&bzoj1180]极地旅行社 (lct)

    双倍经验双倍的幸福... 所以另一道是300大洋的世界T_T...虽然题目是一样的,不过2843数据范围小了一点... 都是lct基本操作 #include<cstdio> #includ ...

  2. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  3. SAP CRM 树视图(TREE VIEW)

    树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...

  4. 无限分级和tree结构数据增删改【提供Demo下载】

    无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...

  5. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. kafka4 副本机制

    概述 每个分区有n个副本,可以承受n-1个节点故障. 每个副本都有自己的leader,其余都是follower. zk中存放分区的leader和 follower replica的信息.(get /b ...

  2. 堆(heap)、栈(stack)、方法区(method)

    JVM内存分为3个区:堆(heap).栈(stack).方法区(method) 1.堆(heap):存储的全部对象,每个对象有个与之对应的class信息.即通过new关键字和构造器创建的对象.JVM只 ...

  3. Linux修改IP网络配置

    文件路径:/etc/sysconfig/networe-scripts/ifcfg-eth0 BOOTPROTO="static" IPADDR=IP地址 NETMASK=掩码 G ...

  4. Linux文本编辑器之vim

    VIM 文本编辑器 vim/vi是Unix/Linux上最常用的文本编辑器而且功能强大.注意:只有命令,没有菜单 VIM工作模式 命令模式:又称为一般模式 编辑模式:又称为低行模式,命令行模式 插入命 ...

  5. git push 报错:you are not allowed to upload merges

    git rebase Cannot rebase: You have unstaged changes. git stash # 每次 push 前 git pull --rebase git pus ...

  6. js贪心算法---钱币找零问题

    function MinCoinChange(coins){ var coins = coins.sort(function(a,b){ return b - a; }); this.makeChan ...

  7. MySQL数据类型--与MySQL零距离接触2-8查看数据表

    SHOW COLUMNS FROM tb_name 写入列之后,需要写入行,也就是记录:INSERT 插入记录:INSERT [INTO]  tbl_name  [(col_name,...)]  V ...

  8. 阿里云服务器用smtp发送邮件返失败

    阿里云使用SMTP发送邮件失败,因为阿里云服务器屏蔽了25端口,所以发送不成功,解决办法改用587发送QQ邮件,且必须使用SSL,否则不成功. 经测试QQ的465,995不能使用. https://b ...

  9. TM-align TM-score安装

    TM-align是由zhang yang LAB开发的一款做蛋白结构比对的软件. 下载链接:http://zhanglab.ccmb.med.umich.edu/TM-align/TMtools201 ...

  10. 关于如何利用计算属性进行button的控制

    element分页没用它的 (这个只要上一页下一页),比如共2页的时候,你在第一页,你肯定可以点击下一页,当你进入到第二页的时候这个button肯定就不能点击了啊,它的属性diaabled=true让 ...