LCT..

------------------------------------------------------------------------

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
 
#define rep( i , n ) for( int i = 0 ; i < n ; ++i )
#define clr( x , c ) memset( x , c , sizeof( x ) )
 
using namespace std;
 
const int maxn = 30000 + 5;
const int maxnode = maxn + 100;
 
int seq[ maxn ];
int n;
 
struct Node *pt , *null;
 
struct Node {
Node *ch[ 2 ] , *p , *fa;
int sum , v;
bool rev , isRoot;
Node( int _v = 0 ) : v( _v ) {
ch[ 0 ] = ch[ 1 ] = p = fa = null;
sum = v;
isRoot = true;
rev = false;
}
inline void setc( Node* c , int d ) {
ch[ d ] = c;
c -> p = this;
}
inline void Rev() {
rev ^= 1;
swap( ch[ 0 ] , ch[ 1 ] );
}
inline void relax() {
if( rev ) {
rev = false;
rep( i , 2 ) if( ch[ i ] != null )
   ch[ i ] -> Rev();
}
}
inline bool d() {
return this == p -> ch[ 1 ];
}
inline void upd() {
sum = ch[ 0 ] -> sum + ch[ 1 ] -> sum + v;
}
inline void setRoot() {
isRoot = true;
fa = p;
p = null;
}
void* operator new( size_t ) {
return pt++;
}
};
 
Node NODE[ maxnode ];
 
void rot( Node* t ) {
Node* p = t -> p;
p -> relax();
t -> relax();
int d = t -> d();
p -> p -> setc( t , p -> d() );
p -> setc( t -> ch[ d ^ 1 ] , d );
t -> setc( p , d ^ 1 );
p -> upd();
if( p -> isRoot ) {
p -> isRoot = false;
t -> isRoot = true;
t -> fa = p -> fa;
}
}
 
void splay( Node* t , Node* f = null ) {
static Node* S[ maxn ];
int top = 0;
for( Node* o = t ; o != null ; o = o -> p )
S[ ++top ] = o;
while( top ) S[ top-- ] -> relax();
for( Node* p = t -> p ; p != f ; p = t -> p ) {
if( p -> p != f )
   p -> d() != t -> d() ? rot( t ) : rot( p );
rot( t );
}
t -> upd();
}
 
void access( Node* t ) {
for( Node* o = null ; t != null ; o = t , t = t -> fa ) {
splay( t );
t -> ch[ 1 ] -> setRoot();
t -> setc( o , 1 );
}
}
 
void makeRoot( Node* t ) {
access( t );
splay( t );
t -> Rev();
}
 
Node* findRoot( Node* t ) {
access( t );
splay( t );
for( ; t -> ch[ 0 ] != null ; t = t -> ch[ 0 ] )
   t -> relax();
splay( t );
return t;
}
 
void cut( Node* x , Node* y ) {
makeRoot( x );
access( y );
splay( x );
x -> setc( null , 1 );
y -> setRoot();
}
 
void join( Node* x , Node* y ) {
makeRoot( x );
x -> fa = y;
}
 
void init() {
pt = NODE;
null = new Node( 0 );
}
 
Node* V[ maxn ];
 
int main() {
freopen( "test.in" , "r" , stdin );
init();
cin >> n;
rep( i , n ) scanf( "%d" , seq + i );
rep( i , n ) V[ i ] = new Node( *( seq + i ) );
int m , a , b;
char s[ 20 ];
cin >> m;
while( m-- ) {
scanf( " %s%d%d" , s , &a , &b );
a-- , b--;
if( s[ 0 ] == 'b' ) {
if( findRoot( V[ a ] ) != findRoot( V[ b ] ) ) {
printf( "yes\n" );
join( V[ a ] , V[ b ] );
} else
printf( "no\n" );
} else if( s[ 0 ] == 'e' ) {
if( findRoot( V[ a ] ) != findRoot( V[ b ] ) )
printf( "impossible\n" );
else {
   makeRoot( V[ a ] );
   access( V[ b ] );
   splay( V[ b ] );
   printf( "%d\n" , V[ b ] -> sum );
}
}
else {
b++;
makeRoot( V[ a ] );
V[ a ] -> v = b;
V[ a ] -> upd();
}
}
return 0;
}

------------------------------------------------------------------------

2843: 极地旅行社

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 152  Solved: 93
[Submit][Status][Discuss]

Description

不久之前,Mirko建立了一个旅行社,名叫“极地之梦”。这家旅行社在北极附近购买了N座冰岛,并且提供观光服务。当地最受欢迎的当然是帝企鹅了,这些小家伙经常成群结队的游走在各个冰岛之间。
Mirko的旅行社遭受一次重大打击,以至于观光游轮已经不划算了。旅行社将在冰岛之间建造大桥,并用观光巴士来运载游客。Mirko希望开发一个电脑程序来管理这些大桥的建造过程,以免有不可预料的错误发生。
这些冰岛从1到N标号。一开始时这些岛屿没有大桥连接,并且所有岛上的帝企鹅数量都是知道的。每座岛上的企鹅数量虽然会有所改变,但是始终在[0, 1000]之间。
你的程序需要处理以下三种命令:
1."bridge A B"——在A与B之间建立一座大桥(A与B是不同的岛屿)。由于经费限制,这项命令被接受,当且仅当A与B不联通。若这项命令被接受,你的程序需要输出"yes",之后会建造这座大桥。否则,你的程序需要输出"no"。
2."penguins A X"——根据可靠消息,岛屿A此时的帝企鹅数量变为X。这项命令只是用来提供信息的,你的程序不需要回应。
3."excursion A B"——一个旅行团希望从A出发到B。若A与B连通,你的程序需要输出这个旅行团一路上所能看到的帝企鹅数量(包括起点A与终点B),若不联通,你的程序需要输出"impossible"。

