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

题意:

有N个节点,标号从1到N,这N个节点一开始相互不连通。第i个节点的初始权值为a[i],接下来有如下一些操作:

U x y: 加一条边,连接第x个节点和第y个节点

A1 x v: 将第x个节点的权值增加v

A2 x v: 将第x个节点所在的连通块的所有节点的权值都增加v

A3 v: 将所有节点的权值都增加v

F1 x: 输出第x个节点当前的权值

F2 x: 输出第x个节点所在的连通块中,权值最大的节点的权值

F3: 输出所有节点中,权值最大的节点的权值

N, Q<=200000,-1000<=v, a[i]<=1000

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N=300015, Lim=N;
  4. struct node *null;
  5. struct node {
  6. node *c[2], *f;
  7. int s, tag, mx, w;
  8. void init(int _w=-(~0u>>2)) { c[0]=c[1]=f=null; s=1; tag=0; mx=w=_w; }
  9. void up() { if(this==null) return; s=c[0]->s+c[1]->s+1; mx=max(w, max(c[0]->mx, c[1]->mx)); }
  10. void upd(int add) { if(this==null) return; mx+=add; w+=add; tag+=add; }
  11. void down() { if(tag) c[0]->upd(tag), c[1]->upd(tag), tag=0; }
  12. bool d() { return f->c[1]==this; }
  13. void setc(node *x, int d) { c[d]=x; x->f=this; }
  14. }Po[Lim], *iT=Po, *p[N];
  15. node *newnode(int w=-(~0u>>2)) { iT->init(w); return iT++; }
  16. void rot(node *x) {
  17. node *f=x->f; f->down(); x->down(); bool d=x->d();
  18. if(f->f!=null) f->f->setc(x, f->d());
  19. else x->f=f->f;
  20. f->setc(x->c[!d], d);
  21. x->setc(f, !d);
  22. f->up();
  23. }
  24. void splay(node *x, node *goal) {
  25. if(x==null) return;
  26. while(x->f!=goal)
  27. if(x->f->f==goal) rot(x);
  28. else x->d()==x->f->d()?(rot(x->f), rot(x)):(rot(x), rot(x));
  29. x->up();
  30. }
  31. int getrank(node *x) { splay(x, null); return x->c[0]->s; }
  32. node *sel(int k, node *x) {
  33. int s=x->c[0]->s;
  34. if(s==k) return x;
  35. if(s<k) return sel(k-s-1, x->c[1]);
  36. return sel(k, x->c[0]);
  37. }
  38. node *sel(int k) { splay(&Po[1], null); return sel(k, &Po[1]); }
  39. node *getrange(int l, int r) {
  40. node *nl=sel(l-1), *nr=sel(r+1);
  41. splay(nl, null); splay(nr, nl); return nr;
  42. }
  43. node *getblc(node *x, int len) { int rk=getrank(x); return getrange(rk, rk+len-1); }
  44.  
  45. int pf[N], sz[N], wsum, a[N], n;
  46. int find(int x) { return pf[x]==x?x:pf[x]=find(pf[x]); }
  47. void U(int x, int y) {
  48. int fx=find(x), fy=find(y);
  49. if(fx==fy) return;
  50. if(sz[fx]<sz[fy]) swap(x, y), swap(fx, fy);
  51. sz[fx]+=sz[fy]; pf[fy]=fx;
  52. node *yf=getblc(p[fy], sz[fy]), *ny=yf->c[0];
  53. // printf("%d, %d\n", yf, ny);
  54. yf->c[0]=null; ny->f=null;
  55. splay(yf, null);
  56. splay(p[fx], null);
  57. splay(sel(p[fx]->c[0]->s+1), p[fx]);
  58. p[fx]->c[1]->setc(ny, 0);
  59. splay(ny, null);
  60. }
  61. void A1(int x, int v) { splay(p[x], null); p[x]->w+=v; p[x]->up(); }
  62. void A2(int x, int v) { x=find(x); node *y=getblc(p[x], sz[x]); y->c[0]->upd(v); splay(y->c[0], null); }
  63. void A3(int v) { wsum+=v; }
  64. int F1(int x) { splay(p[x], null); return p[x]->w; }
  65. int F2(int x) { int rt=find(x); node *y=getblc(p[rt], sz[rt]); return y->c[0]->mx; }
  66. int F3() { splay(&Po[1], null); return Po[1].mx; }
  67. node *build(int l, int r) {
  68. if(l>r) return null;
  69. int mid=(l+r)>>1;
  70. node *x=p[mid]=newnode(a[mid]), *nl=build(l, mid-1), *nr=build(mid+1, r);
  71. if(nl!=null) x->setc(nl, 0);
  72. if(nr!=null) x->setc(nr, 1);
  73. x->up();
  74. return x;
  75. }
  76. void init() {
  77. null=iT++; null->init(); null->s=0;
  78. node *l=newnode(), *r=newnode();
  79. l->setc(r, 1);
  80. r->setc(build(1, n), 0);
  81. r->up(); l->up();
  82. //D(l);
  83. for(int i=1; i<=n; ++i) pf[i]=i, sz[i]=1;
  84. }
  85. int main() {
  86. scanf("%d", &n);
  87. for(int i=1; i<=n; ++i) scanf("%d", &a[i]);
  88. init();
  89. int Q; scanf("%d", &Q);
  90. while(Q--) {
  91. char cs[5];
  92. int x, v;
  93. scanf("%s", cs);
  94. if(cs[0]=='A') {
  95. if(cs[1]=='1') scanf("%d%d", &x, &v), A1(x, v);
  96. else if(cs[1]=='2') scanf("%d%d", &x, &v), A2(x, v);
  97. else scanf("%d", &v), A3(v);
  98. }
  99. else if(cs[0]=='U') scanf("%d%d", &x, &v), U(x, v);
  100. else {
  101. int ans;
  102. if(cs[1]=='1') scanf("%d", &x), ans=F1(x);
  103. else if(cs[1]=='2') scanf("%d", &x), ans=F2(x);
  104. else ans=F3();
  105. printf("%d\n", ans+wsum);
  106. }
  107. //puts("");
  108. //splay(&Po[1], null);
  109. //D(&Po[1]);
  110. }
  111. return 0;
  112. }

  

