emmm…标题卡着长度上限…

LCT板题…(ε=ε=ε=┏(゜ロ゜;)┛)

CODE

  1. #include <cctype>
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <algorithm>
  6. using namespace std;
  7. typedef long long LL;
  8. char cb[1<<15],*cs=cb,*ct=cb;
  9. #define getc() (cs==ct&&(ct=(cs=cb)+fread(cb,1,1<<15,stdin),cs==ct)?0:*cs++)
  10. template<class T>inline void read(T &res) {
  11. char ch; int flg = 1; for(;!isdigit(ch=getchar());)if(ch=='-')flg=-flg;
  12. for(res=ch-'0';isdigit(ch=getchar());res=res*10+ch-'0'); res*=flg;
  13. }
  14. const int MAXN = 30005;
  15. int n, q, w[MAXN];
  16. namespace LCT {
  17. #define ls ch[x][0]
  18. #define rs ch[x][1]
  19. int ch[MAXN][2], fa[MAXN], sum[MAXN];
  20. bool rev[MAXN];
  21. inline bool isr(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
  22. inline bool get(int x) { return ch[fa[x]][1] == x; }
  23. inline void upd(int x) { sum[x] = w[x] + sum[ls] + sum[rs]; }
  24. inline void mt(int x) { if(rev[x]) rev[x]^=1, rev[ls]^=1, rev[rs]^=1, swap(ls, rs); }
  25. void mtpath(int x) { if(!isr(x)) mtpath(fa[x]); mt(x); }
  26. inline void rot(int x) {
  27. int y = fa[x], z = fa[y];
  28. bool l = get(x), r = l^1;
  29. if(!isr(y)) ch[z][get(y)] = x;
  30. fa[ch[x][r]] = y, fa[y] = x, fa[x] = z;
  31. ch[y][l] = ch[x][r], ch[x][r] = y;
  32. upd(y), upd(x);
  33. }
  34. inline void splay(int x) { mtpath(x);
  35. for(; !isr(x); rot(x))
  36. if(!isr(fa[x])) rot(get(x) == get(fa[x]) ? fa[x] : x);
  37. }
  38. inline void access(int x) { int y = 0;
  39. for(; x; x=fa[y=x]) splay(x), ch[x][1] = y, upd(x);
  40. }
  41. inline void bert(int x) { access(x), splay(x), rev[x]^=1; }
  42. inline int sert(int x) {
  43. access(x), splay(x);
  44. while(ch[x][0]) x=ch[x][0];
  45. return x;
  46. }
  47. inline bool judge(int x, int y) { bert(x); return sert(y) == x; }
  48. inline bool link(int x, int y) { if(judge(x, y)) return 0; fa[x] = y; return 1; }
  49. inline int querysum(int x, int y) { if(x == y) return w[x]; if(!judge(x, y)) return -1; return sum[y]; }
  50. inline void modify(int x, int val) {
  51. bert(x); w[x] = val; upd(x);
  52. }
  53. }
  54. int main () {
  55. read(n);
  56. for(int i = 1; i <= n; ++i)
  57. read(w[i]), LCT::sum[i] = w[i];
  58. read(q);
  59. int x, y; char s[2];
  60. while(q--) {
  61. while(!isalpha(s[0]=getchar()));
  62. while(isalpha(s[1]=getchar()));
  63. read(x), read(y);
  64. if(s[0] == 'b') puts(LCT::link(x, y) ? "yes" : "no");
  65. else if(s[0] == 'p') LCT::modify(x, y);
  66. else {
  67. int ans = LCT::querysum(x, y);
  68. if(~ans) printf("%d\n", ans);
  69. else puts("impossible");
  70. }
  71. }
  72. }

BZOJ 1180 [CROATIAN 2009]OTOCI // BZOJ 2843 极地旅行社 // Luogu P4321 [COCI 2009] OTOCI / 极地旅行社 (LCA板题)的更多相关文章

  1. [luogu]P4312 [COCI 2009] OTOCI / 极地旅行社(LCT)

    P4312 [COCI 2009] OTOCI / 极地旅行社 题目描述 不久之前,Mirko建立了一个旅行社,名叫"极地之梦".这家旅行社在北极附近购买了N座冰岛,并且提供观光服 ...

  2. 【题解】 Luogu P4312 / SP4155 [COCI 2009] OTOCI / 极地旅行社

    原题地址:P4312 [COCI 2009] OTOCI / 极地旅行社/SP4155 OTOCI - OTOCI lct入门难度的题,十分弱智(小蒟蒻说lct是什么,能吃吗?) bridge操作判联 ...

  3. bzoj 2223 [Coci 2009]PATULJCI

    [Coci 2009]PATULJCI Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1286  Solved: 553[Submit][Status ...

  4. Kruskal算法及其类似原理的应用——【BZOJ 3654】tree&&【BZOJ 3624】[Apio2008]免费道路

    首先让我们来介绍Krukal算法,他是一种用来求解最小生成树问题的算法,首先把边按边权排序,然后贪心得从最小开始往大里取,只要那个边的两端点暂时还没有在一个联通块里,我们就把他相连,只要这个图里存在最 ...

  5. BZOJ_2223_[Coci 2009]PATULJCI_主席树

    BZOJ_2223_[Coci 2009]PATULJCI_主席树 Description Input 10 3 1 2 1 2 1 2 3 2 3 3 8 1 2 1 3 1 4 1 5 2 5 2 ...

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

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

  7. BZOJ 1180: [CROATIAN2009]OTOCI

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

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

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

  9. OTOCI(bzoj 1180)

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

随机推荐

  1. HDFS 读/写数据流程

    1. HDFS 写数据流程 客户端通过 Distributed FileSystem 模块向 NameNode 请求上传文件, NameNode 检查目标文件是否已存在,父目录是否存在: NameNo ...

  2. PTA(Advanced Level)1067.Sort with Swap(0, i)

    Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...

  3. [转帖]centos7设置CPU的运行频率为performance

    centos7设置CPU的运行频率为performance http://www.512873.com/archives/612.html Publish: March 6, 2019 Categor ...

  4. go 二进制数据处理

    以下是利用标准库binary来进行编解码 编码 ①使用bytes.Buffer来存储编码生成的串②使用binary.Write来编码存储在①的buf中 package main import ( &q ...

  5. 自定义的C4P数据库连接池

    要想自己写一个数据库连接池 需要实现DataSoure这个类,然后重写getConnection 这个方法 public class C4p0 implements DataSource{ priva ...

  6. 记录一次线上yarn RM频繁切换的故障

    周末一大早被报警惊醒,rm频繁切换 急急忙忙排查 看到两处错误日志 错误信息1 ervation <memory:0, vCores:0> 2019-12-21 11:51:57,781 ...

  7. php 连接webservice接口

    首先谢谢前人, 引用:https://www.cnblogs.com/xbxxf/p/10103430.html 本来说对接接口,我以为是一扮curl接口形式,结果最后给接口锝时候才告诉我是webse ...

  8. Delphi编译器属性(特别修饰符Ref,Unsafe,Volatile,Weak)

    1 Refdelphi中常量参数就像一个本地常量,或者说只读变量.常量参数与值参数类似,除了不能在过程或函数体内给常量参数赋值,并且不能将常量参数传给另一个var类型参数.(但是,如果你常量参数传递的 ...

  9. 怎样创建一个canvas画布环境

    1. 由于canvas画布在网页中, 所以需要在html中添加canvas标签: <!DOCTYPE html> <html lang="en"> < ...

  10. Angular7如何动态刷新Echarts图表

    1 概述 echarts是百度的开源图表插件 Angular中引入echarts网上教程很多 Angular引入echarts,并使用动态刷新 2 安装 请参考大神的博客:https://blog.c ...