SPOJ - OTOCI LCT
OTOCI
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18141
Description
Some time ago Mirko founded a new tourist agency named "Dreams of Ice". The agency purchased N icy islands near the South Pole and now offers excursions. Especially popular are the emperor penguins, which can be found in large numbers on the islands.
Mirko's agency has become a huge hit; so big that it is no longer cost-effective to use boats for the excursions. The agency will build bridges between islands and transport tourists by buses. Mirko wants to introduce a computer program to manage the bridge building process so that fewer mistakes are made.
The islands are numbered 1 through N. No two islands are initially connected by bridges. The initial number of penguins on each island is known. That number may change, but will always be between 0 and 1000 (inclusive).
Your program must handle the following three types of commands:
- "bridge A B" – an offer was received to build a bridge between islands A and B (A and B will be different). To limit costs, your program must accept the offer only if there isn't already a way to get from one island to the other using previously built bridges. If the offer is accepted, the program should output "yes", after which the bridge is built. If the offer is rejected, the program should output "no".
- "penguins A X" – the penguins on island A have been recounted and there are now X of them. This is an informative command and your program does not need to respond.
- "excursion A B" – a group of tourists wants an excursion from island A to island B. If the excursion is possible (it is possible to get from island A to B), the program should output the total number of penguins the tourists would see on the excursion (including islands A and B). Otherwise, your program should output "impossible".
Input
The first line contains the integer N (1 ≤ N ≤ 30 000), the number of islands.
The second line contains N integers between 0 and 1000, the initial number of penguins on each of the islands.
The third line contains an integer Q (1 ≤ Q ≤ 300 000), the number of commands.
Q commands follow, each on its own line.
Output
Output the responses to commands "bridge" and "excursion", each on its own line.
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
题意
让你维护一棵树
link操作,update操作,query链上的点权和
题解:
就lct的基本操作啦
这种就主要维护里面的update信息
@)1%KBO0HM418$J94$1R.jpg)
代码:
- //qscqesze
- #pragma comment(linker, "/STACK:1024000000,1024000000")
- #include <cstdio>
- #include <cmath>
- #include <cstring>
- #include <ctime>
- #include <iostream>
- #include <algorithm>
- #include <set>
- #include <bitset>
- #include <vector>
- #include <sstream>
- #include <queue>
- #include <typeinfo>
- #include <fstream>
- #include <map>
- #include <stack>
- typedef long long ll;
- using namespace std;
- //freopen("D.in","r",stdin);
- //freopen("D.out","w",stdout);
- #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
- #define maxn 1205000
- #define mod 1000000007
- #define eps 1e-9
- #define e exp(1.0)
- #define PI acos(-1)
- #define lowbit(x) (x)&(-x)
- const double EP = 1E- ;
- int Num;
- //const int inf=0x7fffffff;
- const ll inf=;
- inline ll read()
- {
- ll x=,f=;char ch=getchar();
- while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
- while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
- return x*f;
- }
- //*************************************************************************************
- const int MAXN = ;
- struct Node {
- Node *ch[], *p; int size, value;
- int w;
- bool rev;
- Node(int t = );
- inline bool dir(void) {return p->ch[] == this;}
- inline void SetC(Node *x, bool d) {
- ch[d] = x; x->p = this;
- }
- inline void Rev(void) {
- swap(ch[], ch[]); rev ^= ;
- }
- inline void Push(void) {
- if (rev) {
- ch[]->Rev();
- ch[]->Rev();
- rev = ;
- }
- }
- inline void Update(void) {
- value = w+ch[]->value + ch[]->value;
- size = ch[]->size + ch[]->size + ;
- }
- }Tnull, *null = &Tnull, *fim[MAXN];
- // 要记得额外更新null的信息
- Node::Node(int _value){ch[] = ch[] = p = null; rev = ;w = value = _value;}
- inline bool isRoot(Node *x) {return x->p == null || (x != x->p->ch[] && x != x->p->ch[]);}
- inline void rotate(Node *x) {
- Node *p = x->p; bool d = x->dir();
- p->Push(); x->Push();
- if (!isRoot(p)) p->p->SetC(x, p->dir()); else x->p = p->p;
- p->SetC(x->ch[!d], d);
- x->SetC(p, !d);
- p->Update();
- }
- inline void splay(Node *x) {
- x->Push();
- while (!isRoot(x)) {
- if (isRoot(x->p)) rotate(x);
- else {
- if (x->dir() == x->p->dir()) {rotate(x->p); rotate(x);}
- else {rotate(x); rotate(x);}
- }
- }
- x->Update();
- }
- inline Node* Access(Node *x) {
- Node *t = x, *q = null;
- for (; x != null; x = x->p) {
- splay(x); x->ch[] = q; q = x;
- }
- splay(t); //info will be updated in the splay;
- return q;
- }
- inline void Evert(Node *x) {
- Access(x); x->Rev();
- }
- inline void link(Node *x, Node *y) {
- Evert(x); x->p = y;
- }
- inline Node* getRoot(Node *x) {
- Node *tmp = x;
- Access(x);
- while (tmp->Push(), tmp->ch[] != null) tmp = tmp->ch[];
- splay(tmp);
- return tmp;
- }
- // 一定要确定x和y之间有边
- inline void cut(Node *x, Node *y) {
- Access(x); splay(y);
- if (y->p != x) swap(x, y);
- Access(x); splay(y);
- y->p = null;
- }
- inline Node* getPath(Node *x, Node *y) {
- Evert(x); Access(y);
- return y;
- }
- inline void clear(void) {
- null->rev = ; null->size = ; null->value = ;
- }
- int main()
- {
- int n=read();
- for(int i=;i<=n;i++)
- {
- int x = read();
- fim[i] = new Node(x);
- }
- int q = read();
- char s[];
- while(q--)
- {
- scanf("%s",s);
- if(s[]=='e')
- {
- int x=read(),y=read();
- if(getRoot(fim[x])!=getRoot(fim[y]))
- {
- printf("impossible\n");continue;
- }
- Evert(fim[x]);
- Access(fim[y]);
- splay(fim[y]);
- printf("%d\n",fim[y]->value);
- }
- if(s[]=='b')
- {
- int x=read();
- int y=read();
- if(getRoot(fim[x])==getRoot(fim[y]))
- puts("no");
- else
- {
- puts("yes");
- link(fim[x],fim[y]);
- }
- }
- if(s[]=='p')
- {
- int x=read(),y=read();
- Evert(fim[x]);fim[x]->w = y;
- fim[x]->Update();
- }
- }
- }
SPOJ - OTOCI LCT的更多相关文章
- SPOJ OTOCI 动态树 LCT
SPOJ OTOCI 裸的动态树问题. 回顾一下我们对树的认识. 最初,它是一个连通的无向的无环的图,然后我们发现由一个根出发进行BFS 会出现层次分明的树状图形. 然后根据树的递归和层次性质,我们得 ...
- SPOJ QTREE4 lct
题目链接 这个题已经处于花式tle了,改版后的spoj更慢了.. tle的话就多交几把... #include <iostream> #include <fstream> #i ...
- BZOJ 1180: [CROATIAN2009]OTOCI [LCT]
1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 961 Solved: 594[Submit][S ...
- BZOJ2843极地旅行社&BZOJ1180[CROATIAN2009]OTOCI——LCT
题目描述 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通. 如果是则输出“no”.否则输出“yes”,并且在 ...
- BZOJ1180 [CROATIAN2009]OTOCI LCT
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1180 本题和BZOJ2843一样. BZOJ2843 极地旅行社 LCT 题意概括 有n座岛 每座 ...
- 【bzoj1180】[CROATIAN2009]OTOCI LCT
题目描述 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通.如果是则输出“no”.否则输出“yes”,并且在结点 ...
- SPOJ QTREE3 lct
题目链接 题意: 给定n个点 q个询问 以下n-1行给出树边,点有黑或白色.初始化为白色 以下q行: 询问有2种: 1. 0 x 把x点黑变白,白变黑 2.1 x 询问Path(1,x)路径上第一个黑 ...
- SPOJ QTREE2 lct
题目链接 题意: 给一棵树.有边权 1.询问路径的边权和 2.询问沿着路径的第k个点标. 思路:lct裸题. #include <iostream> #include <fstrea ...
- SPOJ QTREE5 lct
题目链接 对于每一个节点,记录这个节点所在链的信息: ls:(链的上端点)距离链内部近期的白点距离 rs:(链的下端点)距离链内部近期的白点距离 注意以上都是实边 虚边的信息用一个set维护. set ...
随机推荐
- 大四实习准备5_android广播机制
2015-5-1 android 广播机制 5.1简介 分为标准广播(Normal broadcasts)(无先后顺序,几乎同时接收,不可截断)和有序广播(Ordered broadcasts)(有先 ...
- 浏览器兼容问题系列---使IE支持CSS3 Media Quary
兼容是一件很让前端攻城师头疼的事情,笔者今天在做一个Demo的时候就碰到了一个问题(大牛就不要拍砖了,谢谢!) 经常做移动互联网前端的攻城师想必对于css3 media quary已经很熟悉了,但是碰 ...
- Erlang入门(二)—并发编程
Erlang中的process——进程是轻量级的,并且进程间无共享.查了很多资料,似乎没人说清楚轻量级进程算是什么概念,继续查找中...闲话不提,进入并发编程的世界.本文算是学习笔记,也可以说是< ...
- (一)学习MVC之制作验证码
制作验证码的方法在@洞庭夕照 看到的,原文链接:http://www.cnblogs.com/mzwhj/archive/2012/10/22/2720089.html 现自己利用该方法制作一个简单的 ...
- android利用WebView实现浏览器的封装
android提供了封装浏览器的接口,可以让开发者利用自己的view显示网页内容.今天又实现研究了一下,利用WebView显示浏览器内容,还可以利用 WebViewClient显示自己需要的内容. 参 ...
- (Android Studio)添加文本框
此文大部分摘自http://hukai.me/android-training-course-in-chinese/basics/firstapp/building-ui.html android : ...
- 如何添加网站for Linux(绑定域名)
[以下配置的路径以阿里云提供的标准环境路径为准,如果您另行安装,请根据实际安装路径配置]. 1.cd /alidata/server/httpd/conf/vhosts/ 进入绑定域名所在目录, ...
- python PIL下的各种问题
为了实现验证码的功能,使用了PIL.结果出现各种问题: 先是"ImportError: The _imagingft C module is not installed",goog ...
- php 实现文件下载,兼容IE、Firefox、Chrome等浏览器
一.下载任意文件: Header ( "Content-type: application/octet-stream" ); $ua = $_SERVER ["HTTP_ ...
- 云计算分布式大数据Hadoop实战高手之路第八讲Hadoop图文训练课程:Hadoop文件系统的操作实战
本讲通过实验的方式讲解Hadoop文件系统的操作. “云计算分布式大数据Hadoop实战高手之路”之完整发布目录 云计算分布式大数据实战技术Hadoop交流群:312494188,每天都会在群中发布云 ...