P3273 【[SCOI2011]棘手的操作】
此题用可并堆勉强过,需加输入优化,但是这里有个问题就是set总是过不了一组数据,用multiset时间有点高,不懂这个问题,请懂此问题的给我留言。
左偏树+并查集
下面上代码:
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <utility>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <iostream>
#include <stack>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-6
#define CLR( a, v ) memset ( a, v, sizeof ( a ) )
#define LL long long
#define DBUG printf ( "here!!!\n" )
#define rep( i, a, b ) for ( int i = ( a ); i < ( b ); i ++ )
#define PB push_back
#define ULL unsigned long long
#define PI acos ( -1.0 )
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1 | 1
#define lowbit( x ) ( ( x )&( -x ) )
#define CASE int Test; scanf ( "%d", &Test ); for ( int cas = 1; cas <= Test; cas ++ )
#define ALL( x ) x.begin ( ), x.end ( )
#define INS( x ) x, x.begin ( )
typedef pair < int, int > Pii;
typedef pair < double, double > Pdd;
typedef set < int > Set;
const int maxn = ;
int read_int ( ) {
int res = , f = ;
int ch = getchar ( );
while ( ch < '' || ch > '' ) {
if ( ch == - )
return -;
if ( ch == '-' )
f = -;
ch = getchar ( );
}
while ( ch >= '' && ch <= '' ) {
res = res*+( ch-'' );
ch = getchar ( );
}
return res*f;
}
int rt[maxn], ch[maxn][], sum[maxn], S[maxn], c;
int con[maxn], fa[maxn], a[maxn], dis[maxn];
int find ( int x ) {
return con[x] == x ? x : con[x] = find ( con[x] );
}
void PushDown ( int u ) {
if ( sum[u] ) {
if ( ch[u][] ) {
sum[ ch[u][] ] += sum[u];
a[ ch[u][] ] += sum[u];
}
if ( ch[u][] ) {
sum[ ch[u][] ] += sum[u];
a[ ch[u][] ] += sum[u];
}
sum[u] = ;
}
}
void PushAll ( int x ) {
c = ;
while ( x ) {
S[c ++] = x;
x = fa[x];
}
while ( c > )
PushDown ( S[-- c] );
}
int Merge ( int u, int v ) {
if ( u == )
return v;
if ( v == )
return u;
if ( a[u] < a[v] )
swap ( u, v );
PushDown ( u );
ch[u][] = Merge ( ch[u][], v );
fa[ ch[u][] ] = u;
if ( dis[ ch[u][] ] < dis[ ch[u][] ] )
swap ( ch[u][], ch[u][] );
dis[u] = dis[ ch[u][] ]+;
return u;
}
void remove ( int u ) {
PushAll ( u );
int p = fa[u];
int x = Merge ( ch[u][], ch[u][] );
fa[x] = p;
ch[p][ ch[p][] == u ] = x;
while ( p ) {
if ( dis[ ch[p][] ] < dis[ ch[p][] ] )
swap ( ch[p][], ch[p][] );
if ( dis[ ch[p][] ]+ == dis[p] )
break ;
dis[p] = dis[ ch[p][] ]+;
p = fa[p];
}
}
multiset < int > vis;
void solve ( ) {
int n, Q, x, v, fx, fy, all = ;
char op[];
dis[] = -;
n = read_int ( );
vis.clear ( );
for ( int i = ; i <= n; i ++ ) {
a[i] = read_int ( );
rt[i] = con[i] = i;
vis.insert ( a[i] );
}
Q = read_int ( );
while ( Q -- ) {
scanf ( "%s", op );
if ( op[] == 'U' ) {
x = read_int ( ), v = read_int ( );
fx = find ( x ), fy = find ( v );
if ( fx == fy )
continue ;
con[fx] = fy;
vis.erase ( vis.find ( min ( a[ rt[fx] ], a[ rt[fy] ] ) ) );
rt[fy] = Merge ( rt[fy], rt[fx] );
fa[ rt[fy] ] = ;
} else if ( op[] == 'A' ) {
if ( op[] == '' ) {
x = read_int ( ), v = read_int ( );
fx = find ( x );
vis.erase ( vis.find ( a[ rt[fx] ] ) );
if ( x != rt[fx] )
remove ( x );
else {
PushDown ( x );
rt[fx] = Merge ( ch[x][], ch[x][] );
fa[ rt[fx] ] = ;
}
sum[x] = fa[x] = ch[x][] = ch[x][] = ;
a[x] += v;
rt[fx] = Merge ( rt[fx], x );
vis.insert ( a[ rt[fx] ] );
fa[ rt[fx] ] = ;
} else if ( op[] == '' ) {
x = read_int ( ), v = read_int ( );
fx = find ( x );
vis.erase ( vis.find ( a[ rt[fx] ] ) );
a[ rt[fx] ] += v;
vis.insert ( a[ rt[fx] ] );
sum[ rt[fx] ] += v;
} else if ( op[] == '' ) {
v = read_int ( );
all += v;
}
} else if ( op[] == 'F' ) {
if ( op[] == '' ) {
x = read_int ( );
PushAll ( x );
printf ( "%d\n", a[x]+all );
} else if ( op[] == '' ) {
x = read_int ( );
fx = find ( x );
printf ( "%d\n", a[ rt[fx] ]+all );
} else if ( op[] == '' )
printf ( "%d\n", *( -- vis.find ( INF ) )+all );
}
}
}
int main ( ) {
solve ( );
return ;
}
P3273 【[SCOI2011]棘手的操作】的更多相关文章
- 洛谷P3273 [SCOI2011] 棘手的操作 [左偏树]
题目传送门 棘手的操作 题目描述 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边,连接第x个节点和第y个节点 A1 ...
- P3273 [SCOI2011]棘手的操作
吐槽 上午风浔凌julao问我的神题 操作又多又毒瘤又棘手... 然后bzoj题号正好是2333,2333333333 思路 貌似只有我是这么写的 线段树合并, 每个线段树存每个连通块的信息,维护点的 ...
- 洛谷P3273 [SCOI2011]棘手的操作
题目描述 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作:U x y: 加一条边,连接第x个节点和第y个节点A1 x v: 将第x个节点的权 ...
- bzoj2333[SCOI2011]棘手的操作 洛谷P3273 [SCOI2011]棘手的操作
2333? 先记一下吧,这题现在全部都是照着题解做的,因为怎么改都改不出来,只好对着题解改,以后还要再做过 以后再也不用指针了!太恶心了!空指针可不止直接特判那么简单啊,竟然还要因为空指针写奇怪的分类 ...
- Luogu P3273 [SCOI2011]棘手的操作(左偏树)
什么恶心东西啊,又水又烦 两个可并堆维护即可 #include <cstdio> #include <iostream> #include <cstring> #i ...
- 【bzoj2333】 [SCOI2011]棘手的操作 可并堆+lazy标记
2016-05-31 21:45:41 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2333 (学习了黄学长的代码 有如下操作: U x y ...
- 【BZOJ 2333 】[SCOI2011]棘手的操作(离线+线段树)
2333: [SCOI2011]棘手的操作 Description 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边 ...
- 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 ...
随机推荐
- 下载代码的时候 SSH与http的区别
SSH: git@gitlab.alibaba-inc.com:damai-mz/mz-market.git HTTP:http://gitlab.alibaba-inc.com/damai-mz/m ...
- antd模块组件文档思维导图整理
- STS 创建 Maven 项目填坑
用 STS 创建 Maven 项目并不复杂,只是其中有一些坑在里面,我在解决这些坑的时候发现很多人都遇到了相同的问题,因此把创建的步骤记录在这里.所有的步骤不外乎就是一些套路,并没有什么复杂的地方,只 ...
- vue 移动端上传图片结合localResizeIMG插件进行图片压缩
localResizeIMG插件的功能是将图片进行压缩,然后转换成base64传给后台. 首先, npm i lrz -save 然后,再main.js里面引入lrz import lrz from ...
- JVM的内存分配策略
1.对象优先在Eden区分配大多数情况下,对象在新生代Eden区中分配.当Eden区没有足够空间进行分配时,虚拟机将发起一次Minor GC. 2.大对象直接进入老年代 所谓的大对象是指,需要大量连续 ...
- 算法初步---基本的数据结构(java为例)
最近搞算法,觉得超级吃力的,一直以为数学好的,数学可以考试满分,算法一定没什么问题,贱贱地,我发现我自己想多了,还是自己的基础薄弱吧,今天我来补补最基础的知识. 算法(Algorithm)是指解题方案 ...
- 如何使用gitlab自建golang基础库
这里以go mod方式建立golang基础库 一.gitlab创建项目golib 地址为gitlab.xxx.com/base/golib 示例如下 go mod初始化命令 go mod init g ...
- ICSharpCode.SharpZipLib 中文乱码问题
今天在调用ICSharpCode.SharpZipLib解压zip文件时出现了中文文件乱码的问题. 解决过程如下: 1.判断是否压缩包本身问题.经查zip文件夹在本地直接解压打开时正确的中文名称,所以 ...
- codeforces #579(div3)
codeforces #579(div3) A. Circle of Students 题意: 给定一个n个学生的编号,学生编号1~n,如果他们能够在不改变顺序的情况下按编号(无论是正序还是逆序,但不 ...
- 奖金高达150万元的NEST即将上线,速来报名
表哥.表姐们快粗来~~~分享一个劲爆消息: 年度奖金总额高达150万元的众测平台(NEST)已于2019年6月10日启动全球报名,6月26号开始首批线上众测,欢迎精英白帽们前来挑战! 更多详情请认真阅 ...