답안 #553296

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
553296 2022-04-25T11:10:46 Z RaresFelix Collapse (JOI18_collapse) C++17
100 / 100
5305 ms 459620 KB
#include "collapse.h"
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

using namespace std;
using Edge = tuple<int, int, int, int>; //from, to, begin, end
using vEd = vector<Edge>;
using vi = vector<int>;


struct RollbackUF {
    vi e; vector<pair<int, int> > st;
    RollbackUF(int n) : e(n, -1) {}
    int size(int x) { return -e[find(x)]; }
    int find(int x) { return e[x] < 0 ? x : find(e[x]); }
    int time() { return int(st.size()); }
    void rollback(int t) {
        for (int i = time(); i --> t;)
            e[st[i].first] = st[i].second;
        st.resize(t);
    }
    bool join(int a, int b) {
        a = find(a), b = find(b);
        if (a == b) return false;
        if (e[a] > e[b]) swap(a, b);
        st.push_back({a, e[a]});
        st.push_back({b, e[b]});
        e[a] += e[b]; e[b] = a;
        return true;
    }
};

struct Bucket {
    RollbackUF Simu;
    int begin, end;
    Bucket(int n, int begin, int end) : Simu(n), begin(begin), end(end) {}
    vector<Edge> idk;
    void add_edge(Edge nou) {
        auto &[f, t, b, e] = nou;
        if(e < begin || end < b) return;
        if(b <= begin && end <= e) { //complet
            Simu.join(f, t);
            return;
        }
        idk.push_back(nou);
    }
    int query(int timp) {
        int count0 = Simu.time();
        for(auto &it : idk) {
            auto &[f, t, b, e] = it;
            if(b <= timp && timp <= e) Simu.join(f, t);
        }
        int re = Simu.st.size() >> 1;
        Simu.rollback(count0);
        return re;
    }
};

void solve(int n, int c, int q, vEd &edges, vi &W, vi &P, vi &R) {
    R.assign(q, 0);
    vector<vi> Queries(n + 2);
    for(int i = 0; i < q; ++i)
        Queries[P[i]].push_back(i);
    vector<vEd> Add(n + 1);
    for(auto &[f, t, b, e] : edges)
        Add[t].push_back({f, t, b, e});

    int bucketSize = int(sqrt(c + 1));
    vector<Bucket> Sol;
    for(int lp = 0; lp <= c + 1; lp += bucketSize)
        Sol.emplace_back(n, lp, lp + bucketSize - 1);

    for(int i = 0; i < n; ++i) {
        for(auto &it : Add[i])
            for(auto &seg : Sol) seg.add_edge(it);
        for(auto &qid : Queries[i]) {
            int w = W[qid];
            int re = 0;
            for(auto &seg : Sol) {
                if(seg.begin <= w && w <= seg.end) {
                    re = seg.query(w);
                    break;
                }
            }
            R[qid] = i + 1 - re;
        }
    }
}

