BZOJ 1180: [CROATIAN2009]OTOCI
1180: [CROATIAN2009]OTOCI
Time Limit: 50 Sec Memory Limit: 162 MB
Submit: 989 Solved: 611
[Submit][Status][Discuss]
Description
给出n个结点以及每个点初始时对应的权值wi。起始时点与点之间没有连边。有3类操作:
1、bridge A B:询问结点A与结点B是否连通。如果是则输出“no”。否则输出“yes”,并且在结点A和结点B之间连一条无向边。
2、penguins A X:将结点A对应的权值wA修改为X。
3、excursion A B:如果结点A和结点B不连通,则输出“impossible”。否则输出结点A到结点B的路径上的点对应的权值的和。
给出q个操作,要求在线处理所有操作。数据范围:1<=n<=30000, 1<=q<=300000, 0<=wi<=1000。
Input
第一行包含一个整数n(1<=n<=30000),表示节点的数目。
第二行包含n个整数,第i个整数表示第i个节点初始时对应的权值。
第三行包含一个整数q(1<=n<=300000),表示操作的数目。
以下q行,每行包含一个操作,操作的类别见题目描述。
Output
输出所有bridge操作和excursion操作对应的输出,每个一行。
Sample Input
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
Sample Output
impossible
yes
6
yes
yes
15
yes
15
16
HINT
Source
又是一道LCT模板题,Splay上维护子树权值和,就相当于维护了路径权值和。
#include <cstdio> template <class T>
inline void swap(T &a, T &b)
{
T c; c = a;
a = b;
b = c;
} const int mxn = ; int n, m; struct node
{
int val;
int sum;
node *son[];
node *father;
bool reverse; inline node(int v = )
{
val = v;
sum = v;
son[] = NULL;
son[] = NULL;
father = NULL;
reverse = false;
} inline void update(void)
{
sum = val; if (son[])sum += son[]->sum;
if (son[])sum += son[]->sum;
} inline bool isRoot(void)
{
if (father == NULL)
return true; if (father->son[] == this)
return false;
if (father->son[] == this)
return false; return true;
} inline void pushDown(void)
{
if (reverse)
{
reverse = false; swap(son[], son[]); if (son[])son[]->reverse ^= true;
if (son[])son[]->reverse ^= true;
}
}
}tree[mxn]; inline void connect(node *f, node *t, bool k)
{
if (t != NULL)t->father = f;
if (f != NULL)f->son[k] = t;
} inline void rotate(node *t)
{
node *f = t->father;
node *g = f->father; bool s = f->son[] == t; connect(f, t->son[!s], s);
connect(t, f, !s); t->father = g;
if (g && g->son[] == f)g->son[] = t;
if (g && g->son[] == f)g->son[] = t; f->update();
t->update();
} inline void push(node *t)
{
static node *stk[mxn]; int top = ; stk[top++] = t; while (!t->isRoot())
stk[top++] = t = t->father; while (top)stk[--top]->pushDown();
} inline void splay(node *t)
{
push(t); while (!t->isRoot())
{
node *f = t->father;
node *g = f->father; if (f->isRoot())
rotate(t);
else
{
bool a = f && f->son[] == t;
bool b = g && g->son[] == f; if (a == b)
rotate(f), rotate(t);
else
rotate(t), rotate(t);
}
}
} inline void access(node *t)
{
node *p = NULL; while (t != NULL)
{
splay(t);
t->son[] = p, t->update();
p = t, t = t->father;
}
} inline void makeRoot(node *t)
{
access(t), splay(t), t->reverse ^= true;
} inline void link(node *t, node *f)
{
makeRoot(t), t->father = f;
} inline void cut(node *t)
{
splay(t); if (t->son[])t->son[]->father = NULL;
if (t->son[])t->son[]->father = NULL; t->son[] = t->son[] = NULL, t->update();
} inline node *find(node *t)
{
access(t), splay(t); node *r = t; while (r->son[])
r = r->son[]; return r;
} signed main(void)
{
scanf("%d", &n); for (int i = , v; i <= n; ++i)
scanf("%d", &v), tree[i] = node(v); scanf("%d", &m); while (m--)
{
static int x, y;
static char s[]; scanf("%s%d%d", s, &x, &y); if (s[] == 'b')
{
if (find(tree + x) == find(tree + y))
puts("no");
else
puts("yes"), link(tree + x, tree + y);
}
else if (s[] == 'p')
{
access(tree + x), splay(tree + x);
tree[x].val = y, tree[x].update();
}
else
{
if (find(tree + x) != find(tree + y))
puts("impossible");
else
{
makeRoot(tree + x), access(tree + y), splay(tree + y);
printf("%d\n", tree[y].sum);
}
}
}
}
@Author: YouSiki
BZOJ 1180: [CROATIAN2009]OTOCI的更多相关文章
- 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
Description 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通. 如果是则输出"no&quo ...
- bzoj 1180: [CROATIAN2009]OTOCI【LCT】
一道几乎是板子的LCT,但是沉迷数学很久时候突然1A了这道题还是挺开心的 #include<iostream> #include<cstdio> using namespace ...
- 1180: [CROATIAN2009]OTOCI(LCT)
1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 1200 Solved: 747[Submit][ ...
- 1180: [CROATIAN2009]OTOCI
1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 1032 Solved: 638[Submit][ ...
- 【BZOJ】1180: [CROATIAN2009]OTOCI & 2843: 极地旅行社(lct)
http://www.lydsy.com/JudgeOnline/problem.php?id=1180 今天状态怎么这么不好..................................... ...
- 【BZOJ 1180】 (LCT)
1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 1078 Solved: 662 Descript ...
- BZOJ1180: [CROATIAN2009]OTOCI
传送门 一遍AC,开心! $Link-Cut-Tree$最后一题 //BZOJ 1180 //by Cydiater //2016.9.18 #include <iostream> #in ...
- OTOCI(bzoj 1180)
Description 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通.如果是则输出“no”.否则输出“yes ...
随机推荐
- 《Postgre SQL 即学即用 (第三版)》 分享 pdf下载
链接:https://pan.baidu.com/s/1akR33VqEkt99UqJUfiy2OA提取码:3p1k
- 【SIKIA计划】_07_Unity3D游戏开发-坦克大战笔记
[新增分类][AudioClips]音频剪辑[AudioMixers]音频混合器[Editor][Fonts]字体[Materials]材质[Models]模型[Standard Assets] [渲 ...
- Influxdb配置文件详解---influxdb.conf
官方介绍:https://docs.influxdata.com/influxdb/v1.2/administration/config/ 全局配置 1 2 reporting-disabled = ...
- 从零开始的Python学习Episode 20——面向对象(3)
面向对象之封装 封装,即隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别:将抽象得到的数据和行为(或功能)相结合,形成一个有机的整体. 隐藏 在python中用双下划线开 ...
- js多条件if语句简写发生Uncaught SyntaxError: Unexpected token }
改写原生js 多条件if判断语句时,采用三元方法,发生Uncaught SyntaxError: Unexpected token } function compareImgSize() { var ...
- Python基础系列讲解——继承派生和组合的概念剖析
Python作为一门面向对象的语言,它的面向对象体系中主要存在这么两种关系,一个是“类”和“实例”的关系,另一个是“父类”和“子类”的关系. 所谓“类”是从一堆对象中以抽象的方式把相同的特征归类得到的 ...
- 随手记录-linux-添加epel源
下载各种yum源 https://opsx.alibaba.com/mirror https://blog.csdn.net/harbor1981/article/details/51135623
- jenkins部署时遇到“似乎无法联网”,导致无法安装默认插件的解决方案
jenkins安装更新时,默认会检查网络连接,而默认的checkulr 是http://www.google.com/ ,国内是无法访问的,所以修改成任意可以访问的地址即可,比如http://www. ...
- 利用cocoapods创建基于git的私有库Spec Repo
上一篇文章记录了我利用cocoapods创建基于SVN的私有库的全部过程,今天我再记录一下基于git创建的过程. 整体先说明一下创建一个私有的podspec包括如下那么几个步骤: 创建并设置一个私有的 ...
- 2017-2018-20172311 暑期编程作业:APP
2017-2018-20172311 暑期编程作业:实现一个简单倒计时APP 写在前面:暑假的时候就单纯的想要设计一个倒计时软件,然后就通过查阅资料等学了一些,包括实现倒计时功能及显示:背景音乐的添加 ...