题目链接

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. 腾讯游戏DBA团队的发展自白

    BA这个岗位跟仓管员很像,就是每天给别人发点货,别人在你这儿放点货,DBA工作就是把货尽快给送出去或者让人家尽快放进来.当然,还有一份重要的工作,就是让仓库里摆放的货物尽可能整齐,这也是仓管员的本职工 ...

  2. kafka4 副本机制

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

  3. 帝国cms建站-动态获取栏目id

    <?php $fcr=explode('|',$class_r[$GLOBALS[navclassid]][featherclass]); $topbclassid=$fcr[1]?$fcr[1 ...

  4. 导出mysql数据库数据

    1.phpmyadmin导出 ) AS `a` LEFT JOIN ( SELECT * FROM `newerp_jifen_order_log` WHERE `content` = '客户确认收货 ...

  5. linux配置基本业务

    1.安装一些必备软件 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel ...

  6. spark.yarn.jar和spark.yarn.archive的使用

    启动Spark任务时,在没有配置spark.yarn.archive或者spark.yarn.jars时, 会看到不停地上传jar非常耗时:使用spark.yarn.archive可以大大地减少任务的 ...

  7. [LeetCode] 584. Find Customer Referee_Easy tag: SQL

    Given a table customer holding customers information and the referee. +------+------+-----------+ | ...

  8. leetcode 343 整数拆分

    1.这个题拿到之后没有什么思路,此时就应该考虑暴力法.然而每次不知道要拆成几份,没办法用循环,所以想到用递归. 如图所示进行递归,显然有很多重复的计算,所以用自底向上的动态规划. 2.还有一个问题就是 ...

  9. 3.C#的访问权限修饰符

    C#里类及类成员的修饰符有以下五个如下:public 公开 类及类成员的修饰符 对访问成员没有级别限制private 私有 类成员的修饰符 只能在类的内部访问protected 受保护的 类成员的修饰 ...

  10. MFC AfxMessageBox MessageBox MessageBoxA 默认标题修改

    在工程的资源里添加String Table资源,AFX_IDS_APP_TITLE,然后设置其值即可,AFX_IDS_APP_TITLE的值就是AfxMessageBox.MessageBox.Mes ...