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 ...
随机推荐
- 使用git提交和拉取gitee的代码
使用git提交和拉取gitee的代码 1. 安装Git(自行摸索) 2. 在gitee新建仓库 名称和路径自己写 这两个二选一足矣 默认分支master就行 复制这个链接,待会要用 3. 新建项目目录 ...
- MySQL数据库4
内容概要 查询关键字 查询关键字之having过滤 查询关键字之distinct去重 查询关键字之order by排序 查询关键字之limit分页 查询关键字之regexp正则 多表查询思路 可视化软 ...
- LVS+keepalived高可用
1.keeplived相关 1.1工作原理 Keepalived 是一个基于VRRP协议来实现的LVS服务高可用方案,可以解决静态路由出现的单点故障问题. 在一个LVS服务集群中通常有主服务器(MAS ...
- c++ 平衡树
平衡树的性质 它其实就是一个 BST(Binary Search Tree 二叉搜索树). 当然,不同的平衡树会有自己的特性 BST 的性质 只有一个:任意一个节点的左子树的所有节点都比它的优先级高, ...
- Visual Studio 2010 ~ 2022 全系列密钥
更新记录 2022年6月10日 修改序列号顺序. Visual Studio 2022 Professional(专业版): TD244-P4NB7-YQ6XK-Y8MMM-YWV2J Enterpr ...
- 部署ASP.NET Core最简单的办法,使用IIS部署ASP.NET Core应用
本文迁移自Panda666原博客,原发布时间:2021年3月28日.写原文的时候.NET的最新版本是5.0,现在7的preview出来了,时间真快啊.抽空再写个在Windows Server Core ...
- javaweb获取客户端真实ip
在安全性要求较高的web项目中,我们经常有这样的需求: 黑名单:禁止指定ip访问. 白名单:允许指定ip访问. 根据ip追踪恶意入侵系统者. 在java中我们通常可以这样获取客户端ip地址: requ ...
- BUUCTF-RAR
rar 看提示知道爆破压缩包的题,纯数字4位数拿出ARCHPR爆破即可.
- SpringBoot + JWT + Redis 开源知识社区系统
「Java学习+面试指南」一份涵盖大部分 Java 程序员所需要掌握的核心知识.准备 Java 面试,首选 JavaGuide!:https://javaguide.cn/ 你好,我是 Guide!这 ...
- SAP 实例 1 Images in HTML
REPORT zharpo_010 NO STANDARD PAGE HEADING. TABLES : t001. TYPE-POOLS: slis. DATA : w_repid LIKE sy- ...