vector<int> simulateCollapse(
        int n,
        vector<int> T,
        vector<int> X,
        vector<int> Y,
        vector<int> W,
        vector<int> P
) {
    vEd edges;
    int c = T.size(), q = W.size();
    map<pair<int, int>, int> enabled;
    for(int i = 0 ; i < c; ++i)
        if(X[i] > Y[i]) swap(X[i], Y[i]);
    for(int i = 0 ; i < c; ++i) {
        if(!T[i])
            enabled[{X[i], Y[i]}] = i;
        else {
            edges.push_back({X[i], Y[i], enabled[{X[i], Y[i]}], i - 1});
            enabled[{X[i], Y[i]}] = -1;
        }
    }
    for(auto [p, e] : enabled) if(e != -1) {
            auto [f, t] = p;
            edges.push_back({f, t, e, c + 1});
        }
    vector<int> R, R1, R2;
    solve(n, c, q, edges, W, P, R1);
    for(auto &it : P) it = n - 2 - it;
    for(auto &[a, b, c, d] : edges)
        a = n - 1 - a, b = n - 1 - b, a ^= b, b ^= a, a ^= b;
    solve(n, c, q, edges, W, P, R2);
    for(int i = 0; i < q; ++i) R.push_back(R1[i] + R2[i]);
    return R;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 724 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 7 ms 852 KB Output is correct
6 Correct 10 ms 1540 KB Output is correct
7 Correct 3 ms 852 KB Output is correct
8 Correct 3 ms 852 KB Output is correct
9 Correct 9 ms 2772 KB Output is correct
10 Correct 16 ms 3540 KB Output is correct
11 Correct 33 ms 6536 KB Output is correct
12 Correct 25 ms 6228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 3892 KB Output is correct
2 Correct 32 ms 3908 KB Output is correct
3 Correct 163 ms 8964 KB Output is correct
4 Correct 37 ms 4008 KB Output is correct
5 Correct 291 ms 10616 KB Output is correct
6 Correct 74 ms 4688 KB Output is correct
7 Correct 558 ms 26820 KB Output is correct
8 Correct 362 ms 26928 KB Output is correct
9 Correct 40 ms 9404 KB Output is correct
10 Correct 39 ms 10036 KB Output is correct
11 Correct 82 ms 22872 KB Output is correct
12 Correct 469 ms 142412 KB Output is correct
13 Correct 1603 ms 256820 KB Output is correct
14 Correct 4512 ms 455284 KB Output is correct
15 Correct 2540 ms 442068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 3900 KB Output is correct
2 Correct 30 ms 3648 KB Output is correct
3 Correct 32 ms 3456 KB Output is correct
4 Correct 33 ms 3760 KB Output is correct
5 Correct 62 ms 3776 KB Output is correct
6 Correct 92 ms 4984 KB Output is correct
7 Correct 462 ms 22112 KB Output is correct
8 Correct 1732 ms 99348 KB Output is correct
9 Correct 86 ms 11892 KB Output is correct
10 Correct 132 ms 23008 KB Output is correct
11 Correct 2846 ms 413096 KB Output is correct
12 Correct 5305 ms 456016 KB Output is correct
13 Correct 3475 ms 393152 KB Output is correct
14 Correct 5086 ms 459620 KB Output is correct
15 Correct 3066 ms 400380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 724 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 7 ms 852 KB Output is correct
6 Correct 10 ms 1540 KB Output is correct
7 Correct 3 ms 852 KB Output is correct
8 Correct 3 ms 852 KB Output is correct
9 Correct 9 ms 2772 KB Output is correct
10 Correct 16 ms 3540 KB Output is correct
11 Correct 33 ms 6536 KB Output is correct
12 Correct 25 ms 6228 KB Output is correct
13 Correct 27 ms 3892 KB Output is correct
14 Correct 32 ms 3908 KB Output is correct
15 Correct 163 ms 8964 KB Output is correct
16 Correct 37 ms 4008 KB Output is correct
17 Correct 291 ms 10616 KB Output is correct
18 Correct 74 ms 4688 KB Output is correct
19 Correct 558 ms 26820 KB Output is correct
20 Correct 362 ms 26928 KB Output is correct
21 Correct 40 ms 9404 KB Output is correct
22 Correct 39 ms 10036 KB Output is correct
23 Correct 82 ms 22872 KB Output is correct
24 Correct 469 ms 142412 KB Output is correct
25 Correct 1603 ms 256820 KB Output is correct
26 Correct 4512 ms 455284 KB Output is correct
27 Correct 2540 ms 442068 KB Output is correct
28 Correct 27 ms 3900 KB Output is correct
29 Correct 30 ms 3648 KB Output is correct
30 Correct 32 ms 3456 KB Output is correct
31 Correct 33 ms 3760 KB Output is correct
32 Correct 62 ms 3776 KB Output is correct
33 Correct 92 ms 4984 KB Output is correct
34 Correct 462 ms 22112 KB Output is correct
35 Correct 1732 ms 99348 KB Output is correct
36 Correct 86 ms 11892 KB Output is correct
37 Correct 132 ms 23008 KB Output is correct
38 Correct 2846 ms 413096 KB Output is correct
39 Correct 5305 ms 456016 KB Output is correct
40 Correct 3475 ms 393152 KB Output is correct
41 Correct 5086 ms 459620 KB Output is correct
42 Correct 3066 ms 400380 KB Output is correct
43 Correct 300 ms 15772 KB Output is correct
44 Correct 548 ms 28428 KB Output is correct
45 Correct 470 ms 29212 KB Output is correct
46 Correct 1713 ms 99960 KB Output is correct
47 Correct 86 ms 12388 KB Output is correct
48 Correct 68 ms 12232 KB Output is correct
49 Correct 132 ms 25276 KB Output is correct
50 Correct 343 ms 65376 KB Output is correct
51 Correct 667 ms 147124 KB Output is correct
52 Correct 1321 ms 210928 KB Output is correct
53 Correct 1122 ms 215772 KB Output is correct
54 Correct 2008 ms 274960 KB Output is correct
55 Correct 1774 ms 268500 KB Output is correct
56 Correct 2456 ms 319500 KB Output is correct
57 Correct 2952 ms 353912 KB Output is correct
58 Correct 4081 ms 400708 KB Output is correct
59 Correct 2910 ms 411960 KB Output is correct
60 Correct 5039 ms 458008 KB Output is correct