【BZOJ】2333: [SCOI2011]棘手的操作
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
- #include <bits/stdc++.h>
- using namespace std;
- const int N=300015, Lim=N;
- struct node *null;
- struct node {
- node *c[2], *f;
- int s, tag, mx, w;
- void init(int _w=-(~0u>>2)) { c[0]=c[1]=f=null; s=1; tag=0; mx=w=_w; }
- void up() { if(this==null) return; s=c[0]->s+c[1]->s+1; mx=max(w, max(c[0]->mx, c[1]->mx)); }
- void upd(int add) { if(this==null) return; mx+=add; w+=add; tag+=add; }
- void down() { if(tag) c[0]->upd(tag), c[1]->upd(tag), tag=0; }
- bool d() { return f->c[1]==this; }
- void setc(node *x, int d) { c[d]=x; x->f=this; }
- }Po[Lim], *iT=Po, *p[N];
- node *newnode(int w=-(~0u>>2)) { iT->init(w); return iT++; }
- void rot(node *x) {
- node *f=x->f; f->down(); x->down(); bool d=x->d();
- if(f->f!=null) f->f->setc(x, f->d());
- else x->f=f->f;
- f->setc(x->c[!d], d);
- x->setc(f, !d);
- f->up();
- }
- void splay(node *x, node *goal) {
- if(x==null) return;
- while(x->f!=goal)
- if(x->f->f==goal) rot(x);
- else x->d()==x->f->d()?(rot(x->f), rot(x)):(rot(x), rot(x));
- x->up();
- }
- int getrank(node *x) { splay(x, null); return x->c[0]->s; }
- node *sel(int k, node *x) {
- int s=x->c[0]->s;
- if(s==k) return x;
- if(s<k) return sel(k-s-1, x->c[1]);
- return sel(k, x->c[0]);
- }
- node *sel(int k) { splay(&Po[1], null); return sel(k, &Po[1]); }
- node *getrange(int l, int r) {
- node *nl=sel(l-1), *nr=sel(r+1);
- splay(nl, null); splay(nr, nl); return nr;
- }
- node *getblc(node *x, int len) { int rk=getrank(x); return getrange(rk, rk+len-1); }
- int pf[N], sz[N], wsum, a[N], n;
- int find(int x) { return pf[x]==x?x:pf[x]=find(pf[x]); }
- void U(int x, int y) {
- int fx=find(x), fy=find(y);
- if(fx==fy) return;
- if(sz[fx]<sz[fy]) swap(x, y), swap(fx, fy);
- sz[fx]+=sz[fy]; pf[fy]=fx;
- node *yf=getblc(p[fy], sz[fy]), *ny=yf->c[0];
- // printf("%d, %d\n", yf, ny);
- yf->c[0]=null; ny->f=null;
- splay(yf, null);
- splay(p[fx], null);
- splay(sel(p[fx]->c[0]->s+1), p[fx]);
- p[fx]->c[1]->setc(ny, 0);
- splay(ny, null);
- }
- void A1(int x, int v) { splay(p[x], null); p[x]->w+=v; p[x]->up(); }
- 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); }
- void A3(int v) { wsum+=v; }
- int F1(int x) { splay(p[x], null); return p[x]->w; }
- int F2(int x) { int rt=find(x); node *y=getblc(p[rt], sz[rt]); return y->c[0]->mx; }
- int F3() { splay(&Po[1], null); return Po[1].mx; }
- node *build(int l, int r) {
- if(l>r) return null;
- int mid=(l+r)>>1;
- node *x=p[mid]=newnode(a[mid]), *nl=build(l, mid-1), *nr=build(mid+1, r);
- if(nl!=null) x->setc(nl, 0);
- if(nr!=null) x->setc(nr, 1);
- x->up();
- return x;
- }
- void init() {
- null=iT++; null->init(); null->s=0;
- node *l=newnode(), *r=newnode();
- l->setc(r, 1);
- r->setc(build(1, n), 0);
- r->up(); l->up();
- //D(l);
- for(int i=1; i<=n; ++i) pf[i]=i, sz[i]=1;
- }
- int main() {
- scanf("%d", &n);
- for(int i=1; i<=n; ++i) scanf("%d", &a[i]);
- init();
- int Q; scanf("%d", &Q);
- while(Q--) {
- char cs[5];
- int x, v;
- scanf("%s", cs);
- if(cs[0]=='A') {
- if(cs[1]=='1') scanf("%d%d", &x, &v), A1(x, v);
- else if(cs[1]=='2') scanf("%d%d", &x, &v), A2(x, v);
- else scanf("%d", &v), A3(v);
- }
- else if(cs[0]=='U') scanf("%d%d", &x, &v), U(x, v);
- else {
- int ans;
- if(cs[1]=='1') scanf("%d", &x), ans=F1(x);
- else if(cs[1]=='2') scanf("%d", &x), ans=F2(x);
- else ans=F3();
- printf("%d\n", ans+wsum);
- }
- //puts("");
- //splay(&Po[1], null);
- //D(&Po[1]);
- }
- return 0;
- }
其实我是直接输入2333来做的233333333
想了一下发现可以用splay来做0.0
大概就是维护一个序列,然后同一连通块在一段连续的区间,然后区间修改就行辣
然后连通块大小用并查集维护一下就行辣
(然后看到题解是一堆可并堆是什么鬼。。。。可并堆是什么.......QAQ
(窝看了一下,妈呀你们这个左偏树查询和深度有关,居然没被卡!!!差评!!然后我的splay是单次查询是$O(logn)$的居然还被卡常熟!!!!!!
【BZOJ】2333: [SCOI2011]棘手的操作的更多相关文章
- BZOJ 2333: [SCOI2011]棘手的操作
题目描述 真的是个很棘手的操作.. 注意每删除一个点,就需要clear一次. #include<complex> #include<cstdio> using namespac ...
- BZOJ 2333: [SCOI2011]棘手的操作 可并堆 左偏树 set
https://www.lydsy.com/JudgeOnline/problem.php?id=2333 需要两个结构分别维护每个连通块的最大值和所有连通块最大值中的最大值,可以用两个可并堆实现,也 ...
- BZOJ 2333 SCOI2011 棘手的操作 并查集+可并堆
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2333 ..题意概述就不写了,各位老爷如果是看着玩的可以去搜一下,如果是做题找来的也知道题干 ...
- bzoj 2333 [SCOI2011]棘手的操作 —— 可并堆
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2333 稍微复杂,参考了博客:http://hzwer.com/5780.html 用 set ...
- BZOJ 2333 [SCOI2011]棘手的操作 (可并堆)
码农题.. 很显然除了两个全局操作都能用可并堆完成 全局最大值用个multiset记录,每次合并时搞一搞就行了 注意使用multiset删除元素时 如果直接delete一个值,会把和这个值相同的所有元 ...
- 2333: [SCOI2011]棘手的操作[写不出来]
2333: [SCOI2011]棘手的操作 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1979 Solved: 772[Submit][Stat ...
- 2333: [SCOI2011]棘手的操作[离线线段树]
2333: [SCOI2011]棘手的操作 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2325 Solved: 909[Submit][Stat ...
- 2333: [SCOI2011]棘手的操作[我不玩了]
2333: [SCOI2011]棘手的操作 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1979 Solved: 772[Submit][Stat ...
- 【BZOJ 2333 】[SCOI2011]棘手的操作(离线+线段树)
2333: [SCOI2011]棘手的操作 Description 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边 ...
随机推荐
- 【JAVA单例模式详解】
设计模式是一种思想,适合于任何一门面向对象的语言.共有23种设计模式. 单例设计模式所解决的问题就是:保证类的对象在内存中唯一. 举例: A.B类都想要操作配置文件信息Config.java,所以在方 ...
- 在Activity和Application中使用SharedPreferences存储数据
1.在Activity中创建SharedPreferences对象及操作方法 SharedPreferences pre=getSharedPreferences("User", ...
- hdu 4751 2013南京赛区网络赛 二分图判断 **
和以前做过的一个二分图颇为相似,以前的是互相不认识的放在一组,这个是互相认识的,本质上是相同的 是 hdu 2444 #include<cstdio> #include<iostre ...
- 使用AStyle进行代码格式化
转自:http://www.cnblogs.com/JerryTian/archive/2012/09/20/AStyle.html 在日常的编码当中,大家经常要遵照一些设计规范,如命名规则.代码格式 ...
- 学习设计接口api(转)
介绍 先说说啥是 Api 吧,以下摘自百度百科: API (Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于 ...
- Android在listview添加checkbox实现单选多选操作问题(转)
转自:http://yangshen998.iteye.com/blog/1310183 在Android某些开发需求当中,有时候需要在listveiw中加入checkbox实现单选,多选操作.表面上 ...
- 通讯录(ios自带有界面)
1.添加AddressBookUI.framework框架 2控制器中实现 #import "ViewController.h" #import <AddressBookUI ...
- Linux内核system_call中断处理过程
“平安的祝福 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” men ...
- hdu 2669 Romantic
Romantic Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- Android如何缩减APK包大小
代码 保持良好的编程习惯,不要重复或者不用的代码,谨慎添加libs,移除使用不到的libs. 使用proguard混淆代码,它会对不用的代码做优化,并且混淆后也能够减少安装包的大小. native c ...