QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#792359#21403. 文艺平衡树oyzrML 0ms0kbC++232.5kb2024-11-29 09:32:072024-11-29 09:32:07

Judging History

你现在查看的是最新测评结果

  • [2024-11-29 09:32:07]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2024-11-29 09:32:07]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
int read(){
    int res = 0, flag = 1;
    char c = getchar();
    for (; c < '0' || c > '9'; c = getchar())
        if (c == '-')
            flag = -1;
    for (; c >= '0' && c <= '9'; c = getchar())
        res = (res << 1) + (res << 3) + (c ^ 48);
    return res * flag;
}
class Treap{
private:
    const static int MAXNODE = MAXN;
    #define ls(x) t[x].lson
    #define rs(x) t[x].rson
    struct Node{
        int lson, rson;
        bool rev;
        int rnd, val, size;
    }t[MAXNODE];
    int tot = 0;
    void pushDown(int id){
        if (t[id].rev){
            reverse(ls(id));
            reverse(rs(id));
        }
        t[id].rev = false;
    }
    void update(int id){
        t[id].size = t[ls(id)].size + t[rs(id)].size + 1;
    }
public:
    int newNode(int val){
        ++tot;
        t[tot].lson = t[tot].rson = 0;
        t[tot].rev = false;
        t[tot].rnd = rand();
        t[tot].val = val;
        t[tot].size = 1;
        return tot;
    }
    void reverse(int id){
        t[id].rev ^= 1;
        swap(ls(id), rs(id));
    }
    void split(int id, int size, int &x, int &y){
        if (!id){
            x = y = 0;
            return;
        }
        pushDown(id);
        if (t[ls(id)].size + 1 <= size){
            x = id;
            split(rs(id), size - (t[ls(id)].size + 1), rs(x), y);
        }else{
            y = id;
            split(ls(id), size, x, ls(y));
        }
        update(id);
    }
    int merge(int x, int y){
        if (!x || !y)
            return x + y;
        pushDown(x); pushDown(y);
        if (t[x].rnd < t[y].rnd){
            rs(x) = merge(rs(x), y);
            update(x);
            return x;
        }else{
            ls(y) = merge(x, ls(y));
            update(y);
            return y;
        }
    }
    void output(int id){
        if (!id)
            return;
        pushDown(id);
        output(ls(id));
        cout << t[id].val << ' ';
        output(rs(id));
    }
}treap;
int main(){
    int n = read(), m = read();
    int rt = 0;
    for (int i = 1; i <= n; i++)
        rt = treap.merge(rt, treap.newNode(i));
    while (m--){
        int l = read(), r = read();
        int rt1, rt2, rt3;
        treap.split(rt, r, rt2, rt3);
        treap.split(rt, l - 1, rt1, rt2);
        treap.reverse(rt2);
        rt = treap.merge(treap.merge(rt1, rt2), rt3);
    }
    treap.output(rt);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Memory Limit Exceeded

input:

30 5
7 26
7 18
5 9
4 15
3 15

output:


result: