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

今天状态怎么这么不好。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

又是调了好久。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

我竟然忘记更改值那里要先splay后再更改,而且还要pushup先!!!!!

QAQ

太弱了。。

记住!!!更改信息一定要先变成了根再修改,而且还要pushup!

(蒟蒻问了下神犇,总算弄懂了:“原树里面的父亲 在splay中可以是儿子”,所以在更新了父亲的点时,由于在splay中是儿子,所以没更新到在原树中是儿子的点。因此就错了。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <string>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <queue>
  8. #include <set>
  9. #include <map>
  10. using namespace std;
  11. typedef long long ll;
  12. #define rep(i, n) for(int i=0; i<(n); ++i)
  13. #define for1(i,a,n) for(int i=(a);i<=(n);++i)
  14. #define for2(i,a,n) for(int i=(a);i<(n);++i)
  15. #define for3(i,a,n) for(int i=(a);i>=(n);--i)
  16. #define for4(i,a,n) for(int i=(a);i>(n);--i)
  17. #define CC(i,a) memset(i,a,sizeof(i))
  18. #define read(a) a=getint()
  19. #define print(a) printf("%d", a)
  20. #define dbg(x) cout << (#x) << " = " << (x) << endl
  21. #define error(x) (!(x)?puts("error"):0)
  22. #define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
  23. inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
  24.  
  25. struct node* null;
  26. struct node {
  27. node *f, *c[2];
  28. int w, s; bool rev;
  29. node(int _k=0) { w=s=_k; f=c[0]=c[1]=null; rev=0; }
  30. void setc(node *x, bool d) { c[d]=x; x->f=this; }
  31. bool check() { return f==null || (f->c[0]!=this && f->c[1]!=this); }
  32. bool d() { return f->c[1]==this; }
  33. void pushup() { s=w+c[0]->s+c[1]->s; }
  34. void upd() { if(this==null) return; rev=!rev; swap(c[0], c[1]); }
  35. void pushdown() {
  36. if(rev) {
  37. rev=0;
  38. c[0]->upd();
  39. c[1]->upd();
  40. }
  41. }
  42. };
  43. void rot(node *x) {
  44. node *f=x->f;
  45. f->pushdown(); x->pushdown(); bool d=x->d();
  46. if(f->check()) x->f=f->f;
  47. else f->f->setc(x, f->d());
  48. f->setc(x->c[!d], d);
  49. x->setc(f, !d);
  50. f->pushup();
  51. }
  52. void fix(node *x) { if(!x->check()) fix(x->f); x->pushdown(); }
  53. void splay(node *x) {
  54. fix(x);
  55. while(!x->check())
  56. if(x->f->check()) rot(x);
  57. else x->d()==x->f->d()?(rot(x->f), rot(x)):(rot(x), rot(x));
  58. x->pushup();
  59. }
  60. node* access(node *x) {
  61. node *y=null;
  62. for(; x!=null; y=x, x=x->f) splay(x), x->c[1]=y;
  63. return y;
  64. }
  65. node* findroot(node *x) {
  66. access(x); splay(x);
  67. while(x->c[0]!=null) x=x->c[0];
  68. return x;
  69. }
  70. void mkroot(node *x) { access(x)->upd(); splay(x); }
  71. void link(node *x, node *y) { mkroot(x); x->f=y; }
  72.  
  73. const int N=30005;
  74. node *root[N];
  75. int n;
  76. int main() {
  77. null=new node; null->f=null->c[0]=null->c[1]=null;
  78. read(n);
  79. for1(i, 1, n) root[i]=new node(getint());
  80. int m=getint();
  81. while(m--) {
  82. char c=getchar(); while(c<'a'||c>'z') c=getchar();
  83. int a=getint(), b=getint();
  84. if(c=='b') {
  85. if(findroot(root[a])==findroot(root[b])) puts("no");
  86. else { puts("yes"); link(root[a], root[b]); };
  87. }
  88. else if(c=='p') { mkroot(root[a]); root[a]->w=b; root[a]->pushup(); }
  89. else {
  90. if(findroot(root[a])!=findroot(root[b])) { puts("impossible"); continue; }
  91. mkroot(root[a]); access(root[b]); splay(root[b]);
  92. printf("%d\n", root[b]->s);
  93. }
  94. }
  95. return 0;
  96. }

  


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

【BZOJ】1180: [CROATIAN2009]OTOCI & 2843: 极地旅行社(lct)的更多相关文章

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

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

  2. BZOJ 2843: 极地旅行社( LCT )

    LCT.. ------------------------------------------------------------------------ #include<cstdio> ...

  3. BZOJ 1180: [CROATIAN2009]OTOCI [LCT]

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 961  Solved: 594[Submit][S ...

  4. BZOJ 1180: [CROATIAN2009]OTOCI

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 989  Solved: 611[Submit][S ...

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

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

  6. bzoj 1180: [CROATIAN2009]OTOCI【LCT】

    一道几乎是板子的LCT,但是沉迷数学很久时候突然1A了这道题还是挺开心的 #include<iostream> #include<cstdio> using namespace ...

  7. 【刷题】BZOJ 1180 [CROATIAN2009]OTOCI

    Description 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通. 如果是则输出"no&quo ...

  8. 1180: [CROATIAN2009]OTOCI(LCT)

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 1200  Solved: 747[Submit][ ...

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

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

随机推荐

  1. css+div绝对定位

    <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" ...

  2. Delphi经验总结(1)

    先人的DELPHI基础开发技巧 ◇[DELPHI]网络邻居复制文件 uses shellapi; copyfile(pchar('newfile.txt'),pchar('//computername ...

  3. poj_2674 弹性碰撞

    题目大意 给定一条直线,长度为L 表示区间[0, L].在直线上开始放置N个人,每个人有一个初始位置pos(用到直线上端点0的距离表示)和初始方向dir('p' 或 'P' 表示向端点L行走, 'n' ...

  4. int *p()与int (*p)()的区别

    int *p()是返回指针的函数 int (*p)()是指向函数的指针   返回指针的函数: int *a(int x,int y); 有若干个学生的成绩(每个学生有4门课程),要求在用户输入学生序号 ...

  5. Android主题换肤实现

    本系列文章主要是对一个Material Design的APP的深度解析,主要包括以下内容 基于Material Design Support Library作为项目整体框架.对应博文:Android ...

  6. markdown下编辑latex数学公式

    在利用为知笔记编写笔记的时候,有时需要用的markdown,只要把文件名加上后缀.md,就可以使用markdown语法,以下介绍在markdown下编辑latex数学公式. 使用LaTeX写公式的基本 ...

  7. DRF如何序列化外键的字段

    我觉得在有些应用场景下,这个操作是有用的,因为可以减少一个AJAX的请求,以增加性能. 当然,是二次请求,还是一次传输.这即要考虑用户体验,还要兼顾服务器性能. 一切是有条件的平衡吧.就算是一次传输, ...

  8. linux tricks 之 BUILD_BUG_ON_ZERO.

    ------------------------------------------- 本文系作者原创, 欢迎大家转载! 转载请注明出处:netwalker.blog.chinaunix.net -- ...

  9. linux tricks 之数据对齐。

    转载:http://blog.chinaunix.net/uid-20608849-id-3027953.html   内核为了保持最大的兼容性和代码灵活性,不可能直接对某个数据类型定义它的大小范围. ...

  10. poj 2392 多重背包

    题意:有几个砖,给出高度,能放的最大高度和数目,求这些砖能垒成的最大高度 依据lim排个序,按一层一层进行背包 #include<cstdio> #include<iostream& ...