QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#578579#5704. Jokerisirazeev0 62ms29600kbC++204.0kb2024-09-20 20:08:152024-09-20 20:08:15

Judging History

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

  • [2024-09-20 20:08:15]
  • 评测
  • 测评结果:0
  • 用时:62ms
  • 内存:29600kb
  • [2024-09-20 20:08:15]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
#define int long long

struct change {
    int *cell, val, time;
};

struct DSU {
    vector<int> p, sz, len;
    vector<change> changes;
    int flag, timer;

    void init(int n) {
        p.resize(n), sz.resize(n), len.resize(n);
        for (int i = 0; i < n; i++)
            p[i] = i, sz[i] = 1, len[i] = 0;
        flag = -1, timer = 0;
    }

    void undo() {
        if (changes.empty()) return;
        int time = changes.back().time;
        if (time == flag) flag = -1;
        while (!changes.empty() &&
               changes.back().time == time) {
            *changes.back().cell = changes.back().val;
            changes.pop_back();
        }
    }

    pair<int, int> find(int v) {
        if (v == p[v]) return {v, 0};
        auto pr = find(p[v]);
        return {pr.first, (pr.second + len[v]) % 2};
    }

    bool isBipartite() {
        return flag == -1;
    }

    void unite(int u, int v) {
        timer++;
        auto P = find(u), Q = find(v);
        int p1 = P.first, p2 = Q.first;
        if (sz[p1] > sz[p2]) swap(p1, p2), swap(P, Q);
        if (p1 == p2) {
            if (P.second == Q.second && flag == -1) flag = timer;
            changes.emplace_back(&p[p1], p1, timer);
            return;
        }
        changes.emplace_back(&p[p1], p[p1], timer);
        p[p1] = p2;
        changes.emplace_back(&len[p1], len[p1], timer);
        len[p1] = (P.second + Q.second + 1) % 2;
        changes.emplace_back(&sz[p2], sz[p2], timer);
        sz[p2] += sz[p1];
    }
};

struct update {
    char type;
    int u, v;
};

struct QueueUndoTrick {
    DSU T;
    int bottom = 0;
    vector<update> updates;

    void init(int n) {
        T.init(n);
    }

    void advance_bottom() {
        while (bottom < updates.size() && updates[bottom].type == 'B') bottom++;
    }

    void reverse_updates() {
        for (int i = 0; i < (int) updates.size(); i++)
            T.undo();
        reverse(updates.begin(), updates.end());
        for (int i = 0; i < (int) updates.size(); i++) {
            T.unite(updates[i].u, updates[i].v);
            updates[i].type = 'A';
        }
        bottom = 0;
    }

    void fix() {
        if (updates.empty() || updates.back().type == 'A') return;
        advance_bottom();
        vector<update> saveA, saveB = {updates.back()};
        updates.pop_back();
        while (saveA.size() != saveB.size() && updates.size() > bottom) {
            if (updates.back().type == 'A')
                saveA.emplace_back(updates.back());
            else
                saveB.emplace_back(updates.back());
            T.undo();
            updates.pop_back();
        }
        reverse(saveA.begin(), saveA.end());
        reverse(saveB.begin(), saveB.end());
        for (auto &u: saveB)
            updates.emplace_back(u), T.unite(u.u, u.v);
        for (auto &u: saveA)
            updates.emplace_back(u), T.unite(u.u, u.v);
        advance_bottom();
    }

    void undo() {
        advance_bottom();
        if (bottom == updates.size())
            reverse_updates();
        fix();
        T.undo();
        updates.pop_back();
    }

    void unite(int u, int v) {
        T.unite(u, v);
        updates.emplace_back('B', u, v);
    }

    bool isBipartite() {
        return T.isBipartite();
    }
};

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int n, m, q;
    cin >> n >> m >> q;
    QueueUndoTrick T;
    vector<pair<int, int>> e(m);
    for (int i = 0; i < m; i++) cin >> e[i].first >> e[i].second;
    T.init(n);
    int r = -1, right[m];
    for (int i = 0; i < m; i++) {
        while (r < m && T.isBipartite()) {
            r++;
            if (r < m)
                T.unite(e[r].first, e[r].second);
        }
        right[i] = r;
        T.undo();
    }
    for (int i = 0; i < q; i++) {
        int L, R;
        cin >> L >> R;
        if (right[L - 1] <= R - 1)
            cout << "NO\n";
        else
            cout << "YES\n";
    }
    return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 6
Accepted
time: 0ms
memory: 3616kb

input:

6 8 2
1 3
1 5
1 6
2 5
2 6
3 4
3 5
5 6
4 8
4 7

output:

NO
YES

result:

ok 2 lines

Test #2:

score: 0
Wrong Answer
time: 0ms
memory: 3612kb

input:

2 1 1
1 2
1 1

output:

YES

result:

wrong answer 1st lines differ - expected: 'NO', found: 'YES'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #55:

score: 0
Wrong Answer
time: 62ms
memory: 29600kb

input:

100000 199997 200000
79109 44896
79109 66117
66117 91800
91800 24387
24387 74514
48558 74514
48558 37561
37561 76920
79598 76920
79598 69196
69196 79004
49065 79004
70038 49065
15497 70038
15497 67507
25073 67507
25073 41762
41762 71848
71848 32073
32073 43754
72852 43754
41209 72852
68112 41209
629...

output:

NO
NO
YES
YES
NO
NO
NO
NO
YES
NO
NO
YES
NO
YES
NO
YES
NO
NO
NO
NO
NO
YES
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
YES
NO
NO
YES
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
Y...

result:

wrong answer 4th lines differ - expected: 'NO', found: 'YES'

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Skipped

Dependency #2:

0%

Subtask #6:

score: 0
Skipped

Dependency #1:

0%