fhq treap 求调

P3391 【模板】文艺平衡树

qwq_it_is_me @ 2023-06-15 20:29:46

106行的两个print,如果注释上就不能过, 取消注释tle(此处废话),求调/bx

个人估计是有关pushdown的函数出了问题,但是看了好几份标程都没有发现问题,快调吐了

#include<iostream>
#include<cstdlib>
using namespace std;

struct node{
    int val;
    int pri;
    int ls, rs;
    bool laz=0;
    int size=0;
}t[100005];
int root=0, head=0;

int newnode(int val){
    head++;
    t[head].val = val;
    t[head].pri = rand();
    t[head].ls = t[head].rs = 0;
    t[head].size = 1;
    return head;
}

void pushdown(int rt){
    if(t[rt].laz){

        //std::swap(t[rt].ls, t[rt].rs);
        int tmp=t[rt].ls;
        t[rt].ls = t[rt].rs;
        t[rt].rs = tmp;
        t[t[rt].ls].laz ^= 1;
        t[t[rt].rs].laz ^= 1;
        t[rt].laz=false; 
    }
}

void pushup(int rt){
    t[rt].size = t[t[rt].rs].size + t[t[rt].ls].size + 1;
}

int merge(int x, int y){
    if(!x or !y) return x|y;
    if(t[x].pri>t[y].pri){
        pushdown(x);
        t[x].rs = merge(t[x].rs, y);
        pushup(x);
        return x;
    }
    else{
        pushdown(y);
        t[y].ls = merge(x, t[y].ls);
        pushup(y); 
        return y;
    }
}

void split(int rt, int k, int&x, int&y){
    if(rt == 0) return (void) (x = y = 0);
    int rnk = t[t[rt].ls].size +1;
    pushdown(rt);
    if(rnk<k){
        x = rt;
        split(t[rt].rs, k-rnk, t[x].rs, y);
    }
    else{
        y = rt;
        split(t[rt].ls, k, x, t[y].ls);
    }
    pushup(rt);
    return; 
}

void print(int rt){
    if(rt == 0) return;
    pushdown(rt);
    print(t[rt].ls);
    printf("%d ", t[rt].val);
    //printf("%d\t%d\t%d\n",t[t[rt].ls].val, t[rt].val, t[t[rt].rs].val);
    print(t[rt].rs);
    return;
}
void flip(int l, int r){
    int x, y, z;
    split(root, r+1, x, z);
    split(x, l, x, y);
    //print(x);print(y);print(z);
    //cout<<endl;
    t[y].laz ^= 1;
    root = merge(merge(x, y), z);
}

int n, m;
int main(){
    srand(0);
    cin>>n>>m;
    for(int i = 1; i<=n; i++){
        root = merge(root, newnode(i));
    }
    //print(root);
    //cout<<endl;
    while(m--){
        int l, r;
        cin>>l>>r;
        flip(l,r);
        //printf(">>");
        print(root);  //就是这里 
        printf("\n");
    }
    print(root);
    return 0;
}

by Liuyuzhuo @ 2023-06-15 21:08:43

@qwq_it_is_me split函数里的pushdown要往前移一行


by qwq_it_is_me @ 2023-06-15 21:14:07

@Liuyuzhuo 感谢神犇!!!是我瞎了pwq


|