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

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

Sample Output

4
impossible
yes
6
yes
yes
15
yes
15
16

HINT

任意时刻每个节点对应的权值都是1到1000的整数。

Source

 

[Submit][Status][Discuss]

又是一道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的更多相关文章

  1. BZOJ 1180: [CROATIAN2009]OTOCI [LCT]

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 961  Solved: 594[Submit][S ...

  2. 【刷题】BZOJ 1180 [CROATIAN2009]OTOCI

    Description 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通. 如果是则输出"no&quo ...

  3. bzoj 1180: [CROATIAN2009]OTOCI【LCT】

    一道几乎是板子的LCT,但是沉迷数学很久时候突然1A了这道题还是挺开心的 #include<iostream> #include<cstdio> using namespace ...

  4. 1180: [CROATIAN2009]OTOCI(LCT)

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 1200  Solved: 747[Submit][ ...

  5. 1180: [CROATIAN2009]OTOCI

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 1032  Solved: 638[Submit][ ...

  6. 【BZOJ】1180: [CROATIAN2009]OTOCI & 2843: 极地旅行社(lct)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1180 今天状态怎么这么不好..................................... ...

  7. 【BZOJ 1180】 (LCT)

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 1078  Solved: 662 Descript ...

  8. BZOJ1180: [CROATIAN2009]OTOCI

    传送门 一遍AC,开心! $Link-Cut-Tree$最后一题 //BZOJ 1180 //by Cydiater //2016.9.18 #include <iostream> #in ...

  9. OTOCI(bzoj 1180)

    Description 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通.如果是则输出“no”.否则输出“yes ...

随机推荐

  1. Python3列表中获取相同元素出现位置的下标

    前言 list: Python3的列表类型, 和其他语言中的数组类似 定义格式: l = ["a", "b", "c", "a&q ...

  2. Docker配置

    Docker基本配置 1.安装 在ubuntu下面执行 wget -qO- https://get.docker.com/ | sh 命令安装Docker. 如果命令的方式无法安装,也可以使用apt- ...

  3. yocto-sumo源码解析(九): ProcessServer.main

    前面讲到BitbakeServer实际上是一个ProcessServer,因此对ProcessServer进行了一个大略的分析集,这里着重再介绍一下ProcessServer.main. 1. 初始化 ...

  4. docker入门使用教程

    Docker概念 Docker是开发人员和系统管理员 使用容器开发,部署和运行应用程序的平台.使用Linux容器部署应用程序称为容器化.容器不是新的,但它们用于轻松部署应用程序. 容器化越来越受欢迎, ...

  5. Streamr助你掌控自己的数据(2)——三种整合数据至Streamr的典型场景

    博客说明 所有刊发内容均可转载但是需要注明出处. 三种整合数据至Streamr的典型场景 本系列文档主要介绍怎么通过Streamr管理自己的DATA,整个系列包括三篇教程文档,分别是:教你5分钟上传数 ...

  6. 前端_JQuery

    使用参考:http://jquery.cuishifeng.cn/ 目录 jQuery是什么 jQuery对象 寻找元素(选择器和筛选器) 选择器 表单属性选择器 筛选器 操作元素(属性.css.文档 ...

  7. oracle varchar2改成大字段类型clob,读取大字段内容

    http://blog.csdn.net/cai7095576/article/details/23999549

  8. 2017秋软工 - 本周PSP

    1. PSP 2. PSP饼状图 3. 进度条 4. 累计进度图

  9. 2018-2019-20172321 《Java软件结构与数据结构》第九周学习总结

    2018-2019-20172321 <Java软件结构与数据结构>第九周学习总结 教材学习内容总结 第15章 图 无向图 图由顶点和边组成. 顶点由名字或标号来表示,如:A.B.C.D: ...

  10. stateful openflow------整理openstate原理以及具体应用

    openstate基本思想就是控制器下放一部分功能,交换机不再是简单的dumb,而是保留一些简单的wise. 论文中以端口锁定为例,提出了米粒型状态机在交换机内部的应用从而可以大大减少交换机和控制器之 ...