此题用可并堆勉强过,需加输入优化,但是这里有个问题就是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]棘手的操作】的更多相关文章

  1. 洛谷P3273 [SCOI2011] 棘手的操作 [左偏树]

    题目传送门 棘手的操作 题目描述 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边,连接第x个节点和第y个节点 A1 ...

  2. P3273 [SCOI2011]棘手的操作

    吐槽 上午风浔凌julao问我的神题 操作又多又毒瘤又棘手... 然后bzoj题号正好是2333,2333333333 思路 貌似只有我是这么写的 线段树合并, 每个线段树存每个连通块的信息,维护点的 ...

  3. 洛谷P3273 [SCOI2011]棘手的操作

    题目描述 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作:U x y: 加一条边,连接第x个节点和第y个节点A1 x v: 将第x个节点的权 ...

  4. bzoj2333[SCOI2011]棘手的操作 洛谷P3273 [SCOI2011]棘手的操作

    2333? 先记一下吧,这题现在全部都是照着题解做的,因为怎么改都改不出来,只好对着题解改,以后还要再做过 以后再也不用指针了!太恶心了!空指针可不止直接特判那么简单啊,竟然还要因为空指针写奇怪的分类 ...

  5. Luogu P3273 [SCOI2011]棘手的操作(左偏树)

    什么恶心东西啊,又水又烦 两个可并堆维护即可 #include <cstdio> #include <iostream> #include <cstring> #i ...

  6. 【bzoj2333】 [SCOI2011]棘手的操作 可并堆+lazy标记

    2016-05-31  21:45:41 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2333 (学习了黄学长的代码 有如下操作: U x y ...

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

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

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

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

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

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

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

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

随机推荐

  1. RocketMQ的顺序消费和事务消费

    一.三种消费 :1.普通消费 2. 顺序消费 3.事务消费 1.1  顺序消费:在网购的时候,我们需要下单,那么下单需要假如有三个顺序,第一.创建订单 ,第二:订单付款,第三:订单完成.也就是这个三个 ...

  2. 数据分析常用的Excel函数

    Excel是我们工作中经常使用的一种工具,对于数据分析来说,这也是处理数据最基础的工具. 本文对数据分析需要用到的函数做了分类,并且有详细的例子说明,文章已做了书签处理,点击可跳转至相应位置. 函数分 ...

  3. (十八)golang--defer关键字

    在函数中,程序员经常需要创建资源(比如,数据库连接,文件句柄,锁等),为了在函数执行完毕后,及时释放资源,go设计者提供defer(延时机制) 用defer申明的语句不会立即执行,而是被存入到defe ...

  4. C语言中的scanf与scanf_s 以及循环输入的问题解决

    Scanf 在标准C中,scanf提供了键盘输入功能. scanf函数是一个标准库函数,它的函数原型在头文件“stdio.h”中.与printf函数相同,C语言也允许在使用scanf函数之前不必包含s ...

  5. Java & PHP RSA 互通密钥、签名、验签、加密、解密

    RSA加密算法是一种非对称加密算法.在公开密钥加密和电子商业中RSA被广泛使用.RSA是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Le ...

  6. DAX 第七篇:分组聚合

    DAX有三个用于生成分组聚合数据的函数,这三个函数有两个共同的特征:分组列和扩展列. 分组列是用于分组的列,只能来源于基础表中已存的列,分组列可以来源于同一个表,也可以来源于相关的列. 扩展列是由na ...

  7. c# 调用接口返回json

    需要命名空间 using System.Net; using System.Net.Security using System.Security.Cryptography.X509Certificat ...

  8. C#开发自动照片(图片)裁剪(缩放)工具

    1.需求分析 用winform窗体程序,开发一个能够自动.批量对图片进行缩放和裁剪的程序. 原本想直接从网上找类型的工具直接用,但是无奈现在网上能找到的工具,要么不能用,要么就是很 恶心的下载完后还有 ...

  9. C#使用HttpClient上传文件并附带其他参数

    HttpClient和MultipartFormDataContent(传送门)最低适用于.NET Framework 4.5版本 发送端代码 using (HttpClient client = n ...

  10. DevExpress的TreeList怎样给树节点设置图标

    场景 DevExpress的TreeList怎样设置数据源使其显示成单列树形结构: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/ ...