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 ...
随机推荐
- CF Divan and Kostomuksha
题意:NKOJ CF 思路:首先发现贪心不了.因此dp.然后这题需要维护的就\(g_i\)和\(sum{g_i}\) 状态:\(dp[i]\): 当前最后一个为\(g_i\)的最大值 \(dp[i]= ...
- [第18届 科大讯飞杯 J] 能到达吗
能到达吗 题目链接:牛客5278 J 能到达吗 Description 给定一个 \(n\times m\) 的地图,地图的左上角为 \((1, 1)\) ,右下角为 \((n,m)\). 地图上有 ...
- 【原创】项目一GoldenEye
实战流程 1,通过nmap查找本段IP中存活的机器 ┌──(root㉿whoami)-[/home/whoami/Desktop] └─# nmap -sP 192.168.186.0/24 排查网关 ...
- vscode远程调试c++
0.背景 最近在学习linux webserver开发,需要在linux下调试自己的C/C++代码,但是linux下不像在windows下,直接Visio Studio或者其它集成开发环境那么方便,现 ...
- 【RocketMQ】MQ消息发送
消息发送 首先来看一个RcoketMQ发送消息的例子: @Service public class MQService { @Autowired DefaultMQProducer defaultMQ ...
- html关键字大全
html标签属性大全 html标签属性大全从网上搜集整理的常用html标签,供朋友们交流学习html用. html标签<marquee> <marquee>...</ma ...
- Linux用户、组 管理
Linux安全模型 3A: Authentication:认证,验证用户身份 Authorization:授权,不同的用户设置不同权限 Accouting|Audition:审计 Linux用户: 管 ...
- Redis布隆过滤器和布谷鸟过滤器
一.过滤器使用场景:比如有如下几个需求:1.原本有10亿个号码,现在又来了10万个号码,要快速准确判断这10万个号码是否在10亿个号码库中? 解决办法一:将10亿个号码存入数据库中,进行数据库查询,准 ...
- idea部署项目运行没问题,但是页面404。
解决方案: 这个位置不要添加内容. 参考:https://blog.csdn.net/hupixiong/article/details/105443606
- Spring框架系列(14) - SpringMVC实现原理之DispatcherServlet处理请求的过程
前文我们有了IOC的源码基础以及SpringMVC的基础,我们便可以进一步深入理解SpringMVC主要实现原理,包含DispatcherServlet的初始化过程和DispatcherServlet ...