1. 程式人生 > >「BZOJ1251」序列終結者 (splay 區間操作)

「BZOJ1251」序列終結者 (splay 區間操作)

bit pac using == sample tag fin || find

題面:

1251: 序列終結者

Time Limit: 20 Sec Memory Limit: 162 MB
Submit: 5367 Solved: 2323
[Submit][Status][Discuss]

Description

網上有許多題,就是給定一個序列,要你支持幾種操作:A、B、C、D。一看另一道題,又是一個序列 要支持幾種操作:D、C、B、A。尤其是我們這裏的某人,出模擬試題,居然還出了一道這樣的,真是沒技術含量……這樣 我也出一道題,我出這一道的目的是為了讓大家以後做這種題目有一個“庫”可以依靠,沒有什麽其他的意思。這道題目 就叫序列終結者吧。 【問題描述】 給定一個長度為N的序列,每個序列的元素是一個整數(廢話)。要支持以下三種操作: 1. 將[L,R]這個區間內的所有數加上V。 2. 將[L,R]這個區間翻轉,比如1 2 3 4變成4 3 2 1。 3. 求[L,R]這個區間中的最大值。 最開始所有元素都是0。

Input

第一行兩個整數N,M。M為操作個數。 以下M行,每行最多四個整數,依次為K,L,R,V。K表示是第幾種操作,如果不是第1種操作則K後面只有兩個數。

Output

對於每個第3種操作,給出正確的回答。

Sample Input

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

Sample Output

2
【數據範圍】
N<=50000,M<=100000。
思路: 參考hzwer博客 一開始一直沒看懂平衡樹到底是怎麽維護的,後面看了下其他人博客,平衡樹維護的不是單個的數,而是區間。 實現代碼:
#include<bits/stdc++.h>
using namespace std;
const int M = 5e4+10;
const int inf = 0x3f3f3f;
int n,m,sz,rt;
int c[M][2],fa[M],id[M],tag[M],v[M],mx[M],siz[M];
bool rev[M];
inline void pushup(int k){
    int l = c[k][0],r = c[k][1];
    mx[k] = max(max(mx[l],mx[r]),v[k]);
    siz[k] 
= siz[l] + siz[r] + 1; } void pushdown(int k){ int l = c[k][0],r = c[k][1],t = tag[k]; if(t){ tag[k] = 0; if(l) tag[l]+=t,mx[l]+=t,v[l]+=t; if(r) tag[r]+=t,mx[r]+=t,v[r]+=t; } if(rev[k]){ rev[k] = 0; rev[l]^=1; rev[r]^=1; swap(c[k][0],c[k][1]); } } void rotate(int x,int &k){ int y = fa[x],z = fa[y],l,r; if(c[y][0] == x) l = 0; else l = 1; r = l^1; if(y == k) k = x; else { if(c[z][0]==y) c[z][0]=x; else c[z][1] = x; } fa[x] = z;fa[y] = x;fa[c[x][r]]=y; c[y][l]=c[x][r]; c[x][r]=y; pushup(y); pushup(x); } void splay(int x,int &k){ while(x != k){ int y = fa[x],z = fa[y]; if(y != k){ if(c[y][0]==x^c[z][0]==y)rotate(x,k); else rotate(y,k); } rotate(x,k); } } int Find(int k,int rk){ if(tag[k]||rev[k]) pushdown(k); int l = c[k][0],r = c[k][1]; if(siz[l]+1==rk) return k; else if(siz[l]>=rk) return Find(l,rk); else return Find(r,rk-siz[l]-1); } inline void update(int l,int r,int val){ int x = Find(rt,l),y = Find(rt,r+2); splay(x,rt); splay(y,c[x][1]); int z = c[y][0]; tag[z] += val;v[z] += val; mx[z] += val; } inline void rever(int l,int r){ int x = Find(rt,l),y = Find(rt,r+2); splay(x,rt); splay(y,c[x][1]); int z = c[y][0]; rev[z] ^= 1; } inline void query(int l,int r){ int x = Find(rt,l),y = Find(rt,r+2); splay(x,rt); splay(y,c[x][1]); int z = c[y][0]; printf("%d\n",mx[z]); } inline void build(int l,int r,int f){ if(l > r) return ; int now = id[l],last = id[f]; if(l == r){ fa[now] = last;siz[now]=1; if(l < f) c[last][0] = now; else c[last][1] = now; return ; } int mid = (l + r) >> 1; now = id[mid]; build(l,mid-1,mid); build(mid+1,r,mid); fa[now] = last; pushup(now); if(mid < f) c[last][0] = now; else c[last][1] = now; } int main() { mx[0] = -inf; scanf("%d%d",&n,&m); for(int i = 1;i <= n+2;i ++) id[i] = ++sz; build(1,n+2,0); rt = (n + 3) >> 1; for(int i = 1;i <= m;i ++){ int f,l,r,val; scanf("%d",&f); scanf("%d%d",&l,&r); if(f == 1) scanf("%d",&val),update(l,r,val); if(f == 2) rever(l,r); if(f == 3) query(l,r); } return 0; }

「BZOJ1251」序列終結者 (splay 區間操作)