P5055 [template] can persist literary balanced tree can persist fhqtreap

P5055 [template] can persist literary balanced tree

link

luogu

Thinking

Persistable fhq-treap sets set on the line, pushdown and split must be persistent, but do not merge durable. That has not been previously difficult to see, is a board.

error

t[++num].sum=x;t[num].val=x;
t[++num].sum=t[num].val=x;

These two words actually not the same.
I rely I rely I rely on, debugging afternoon.

Code

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int _=2e5+7;
ll read() {
    ll x=0,f=1;char s=getchar();
    for(;s>'9'||s<'0';s=getchar()) if(s=='-') f=-1;
    for(;s>='0'&&s<='9';s=getchar()) x=x*10+s-'0';
    return x*f;
}
namespace fhq_treap {
    #define ls t[u].ch[0]
    #define rs t[u].ch[1]
    int num;
    struct node {
        int ch[2],siz,pri,lazy;
        ll val,sum;
    }t[_*80];
    int make_node(ll x) {
        t[++num].sum=x;t[num].val=x;
        t[num].pri=rand(),t[num].siz=1;
        return num;
    }
    void pushup(int u) {
        t[u].siz=t[ls].siz+t[rs].siz+1;
        t[u].sum=t[ls].sum+t[rs].sum+t[u].val;
    }
    int copy_node(int u) {t[++num]=t[u];return num;}
    void pushdown(int u) {
        if(t[u].lazy) {
            if(ls) ls=copy_node(ls);
            if(rs) rs=copy_node(rs);
            if(ls) t[ls].lazy^=1;
            if(rs) t[rs].lazy^=1;
            swap(ls,rs);
            t[u].lazy^=1;
        }
    }
    int merge(int x,int y) {
        if(!x||!y) return x+y;
        pushdown(x),pushdown(y);
        if(t[x].pri<t[y].pri) {
            t[x].ch[1]=merge(t[x].ch[1],y);
            pushup(x);return x;
        } else {
            t[y].ch[0]=merge(x,t[y].ch[0]);
            pushup(y);return y;
        }   
    }
    void split(int u,int k,int &x,int &y) {
        if(!u) x=y=0;
        else {
            pushdown(u);
            u=copy_node(u);
            if(t[ls].siz+1<=k)
                x=u,split(rs,k-t[ls].siz-1,t[x].ch[1],y);
            else
                y=u,split(ls,k,x,t[y].ch[0]);
            pushup(u);
        }
    }
}
using namespace fhq_treap;
int rt[_];
int main() {
    srand(19260817); 
    int T=read();
    ll lastans=0;
    for(int i=1;i<=T;++i) {
        int super_cool=read(),opt=read(),x,y,z;
        if(opt==1) {
            ll p=read()^lastans,v=read()^lastans;
            split(rt[super_cool],p,x,y);
            rt[i]=merge(merge(x,make_node(v)),y);
        } else if(opt==2) {
            ll p=read()^lastans;
            split(rt[super_cool],p,x,z);
            split(x,p-1,x,y);
            rt[i]=merge(x,z);
        } else if(opt==3) {
            ll l=read()^lastans,r=read()^lastans;
            split(rt[super_cool],r,x,z);
            split(x,l-1,x,y);
            t[y].lazy^=1;
            rt[i]=merge(merge(x,y),z);
        } else {
            ll l=read()^lastans,r=read()^lastans;
            split(rt[super_cool],r,x,z);
            split(x,l-1,x,y);
            printf("%lld\n",lastans=t[y].sum);
            rt[i]=merge(merge(x,y),z);
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/dsrdsr/p/11228084.html