题目背景

网上有许多题,就是给定一个序列,要你支持几种操作: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;
}

最新文章

  1. 【Django】--Model字段
  2. MongoDB学习系列(3)--解决MongoDB Unexpected Shutdown问题
  3. HDU 3111 Sudoku(精确覆盖)
  4. Android-Universal-Image-Loader的缓存处理机制
  5. Android 多国语言
  6. FusionCharts 3.2.1 flash 图表展示、数据钻取
  7. 收集的URL
  8. SQL Server Primary key 、clustered index 、 unique
  9. IceMx.Mvc 我的js MVC 框架七、完善植物大战僵尸(增加阳光的消费和获得)
  10. maven项目添加websocket
  11. 树上操作[HAOI 2015]
  12. HashMap源码阅读
  13. XMAN-level4
  14. 背水一战 Windows 10 (95) - 选取器: 自定义文件保存选取器
  15. 腾讯技术分享:微信小程序音视频技术背后的故事
  16. spring4笔记----使用装配注入合作者Bean的三种方式
  17. POJ 1230 Pass-Muraille
  18. Linux系统调用的运行过程【转】
  19. [漏洞分析]thinkphp 5.1.25 insert、insetAll、update方法注入
  20. Asp.Net Core异常处理整理

热门文章

  1. 新版本Ubuntu本地提权漏洞复现
  2. EF CODEFIRST WITH ORACLE 存储过程
  3. python dict.fromkeys()研究
  4. 【转载】eclipse如何传递main参数
  5. dos 下bat命令
  6. PagerSlidingTabStrip(viewPage滑动菜单)
  7. bootstrap媒体查询常用写法
  8. (内存地址hashcode与对象内容hashcode)分析== 和 equal()方法
  9. [tensorflow]异或门的实现
  10. C++数组对象和构造函数