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 ...
随机推荐
- python时间模块详解(time模块)
time 模块 -- 时间获取和转换 time 模块提供各种时间相关的功能 在 Python 中,与时间处理有关的模块包括:time,datetime 以及 calendar 必要说明: 虽然这个模块 ...
- 小冷-wireshark的标志位的值是啥
小冷系列之 wireshark的标志位的值是啥,在用wireshark抓包时,发现Flags = 0x002(SYN),很好奇0x002是什么意思. 好不好先上图: 上图是一个三次握手第一次的标志位, ...
- SQL Server复制
SQL Server复制的阶梯:级别1-SQL Server复制介绍 By Sebastian Meine, 2012/12/26 原文链接:http://www.sqlservercentral.c ...
- 最优方向法(MOD)
算法描述 求解模型: \[\min\sum\limits_i\|x_i\|_0 \quad \mathrm{s.t.} \; \|Y-DX\|^2_F \leq \varepsilon\] 或 \[\ ...
- 从零开始的Python学习Episode 23——进程
---恢复内容开始--- 进程 由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程.Python提供了非常好用 ...
- 基于C#的机器学习--贝叶斯定理-执行数据分析解决肇事逃逸之谜
贝叶斯定理-执行数据分析解决肇事逃逸之谜 在这一章中,我们将: 应用著名的贝叶斯定理来解决计算机科学中的一个非常著名的问题. 向您展示如何使用贝叶斯定理和朴素贝叶斯来绘制数据,从真值表中发现异常值 ...
- 【RL系列】马尔可夫决策过程——状态价值评价与动作价值评价
请先阅读上两篇文章: [RL系列]马尔可夫决策过程中状态价值函数的一般形式 [RL系列]马尔可夫决策过程与动态编程 状态价值函数,顾名思义,就是用于状态价值评价(SVE)的.典型的问题有“格子世界(G ...
- 跨域Ajax -- jsonp和cors
跨域Ajax - jsonp - cors 参考博客: http://www.cnblogs.com/wupeiqi/articles/5703697.html http://www.cnblogs. ...
- linux 常用反弹shell小记
在渗透测试过程中由于防火墙和其它安全防御措施,很多服务器只能单向向外访问,不能被访问,我们常常需要反弹shell. 1.bash反弹shell 本地开启监听 nc -lvvp 受害主机命令 bash ...
- Navicat新建查询,系统找不到指定路径 独家解决办法
Navicat新建查询系统找不到指定路径,很多人用了网上流行的那些解决办法,还是无法解决.比如: https://jingyan.baidu.com/article/86112f1387a713273 ...