【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: 加一条边 ...
随机推荐
- ASP.NET MVC 使用带有短横线的html Attributes(转载)
转载地址:http://www.nmtree.net/2013/10/25/asp-net-mvc-use-dash-in-html-attributes.html 情景再现 我们常常需要一个文本框来 ...
- Task使用小结
Task是.NET推出数据任务处理的工作类,Task的使用也被越来越多的人讲解,这里仅仅介绍Task的部分使用介绍: 1.Task简单创建 --无返回值 Task.Factory.StartNew(( ...
- OS X thrift setup
OS X Setup The following command install all the required tools and libraries to build and install t ...
- oracle检查点队列(checkpoint queue)
buffer cache CBC链 按地址链 LRU 干净buffer LRUW 脏buffer 按照冷热 checkpoint queue:链buffer,①链脏块②按buffer第一次脏的时 ...
- WebRTC之带宽控制部分学习(1) ------基本demo的介绍
转自:http://blog.csdn.net/u013160228/article/details/46392037 WebRTC的代码真是非常之大啊,下载以及编译了我好几天才搞完..... 可以看 ...
- 在Salesforce中避免对Trigger中Update的无限循环操作
在Salesforce中避免对Trigger中Update的无限循环操作: 处理Trigger的时候会有这么一个场景:在Trigger中想修改该Object的某些字段的值,那么如果们在程序中再用代码的 ...
- tab_切换
记忆: 一.这里用到了jQuery遍历---filter()方法: filter() 方法将匹配元素集合缩减为匹配指定选择器的元素. 二.HTML DOM hash属性 hash 属性是一个可读可写的 ...
- LR通过snmp监控linux下的mysql
LR通过snmp监控linux下的mysql 在linux底下安装配置snmp: 1.使用系统盘安装rpm包(这种方式最好) 2.在www.net-snmp.org处下载net-snmp安装(安装后有 ...
- 《DSP using MATLAB》示例Example4.3 双边序列
- 标准W3C盒子模型和IE盒子模型
标准W3C盒子模型和IE盒子模型 CSS盒子模型:网页设计中CSS技术所使用的一种思维模型. CSS盒子模型组成:外边距(margin).边框(border).内边距(padding).内容(co ...