Splay运用——区间和

· · 题解

        (对Splay轻车熟路的各位大佬就可以跳过本帖了)

前置知识

        如果不知道Splay是什么,请移步洛谷日报第62期。

正文

        众所周知(雾),Splay有着强大的处理区间的能力。由此我们很自然地想到让它来处理经典的RSQ问题(区间和问题)。
        Splay节点里面维护的是子树的权值和。
        首先来考虑单点修改,区间查询的简单模型:

单点修改,区间查询

        修改:
        肥肠非常简单,只需要把对应的节点拉出来修理一通修改存的权值即可。
        查询:
        以下假设这个查询的区间为[l,r](1\leq l\leq r\leq n,l,r,n\in N_+)
        很容易发现,在序列上,[l,r]=(l-1,r+1),也就是说,假如我们把l-1splay到根,然后把r+1splay到根的儿子,这样r+1的左儿子就必然是区间[l,r],所以r+1的左儿子维护的子树权值和也就是区间和。
        看图说明一切(不好意思是从洛谷日报上面抠下来的):
        另外,为了避免在处理区间的边界情况的时候翻车,所以我们分别新建两个“哨兵”节点(就像zkw线段树一样),权值均为0。这样就不用考虑边界了。
        就此,单点修改,区间查询的问题就被我们轻松地解决了。
        例题见P3372 【模板】树状数组 1 。(由于我的Splay有常数问题,所以有可能需要开O2)
        代码如下:

#include <cstdio>

const int MAXN = 500005;

template<typename _T>
void read( _T &x )
{
    x = 0;char s = getchar();int f = 1;
    while( s > '9' || s < '0' ){if( s == '-' ) f = -1; s = getchar();}
    while( s >= '0' && s <= '9' ){x = ( x << 3 ) + ( x << 1 ) + ( s - '0' ), s = getchar();}
    x *= f;
}

template<typename _T>
void write( _T x )
{
    if( x < 0 ){ putchar( '-' ); x = ( ~ x ) + 1; }
    if( 9 < x ){ write( x / 10 ); }
    putchar( x % 10 + '0' );
}

int ch[MAXN][2], par[MAXN], cnt[MAXN], s[MAXN], key[MAXN], val[MAXN];
int N, M, rt = 0, len = 0;

int chk( const int x )
{
    return ch[par[x]][1] == x;
}

void pushup( const int x )
{
    s[x] = s[ch[x][0]] + s[ch[x][1]] + cnt[x] * val[x];
}

void rotate( const int x )
{
    int y = par[x], z = par[y], side = chk( x ), son = ch[x][! side];
    if( y ) ch[y][side] = son;
    if( son ) par[son] = y;
    if( z ) ch[z][chk( y )] = x;
    if( x ) par[x] = z;
    if( x ) ch[x][! side] = y;
     par[y] = x;
    pushup( y ), pushup( x );
}

void splay( const int x, const int tar = 0 )
{
    int y, z;
    while( ( y = par[x] ) ^ tar )
    {
        z = par[y];
        if( z ^ tar )
        {
            if( chk( y ) == chk( x ) ) rotate( y );
            else rotate( x );
        }
        rotate( x );
    }
    if( ! tar ) rt = x;
}

void find( const int nKey )
{
    if( ! rt ) return;
    int p = rt;
    while( ch[p][nKey > key[p]] && nKey ^ key[p] )
    {
        p = ch[p][nKey > key[p]];
    }
    splay( p );
    return;
}

void insert( const int nKey, const int nVal )
{
    int p = rt, fa = 0;
    while( p && nKey ^ key[p] )
    {
        fa = p;
        p = ch[p][nKey > key[p]];
    }
    if( p ) cnt[p] ++;
    else
    {
        p = ++len;
        if( p == 1 ) rt = p;
        if( fa ) par[p] = fa, ch[fa][nKey > key[fa]] = p;
        ch[p][0] = ch[p][1] = 0;
        cnt[p] = 1, key[p] = nKey, val[p] = nVal;
    }
    pushup( p );
    splay( p );
}

//正紧的Splay

void update( const int nKey, const int addVal )
{
    find( nKey );
    if( key[rt] ^ nKey ) return;
    val[rt] += addVal;
    pushup( rt );
}

int query( const int segL, const int segR )
{
    int ll = segL, rr = segR + 2;
    splay( ll ), splay( rr, ll );
    return s[ch[rr][! chk( rr )]];
}

//修改与查询

int main()
{
    int v;
    read( N ), read( M );
    insert( 0, 0 );
    for( int i = 1 ; i <= N ; i ++ )
    {
        read( v );
        insert( i, v );
    }
    insert( N + 1, 0 );
    //在循环外的insert就是在新建“哨兵”
    int op, x, y;
    while( M -- )
    {
        read( op );
        read( x ), read( y );
        if( op == 1 )
        {
            update( x, y );
        }
        else
        {
            write( query( x, y ) ), putchar( '\n' );
        }
    }
    return 0;
}

