救救萌新吧,WBLT 10 分
查看原帖
救救萌新吧,WBLT 10 分
52381
CodingJellyfish楼主2021/10/6 09:33
// WBLT 真不错

#include <ctype.h>
#include <stdio.h>
#include <string.h>

// IO

#define IO 10000
char _ibuf[IO]  , _obuf[IO];
char *_i = _ibuf, *_o = _obuf;

void initIO(void)
{
    fread(_ibuf, sizeof(char), IO, stdin);
}

void endIO(void)
{
    fwrite(_obuf, sizeof(char), _o - _obuf, stdout);
}

static inline char getch(void)
{
    if (_i == _ibuf + IO)
    {
        memset(_ibuf, 0, sizeof(_ibuf));
        fread(_ibuf, sizeof(char), IO, stdin);
        _i = _ibuf;
    }
    return *_i++;
}

static inline void putch(char ch)
{
    if (_o == _obuf + IO)
    {
        fwrite(_obuf, sizeof(char), IO, stdout);
        _o = _obuf;
    }
    *_o++ = ch;
}

int getint(void)
{
    int num = 0, fac = 1;
    char ch = getch();
    
    while (!isdigit(ch))
    {
        if (ch == '-')
            fac = -1;
        ch = getch();
    }
    while (isdigit(ch))
    {
        num = num * 10 + ch - '0';
        ch = getch();
    }
    return num * fac;
}

void putint(int val)
{
    if (val < 0)
    {
        val = -val;
        putch('-');
    }
    if (val >= 10)
        putint(val / 10);
    putch(val % 10 + '0');
}

void readstr(char *str)
{
    char ch = getch();
    
    while (!isgraph(ch))
        ch = getch();
    
    while (isgraph(ch))
    {
        *str++ = ch;
        ch = getch();
    }
    *str = '\0';
}

// Consts

#define RPOOL 1000005
#define RN    1000005

// Defs

typedef struct
{
    int sum;
    int lmx;
    int rmx;
    int mx;
}
Query;

// Utility

static inline int min32(int a, int b)
{
    int diff = b - a;
    return a + (diff & (diff >> 31));
}

static inline int max32(int a, int b)
{
    int diff = b - a;
    return b - (diff & (diff >> 31));
}

#define SWAP(T, a, b) ({T t = a; a = b; b = t;})

// Pools

typedef struct
{
    Query val;
    
    
    int   siz;
    int   ch[2];
    char  tag;
    int   lazy;
}
WBLTNode;

WBLTNode wblt_pool[RPOOL];
int      wpsiz;

#define wnode(x) wblt_pool[x]
#define wval(x) wblt_pool[x].val
#define wsiz(x) wblt_pool[x].siz
#define wlch(x) wblt_pool[x].ch[0]
#define wrch(x) wblt_pool[x].ch[1]
#define wtag(x) wblt_pool[x].tag
#define wlazy(x) wblt_pool[x].lazy

int wstk[RPOOL];
int wtop;

#define wempty (WBLTNode){(Query){0,0,0,0},0,{0,0},0,1001}
#define wnew() (wtop ? (wnode(wstk[wtop]) = wempty, wstk[wtop--]) : ++wpsiz)
#define wdel(x) (wstk[++wtop] = (x))

// WBLT

static inline void mergeq_assign(Query *a, Query* l, Query* r)
{
    a->sum = l->sum + r->sum;
    
    a->lmx = max32(l->lmx, l->sum + r->lmx);
    a->rmx = max32(r->rmx, r->sum + l->rmx);
                        
    a->mx  = max32(max32(l->mx, r->mx), l->rmx + r->lmx);
}

static inline void upw(int x)
{
    if (wlch(x))
    {
        mergeq_assign(&wval(x), &wval(wlch(x)), &wval(wrch(x)));
        wsiz(x) = wsiz(wlch(x)) + wsiz(wrch(x));
    }
}