Input

第一行一个正整数N,表示冰岛的数量。

第二行N个范围[0, 1000]的整数,为每座岛屿初始的帝企鹅数量。

第三行一个正整数M,表示命令的数量。

接下来M行即命令,为题目描述所示。

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<=N<=30000

1<=M<=100000

Source

BZOJ 2843: 极地旅行社( LCT )的更多相关文章

  1. BZOJ 2843: 极地旅行社 lct splay

    http://www.lydsy.com/JudgeOnline/problem.php?id=2843 https://blog.csdn.net/clove_unique/article/deta ...

  2. bzoj 2843 极地旅行社(LCT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2843 [题意] 给定一个森林,要求提供连边,修改点值,查询路径和的操作. [思路] L ...

  3. bzoj 2843: 极地旅行社

    Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1077  Solved: 645[Submit][Status][Discuss] Descripti ...

  4. 【BZOJ1180】: [CROATIAN2009]OTOCI & 2843: 极地旅行社 LCT

    竟然卡了我....忘记在push_down先下传父亲的信息了....还有splay里for():卡了我10min,但是双倍经验还是挺爽的,什么都不用改. 感觉做的全是模板题,太水啦,不能这么水了... ...

  5. BZOJ 1180 [CROATIAN 2009]OTOCI // BZOJ 2843 极地旅行社 // Luogu P4321 [COCI 2009] OTOCI / 极地旅行社 (LCA板题)

    emmm-标题卡着长度上限- LCT板题-(ε=ε=ε=┏(゜ロ゜;)┛) CODE #include <cctype> #include <cmath> #include & ...

  6. [bzoj2843&&bzoj1180]极地旅行社 (lct)

    双倍经验双倍的幸福... 所以另一道是300大洋的世界T_T...虽然题目是一样的,不过2843数据范围小了一点... 都是lct基本操作 #include<cstdio> #includ ...

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

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

  8. 【bzoj2843】极地旅行社 LCT

    题目描述 不久之前,Mirko建立了一个旅行社,名叫“极地之梦”.这家旅行社在北极附近购买了N座冰岛,并且提供观光服务.当地最受欢迎的当然是帝企鹅了,这些小家伙经常成群结队的游走在各个冰岛之间.Mir ...

  9. BZOJ2843 极地旅行社 LCT

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2843 题意概括 有n座岛 每座岛上的企鹅数量虽然会有所改变,但是始终在[0, 1000]之间.你的 ...

随机推荐

  1. MSSQL 修改数据库的排序规则

    1.修改数据库排序规则 ALTER DATABASE [CHARACTER] COLLATE Chinese_PRC_CI_AS ; 2.修改表中列的排序规则 如果下列其中之一当前正在引用一个列,则无 ...

  2. html的显示消息和留言板

    <div class="inner_content"> <c:forEach items="${notices}" var="n&q ...

  3. python自学笔记(八)python语句

    一.print语句 1.1 基本输出,自动分行 1.2 print的逗号,可以衔接前面的内容而不换行,在一行内 1.3 >>重定向,输出到文件 print >> 文件名,&qu ...

  4. ie7下div覆盖在iframe上方,ie8就不行,怎么解决

    <div style="position:relative;display:inline-block;width:178px;height:90px;z-index:9999;top: ...

  5. 转: 关于viewport的理解

    最近我做了一点儿针对手机的Web开发和相关研究.按说,Web自设计之初,就已经考虑了设备无关性.然而,现实总是不尽如人意. 我们知道大多数网页都是针对桌面显示器开发和测试的,但是手机屏幕通常要比桌面显 ...

  6. Flex 全屏显示方法

    1,修改html-template下的index.template.html文件…增加四行 1</html> 上述文件增加了四行…见我文中有提示 2,Mxml文件: 假如一个button按 ...

  7. Linux BFS简介

    1. 什么是BFS 这里的BFS可不是广度优先算法,本文介绍的BFS是Linux的一个非Linux mainline调度算法.根据作者的描述BFS能够极大的提高低端设备(这里的低端设备的定义为:CPU ...

  8. 用 jQuery Masonry 插件创建瀑布流式的页面

    瀑布流式的页面,最早我是在国外的一个叫 Pinterest 的网站上看到,这个网站爆发,后来国内的很多网站也使用了这种瀑布流方式来展示页面(我不太喜欢瀑布流这个名字). 我们可以使用 jQuery 的 ...

  9. 对象图(Object Diagram)—UML图(三)

    一.用一张图来介绍一下对象图的基本内容 二.对象图与类图的基本差别 三.对象图实例

  10. LINQ 图解

    LINQ 图解 原创地址:http://www.cnblogs.com/jfzhu/archive/2013/01/01/2841332.html 转载请注明出处 LINQ,语言集成查询(Langua ...