我在本地测没有任何问题,但是不知道为什么RE了,哪位大佬麻烦往我看下,谢谢。
查看原帖
我在本地测没有任何问题,但是不知道为什么RE了,哪位大佬麻烦往我看下,谢谢。
227587
JerryZiruiZhang楼主2021/4/2 15:01

下面是我的代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
struct node{
    int fa, lson, rson;
    int sz;
    bool lazy;
}N[maxn];
int build(int l, int r, int fa){
    if(l > r){
        return -1;
    }
    if(l == r){
        N[l].sz = 1;
        N[l].fa = fa;
        N[l].lson = N[l].rson = -1;
        return l;
    }
    int mid = (l + r) >> 1;
    N[mid].sz = 1;
    N[mid].fa = fa;
    N[mid].lson = build(l, mid - 1, mid);
    N[mid].sz += (N[mid].lson == -1? 0: N[N[mid].lson].sz);
    N[mid].rson = build(mid + 1, r, mid);
    N[mid].sz += (N[mid].rson == -1? 0: N[N[mid].rson].sz);
    return mid;
}
int pushdown(int id){
    if(N[id].lazy == 1){
        int ll = N[id].lson, rr = N[id].rson;
        if(ll != -1){
	        N[ll].lazy ^= 1;
	        swap(N[ll].lson, N[ll].rson);
		}
		if(rr != -1){
	        N[rr].lazy ^= 1;
	        swap(N[rr].lson, N[rr].rson);
		}
        N[id].lazy = 0;//警告
    }
}
int Right_rotate(int x){//警告
    int y = N[x].fa, z = N[y].fa;
    int a = N[x].lson, b = N[x].rson, c = N[y].rson;
    int sza = (a == -1? 0: N[a].sz);
    int szc = (c == -1? 0: N[c].sz);
    N[y].lson = b;
    if(b != -1)N[b].fa = y;//警告
    N[x].fa = z;
    if(z != -1){//警告(逆向定义)
        if(N[z].lson == y){
            N[z].lson = x;
        } else{
            N[z].rson = x;
        }
    }
    N[x].rson = y;
    N[y].fa = x;
    N[x].sz += (szc + 1);
    N[y].sz -= (sza + 1);
}
int Left_rotate(int x){
    int y = N[x].fa, z = N[y].fa;
    int a = N[y].lson, b = N[x].lson, c = N[x].rson;
    int sza = (a == -1? 0: N[a].sz);
    int szc = (c == -1? 0: N[c].sz);
    N[y].rson = b;
    if(b != -1)N[b].fa = y;
    N[x].fa = z;
    if(z != -1){
        if(N[z].lson == y){
            N[z].lson = x;
        } else{
            N[z].rson = x;
        }
    }
    N[x].lson = y;
    N[y].fa = x;
    N[x].sz += (sza + 1);
    N[y].sz -= (szc + 1);
}
void dfs(int x, int n){
    pushdown(x);//警告
    if(N[x].lson != -1){
        dfs(N[x].lson, n);
    }
    if(x != 0 && x != n + 1){
        printf("%d ", x);
	}
    if(N[x].rson != -1){
        dfs(N[x].rson, n);
    }
}
int sta[maxn], top;
int To_target(int x, int target){
    int top = 0;
    int xx = x;
    while(xx != -1){
        sta[++top] = xx;
        xx = N[xx].fa;
    }
    while(top > 0){
        pushdown(sta[top--]);
    }
    int y = N[x].fa; int z = N[y].fa;
    while(y != target){
        if(z != target){
            if(N[z].lson == y && N[y].lson == x){
                Right_rotate(y);
                Right_rotate(x);
            }
            else if(N[z].rson == y && N[y].rson == x){
                Left_rotate(y);
                Left_rotate(x);
            }
            else if(N[z].lson == y && N[y].rson == x){
                Left_rotate(x);
                Right_rotate(x);
            }
            else if(N[z].rson == y && N[y].lson == x){
                Right_rotate(x);
                Left_rotate(x);
            }
        } else{
            if(N[y].lson == x){
                Right_rotate(x);
            }
            else if(N[y].rson == x){
                Left_rotate(x);
            }
        }
        y = N[x].fa; z = N[y].fa;
    }
}
int kth(int x, int k){
    int a = N[x].lson, b = N[x].rson ;
    int sza = (a != -1? N[a].sz: 0);
    pushdown(x);
    if(a != -1 && sza >= k){
        return kth(a, k);
    }
    if(sza + 1 == k){
        return x;
    }
    if(b != -1 && k > sza + 1){
        return kth(b, k - sza - 1);
    }
}
int n, m;
int Flip(int l, int r){
	To_target(l, -1);
	To_target(r, l);
	int rev = N[r].lson;
	N[rev].lazy ^= 1;
	swap(N[rev].lson, N[rev].rson);
}
int main (){
    int rt;
    scanf("%d%d", &n, &m);
    build(0, n + 1, -1);
    rt = (0 + n + 1) >> 1;
    for(int i = 1; i <= m; i++){
    	int x, y;
    	scanf("%d%d", &x, &y);
    	x = kth(rt, x), y = kth(rt, y + 2);
    	Flip(x, y);
    	rt = x;
    }
    dfs(rt, n);
    return 0;
}

下面是第一组数据输入:

100 100
47 91
11 15
45 57
35 68
20 90
2 23
45 83
12 76
55 57
69 75
17 21
1 5
9 86
33 63
11 23
54 63
4 72
37 89
16 95
43 44
51 72
13 13
12 32
15 72
63 72
24 75
54 69
7 10
13 96
22 25
7 31
36 66
29 52
86 87
56 62
29 65
24 54
29 85
3 22
39 88
72 79
54 62
52 75
75 76
31 91
6 26
56 74
74 86
8 78
47 50
68 99
27 100
31 78
1 80
45 92
44 72
1 87
49 79
13 37
9 49
67 72
17 97
44 88
40 84
8 89
4 42
2 80
24 99
11 35
35 50
50 79
49 62
11 37
16 40
24 37
79 84
7 37
53 64
46 66
8 75
41 66
50 77
16 49
33 34
14 23
2 5
45 91
48 86
15 92
18 31
61 80
31 57
75 76
40 62
38 89
11 90
57 57
5 86
61 86
45 48

下面是第一组数据输出:

93 6 13 44 32 81 74 51 63 35 100 12 11 94 88 87 30 29 62 21 71 70 34 43 78 23 68 96 18 16 61 59 95 55 79 75 42 33 41 90 82 85 8 4 99 98 97 84 66 69 65 52 2 83 37 36 27 28 80 19 77 7 10 5 56 57 60 64 54 40 3 76 86 14 31 89 15 91 92 53 26 38 39 73 72 1 50 48 49 58 45 46 20 47 22 17 24 25 67 9 
2021/4/2 15:01
加载中...