题目背景

网上有许多题,就是给定一个序列,要你支持几种操作:A、B、C、D。一看另一道题,又是一个序列要支持几种操作:D、C、B、A。尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量……

这样我也出一道题,我出这一道的目的是为了让大家以后做这种题目有一个“库”可以依靠,没有什么其他的意思。

这道题目就叫序列终结者吧。

题目描述

给定一个长度为N的序列,每个序列的元素是一个整数(废话)。要支持以下三种操作:

  1. 将[L,R][L,R][L,R]这个区间内的所有数加上VVV。
  2. 将[L,R][L,R][L,R]这个区间翻转,比如1 2 3 4变成4 3 2 1
  3. 求[L,R][L,R][L,R]这个区间中的最大值。

最开始所有元素都是000。

输入输出格式

输入格式:

第一行两个整数N,MM为操作个数。

以下MMM行,每行最多四个整数,依次为K,L,R,VK表示是第几种操作,如果不是第1种操作则K后面只有两个数。

输出格式:

对于每个第3种操作,给出正确的回答。

输入输出样例

输入样例#1: 复制

4 4
1 1 3 2
1 2 4 -1
2 1 3
3 2 4
输出样例#1: 复制

2

splay Tree ;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#pragma GCC optimize(2)
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 100005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++) inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll qpow(ll a, ll b, ll c) {
ll ans = 1;
a = a % c;
while (b) {
if (b % 2)ans = ans * a%c;
b /= 2; a = a * a%c;
}
return ans;
} struct splay {
int fa, ch[2], size;
int lazy, rev, maxx, value;
}Sp[maxn]; int n, m, root, a[maxn];
void pushup(int rt) {
Sp[rt].size = Sp[Sp[rt].ch[0]].size + Sp[Sp[rt].ch[1]].size + 1;
Sp[rt].maxx = max(Sp[rt].value, max(Sp[Sp[rt].ch[0]].maxx, Sp[Sp[rt].ch[1]].maxx));
} void pushdown(int rt) {
if (Sp[rt].lazy) {
if (Sp[rt].ch[0]) {
Sp[Sp[rt].ch[0]].lazy += Sp[rt].lazy;
Sp[Sp[rt].ch[0]].maxx += Sp[rt].lazy;
Sp[Sp[rt].ch[0]].value += Sp[rt].lazy;
}
if (Sp[rt].ch[1]) {
Sp[Sp[rt].ch[1]].lazy += Sp[rt].lazy;
Sp[Sp[rt].ch[1]].maxx += Sp[rt].lazy;
Sp[Sp[rt].ch[1]].value += Sp[rt].lazy;
}
Sp[rt].lazy = 0;
}
if (Sp[rt].rev) {
if (Sp[rt].ch[0]) {
Sp[Sp[rt].ch[0]].rev ^= 1;
swap(Sp[Sp[rt].ch[0]].ch[0], Sp[Sp[rt].ch[0]].ch[1]);
}
if (Sp[rt].ch[1]) {
Sp[Sp[rt].ch[1]].rev ^= 1;
swap(Sp[Sp[rt].ch[1]].ch[0], Sp[Sp[rt].ch[1]].ch[1]);
}
Sp[rt].rev = 0;
}
} int id(int x) {
return Sp[Sp[x].fa].ch[1] == x;
}
void link(int son, int fa, int k) {
Sp[son].fa = fa; Sp[fa].ch[k] = son;
} void rotate(int x) {
int y = Sp[x].fa;
int z = Sp[y].fa;
int yk = id(x);
int zk = id(y);
int b = Sp[x].ch[yk ^ 1];
link(b, y, yk); link(y, x, yk ^ 1);
link(x, z, zk);
pushup(y); pushup(x);
} void SPLAY(int x, int aim) {
while (Sp[x].fa != aim) {
int y = Sp[x].fa;
int z = Sp[y].fa;
if (z != aim)id(x) == id(y) ? rotate(y) : rotate(x);
rotate(x);
}
if (aim == 0)root = x;
} int kth(int k) {
int now = root;
while (1) {
pushdown(now);
int left = Sp[now].ch[0];
if (Sp[left].size + 1 < k) {
k -= Sp[left].size + 1; now = Sp[now].ch[1];
}
else if (Sp[left].size >= k)now = left;
else return now;
}
} int build(int l, int r, int fa) {
if (l > r)return 0;
if (l == r) {
Sp[l].fa = fa; Sp[l].maxx = Sp[l].value = a[l];
Sp[l].size = 1; return l;
}
int mid = (l + r) >> 1;
Sp[mid].ch[0] = build(l, mid - 1, mid);
Sp[mid].ch[1] = build(mid + 1, r, mid);
Sp[mid].value = a[mid];
Sp[mid].fa = fa;
pushup(mid);
return mid;
} int split(int l, int r) {
l = kth(l); r = kth(r + 2);
SPLAY(l, 0); SPLAY(r, l);
return Sp[Sp[root].ch[1]].ch[0];
} void upd(int l, int r, int v) {
int now = split(l, r);
Sp[now].lazy += v; Sp[now].maxx += v; Sp[now].value += v;
pushup(Sp[root].ch[1]); pushup(root);
} void Reverse(int l, int r) {
int now = split(l, r);
Sp[now].rev ^= 1;
swap(Sp[now].ch[0], Sp[now].ch[1]);
pushup(Sp[root].ch[1]); pushup(root);
}
int query(int l, int r) {
return Sp[split(l, r)].maxx;
} int main()
{
//ios::sync_with_stdio(0);
rdint(n); rdint(m);
a[1] = a[n + 2] = Sp[0].maxx = -inf;
root = build(1, n + 2, 0);
int k, l, r, v;
while (m--) {
rdint(k); rdint(l); rdint(r);
if (k == 1) {
rdint(v); upd(l, r, v);
}
else if (k == 2)Reverse(l, r);
else if (k == 3)cout << query(l, r) << endl;
}
return 0;
}

