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. yum指令常用参数说明

    1.使用YUM查找软件包 命令:yum search 2.列出所有可安装的软件包 命令:yum list 3.列出所有可更新的软件包 命令:yum list updates 4.列出所有已安装的软件包 ...

  2. 解决Unity烘焙阴影锯齿精度不足的问题

    烘焙阴影锯齿问题  烘焙后阴影锯齿明显,如下图: 烘焙的光照贴图质量主要受LightmapParameters 的Blur Radius和抗锯齿级别影响, 默认最高级别如下: 如果最高级别不能达到好的 ...

  3. 将jira添加至开机自启动

    东北证券网金部jira项目管理系统,经常莫名挂掉,于是乎将jira服务加入开机自启动. jira.sh脚本代码如下: #!/bin/sh # chkconfig: # description:jira ...

  4. Netty源码分析第8章(高性能工具类FastThreadLocal和Recycler)---->第1节: FastThreadLocal的使用和创建

    Netty源码分析第八章: 高性能工具类FastThreadLocal和Recycler 概述: FastThreadLocal我们在剖析堆外内存分配的时候简单介绍过, 它类似于JDK的ThreadL ...

  5. 私有云搭建:树莓派+kodexplorer可道云,几步搞定!

    目前蒲公英异地组网则是推出了树莓派1.0软件客户端.无需公网IP!简单60秒设置!轻松远程访问树莓派!实现远程登录.远程配置.远程访问服务.传输数据等等操作.例如:蒲公英树莓派1.0软件客户端+可道云 ...

  6. Mac 终端快捷键

    ctrl+A           跳转到行开头 ctrl+E           跳转到行结尾 ctrl+U           清空当前行 Command+K 清屏 Command+→多终端页面跳转 ...

  7. nginx之location(root/alias)

    location配置 1. 语法规则(按优先级) =        表示精确匹配,优先级最高 ^~      表示uri以某个常规字符串开头,用于匹配url路径(而且不对url做编码处理,例如请求/s ...

  8. linux命令系列 ls

    ls是linux中最常用的命令之一 ls 的功能是list directory contents,其常用的选项如下: (1) -l   use a long listing format(长格式,显示 ...

  9. 一个demo 理解 vuex

    相比接触vue的同学们已经看了官方文档了.这里我用一个简单的demo来阐述下vuex的知识点,虽然简单,但是容易理解.也加深自己的记忆. 用脚手架建立个项目vue init webpakc-simpl ...

  10. BugPhobia开发篇章:绩效管理的层次优化

    0x00 :用0x00去书写一段故事 If you weeped for the missing sunset, you would miss all the shining stars 绩效管理,恐 ...