God-level data structure maintenance questions ... Splay practiced hand problem.
#include <cstdlib> #include <cstdio> #include <cstring> #include <cctype> #include < algorithm> #include <iostream> #include <queue> #define REP (i, L, R) for (int i=l; i<=r; i++) #define CLR (x , c) memset (x, C, sizeof (x)) #define INF 1000000000#define maxn 1000005using namespace Std;inline int read () {int x=0, f=1; Char Ch=getchar (), while (!isdigit (CH)) {if (ch== '-') f=-1; Ch=getchar ();} while (IsDigit (CH)) x=x*10+ch-' 0 ', Ch=getchar (); return x*f;} int c[maxn][2], H[MAXN], SIZE[MAXN], SUM[MAXN], LS[MAXN], RS[MAXN], MS[MAXN], V[MAXN], RT, ID[MAXN], A[maxn];int Cnt;char S[10];bool REV[MAXN], tag[maxn];queue <int> q;inline void Update (int x) {int l=c[x][0], r=c[x][1];sum[x]=sum[l]+ Sum[r]+v[x];size[x]=size[l]+size[r]+1;ms[x]=max (Ms[l], ms[r]); Ms[x]=max (Ms[x], rs[l]+v[x]+ls[r]); Ls[x]=max (Ls[l) , Sum[l]+v[x]+ls[r]); Rs[x]=max (Rs[r], rs[l]+v[x]+sum[r]);} void pushdown (int x) {int l=c[x][0], r=c[x][1];if (Tag[x]) {rev[x]=tag[x]=0;if (L) tag[l]=1, v[l]=v[x], sum[l]=V[l]*size[l];if (R) tag[r]=1, V[r]=v[x], sum[r]=v[r]*size[r];if (v[x]>=0) {if (L) ls[l]=rs[l]=ms[l]=sum[l];if (R) ls[ R]=RS[R]=MS[R]=SUM[R];} else {if (l) ls[l]=rs[l]=0, Ms[l]=v[l];if (R) ls[r]=rs[r]=0, Ms[r]=v[r];}} if (Rev[x]) {rev[x]^=1; rev[l]^=1; Rev[r]^=1;swap (Ls[l], rs[l]), swap (Ls[r], rs[r]), swap (c[l][0], c[l][1]); Swap (c[r][0 ], c[r][1]);}} inline void rotate (int x, int &k) {int y=h[x], z=h[y], l= (c[y][1]==x), r=l^1;if (y==k) k=x; else c[z][c[z][1]==y]=x;h[c [x] [R]] =y, H[y]=x, H[x]=z;c[y][l]=c[x][r]; C[x][r]=y;update (y), update (x);} inline void splay (int x, int &k) {/* while (x!=k) {int y=h[x], z=h[y];if (y!=k) {(c[y][0]==x) ^ (c[z][0]==y)? Rotate (x, k) : Rotate (y, k);} Rotate (x, k);} */while (x!=k) rotate (x, k);} int find (int x, int k) {pushdown (x); int l=c[x][0], r=c[x][1];if (size[l]+1==k) return x;if (size[l]>=k) return find (L, K ); return Find (R, k-size[l]-1);} void Del (int x) {if (!x) Return;int l=c[x][0], r=c[x][1];D El (L); Del (R); Q.push (x); h[x]=c[x][0]=c[x][1]=0;tag[x]=rev[x]=0;} inline int Split (int k, int n) {int x=find (RT, K), Y=find (RT, k+n+1); Splay (x, RT); Splay (y, c[x][1]); return c[y][0];} inline void qsum (int k, int n) {int x=split (k, n); printf ("%d\n", Sum[x]);} inline void Same (int k, int n, int val) {int x=split (k, n), Y=h[x];v[x]=val; tag[x]=1; sum[x]=size[x]*val;if (val>=0) LS [X]=rs[x]=ms[x]=sum[x]; else ls[x]=rs[x]=0, Ms[x]=val;update (y), update (H[y]);} inline void rever (int k, int n) {int x=split (k, N), Y=h[x];if (Tag[x]) Return;rev[x]^=1;swap (c[x][0], c[x][1]); swap (ls[x], RS[X]); Update (y), update (H[y]);} inline void Delete (int k, int n) {int x=split (k, N), Y=h[x];D el (x); c[y][0]=0; update (y), update (H[y]);} void Build (int l, int r, int f) {if (l>r) Return;int mid= (l+r) >>1, Now=id[mid], last=id[f];if (l==r) {sum[now]=a[l] , Size[now]=1, Tag[now]=rev[now]=0;if (a[l]>=0) ls[now]=rs[now]=ms[now]=a[l]; else ls[now]=rs[now]=0, ms[now]=a[l];} else build (L, Mid-1, mid), build (Mid+1, R, Mid), V[now]=a[mid], h[now]=last, update (now); C[last][mid>f]=noW;} inline void Insert (int k, int n) {Rep (i, 1, N) a[i]=read (), Rep (i, 1, N) if (!q.empty ()) Id[i]=q.front (), Q.pop (); Else id[i ]=++cnt; Build (1, n, 0); int X=find (RT, k+1), Y=find (RT, k+2), z=id[(1+n) >>1]; Splay (x, RT); Splay (y, c[x][1]); h[z]=y, C[y][0]=z;update (y), update (x);} int main () {int n=read (), M=read (), Ms[0]=a[1]=a[n+2]=-inf;rep (i, 1, N) a[i+1]=read (); Rep (i, 1, n+2) id[i]=i; Build (1, n+2, 0), rt= (n+3) >>1, Cnt=n+2;rep (i, 1, m) {scanf ("%s", s), if (s[0]== ' I ') {int a=read (), B=read (); Insert (A, b);} else if (s[0]== ' D ') {int a=read (), B=read (); Delete (A, b);} else if (s[0]== ' R ') {int a=read (), B=read (); Rever (A, b);} else if (s[0]== ' G ') {int a=read (), B=read (); Qsum (A, b);} else if (s[2]== ' K ') {int a=read (), B=read (); Same (A, B, read ());} elseprintf ("%d\n", Ms[rt]);} return 0;}
BZOJ-1500 [NOI2005] repair sequence