此题用可并堆勉强过,需加输入优化,但是这里有个问题就是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. (二十二)golang--时间和日期相关函数

    时间的常量,可以获得指定时间单位 Unix和UnixNano   小例子:统计函数运行的时间:

  2. telnet: Unable to connect to remote host: Connection refused

    问题描述: telnet: Unable to connect to remote host: Connection refused 已解决,需要安装telent 服务,请查看下方的链接文章: htt ...

  3. UVA 10790 How Many Points of Intersection? 组合数学

    We have two rows. There are a dots on the top row and b dots on the bottom row. We draw line segment ...

  4. C#猜测识别文件编码

    项目 gitee地址:https://gitee.com/dhclly/IceDog.SmallProject/tree/master/src/IceDog.SmallProject.CodeConv ...

  5. 一个简单的利用 WebClient 异步下载的示例(五)(完结篇)

    接着上一篇,我们继续来优化.我们的 SkyParallelWebClient 可否支持切换“同步下载模式”和“异步下载模式”呢,好处是大量的代码不用改,只需要调用 skyParallelWebClie ...

  6. 机器学习(八)--------支持向量机 (Support Vector Machines)

    与逻辑回归和神经网络相比,支持向量机或者简称 SVM,更为强大. 人们有时将支持向量机看作是大间距分类器. 这是我的支持向量机模型代价函数 这样将得到一个更好的决策边界 理解支持向量机模型的做法,即努 ...

  7. JavaScript的__proto__、prototype和继承

    JavaScript也是可以“继承”的! 各位看官或是好奇,或是一知半解.什么是prototype,__proto__,constructor.哪种继承方式好.今天就在这交流交流. 什么是protot ...

  8. easyui 扩展 datagrid 数据网格视图

    效果如图: js代码: $("#tdg").datagrid({            width: 200,            url: "/Laboratory/ ...

  9. Flask--闪现、中间件、多app应用

    目录 闪现 源码 案例 中间件 自定义局部中间件 自定义全局装饰器 多app应用 闪现 flask提供了一个非常有用的flash()函数,它可以用来"闪现"需要提示给用户的消息,比 ...

  10. 开发技术--Python核心知识A

    开发|Python核心知识A A篇,主要介绍Python基础中列表,元祖,字典,集合,字符串,I/O,条件与循环,异常处理的底层知识与使用的注意事项. 希望大家对于Python会有新的收获,本篇不同于 ...