区间修改,区间查询

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;由于这个的问题查询和单点修改的情况没有区别,所以查询部分就不再赘述。这主谈修改。
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;修改:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;首先按照查询的方法把区间给splay出来。模仿线段树,我们对这个区间打标记。
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;怎么打呢?
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;好问题!(大雾)我们先对这个区间对应的子树的根上标记。在什么时候下传标记呢?这遵循一个原则,即——
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;当父子关系即将改变时,就一定要先下传父亲上面的懒标记!
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;然后除了这个原则,就没什么可写的了
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;另外,记得在pushup的时候也要注意一下懒标记的影响哟。
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;例题:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;P3372 【模板】线段树 1
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;P3373 【模板】线段树 2
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;参考代码:(只有第一道例题的)

#include <cstdio>

typedef long long LL;

const int MAXN = 500005;

template<typename _T>
void read( _T &x )
{
    x = 0;char s = getchar();int f = 1;
    while( s > '9' || s < '0' ){if( s == '-' ) f = -1; s = getchar();}
    while( s >= '0' && s <= '9' ){x = ( x << 3 ) + ( x << 1 ) + ( s - '0' ), s = getchar();}
    x *= f;
}

template<typename _T>
void write( _T x )
{
    if( x < 0 ){ putchar( '-' ); x = ( ~ x ) + 1; }
    if( 9 < x ){ write( x / 10 ); }
    putchar( x % 10 + '0' );
}

LL s[MAXN], val[MAXN], lazy[MAXN];
int ch[MAXN][2], par[MAXN], cnt[MAXN], key[MAXN], siz[MAXN];
int N, M, rt = 0, len = 0;

int chk( const int x )
{
    return ch[par[x]][1] == x;
}

void pushup( const int x )
{
    siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + cnt[x];
    s[x] = s[ch[x][0]] + s[ch[x][1]] + cnt[x] * val[x] + lazy[x] * siz[x];
    //用懒标记修正
}

void pushdown( const int x )
{
    if( x && lazy[x] )
    {
        val[x] += lazy[x];
        if( ch[x][0] ) lazy[ch[x][0]] += lazy[x];
        if( ch[x][1] ) lazy[ch[x][1]] += lazy[x];
        lazy[x] = 0;
        pushup( ch[x][0] );
        pushup( ch[x][1] );
        pushup( x );
        //记得随时pushup!
    }
}

void rotate( const int x )
{
    int y = par[x], z = par[y], side = chk( x ), son = ch[x][! side];
    pushdown( z );
    pushdown( y );
    pushdown( x );
    //先pushdown再旋转
    if( z ) ch[z][chk( y )] = x;
    if( y ) ch[y][side] = son, par[y] = x;
    if( son ) par[son] = y;
    par[x] = z, ch[x][! side] = y;
    pushup( y );
    pushup( x );
    pushup( z );
}

void splay( const int x, const int tar = 0 )
{
    int y, z;
    while( ( y = par[x] ) ^ tar )
    {
        z = par[y];
        if( z ^ tar )
        {
            if( chk( y ) == chk( x ) ) rotate( y );
            else rotate( x );
        }
        rotate( x );
    }
    if( ! tar ) rt = x;
}

void find( const int nKey )
{
    if( ! rt ) return;
    int p = rt;
    while( ch[p][nKey > key[p]] && nKey ^ key[p] )
    {
        p = ch[p][nKey > key[p]];
    }
    splay( p );
    return;
}

void insert( const int nKey, const int nVal )
{
    int p = rt, fa = 0;
    while( p && nKey ^ key[p] )
    {
        fa = p;
        p = ch[p][nKey > key[p]];
    }
    if( p ) cnt[p] ++;
    else
    {
        p = ++len;
        if( p == 1 ) rt = p;
        if( fa ) par[p] = fa, ch[fa][nKey > key[fa]] = p;
        ch[p][0] = ch[p][1] = 0;
        cnt[p] = 1, key[p] = nKey, val[p] = nVal;
    }
    pushup( p );
    splay( p );
}

void update( const int segL, const int segR, const LL addVal )
{
    int ll = segL, rr = segR + 2;
    splay( ll ), splay( rr, ll );
    lazy[ch[rr][! chk( rr )]] += addVal;
    pushup( ch[rr][! chk( rr )] );
}

LL query( const int segL, const int segR )
{
    int ll = segL, rr = segR + 2;
    splay( ll ), splay( rr, ll );
    pushup( ch[rr][! chk( rr )] );
    return s[ch[rr][! chk( rr )]];
}

int main()
{
    int v;
    read( N ), read( M );
    insert( 0, 0 );
    for( int i = 1 ; i <= N ; i ++ )
    {
        read( v );
        insert( i, v );
    }
    insert( N + 1, 0 );
    int op, x, y;
    while( M -- )
    {
        read( op );
        read( x ), read( y );
        if( op == 1 )
        {
            read( v );
            update( x, y, v );
        }
        else
        {
            write( query( x, y ) ), putchar( '\n' );
        }
    }
    return 0;
}