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.尤其是我们这里的某人,出模拟试题,居然还出了一道这 ...
随机推荐
- ElasticSearch入门简介
ElasticSearch是基于Apache Lucene的分布式搜索引擎, 提供面向文档的搜索服务.本文以6.2.3版本为例介绍ElasticSearch的应用. 本文首先介绍ElasticSear ...
- Java并发编程的艺术(一)
题目1 创建3个线程,让3个线程分别按着顺序打印AAAA,BBBB,CCCC(第一个线程打印AAAA,第二个线程打印BBBB,第一个线程始终在第二个线程之前打印) 代码(该代码为打印3个线程分别打印一 ...
- CMD下进入MYSQL的命令
CMD下进入MYSQL的命令 mysql -h localhost -u root -p 切到MYSQL的bin目录下,输入上面命令,回车 然后输入密码 回车
- ssm简单整合(注释方法)
1.创建web工程,选择web.xml文件,并导入相关jar包(使用的spring4.0以上版本,) 2.配置web.xml文件,包括spring.xml,springMVC.xml的加载映射,核心操 ...
- java基础-配置java的环境变量
学习java之前首先在https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html上面下载与 ...
- php导出excel再IE下乱码问题
$userBrowser = $_SERVER['HTTP_USER_AGENT']; //判断是否是ie内核 $fileName = '会员列表-'.date('Y-m-d', time()).'. ...
- 移动端安卓手机不能识别border 0.5px解决方案
由于安卓手机无法识别border 0.5px,因此我们要用0.5px的话必须要借助css3中的-webkit-transform:scale缩放来实现, 原理:将伪元素的宽设为200%,height设 ...
- python联系题1
一.有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? 程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. # _*_ ...
- JMeter 配置元件之计数器Counter
配置元件之计数器Counter by:授客 QQ:1033553122 测试环境 apache-jmeter-2.13 1. 计数器简介 允许用户创建一个在线程组范围之内都可以被引用的计数器. ...
- Jenkins 开启用户注册机制及用户权限设置
Jenkins 开启用户注册机制及用户权限设置 by:授客 QQ:1033553122 步骤 1. 系统管理-Configure Global Security 2. 设置