其实我是直接输入2333来做的233333333

想了一下发现可以用splay来做0.0

大概就是维护一个序列,然后同一连通块在一段连续的区间,然后区间修改就行辣

然后连通块大小用并查集维护一下就行辣

(然后看到题解是一堆可并堆是什么鬼。。。。可并堆是什么.......QAQ

(窝看了一下,妈呀你们这个左偏树查询和深度有关,居然没被卡!!!差评!!然后我的splay是单次查询是$O(logn)$的居然还被卡常熟!!!!!!

【BZOJ】2333: [SCOI2011]棘手的操作的更多相关文章

  1. BZOJ 2333: [SCOI2011]棘手的操作

    题目描述 真的是个很棘手的操作.. 注意每删除一个点,就需要clear一次. #include<complex> #include<cstdio> using namespac ...

  2. BZOJ 2333: [SCOI2011]棘手的操作 可并堆 左偏树 set

    https://www.lydsy.com/JudgeOnline/problem.php?id=2333 需要两个结构分别维护每个连通块的最大值和所有连通块最大值中的最大值,可以用两个可并堆实现,也 ...

  3. BZOJ 2333 SCOI2011 棘手的操作 并查集+可并堆

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2333 ..题意概述就不写了,各位老爷如果是看着玩的可以去搜一下,如果是做题找来的也知道题干 ...

  4. bzoj 2333 [SCOI2011]棘手的操作 —— 可并堆

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2333 稍微复杂,参考了博客:http://hzwer.com/5780.html 用 set ...

  5. BZOJ 2333 [SCOI2011]棘手的操作 (可并堆)

    码农题.. 很显然除了两个全局操作都能用可并堆完成 全局最大值用个multiset记录,每次合并时搞一搞就行了 注意使用multiset删除元素时 如果直接delete一个值,会把和这个值相同的所有元 ...

  6. 2333: [SCOI2011]棘手的操作[写不出来]

    2333: [SCOI2011]棘手的操作 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1979  Solved: 772[Submit][Stat ...

  7. 2333: [SCOI2011]棘手的操作[离线线段树]

    2333: [SCOI2011]棘手的操作 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2325  Solved: 909[Submit][Stat ...

  8. 2333: [SCOI2011]棘手的操作[我不玩了]

    2333: [SCOI2011]棘手的操作 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1979  Solved: 772[Submit][Stat ...

  9. 【BZOJ 2333 】[SCOI2011]棘手的操作(离线+线段树)

    2333: [SCOI2011]棘手的操作 Description 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边 ...

随机推荐

  1. 【JAVA单例模式详解】

    设计模式是一种思想,适合于任何一门面向对象的语言.共有23种设计模式. 单例设计模式所解决的问题就是:保证类的对象在内存中唯一. 举例: A.B类都想要操作配置文件信息Config.java,所以在方 ...

  2. 在Activity和Application中使用SharedPreferences存储数据

    1.在Activity中创建SharedPreferences对象及操作方法 SharedPreferences pre=getSharedPreferences("User", ...

  3. hdu 4751 2013南京赛区网络赛 二分图判断 **

    和以前做过的一个二分图颇为相似,以前的是互相不认识的放在一组,这个是互相认识的,本质上是相同的 是 hdu 2444 #include<cstdio> #include<iostre ...

  4. 使用AStyle进行代码格式化

    转自:http://www.cnblogs.com/JerryTian/archive/2012/09/20/AStyle.html 在日常的编码当中,大家经常要遵照一些设计规范,如命名规则.代码格式 ...

  5. 学习设计接口api(转)

    介绍 先说说啥是 Api 吧,以下摘自百度百科: API (Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于 ...

  6. Android在listview添加checkbox实现单选多选操作问题(转)

    转自:http://yangshen998.iteye.com/blog/1310183 在Android某些开发需求当中,有时候需要在listveiw中加入checkbox实现单选,多选操作.表面上 ...

  7. 通讯录(ios自带有界面)

    1.添加AddressBookUI.framework框架 2控制器中实现 #import "ViewController.h" #import <AddressBookUI ...

  8. Linux内核system_call中断处理过程

    “平安的祝福 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” men ...

  9. hdu 2669 Romantic

    Romantic Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  10. Android如何缩减APK包大小

    代码 保持良好的编程习惯,不要重复或者不用的代码,谨慎添加libs,移除使用不到的libs. 使用proguard混淆代码,它会对不用的代码做优化,并且混淆后也能够减少安装包的大小. native c ...