[codevs4655] 序列终结者(Splay)
2024-08-28 21:41:51
支持操作:
1.区间加
2.区间翻转
3.区间求最大值
splay模板
注意:update 里更新 max 时需要取 3 个值的 Max
别忘了各种边界讨论
——代码
#include <cstdio>
#define ls son[now][0]
#define rs son[now][1] const int MAXN = , INF = 2e9;
int n, m, root, cnt;
int a[MAXN], size[MAXN], key[MAXN], add[MAXN], max[MAXN], rev[MAXN], f[MAXN], son[MAXN][]; inline void swap(int &x, int &y)
{
x ^= y ^= x ^= y;
} inline int Max(int x, int y)
{
return x > y ? x : y;
} inline int get(int x)
{
return x == son[f[x]][];
} inline void update(int now)
{
if(now)
{
size[now] = ;
if(ls) size[now] += size[ls];
if(rs) size[now] += size[rs]; max[now] = key[now];
if(ls) max[now] = Max(max[now], max[ls]);
if(rs) max[now] = Max(max[now], max[rs]);
}
} inline void pushdown(int now)
{
if(rev[now])
{
swap(ls, rs);
if(ls) rev[ls] ^= ;
if(rs) rev[rs] ^= ;
rev[now] = ;
}
if(add[now])
{
if(ls) add[ls] += add[now], key[ls] += add[now], max[ls] += add[now];
if(rs) add[rs] += add[now], key[rs] += add[now], max[rs] += add[now];
add[now] = ;
}
} inline void build(int x, int y, int fa, int &now)
{
if(x > y) return;
int mid = (x + y) >> ;
now = ++cnt;
f[now] = fa;
build(x, mid - , now, ls);
build(mid + , y, now, rs);
update(now);
} inline void rotate(int x)
{
pushdown(f[x]);
pushdown(x);
int old = f[x], oldf = f[old], wh = get(x); son[old][wh] = son[x][wh ^ ];
f[son[old][wh]] = old; if(oldf) son[oldf][old == son[oldf][]] = x;
f[x] = oldf; son[x][wh ^ ] = old;
f[old] = x; update(old);
update(x);
} inline void splay(int x, int to)
{
for(int fa; (fa = f[x]) != to; rotate(x))
if(f[fa] != to)
rotate(get(x) ^ get(fa) ? x : fa);
if(!to) root = x;
} inline int find(int x)
{
int now = root;
while()
{
pushdown(now);
if(x <= size[ls]) now = ls;
else
{
x -= size[ls];
if(x == ) return now;
x--;
now = rs;
}
}
} int main()
{
int i, k, x, y, v;
scanf("%d %d", &n, &m);
a[] = -INF, a[n + ] = -INF;
build(, n + , , root);
for(i = ; i <= m; i++)
{
scanf("%d %d %d", &k, &x, &y);
x = find(x);
y = find(y + );
splay(x, );
splay(y, x);
if(k == )
{
scanf("%d", &v);
add[son[son[root][]][]] += v;
max[son[son[root][]][]] += v;
key[son[son[root][]][]] += v;
update(son[root][]);
update(root);
}
else if(k == ) rev[son[son[root][]][]] ^= ;
else printf("%d\n", max[son[son[root][]][]]);
}
return ;
}
最新文章
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
- [ASP.NET MVC] ASP.NET Identity登入技术应用
- Spring MVC 学习总结(五)——校验与文件上传
- 蓝牙--对象交换协议(OBEX)
- Spring学习 Ioc篇(一 )
- Inno_setup制作升级包必须面临的几个问题
- 制作LiveCD
- Linux学习笔记22——线程属性(转)
- OC与JS的交互(iOS与H5混编)
- UML和模式应用3:迭代和进化式分析和设计案例研究
- Flask 目录
- 170704、springboot编程之CommandLineRunner
- JS中typeof和instanceof的用法和区别
- 语义分析:C语言表达式的语法树生成——Python实现
- 如何清理Macbook垃圾文件
- mac appstore应用下载失败,不能更新等等问题,都可以解决
- Spark RDD API详解之:Map和Reduce
- SqlServer报错:System.Data.SqlClient.SqlException
- c++原型模式(Prototype)
- HDU3853 概率DP