static inline void downw(int x)
{
    if (wtag(x))
    {
        SWAP(int, wlch(wlch(x)), wrch(wlch(x)));
        SWAP(int, wval(wlch(x)).lmx, wval(wlch(x)).rmx);
        
        SWAP(int, wlch(wrch(x)), wrch(wrch(x)));
        SWAP(int, wval(wrch(x)).lmx, wval(wrch(x)).rmx);
        
        wtag(wlch(x)) ^= 1;
        wtag(wrch(x)) ^= 1;
        wtag(x)        = 0;
    }
    
    if (wlazy(x) != 1001)
    {
        wlazy(wlch(x)) = wlazy(x);
        wlazy(wrch(x)) = wlazy(x);
        int v1 = wsiz(wlch(x)) * wlazy(x);
        int v2 = wsiz(wrch(x)) * wlazy(x);
            
        if (wlazy(x) > 0)
        {    
            wval(wlch(x)) = (Query){v1, v1, v1, v1};
            wval(wrch(x)) = (Query){v2, v2, v2, v2};
        }
        else
        {
            wval(wlch(x)) = (Query){v1, wlazy(x), wlazy(x), wlazy(x)};
            wval(wrch(x)) = (Query){v2, wlazy(x), wlazy(x), wlazy(x)};
        }
        wlazy(x) = 1001;
    }
}

static inline int addw(int ind, int x)
{
    wnode(ind) = (WBLTNode){(Query){x, x, x, x}, 1, {0, 0}, 0, 1001};
    return ind;
}

static inline int jointw(int ind, int a, int b)
{
    wlch(ind) = a;
    wrch(ind) = b;
    upw(ind);
    return ind;
}

static inline void lcutw(int x)
{
    int tmp = wlch(wlch(x));
    wrch(x) = jointw(wlch(x), wrch(wlch(x)), wrch(x));
    wlch(x) = tmp;
}

static inline void rcutw(int x)
{
    int tmp = wrch(wrch(x));
    wlch(x) = jointw(wrch(x), wlch(x), wlch(wrch(x)));
    wrch(x) = tmp;
}

static inline void balancew(int x)
{
    if (wsiz(wlch(x)) > 3 * wsiz(wrch(x)))
    {
        downw(wlch(x));
        
        if (wsiz(wrch(wlch(x))) > 2 * wsiz(wlch(wlch(x))))
            downw(wrch(wlch(x))), rcutw(wlch(x));
            
        lcutw(x);
    }
    else if (wsiz(wrch(x)) > 3 * wsiz(wlch(x)))
    {
        downw(wrch(x));
        
        if (wsiz(wlch(wrch(x))) > 2 * wsiz(wrch(wrch(x))))
            downw(wlch(wrch(x))), lcutw(wrch(x));
            
        rcutw(x);
    }
}

static inline void debugw(int x)
{
    
    printf("%d %d %d %d %d %d %d\n", x, wval(x).mx, wval(x).sum, 
                               wsiz(x), wlazy(x), wlch(x), wrch(x));
    if (!wlch(x)){return;}
    debugw(wlch(x));
    debugw(wrch(x));
}

static inline int buildw(int x, int l, int r, int *arr)
{
    wlazy(x) = 1001;
    
    if (l == r)
    {
        return addw(x, arr[l]);
    }
    
    int mid = l + ((r - l) >> 1);
    
    wlch(x) = buildw(wnew(), l, mid, arr);
    wrch(x) = buildw(wnew(), mid + 1, r, arr);
    
    upw(x);
    
    return x;
}

static inline void splitw(int x, int val, int *lx, int *rx)
{
    if (!wlch(x))
    {
        *lx = *rx = 0;
        val ? (*lx = x) : (*rx = x);
        return;
    }
    downw(x);
    
    if (wsiz(wlch(x)) <= val)
        *lx = x, splitw(wrch(x), val - wsiz(wlch(x)), &wrch(x), rx);
    else
        *rx = x, splitw(wlch(x), val, lx, &wlch(x));
    
         if (!wlch(x)) wdel(wrch(x)), wnode(x) = wnode(wrch(x));
    else if (!wrch(x)) wdel(wlch(x)), wnode(x) = wnode(wlch(x));
    
    balancew(x);
    upw(x);
}

