bzoj 1180: [CROATIAN2009]OTOCI【LCT】
一道几乎是板子的LCT,但是沉迷数学很久时候突然1A了这道题还是挺开心的
#include<iostream>
#include<cstdio>
using namespace std;
const int N=30005;
int n,q,sum[N],v[N],s[N],top;
char c[20];
struct qwe
{
int c[2],f,sum,lz;
}t[N];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
bool srt(int x)
{
return t[t[x].f].c[0]!=x&&t[t[x].f].c[1]!=x;
}
void pd(int x)
{
if(t[x].lz)
{
t[x].lz=0;
t[t[x].c[0]].lz^=1;
t[t[x].c[1]].lz^=1;
swap(t[x].c[0],t[x].c[1]);
}
}
void ud(int x)
{
t[x].sum=t[t[x].c[0]].sum+t[t[x].c[1]].sum+v[x];
}
void zhuan(int x)
{
int y=t[x].f,z=t[y].f,l=t[y].c[0]!=x,r=l^1;
if(!srt(y))
t[z].c[t[z].c[0]!=y]=x;
t[x].f=z;
t[y].c[l]=t[x].c[r];
t[t[x].c[r]].f=y;
t[x].c[r]=y;
t[y].f=x;
ud(y);
ud(x);
}
void splay(int x)
{
top=0;
s[++top]=x;
for(int i=x;!srt(i);i=t[i].f)
s[++top]=t[i].f;
for(int i=top;i>=1;i--)
pd(s[i]);
while(!srt(x))
{
int y=t[x].f,z=t[y].f;
if(!srt(y))
{
if((t[z].c[0]==y)^(t[y].c[0]==x))
zhuan(x);
else
zhuan(y);
}
zhuan(x);
}
}
void acc(int x)
{
for(int i=0;x;i=x,x=t[x].f)
{
splay(x);
t[x].c[1]=i;
ud(x);
}
}
void mkrt(int x)
{
acc(x);
splay(x);
t[x].lz^=1;
}
void lk(int x,int y)
{
mkrt(x);
t[x].f=y;
}
int getrt(int x)
{
acc(x);
splay(x);
while(t[x].c[0])
x=t[x].c[0];
return x;
}
int main()
{
n=read();
for(int i=1;i<=n;i++)
t[i].sum=v[i]=read();
q=read();
while(q--)
{
scanf("%s",c);
int x=read(),y=read();
//cout<<c<<endl;
if(c[0]=='b')
{
if(getrt(x)==getrt(y))
puts("no");
else
{
puts("yes");
lk(x,y);
}
}
else if(c[0]=='p')
{
mkrt(x);
v[x]=y;
ud(x);
}
else
{
if(getrt(x)!=getrt(y))
puts("impossible");
else
{
mkrt(x);
acc(y);
splay(y);
printf("%d\n",t[y].sum);
}
}
}
return 0;
}
/*
5
4 2 4 5 6
10
excursion 1 1
excursion 1 2
bridge 1 2
excursion 1 2
bridge 3 4
bridge 3 5
excursion 4 5
bridge 1 3
excursion 2 4
excursion 2 5
*/
bzoj 1180: [CROATIAN2009]OTOCI【LCT】的更多相关文章
- BZOJ1180 [CROATIAN2009]OTOCI 【LCT】
题目 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通.如果是则输出"no".否则输出&qu ...
- BZOJ 1180: [CROATIAN2009]OTOCI [LCT]
1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 961 Solved: 594[Submit][S ...
- BZOJ 1180: [CROATIAN2009]OTOCI
1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 989 Solved: 611[Submit][S ...
- 1180: [CROATIAN2009]OTOCI(LCT)
1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 1200 Solved: 747[Submit][ ...
- 【刷题】BZOJ 1180 [CROATIAN2009]OTOCI
Description 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通. 如果是则输出"no&quo ...
- 1180: [CROATIAN2009]OTOCI
1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 1032 Solved: 638[Submit][ ...
- 【BZOJ2049】【LCT】Cave 洞穴勘测
Description 辉 辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通 道组成,并且每条通道连接了 ...
- 【HDU4010】【LCT】Query on The Trees
Problem Description We have met so many problems on the tree, so today we will have a query problem ...
- BZOJ 1016 最小生成树计数 【模板】最小生成树计数
[题解] 对于不同的最小生成树,每种权值的边使用的数量是一定的,每种权值的边的作用是确定的 我们可以先做一遍Kruskal,求出每种权值的边的使用数量num 再对于每种权值的边,2^num搜索出合法使 ...
随机推荐
- bzoj4161 (k^2logn求线性递推式)
分析: 我们可以写把转移矩阵A写出来,然后求一下它的特征多项式,经过手动计算应该是这样的p(x)=$x^k-\sum\limits_{i=1}^ka_i*x^{k-i}$ 根据Cayley-Hamil ...
- 10-JS的函数学习
<html> <head> <title>js的函数学习</title> <meta charset="UTF-8"/> ...
- 用WCF服务来动态的获取本地XML省市区文档
建立一个WCF服务. using ClassLibrary; using System; using System.Collections.Generic; using System.Linq; us ...
- Android点击Button水波纹效果
先上图,看看接下来我要向大家介绍的是个什么东西,例如以下图: 接下来要介绍的就是怎样实现上述图中的波纹效果.这样的效果假设大家没有体验过的话,能够看看百度手机卫士或者360手机卫士,里面的按钮点击效果 ...
- css 滤镜之Gradient
CreateTime--2017年12月26日11:09:14 Author:Marydon ie滤镜特效之Gradient 作用: 用于设置渐变背景色 使用条件: IE9及以下版本不支持属性ba ...
- List<Guid?> a = new List<Guid?>();
正常写法 泛型 类型 为 Guid? List<Guid?> a = new List<Guid?>(); Guid? b = null; Nullable<Guid&g ...
- centos7 64位系统jdbc连接oracle报错问题
这两天发生了一个错误,记录下来. 报错如下: ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could n ...
- <s:property>的用法(jsp获取action中的值或者方法)
1,访问Action值栈中的普通属性: <s:property value="attrName"/> 2,访问Action值栈中的对象属性(要有get set方法) ...
- Java 基本类型和对象类型的区别
Java 基本类型和对象类型的区别 基本类型: int long byte float double char boolean short 对象类型: Integer Long Byte Float ...
- codeforces 686C C. Robbers' watch(dfs)
题目链接: C. Robbers' watch time limit per test 2 seconds memory limit per test 256 megabytes input stan ...