序列终结者 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 ...
随机推荐
- node install webpack -cli webpack4.xxxx
webpack 4.xx 版本 分离了 webpack-cli ; 安装webpack4.xx 需要再安装webpack-cli;
- hadoop block大小为128的原因
1.减轻了namenode的压力 原因是hadoop集群在启动的时候,datanode会上报自己的block的信息给namenode.namenode把这些信息放到内存中.那么如果块变大了,那么nam ...
- LAMP 2.5 Apache禁止解析php
某个目录下禁止解析 php,这个很有用,我们做网站安全的时候,这个用的很多,比如某些目录可以上传文件, 为了避免上传的文件有木马, 所以我们禁止这个目录下面的访问解析 php. 安装目录下有个data ...
- springmvc 路径问题
web项目中的相对路径可以分为二类: 1.以斜杠开头:以斜杠开头的又分为二类(分类依据是斜杠出现的位置):如果出现在java代码或者配置文件(xml,properties等),这个路径叫做后台路径. ...
- Go语言-变量和常量
我们在这里需要优先说明的是用于声明变量的关键字var,以及用于声明常量的关键字const.要知道,绝大多数的数据类型的值都可以被赋给一个变量,包括函数.而常量则不同,它只能被赋予基本数据类型的值本身. ...
- Navicat MySQL安装
1 下载安装包 点击下载安装包 2 解压安装包(解压后有三个文件) 第一个 Crack 是注册文件 第二个 chinese.rar 是汉化包 第三个 navicat_trial.exe 是安装程序 3 ...
- MinGW lapack 在windows 上安装
MinGW基本的配置环境 编译安装 方案一:MinGW Makefiles 我的配置好之后mingw文件夹下没有mingw32-make.exe,使用 mingw-get install mingw3 ...
- 长城防火墙(GFW)
一.简介 中国防火长城,官方名为金盾工程,是由政府运作的一个互联网审查监控项目.在其管辖互联网内部建立的多套网络审查系统的总称,包括相关行政审查系统.其英文名称Great Firewall of Ch ...
- C语言中的static的作用?
在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有三条. (1)第一个作用:隐藏. 当我们同时编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性.为理解这句话 ...
- 一个小错误,在for循环中,每次repaint()的时候,记得先把frame涂成白色的。等于擦掉原来的痕迹·。
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Animate { int x=1; in ...