static inline void recyclew(int x)
{
    wdel(x);
    
    if (!wlch(x)) return;
    
    recyclew(wlch(x));
    recyclew(wrch(x));
}

static inline int mergew(int l, int r)
{
    int ind = wnew();
    wlazy(ind) = 1001;
    wlch(ind) = l;
    wrch(ind) = r;
    balancew(ind);
    upw(ind);
    return ind;
}

static inline void modw(int cl, int cr, int val, int x, int l, int r)
{
    if (l >= cl && r <= cr)
    {
        wlazy(x) = val;
        int v1 = wsiz(x) * val;
        if (val > 0) 
            wval(x) = (Query){v1, v1, v1, v1};
        else
            wval(x) = (Query){v1, val, val, val};
        return;
    }
    downw(x);
    
    int mid = l + wsiz(wlch(x)) - 1;
    
    if (mid >= cl)
        modw(cl, cr, val, wlch(x), l, mid);
    if (mid < cr)
        modw(cl, cr, val, wrch(x), mid + 1, r);
    
    upw(x);
}

static inline int qsumw(int cl, int cr, int x, int l, int r)
{
    if (l >= cl && r <= cr)
    {
        return wval(x).sum;
    }
    downw(x);
    
    int mid = l + wsiz(wlch(x)) - 1;
    int ret = 0;
    
    if (mid >= cl)
        ret += qsumw(cl, cr, wlch(x), l, mid);
    if (mid < cr)
        ret += qsumw(cl, cr, wrch(x), mid + 1, r);

    return ret;
}

int addbuf[RN];

int main(void)
{
    initIO();
    
    int n = getint(), m = getint();
    
    for (int i = 1; i <= n; i++)
        addbuf[i] = getint();
    
    int root = buildw(wnew(), 0, n + 1, addbuf);
    char oprbuf[15];
    
    for (int i = 1; i <= m; i++)
    {
        int lroot, rroot, a, b, c;
        readstr(oprbuf);
        
        switch(oprbuf[2])
        {
            case 'S':
            
            a = getint(), b = getint();
            
            for (int i = 1; i <= b; i++)
                addbuf[i] = getint();
            
            splitw(root, a + 1, &lroot, &rroot);
            
            root = buildw(wnew(), 1, b, addbuf);
            n += b;
            
            root = mergew(lroot, root);
            root = mergew(root, rroot);
            
            break;
            case 'L':
            
            a = getint(), b = getint();
            
            splitw(root, a, &lroot, &root);
            splitw(root, b, &root, &rroot);
            
            recyclew(root);
            n -= b;
            
            root = mergew(lroot, rroot);
            
            break;
            case 'K':
            
            a = getint(), b = getint(), c = getint();
            
            modw(a, a + b - 1, c, root, 0, n + 1);
            
            break;
            case 'V':
            
            a = getint(), b = getint();
            
            splitw(root, a, &lroot, &root);
            splitw(root, b, &root, &rroot);
            
            wtag(root) ^= 1;
            SWAP(int, wlch(root), wrch(root));
            SWAP(int, wval(root).lmx, wval(root).rmx);
            
            root = mergew(lroot, root);
            root = mergew(root, rroot);
            
            break;
            case 'T':
            
            a = getint(), b = getint();
            
            putint(qsumw(a, a + b - 1, root, 0, n + 1));
            putch('\n');
            
            break;
            case 'X':
            
            putint(wval(root).mx);
            putch('\n');
            
            break;
        }
    }
    
    endIO();
    return 0;
}
2021/10/6 09:33
加载中...