zoukankan      html  css  js  c++  java
  • BZOJ 2843: 极地旅行社( LCT )

    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 " );
    join( V[ a ] , V[ b ] );
    } else
    printf( "no " );
    } else if( s[ 0 ] == 'e' ) {
    if( findRoot( V[ a ] ) != findRoot( V[ b ] ) )
    printf( "impossible " );
    else {
       makeRoot( V[ a ] );
       access( V[ b ] );
       splay( V[ b ] );
       printf( "%d " , 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

  • 相关阅读:
    Photoshop教程,视频MP4格式转换为GIF格式
    pyqgis环境配置
    R 输出函数 格式化输出 打印函数
    linux ubuntu 更改终端的默认设置,终端大小,字体
    wps 显示所有的字符,将参考文献排序,插入目录
    在 word 中 怎么让表格旋转方向
    linux argc argv
    linux 命令行的快捷键 vim
    linux 操作系统,以及一般的操作系统 所看书籍
    win 10 快速启动 某些程序
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/4598243.html
Copyright © 2011-2022 走看看