Luogu P3273 [SCOI2011]棘手的操作(左偏树)
什么恶心东西啊,又水又烦
两个可并堆维护即可
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
#define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define ll long long
#define u32 unsigned int
#define u64 unsigned long long
#define ON_DEBUGG
#ifdef ON_DEBUGG
#define D_e_Line printf("\n----------\n")
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#include <ctime>
#define TIME() fprintf(stderr, "\ntime: %.3fms\n", clock() * 1000.0 / CLOCKS_PER_SEC)
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#define FileSave() ;
#define TIME() ;
//char buf[1 << 21], *p1 = buf, *p2 = buf;
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
#endif
using namespace std;
struct ios{
template<typename ATP>inline ios& operator >> (ATP &x){
x = 0; int f = 1; char ch;
for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
x *= f;
return *this;
}
}io;
template<typename ATP>inline ATP Max(ATP a, ATP b){
return a > b ? a : b;
}
template<typename ATP>inline ATP Min(ATP a, ATP b){
return a < b ? a : b;
}
template<typename ATP>inline ATP Abs(ATP a){
return a < 0 ? -a : a;
}
const int N = 3e5 + 7;
#define int long long
int n, tagAll, root;
#define ls ch[x][0]
#define rs ch[x][1]
int a[N];
struct LeftTree{
int ch[N][2], val[N], tag[N], fa[N], dis[N];
inline void Clear(int x) {
ch[x][0] = ch[x][1] = fa[x] = 0;
}
inline int Sum(int x) {
int sum = 0;
for(x = fa[x]; x; x = fa[x]) sum += tag[x];
return sum;
}
inline void Pushdown(int x) {
if(!tag[x]) return;
if(ls) val[ls] += tag[x], tag[ls] += tag[x];
if(rs) val[rs] += tag[x], tag[rs] += tag[x];
tag[x] = 0;
}
inline int Merge(int x, int y) {
if(!x || !y) return x | y;
if(val[x] < val[y]) Swap(x, y);
Pushdown(x);
rs = Merge(rs, y);
fa[ls] = fa[rs] = x;
if(dis[rs] > dis[ls]) Swap(ls, rs);
dis[x] = dis[rs] + 1;
return x;
}
inline int Find(int x) {
while(fa[x]) x = fa[x];
return x;
}
inline int Del(int x) {
Pushdown(x);
int father = fa[x];
int rt = Merge(ls, rs);
fa[rt] = father;
if(father) ch[father][x == ch[father][1]] = rt;
while(father){
if(dis[ch[father][0]] < dis[ch[father][1]])
Swap(ch[father][0], ch[father][1]);
if(dis[father] == dis[ch[father][1]] + 1)
return root;
dis[father] = dis[ch[father][1]] + 1;
rt = father;
father = fa[father];
}
return rt;
}
inline int Push(int x, int w) {
int father = Find(x);
if(father == x){
if(!ls && !rs){
val[x] += w;
return x;
}
else{
if(ls)
father = ls;
else
father = rs;
}
}
Del(x);
val[x] += w + Sum(x);
Clear(x);
return Merge(Find(father), x);
}
int q[N];
inline int Build() {
int h = 0, t = 0;
R(i,1,n){
q[++t] = i;
}
while(h != t){
int x = q[++h];
if(h == N - 5) h = 0;
int y = q[++h];
if(h == N - 5) h = 0;
q[++t] = Merge(x, y);
if(t == N - 5) t = 0;
}
return q[t];
}
}T, H;
#undef int
int main() {
#define int long long
//FileOpen();
io >> n;
T.dis[0] = H.dis[0] = -1;
R(i,1,n){
io >> a[i];
T.val[i] = H.val[i] = a[i];
}
root = H.Build();
int m;
io >> m;
char opt[7];
while(m--){
scanf("%s", opt + 1);
if(opt[1] == 'U'){
int x, y;
io >> x >> y;
x = T.Find(x), y = T.Find(y);
if(x == y) continue;
int tmp = T.Merge(x, y);
if(tmp == x)
root = H.Del(y);
else
root = H.Del(x);
}
else if(opt[1] == 'A' && opt[2] == '1'){
int x, w;
io >> x >> w;
root = H.Del(T.Find(x));
int tmp = T.Push(x, w);
H.val[tmp] = T.val[tmp];
H.Clear(tmp);
root = H.Merge(root, tmp);
}
else if(opt[1] == 'A' && opt[2] == '2'){
int x, w;
io >> x >> w;
int father = T.Find(x);
root = H.Del(father);
T.val[father] += w;
T.tag[father] += w;
H.val[father] = T.val[father];
H.Clear(father);
root = H.Merge(root, father);
}
else if(opt[1] == 'A' && opt[2] == '3'){
int w;
io >> w;
tagAll += w;
}
else if(opt[1] == 'F' && opt[2] == '1'){
int x;
io >> x;
printf("%lld\n", T.val[x] + tagAll + T.Sum(x));
}
else if(opt[1] == 'F' && opt[2] == '2'){
int x;
io >> x;
printf("%lld\n", T.val[T.Find(x)] + tagAll);
}
else{
printf("%lld\n", H.val[root] + tagAll);
}
}
return 0;
}
Luogu P3273 [SCOI2011]棘手的操作(左偏树)的更多相关文章
- 洛谷P3273 [SCOI2011] 棘手的操作 [左偏树]
题目传送门 棘手的操作 题目描述 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边,连接第x个节点和第y个节点 A1 ...
- 洛谷.3273.[SCOI2011]棘手的操作(左偏树)
题目链接 还是80分,不是很懂. /* 七个操作(用左偏树)(t2表示第二棵子树): 1.合并:直接合并(需要将一个t2中原有的根节点删掉) 2.单点加:把这个点从它的堆里删了,加了再插入回去(有负数 ...
- 2333: [SCOI2011]棘手的操作[离线线段树]
2333: [SCOI2011]棘手的操作 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2325 Solved: 909[Submit][Stat ...
- Luogu P4331 [BOI2004]Sequence 数字序列 (左偏树论文题)
清晰明了%%% Fairycastle的博客 个人习惯把size什么的存在左偏树结点内,这样在外面好写,在里面就是模板(只用修改update). 可以对比一下代码(好像也差不多-) MY CODE # ...
- 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 2483 K短路 (可持久化左偏树)
题面: 题目大意:给你一张有向图,求1到n的第k短路 $K$短路模板题 假设整个图的边集为$G$ 首先建出以点$n$为根的,沿反向边跑的最短路树,设这些边构成了边集$T$ 那么每个点沿着树边走到点$n ...
- 左偏树 / 非旋转treap学习笔记
背景 非旋转treap真的好久没有用过了... 左偏树由于之前学的时候没有写学习笔记, 学得也并不牢固. 所以打算写这么一篇学习笔记, 讲讲左偏树和非旋转treap. 左偏树 定义 左偏树(Lefti ...
随机推荐
- shellcode编写
shellcode编写 shellcode是一段用于利用软件漏洞而执行的代码,通常使用机器语言编写,其目的往往是让攻击者获得目标机器的命令行shell而得名,其他有类似功能的代码也可以称为shellc ...
- UniqueMergeTree:支持实时更新删除的 ClickHouse 表引擎
UniqueMergeTree 开发的业务背景 首先,我们看一下哪些场景需要用到实时更新. 我们总结了三类场景: 第一类是业务需要对它的交易类数据进行实时分析,需要把数据流同步到 ClickHouse ...
- 在 .NET 中使用 FixedTimeEquals 应对计时攻击
计时攻击 在计算机安全中,计时攻击(Timing attack)是旁道攻击 (Side-channel attack) 的一种,而旁道攻击是根据计算机处理过程发出的信息进行分析,包括耗时,声音,功耗等 ...
- Python列表推导式,字典推导式,元组推导式
参考:https://blog.csdn.net/A_Tu_daddy/article/details/105051821 my_list = [ [[1, 2, 3], [4, 5, 6]] ] f ...
- 合宙AIR105(三): 定时器, 定时器中断和PWM输出
目录 合宙AIR105(一): Keil MDK开发环境, DAP-Link 烧录和调试 合宙AIR105(二): 时钟设置和延迟函数 合宙AIR105(三): 定时器, 定时器中断和PWM输出 Ai ...
- nginx源码层面探究request_time、upstream_response_time、upstream_connect_time与upstream_header_time指标具体含义
背景概述 最近计划着重分析一下线上各api的HTTP响应耗时情况,检查是否有接口平均耗时.99分位耗时等相关指标过大的情况,了解到nginx统计请求耗时有四个指标:request_time.upstr ...
- python小题目练习(五)
题目:Python实现快速排序 代码实现: """Author:mllContent:Python实现快速排序Date:2020-11-05""&qu ...
- DBSync新增对MongoDB、ES的支持
数据库同步工具DBSync近日进行了升级,最新版本为V1.9,新增了对MongoDB.Elasticseach(ES)的支持,具体情况:1.支持同型库之间的同步,如:MongoDB至MongoDB,E ...
- 如何快速体验OneOS
随便逛了逛 之前有简单了解过OneOS,今天逛了下OneOS专区,发现官方终于也在宣传方面发力了啊,很多文章都非常专业,也有开发者在专区里面提问题了.也发现,部分开发者倒在了第一步,如何编译下载运行O ...
- 002 JWT令牌
1.JWT数据 Json Web Token 添加数据 --> 解析数据 --> 传递数据 1.1 JWT数据处传递流程 注意:JWT数据必须携带在请求头(Request Header)的 ...