萌新10pts splay求调,足足三天了~
查看原帖
萌新10pts splay求调,足足三天了~
393674
jixiang楼主2021/12/18 18:58
#include <iostream>
#include <cstring>
#include <algorithm>
const int N= 5e5+9;
const int INF= 1e9;
using namespace std;
int n,m,tt,root;
int poi[N],idx;
int w[N];

struct node
{
    int s[2];
    int siz,p,f,cov;
    int tmax,lmax,rmax;
    int sum ,v;
    void init (int _p,int _v)
    {
        s[0]= s[1]= 0;
        siz= 1;
        f= cov = 0;
        p= _p;
        sum = tmax = v= _v;
        if(v>=0)lmax = rmax = v;
        else lmax= rmax = 0;
    }
}tr[N];


void up (int x)
{
    auto &u= tr[x];
    auto &l= tr[tr[x].s[0]];
    auto &r= tr[tr[x].s[1]];
    u.sum = l.sum+r.sum+u.v;
    u.siz = l.siz+r.siz+1;
    u.rmax =max(r.rmax ,l.rmax + u.v + r.sum);
    u.lmax =max(l.lmax ,r.lmax + u.v + l.sum);
    u.tmax = max (max (l.tmax , r.tmax),l.rmax+ u.v +r.lmax);
}

void down (int x)
{
    auto &u= tr[x];
    auto &l= tr[tr[x].s[0]];
    auto &r= tr[tr[x].s[1]];
    if(u.cov)
    {
        u.cov = u.f = 0;
        if(u.s[0]) l.cov = 1, l.v= u.v, l.sum= l.v * l.siz;
        if(u.s[1]) r.cov = 1, r.v= u.v, r.sum= r.v * r.siz;

        if(u.v >=0)
        {
            if(u.s[0]) l.tmax= l.rmax = l.rmax = l.sum;
            if(u.s[1]) r.tmax= r.rmax = r.lmax = r.sum;
        }

        else
        {
            if(u.s[0]) l.rmax= l.lmax = 0 , l.tmax = l.v;
            if(u.s[1]) r.rmax= l.rmax=  0 , r.tmax = r.v;
        }
    }

    else if(u.f)
    {
        u.f = 0;
        /*if(u.s[0])
        {
            l.f ^= 1;
            swap (l.s[1],l.s[2]);
            swap (l.lmax,l.rmax);
        }

        if(u.s[1])
        {
            r.f ^= 1;
            swap (r.s[1],r.s[0]);
            swap (r.rmax, r.lmax);
        }*/

        l.f ^= 1; r.f ^= 1;
        swap (l.lmax,l.rmax);
        swap (r.lmax,r.rmax);
        swap (l.s[0],l.s[1]);
        swap (r.s[0],r.s[1]);
    }
}

void spin(int x)
{
    int y= tr[x].p;
    int z= tr[y].p;
    int k= tr[y].s[1]==x;

    tr[x].p= z; tr[z].s[tr[z].s[1]==y] = x;
    tr[y].s[k]= tr[x].s[k^1]; tr[tr[x].s[k^1]].p= y;
    tr[x].s[k^1]= y; tr[y].p=x;
    up(y); up(x);
}

void splay(int x,int k)
{
    while (tr[x].p!=k)
    {
        int y= tr[x].p;
        int z= tr[y].p;
        if(z!=k)
        {
            if((tr[z].s[1]==y) ^ (tr[y].s[1]==x))spin(x);
            else spin(y);
        }
        spin(x);
    }

    if(k==0)root = x;
}


int get_id_byk(int k)
{
    int u= root;
    while (u)
    {
        down (u);
        if(tr[tr[u].s[0]].siz >=k)u=tr[u].s[0];
        else if(tr[tr[u].s[0]].siz+1 ==k) return u;
        else k-=tr[tr[u].s[0]].siz+1,u=tr[u].s[1];
    }
    //return -1;
}

int build (int l,int r,int p)
{
    int mid = l+r >>1;
    int u= poi [tt--];
    tr[u].init (p,w[mid]);
    if(l<mid) tr[u].s[0]= build (l,mid-1,u);
    if(r>mid) tr[u].s[1]= build (mid+1,r,u);
    up(u);
    return u;
}

void dfs (int u)
{
    if(tr[u].s[0]) dfs (tr[u].s[0]);
    if(tr[u].s[1]) dfs (tr[u].s[1]);
    poi [++tt]= u;
}

signed main()
{
    scanf("%d%d", &n, &m);
    char op[20];
    tr[0].tmax = w[0] = w[n+1]= -INF ;
    for (int i = 1; i < N; i ++ ) poi [++tt] =i;
    for (int i = 1; i <= n; i ++ ) scanf ("%d",&w[i]);
    root = build (0,n+1,0);
    int pos,tot,num;

    while (m--)
    {
        scanf("%s",op);
        if(!strcmp(op, "INSERT"))
        {
            scanf("%d%d",&pos,&tot);
            for(int i=0;i<tot;i++)scanf("%d",&w[i]);
            int l= get_id_byk(pos+1);
            int r= get_id_byk(pos+2);
            splay (l,0);
            splay (r,l);
            int u=  build (0,tot-1,r);
            tr[r].s[0]= u ;
            up(r); up (l);
        }

        else if (!strcmp(op, "DELETE"))
        {
            scanf("%d%d",&pos,&tot);
            int l= get_id_byk(pos);
            int r= get_id_byk(pos+tot+1);
            splay(l,0);
            splay(r,l);
            dfs(tr[r].s[0]);
            tr[r].s[0]=0;
            up(r); up (l);
        }

        else if(!strcmp(op, "REVERSE"))
        {
             scanf("%d%d",&pos,&tot);
             int l= get_id_byk(pos);
             int r= get_id_byk(pos+tot+1);
             splay(l,0);
             splay(r,l);
             auto &u= tr[tr[r].s[0]];
             u.f ^= 1;

             swap (u.lmax,u.rmax);
             swap (u.s[1],u.s[0]);
             up(r); up(l);
             //cout<<++idx<<" 5656 "<<tr[root].siz<<endl;
        }

        else if(!strcmp(op, "GET-SUM"))
        {
            scanf("%d%d",&pos,&tot);
            int l= get_id_byk(pos);
            int r= get_id_byk(pos+1+tot);
            splay(l,0);
            splay(r,l);
            //up(r); up(l);
            printf("%d\n",tr[tr[r].s[0]].sum);
        }

        else  if(!strcmp(op, "MAKE-SAME"))
            {
                int c;
                scanf("%d%d%d",&pos,&tot,&c);
                int l= get_id_byk(pos);
                int r= get_id_byk(pos+tot+1);
                splay(l,0);
                splay(r,l);
                auto &u= tr[tr[r].s[0]];
                u.cov = 1;
                u.v= c;
                u.sum= c*u.siz;
                if(c>=0) u.lmax= u.rmax = u.tmax = u.sum;
                else u.lmax = u.rmax = 0, u.tmax = c;
                //wangji
                up(r); up(l);

            }

        else printf ("%d\n",tr[root].tmax);

    }
    return 0;
}






2021/12/18 18:58
加载中...