QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#576956#8528. Chordsucup-team3519WA 12ms12028kbC++204.0kb2024-09-19 23:32:572024-09-19 23:32:59

Judging History

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

  • [2024-09-19 23:32:59]
  • 评测
  • 测评结果:WA
  • 用时:12ms
  • 内存:12028kb
  • [2024-09-19 23:32:57]
  • 提交

answer

#include <bits/stdc++.h>

template <typename Mono, int TREE_N>
class LinkCutTree {
#define ls son[x][0]
#define rs son[x][1]
    int son[TREE_N][2], fa[TREE_N];
    Mono dp[TREE_N], val[TREE_N];
    bool tag[TREE_N];
    void push_up(int x) {
        dp[x] = dp[ls] * val[x] * dp[rs];
    }
    void push_down(int x) {
        if (tag[x]) {
            tag[ls] ^= 1;
            tag[rs] ^= 1;
            tag[x] = 0;
            std::swap(ls, rs);
        }
    }
    int get(int x) {
        return son[fa[x]][1] == x;
    }
    bool isroot(int x) {
        return son[fa[x]][0] != x && son[fa[x]][1] != x;
    }
    void rotate(int x) {
        int f = fa[x], ff = fa[f], t = get(x);
        fa[x] = ff;
        if (!isroot(f))
            son[ff][get(f)] = x;

        son[f][t] = son[x][t ^ 1], fa[son[x][t ^ 1]] = f;
        push_up(f);

        son[x][t ^ 1] = f, fa[f] = x;
        push_up(x);
    }
    void update(int x) {
        if (!isroot(x))
            update(fa[x]);
        push_down(x);
    }
    void splay(int x) {
        update(x);
        while (!isroot(x)) {
            if (!isroot(fa[x]))
                rotate(get(x) == get(fa[x]) ? fa[x] : x);
            rotate(x);
        }
    }
    void access(int x) {
        for (int p = 0; x; p = x, x = fa[x])
            splay(x), rs = p, push_up(x);
    }
    void make_root(int x) {
        access(x), splay(x), tag[x] ^= 1;
    }

public:
    void link(int x, int y) {
        make_root(x), fa[x] = y;
    }
    void cut(int x, int y) {
        make_root(x), access(y), splay(x);
        if (son[x][1] == y && !son[y][0]) {
            son[x][1] = fa[y] = 0, push_up(x);
        }
    }
    void set(int x, const Mono &v) {
        val[x] = v;
        splay(x);
    }
    Mono query(int u, int v) {
        make_root(u), access(v), splay(v);
        return dp[v];
    }
    int find(int x) {
        access(x), splay(x);
        while (son[x][0]) {
            x = son[x][0];
            push_down(x);
        }
        splay(x);
        return x;
    }
    void clear() {
        for (int i = 0; i < TREE_N; ++i) {
            son[i][0] = son[i][1] = fa[i] = 0;
            dp[i] = val[i] = Mono{};
            tag[i] = false;
        }
    }
#undef ls
#undef rs
};

struct Mono {
    int x;
    friend Mono operator*(const Mono &lhs, const Mono &rhs) {
        return Mono{lhs.x + rhs.x};
    }
};

constexpr int MAXN = 1e5;
LinkCutTree<Mono, MAXN * 4 + 2> lct;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n;
    std::cin >> n;

    std::vector<std::pair<int, int>> edge(n * 4 + 1);
    for (int i = 1; i <= n; ++i) {
        int u, v;
        std::cin >> u >> v;
        edge[u] = {v, i};
        edge[v] = {u + n * 2, i + n};
    }

    std::vector<int> vals(n * 2 + 1, 1);
    std::vector<int> fa(n * 4 + 2);

    auto iterate = [&]() -> int {
        int res = 0;

        lct.clear();
        std::fill(fa.begin(), fa.end(), 0);

        std::vector<int> nvals = vals;
        for (int rt = n * 4; rt >= 1; --rt) {
            auto [to, id] = edge[rt];

            if (id && rt + 1 < to) {
                nvals[id] = std::max(nvals[id], lct.query(rt + 1, to).x + 1);
            }

            fa[rt + 1] = rt;
            lct.link(fa[rt + 1], rt + 1);

            if (to && lct.query(rt, to + 1).x < vals[id]) {
                lct.cut(fa[to + 1], to + 1);
                fa[to + 1] = rt;
                lct.set(to + 1, Mono{vals[id]});
                lct.link(fa[to + 1], to + 1);
            }

            res =
                std::max(res, lct.query(rt, std::min(rt + n * 2, n * 4 + 1)).x);
        }
        vals = std::move(nvals);

        return res;
    };

    int ans = 0;
    for (int _ = 0; _ < 20; ++_) {
        ans = std::max(ans, iterate());
    }

    // for (int i = 1; i <= n; ++i) {
    //     std::cout << vals[i] << '\n';
    // }
    std::cout << ans << '\n';
}

詳細信息

Test #1:

score: 100
Accepted
time: 12ms
memory: 11728kb

input:

5
1 2
4 10
7 9
3 5
6 8

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 7ms
memory: 11748kb

input:

1
1 2

output:

1

result:

ok 1 number(s): "1"

Test #3:

score: 0
Accepted
time: 12ms
memory: 11820kb

input:

2
1 3
2 4

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 8ms
memory: 11796kb

input:

2
1 4
2 3

output:

2

result:

ok 1 number(s): "2"

Test #5:

score: 0
Accepted
time: 8ms
memory: 12012kb

input:

3
3 5
1 4
2 6

output:

2

result:

ok 1 number(s): "2"

Test #6:

score: 0
Accepted
time: 12ms
memory: 11816kb

input:

4
2 7
1 6
3 8
4 5

output:

2

result:

ok 1 number(s): "2"

Test #7:

score: 0
Accepted
time: 11ms
memory: 11796kb

input:

6
8 9
6 7
4 10
1 5
11 12
2 3

output:

5

result:

ok 1 number(s): "5"

Test #8:

score: 0
Accepted
time: 11ms
memory: 11812kb

input:

9
2 8
10 17
9 15
1 12
6 14
3 13
4 11
5 7
16 18

output:

4

result:

ok 1 number(s): "4"

Test #9:

score: 0
Accepted
time: 7ms
memory: 11796kb

input:

13
8 16
2 13
14 23
10 11
7 17
6 24
12 18
9 20
4 15
19 21
3 26
1 25
5 22

output:

6

result:

ok 1 number(s): "6"

Test #10:

score: 0
Accepted
time: 12ms
memory: 11760kb

input:

19
34 35
4 20
9 31
12 17
18 38
23 29
19 30
25 26
10 36
1 7
13 37
2 11
8 32
6 28
16 24
5 27
14 21
15 22
3 33

output:

8

result:

ok 1 number(s): "8"

Test #11:

score: -100
Wrong Answer
time: 12ms
memory: 12028kb

input:

28
9 45
7 19
15 40
42 44
26 31
20 22
16 55
6 34
41 56
28 51
2 33
36 53
3 13
37 52
4 46
12 48
21 27
24 30
23 38
1 32
8 14
43 54
11 17
47 49
29 35
5 25
18 39
10 50

output:

9

result:

wrong answer 1st numbers differ - expected: '10', found: '9'