首页 技术 正文
技术 2022年11月19日
0 收藏 625 点赞 3,608 浏览 2940 个字

嘟嘟嘟


这是一道splay基础题。

最坑的一点是,因为有些节点可能没有左儿子或右儿子,所以必须把t[0].Max赋成-INF!

因为这个调了半天,看来回头复习复习splay是对的……

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 5e4 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}int n, m, a[maxn];
struct Tree
{
int ch[2], fa;
int val, siz, Max;
int add, rev;
}t[maxn];
int root, tcnt = 0;In void _PrintTr(int now)
{
if(!now) return;
printf("now:%d val:%d Max:%d ls:%d rs:%d\n", now, t[now].val, t[now].Max, t[now].ch[0], t[now].ch[1]);
_PrintTr(t[now].ch[0]); _PrintTr(t[now].ch[1]);
}
void _Travel(int now, int dep)
{
if(t[now].ch[1]) _Travel(t[now].ch[1], dep + 1);
for(int i = 1; i < dep; ++i) putchar('\t');
printf("\033[36m%d %d\033[0m\n", t[now].val, t[now].Max);
if(t[now].ch[0]) _Travel(t[now].ch[0], dep + 1);}In void c_rev(int now)
{
swap(t[now].ch[0], t[now].ch[1]);
t[now].rev ^= 1;
}
In void c_add(int now, int lzy)
{
t[now].val += lzy; t[now].Max += lzy;
t[now].add += lzy;
}
In void pushdown(int now)
{
if(t[now].rev)
{
if(t[now].ch[0]) c_rev(t[now].ch[0]);
if(t[now].ch[1]) c_rev(t[now].ch[1]);
t[now].rev = 0;
}
if(t[now].add)
{
if(t[now].ch[0]) c_add(t[now].ch[0], t[now].add);
if(t[now].ch[1]) c_add(t[now].ch[1], t[now].add);
t[now].add = 0;
}
}
In void pushup(int now)
{
t[now].siz = t[t[now].ch[0]].siz + t[t[now].ch[1]].siz + 1;
t[now].Max = max(max(t[t[now].ch[0]].Max, t[t[now].ch[1]].Max), t[now].val);
}
In void rotate(int x)
{
int y = t[x].fa, z = t[y].fa, k = (t[y].ch[1] == x);
t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z;
t[y].ch[k] = t[x].ch[k ^ 1]; t[t[x].ch[k ^ 1]].fa = y;
t[x].ch[k ^ 1] = y; t[y].fa = x;
pushup(y), pushup(x);
}
In void splay(int x, int s)
{
while(t[x].fa != s)
{
int y = t[x].fa, z = t[y].fa;
if(z != s) rotate(((t[y].ch[0] == x) ^ (t[z].ch[0] == y)) ? x : y);
rotate(x);
}
if(!s) root = x;
}In int build(int L, int R, int _f)
{
if(L > R) return 0;
int mid = (L + R) >> 1, now = ++tcnt;
t[now].fa = _f;
t[now].Max = t[now].val = a[mid];
t[now].ch[0] = build(L, mid - 1, now);
t[now].ch[1] = build(mid + 1, R, now);
pushup(now);
return now;
}
In int getRank(int k)
{
int now = root;
while(1)
{
pushdown(now);
if(t[t[now].ch[0]].siz >= k) now = t[now].ch[0];
else if(t[t[now].ch[0]].siz + 1 == k) return now;
else k -= (t[t[now].ch[0]].siz + 1), now = t[now].ch[1];
}
}
In void split(int L, int R)
{
int a = getRank(L - 1), b = getRank(R + 1);
splay(a, 0); splay(b, a);
pushdown(root), pushdown(t[root].ch[1]);
}int main()
{
n = read(); m = read();
a[1] = -INF; a[n + 2] = -INF;
root = build(1, n + 2, 0);
t[0].Max = -INF;
for(int i = 1; i <= m; ++i)
{
int op = read(), L = read() + 1, R = read() + 1, v;
split(L, R);
if(op == 1) v = read(), c_add(t[t[root].ch[1]].ch[0], v), pushup(t[root].ch[1]), pushup(root);
else if(op == 2) c_rev(t[t[root].ch[1]].ch[0]), pushup(t[root].ch[1]), pushup(root);
else write(t[t[t[root].ch[1]].ch[0]].Max), enter;
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893