序列终结者 BZOJ 1251 Splay
题目背景
网上有许多题,就是给定一个序列,要你支持几种操作:A、B、C、D。一看另一道题,又是一个序列要支持几种操作:D、C、B、A。尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量……
这样我也出一道题,我出这一道的目的是为了让大家以后做这种题目有一个“库”可以依靠,没有什么其他的意思。
这道题目就叫序列终结者吧。
题目描述
给定一个长度为N的序列,每个序列的元素是一个整数(废话)。要支持以下三种操作:
- 将[L,R][L,R][L,R]这个区间内的所有数加上VVV。
- 将[L,R][L,R][L,R]这个区间翻转,比如
1 2 3 4变成4 3 2 1。 - 求[L,R][L,R][L,R]这个区间中的最大值。
最开始所有元素都是000。
输入输出格式
输入格式:
第一行两个整数N,M。M为操作个数。
以下MMM行,每行最多四个整数,依次为K,L,R,V。K表示是第几种操作,如果不是第1种操作则K后面只有两个数。
输出格式:
对于每个第3种操作,给出正确的回答。
输入输出样例
4 4
1 1 3 2
1 2 4 -1
2 1 3
3 2 4
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的更多相关文章
- 1251. 序列终结者【平衡树-splay】
Description 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这 ...
- BZOJ 1251 Splay维护序列
思路: splay维护序列的裸题 啊woc调了一天 感谢yzy大佬的模板-- //By SiriusRen #include <cstdio> #include <cstring&g ...
- BZOJ 1251 序列终结者(Splay)
题目大意 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术 ...
- BZOJ 1251: 序列终结者 [splay]
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3778 Solved: 1583[Submit][Status][Discu ...
- 【BZOJ】1251: 序列终结者(splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1251 不行..为什么写个splay老是犯逗,这次又是null的mx没有赋值-maxlongint.. ...
- bzoj 1251序列终结者 splay 区间翻转,最值,区间更新
序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 4594 Solved: 1939[Submit][Status][Discuss] De ...
- 【BZOJ】【1251】序列终结者
Splay 还是splay序列维护,这题我WA了的原因是:在Push_up的时候,当前子树的max我是直接取的L.R和v[x]的最大值,但是如果没有左/右儿子,默认是会访问0号结点的mx值,而这个值没 ...
- BZOJ 1251: 序列终结者
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 3773 Solved: 1579 [Submit][Status][Dis ...
- 「BZOJ1251」序列终结者 (splay 区间操作)
题面: 1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 5367 Solved: 2323[Submit][Status][D ...
随机推荐
- C# EntityFramwork(Model First)使用要点
本文介绍EntityFramework使用方法 Entity Framework的注意点 由于安装和操作的细节讲起来很琐碎,这部分只罗列出难点,其他细节请自行查阅 安装细节 Pluralize or ...
- Solaris10镜像情况下如何修复boot archive
在某些情况下(比如:异常宕机)solaris10的boot archive可能会损坏,导致solaris无法启动,此时需要手工修复boot archive. 本文通过模拟boot archive损坏, ...
- Delphi Android 询问框
Delphi Android 询问框 http://community.embarcadero.com/blogs/entry/xe7-dialog-box-methods-support-anony ...
- pl/sql对excel数据的导入和导出
本来这部分是在上篇pl/sql的,但笔者介于此篇的内容,就独立出来了, 1.导出查询结果到excel文件,在查询结果上右键,然后弹出选择框如下: 2.从excel向数据库中导入数据: a.创建要导入的 ...
- Linux split命令实例
曾经是否想要把一个大文件分割成多个小文件?比如一个5gb日志文件,我们需要把它分成多个小文件,这样我们才有可能使用普通的文本编辑器读取它.有时我们需要传输20gb的大文件到另一台服务器,这就需要我们把 ...
- 人工智能二之Sublime Text3环境配置
1.在Ubuntu中按CTRL+ALT+T打开命令窗口,按下面步骤和命令进行安装即可: 添加sublime text 3的仓库: sudo add-apt-repository ppa:webupd8 ...
- 数据存储的两种方式:Cookie 和Web Storage(转)
数据存储的两种方式:Cookie 和Web Storage 数据存储的两种方式:Cookie 和Web Storage 1.Cookie Cookie的作用就像你去超市购物时,第一次给你办张购物卡 ...
- Guava Cache本地缓存
Guava介绍 Guava是一种基于开源的Java库,其中包含谷歌正在由他们很多项目使用的很多核心库. 这个库是为了方便编码,并减少编码错误. 这个库提供用于集合,缓存,支持原语,并发性,常见注解,字 ...
- MSScriptControl详解(可实现在C#等语言中调用JAVASCRIPT代码)
ScriptControl接口 属性名称 类型 备注 AllowUI BOOL 检测是否允许运行用户的接口元素.如果为False,则诸如消息框之类的界面元素不可见. CodeObject Object ...
- C++ 面向对象: I/O对象的应用
1. 补充完整从text.txt文件读取数据后,再写入in.txt文件.2. 完成从text.txt文件读取数据后,进行排序,再写入in.txt文件.数据量自行设定.3. 请解决在主程序文件中加载多个 ...