0
点赞
收藏
分享

微信扫一扫

序列终结者 BZOJ 1251 Splay

覃榜言 2022-05-27 阅读 57

题目背景

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

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

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

题目描述

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

  1. 序列终结者 BZOJ 1251 Splay_ios
  2. 序列终结者 BZOJ 1251 Splay_#define_02
  3. 序列终结者 BZOJ 1251 Splay_ios_03

最开始所有元素都是序列终结者 BZOJ 1251 Splay_#define_04

输入输出格式

输入格式:

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

以下序列终结者 BZOJ 1251 Splay_#include_05

输出格式:

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

输入输出样例

输入样例#1: 复制

4 4
1 1 3 2
1 2 4 -1
2 1 3
3 2 4

输出样例#1: 复制

2


splay Tree ;

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#pragma GCC optimize(2)
//#include
//#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 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;
}

 

EPFL - Fighting

举报

相关推荐

0 条评论