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 ...
随机推荐
- 优雅的解决springboot Aop @Cacheable this不生效
问题描述:在同一个类中springAop不生效,例如在同一个类中没有 @Cacheable的方法调用本类有 @Cacheable的方法,则缓存不会设置. 原因:springaop基于java prox ...
- 洛谷 P2656 (缩点 + DAG图上DP)
### 洛谷 P2656 题目链接 ### 题目大意: 小胖和ZYR要去ESQMS森林采蘑菇. ESQMS森林间有N个小树丛,M条小径,每条小径都是单向的,连接两个小树丛,上面都有一定数量的蘑菇.小胖 ...
- 浅谈javascript引擎执行代码的步骤-(2019-2)
平时面试经常会遇到类似下面的这种题,而且千变万化,让人一个头两个.示例这道题算是稍微有点难度的了,这种题考查的是JavaScript引擎执行代码的步骤. b = 'cba'; function a(a ...
- C++ 在线编译器(支持 C++11)
C++11 的 Inheriting constructors 特性在 GCC 4.8 以前的版本及 VS2013 中都没有支持,测试起来比较麻烦,所以搜集到了几个支持 GCC 4.8 及更高版本的在 ...
- Devexpress treelist两张表父子节点设置、筛选、分页、排序、页面跳转demo
效果图 网上查了很多例子自己结合和修改了一下.最下方的分页跳转是dev的datapager控件.控件的属性事件自己研究一下. 代码如下 public partial class MMDefinitio ...
- pycharm替换文件中所有相同字段方法
1.打开要修改的文件 2.ctrl r调出替换功能,如图所示: 3.上面红框是需要更改的部分,下面红框是想要更改为部分,编辑后,点击“replace all”即可
- Javascript 关于基本类型和引用类型的个人理解
一.基础类型 A. 基础类型有5种,Number,String,Boolean,Null,Undefined B. 基础类型没有堆的概念,堆只针对引用类型. 所有基础类型都是以key-value形式存 ...
- 家电制造业中MES系统发挥的作用
人们对MES系统在家电生产领域的应用并不陌生,这是离散型MES应用的一个重要领域. 如空调.冰箱.电视.洗衣机等生产加工中均具有批量制造.多品种小批量.单件生产的等特点,很多企业也是以以订单.合同为核 ...
- i春秋-“百度杯”CTF比赛 十月场-Login
源码发下提示 尝试登陆 得到个什么鬼, 但是相应包里发现个可疑的东西 // CTF中的0 和1 这些一般都有套路的 然后在请求头里 改为 1 ##代码审计来了.. 分析了半天 后来看了别人的 ...
- maven 学习---Maven项目文档
本教程将教你如何一步到位创建应用程序的文档.因此,让我们开始,到 C:/MVN 创建java应用程序consumerBanking. OpenconsumerBanking文件夹,然后执行以下命令m ...