luogu P4146 序列终结者
嘟嘟嘟
这是一道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;
}
luogu P4146 序列终结者的更多相关文章
- 【FHQ-Treap】P4146 序列终结者
题意: 给定一个序列,支持区间加,区间反转,区间max询问 裸的平衡树题,这里采用FHQ-Treap 每个节点多记录一个max值和两个lazy_tag,暴力Push_Down即可(大常数选手) 打完这 ...
- BZOJ 1251: 序列终结者 [splay]
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3778 Solved: 1583[Submit][Status][Discu ...
- [BZOJ1251]序列终结者
[BZOJ1251]序列终结者 试题描述 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题 ...
- 【BZOJ1251】序列终结者 Splay
一道模板题,一直没发现自己的快速读入读不了负数,我竟然能活到现在真是万幸. #include <iostream> #include <cstdio> #define inf ...
- BZOJ 1251: 序列终结者
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 3773 Solved: 1579 [Submit][Status][Dis ...
- BZOJ 1251 序列终结者(Splay)
题目大意 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术 ...
- 【BZOJ】1251: 序列终结者(splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1251 不行..为什么写个splay老是犯逗,这次又是null的mx没有赋值-maxlongint.. ...
- C++之路进阶——codevs4655(序列终结者)
4655 序列终结者 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 网上有许多题,就是给定一个序列,要你支持几 ...
- 【BZOJ1251】序列终结者
Description 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这 ...
随机推荐
- Spring-IOC实现【02-其他实现方式】
接上文Spring-IOC实现[01-XML配置方式] Java配置方式 SpringBoot流行之后,Java 配置开始被广泛使用. Java配置本质上,就是使用一个Java类去代替xml配置,这种 ...
- ExtJS中xtype一览
基本组件: xtype Class 描述 button Ext.Button 按钮 splitbutton Ext.SplitButton 带下拉菜单的按钮 cycle Ext.CycleButton ...
- 将不确定变为确定~老赵写的CodeTimer是代码性能测试的利器
首先,非常感谢赵老大的CodeTimer,它让我们更好的了解到代码执行的性能,从而可以让我们从性能的角度来考虑问题,有些东西可能我们认为是这样的,但经理测试并非如何,这正应了我之前的那名话:“机器最能 ...
- 加快maven中jar包的下载速度,maven镜像收集
maven下载jar包的默认仓库是http://my.repository.com/repo/path速度较慢,通过配置国内镜像提高下载速度 1.打开eclipse--->Window---&g ...
- pom.xml复制过来的代码报错-Maven expected START_TAG or END_TAG not TEXT (positionTEXT se
场景 编译器:IDEA 在网上看一些小实例,跟着做的时候会复制pom.xml文件的代码来加载依赖包.首先需要确定你复制过来的代码本身是没有错的,在复制一些pom.xml文件代码时,有时候会报错.原因是 ...
- Contest2075 - 湖南多校对抗(csu1576)大数 Catalan Square
Problem C: Catalan Square Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 42 Solved: 16[Submit][Stat ...
- C code example for strdup
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <malloc.h&g ...
- code for qint function
function [p,y,a] = qint(ym1,y0,yp1) %QINT - quadratic interpolation of three adjacent samples % % [p ...
- 实现Java Socket 客户端服务端交互实例
SocketService.java package socket; import java.io.BufferedReader; import java.io.IOException; import ...
- jQuery中是事件绑定方式--on、bind、live、delegate
概述:jQuery是我们最常用的js库,对于事件的绑定也是有很多种,on.one.live.bind.delegate等等,接下来我们逐一来进行讲解. 本片文章中事件所带的为版本号,例:v1.7+为1 ...