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 ...
随机推荐
- spring cloud 与spring boot 版本不匹配引发的问题总结
为了将前期项目慢慢转移到微服务上,今天开始搭建eureka服务时,出现以下错误: org.springframework.context.ApplicationContextException: Un ...
- eclipse激活jrebel
1.原本jrebel已经激活了,某天突然失效了.报错如下: JRebel: ERROR Failed to obtain seat. Unable to connect to license serv ...
- linux 查找被删除但是未被释放空间的文件 并释放资源
使用du -sh 和df -h 查看/data目录,发现结果相差一半,后来了解到: 使用rm命令删除文件时,只有当该文件不存在任何link才会被删除 当有进程访问这个文件时,这个文件的实际占用空间就 ...
- DAX 第七篇:分组聚合
DAX有三个用于生成分组聚合数据的函数,这三个函数有两个共同的特征:分组列和扩展列. 分组列是用于分组的列,只能来源于基础表中已存的列,分组列可以来源于同一个表,也可以来源于相关的列. 扩展列是由na ...
- 使用SolrJ客户端管理SolrCloud(Solr集群)
1.使用SolrJ客户端管理SolrCloud(Solr集群). package com.taotao.search.service; import java.io.IOException; impo ...
- C#工具类SqlServerHelper,基于System.Data.SqlClient封装
源码: using System; using System.Collections.Generic; using System.Data; using System.Linq; using Syst ...
- sql2008好看的字体
- SAP MM 公司间STO里外向交货单与内向交货单里序列号对应关系
SAP MM 公司间STO里外向交货单与内向交货单里序列号对应关系 笔者所在的A项目,后勤模块里有启用HU管理,序列号管理,批次管理等功能,以实现各个业务场景下的追溯. 公司间转储订单流程里,如果是整 ...
- android studio学习----android studio断点调试
先编译好要调试的程序. 1.设置断点 选定要设置断点的代码行,在行号的区域后面单击鼠标左键即可. 2.开启调试会话 点击红色箭头指向的小虫子,开始进入调试. IDE下方出现Debug视图,红色的箭头指 ...
- dos转unix
方式一 # yum install dos2unix.x86_64 # dos2unix file 方式二 查看样式: :set ff? //dos/unix 设置: :set fileformat= ...