zoukankan      html  css  js  c++  java
  • BZOJ_1180_[CROATIAN2009]_OTOCI_(LCT)

    描述


    http://www.lydsy.com/JudgeOnline/problem.php?id=1180

    三种操作:

    1.询问x,y是否连通,如果不连通,建一条边x,y

    2.把x节点的权值改为t.

    3.询问节点x,y路径上的权值和.

    分析


    裸的LCT.

    但是...

    但是!

    现在才发现自己LCT改值的函数写的是错的!当我们询问x到y路径上的和值的时候,要先Access(y)(并且splay(y)),此时Splay的根是y,并且y在从最右边被splay转上来的时候所有经过的点都push_up()过了,但是在y被splay转上来之前的splay的根节点的左子树却没有push_up(),这样没有更新,就会出错.

     所以在更改x节点的权值之前一定要把x给splay一下!

    等下去把之前的博文里的错误改掉.

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int maxn=30000+5;
     5 int n,m;
     6 struct node{
     7     node* ch[2],* pa;
     8     int v,s; bool rev;
     9     node(int v,node* x):v(v),s(v){ ch[0]=ch[1]=pa=x; }
    10     bool d(){ return pa->ch[1]==this; }
    11     bool c(){ return pa->ch[0]==this||pa->ch[1]==this; }
    12     void setc(node* x,bool d){ ch[d]=x; x->pa=this; }
    13     void push_up(){ s=ch[0]->s+ch[1]->s+v; }
    14     void push_down(){
    15         if(rev){
    16             ch[0]->rev^=true;
    17             ch[1]->rev^=true;
    18             swap(ch[0],ch[1]);
    19             rev=false;
    20         }
    21     }
    22 }* null,* t[maxn];
    23 void rot(node* x){
    24     node* pa=x->pa; bool d=x->d();
    25     pa->push_down(); x->push_down();
    26     if(pa->c()) pa->pa->setc(x,pa->d());
    27     else x->pa=pa->pa;
    28     pa->setc(x->ch[!d],d);
    29     x->setc(pa,!d);
    30     pa->push_up();
    31 }
    32 void fix(node* x){
    33     if(x->c()) fix(x->pa);
    34     x->push_down();
    35 }
    36 void splay(node* x){
    37     fix(x);
    38     while(x->c())
    39         if(!x->pa->c()) rot(x);
    40         else x->d()==x->pa->d()?(rot(x->pa),rot(x)):(rot(x),rot(x));
    41     x->push_up();
    42 }
    43 void access(node* x){
    44     node *t=x;
    45     for(node* y=null; x!=null; y=x, x=x->pa){
    46         splay(x);
    47         x->ch[1]=y;
    48     }
    49     splay(t);
    50 }
    51 node*  find_root(node* x){
    52     access(x);
    53     while(x->ch[0]!=null) x=x->ch[0];
    54     return x;
    55 }
    56 void make_root(node* x){
    57     access(x);
    58     x->rev^=true;
    59 }
    60 void link(node* x,node* y){
    61     if(find_root(x)==find_root(y)){
    62         puts("no");
    63         return;
    64     }
    65     puts("yes");
    66     make_root(x);
    67     x->pa=y;
    68 }
    69 void query(node* x,node* y){
    70     if(find_root(x)!=find_root(y)){
    71         puts("impossible");
    72         return;
    73     }
    74     make_root(x);
    75     access(y);
    76     printf("%d
    ",y->s);
    77 }
    78 void change(node* x,int y){
    79     x->v=y;
    80     x->push_up();
    81 }
    82 int main(){
    83     null=new node(0,NULL);
    84     scanf("%d",&n);
    85     for(int i=1;i<=n;i++){
    86         int x; scanf("%d",&x);
    87         t[i]=new node(x,null);
    88     }
    89     scanf("%d",&m);
    90     for(int i=1;i<=m;i++){
    91         char c[10]; int x,y;
    92         scanf("%s%d%d",c,&x,&y);
    93         if(c[0]=='b') link(t[x],t[y]);
    94         else if(c[0]=='p') change(t[x],y);
    95         else query(t[x],t[y]);
    96     }
    97     return 0;
    98 }
    View Code

    1180: [CROATIAN2009]OTOCI

    Time Limit: 50 Sec  Memory Limit: 162 MB
    Submit: 848  Solved: 529
    [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行,每行包含一个操作,操作的类别见题目描述。任意时刻每个节点对应的权 值都是1到1000的整数。

    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

    Source

  • 相关阅读:
    ABP 番外篇-容器
    ABP 番外篇-菜单
    三、写服务
    十二、异步
    一、PHP_OSS使用
    十一、泛型
    Automapper
    ABP实践学习
    【2019-07-26】省是缺点
    【2019-07-25】女人,很强大
  • 原文地址:https://www.cnblogs.com/Sunnie69/p/5542651.html
Copyright © 2011-2022 走看看