序列终结者 BZOJ 1251 Splay的更多相关文章

  1. 1251. 序列终结者【平衡树-splay】

    Description 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这 ...

  2. BZOJ 1251 Splay维护序列

    思路: splay维护序列的裸题 啊woc调了一天 感谢yzy大佬的模板-- //By SiriusRen #include <cstdio> #include <cstring&g ...

  3. BZOJ 1251 序列终结者(Splay)

    题目大意 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术 ...

  4. BZOJ 1251: 序列终结者 [splay]

    1251: 序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 3778  Solved: 1583[Submit][Status][Discu ...

  5. 【BZOJ】1251: 序列终结者(splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1251 不行..为什么写个splay老是犯逗,这次又是null的mx没有赋值-maxlongint.. ...

  6. bzoj 1251序列终结者 splay 区间翻转,最值,区间更新

    序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 4594  Solved: 1939[Submit][Status][Discuss] De ...

  7. 【BZOJ】【1251】序列终结者

    Splay 还是splay序列维护,这题我WA了的原因是:在Push_up的时候,当前子树的max我是直接取的L.R和v[x]的最大值,但是如果没有左/右儿子,默认是会访问0号结点的mx值,而这个值没 ...

  8. BZOJ 1251: 序列终结者

    1251: 序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MB Submit: 3773  Solved: 1579 [Submit][Status][Dis ...

  9. 「BZOJ1251」序列终结者 (splay 区间操作)

    题面: 1251: 序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 5367  Solved: 2323[Submit][Status][D ...

随机推荐

  1. 11-28 网页基础--JavaScript(DOM)

    网页基础 第二部分--HTMLDOM操作 一.定义:htmlDOM是一种面向对象的树的模型,它包含html中的所有元素:通过html可以找到所有包含在dom中的元素. 二.作用: 1.查找html元素 ...

  2. 监控和安全运维 1.3 cacti增加客户端监控

    二. 安装客户端(增加一个linux服务器) 1.在客户端上 安装snmp yum install -y net-snmp 2. 修改snmpd.conf vim /etc/snmp/snmpd.co ...

  3. oracle系统函数(日期函数)

    oracle系统函数(日期函数) 调用日期函数时,首先要明确两个概念,时间戳和日期是不同的,日期中包括年月日但不包括小时分钟秒,时间戳包括年月日小时分钟秒.在oracle中,一般情况下,函数中包含da ...

  4. springmvc 中异常处理

    springmvc 中异常处理常见三种处理方式: 1:SimpleMappingExceptionResolver处理的是处理器方法里面出现的异常 2 3.自定义异常处理器:处理的是处理器方法里面出现 ...

  5. oracle -sqlplus 调用存储过程

    sp.batsqlplus user/passwd/sid  @.\sp.sql >sp.sqlexit; sp.sqlexex xxxx()quit;

  6. mongoDB的学习

    一:linux下安装mongoDB 1.在linux系统上安装MongoDB 上传安装包mongodb-linux-x86_64-3.0.6.tgz到linux系统的home目录下 tar -zxvf ...

  7. 基于IFC的大型三维城市群体——智慧城市模拟

  8. Java C++ Socket通讯

    import java.net.*; import javax.swing.plaf.SliderUI; /** * 与c语言通信(java做client,c/c++做server,传送一个结构) * ...

  9. 第一篇:Django基础

    Django框架第一篇基础 一个小问题: 什么是根目录:就是没有路径,只有域名..url(r'^$') 补充一张关于wsgiref模块的图片 一.MTV模型 Django的MTV分别代表: Model ...

  10. 学会使用postman工具模拟请求-----待补充

    登录: backstop 密码:backstop的密码 记得加上header,在swagger中有content-type. 请求,则是api下对应的请求. get请求直接加入链接即可. post请求 ...