답안 #1058012

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1058012 2024-08-14T07:59:42 Z 정민찬(#11077) Telephone Plans (CCO24_day2problem3) C++17
20 / 25
268 ms 193364 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;

set<ll> adj[500010];
ll sz[500010];
ll id[500010];

void dfs(ll x, ll p, ll nw) {
    id[x] = nw;
    for (auto &y : adj[x]) {
        if (y == p) continue;
        dfs(y, x, nw);
    }
}

ll getSize(ll x, ll p) {
    ll ret = 1;
    for (auto &y : adj[x]) {
        if (y == p) continue;
        ret += getSize(y, x);
    }
    return ret;
}

ll add[1500010];

int main() {
    ios_base :: sync_with_stdio(false); cin.tie(NULL);
    ll E;
    cin >> E;
    ll N, Q;
    cin >> N >> Q;
    for (ll i=1; i<=N; i++) {
        id[i] = i;
        sz[i] = 1;
    }
    ll pv = N;
    ll prv = 0;
    ll cur = 0;
    for (ll q=1; q<=Q; q++) {
        if (q != 1) add[q-1] = add[q-2];
        ll cmd;
        cin >> cmd;
        if (cmd == 1) {
            ll x, y;
            cin >> x >> y;
            if (E) {
                x ^= prv;
                y ^= prv;
            }
            if (sz[id[x]] < sz[id[y]]) swap(x, y);
            cur -= sz[id[x]] * (sz[id[x]]-1) / 2;
            cur -= sz[id[y]] * (sz[id[y]]-1) / 2;
            sz[id[x]] += sz[id[y]];
            cur += sz[id[x]] * (sz[id[x]]-1) / 2;
            dfs(y, -1, id[x]);
            adj[x].insert(y);
            adj[y].insert(x);
        }
        else if (cmd == 2) {
            ll x, y;
            cin >> x >> y;
            if (E) {
                x ^= prv;
                y ^= prv;
            }
            adj[x].erase(y);
            adj[y].erase(x);
            ll cnt = 1;
            vector<array<ll,3>> stx, sty;
            stx.push_back({x, -1, -1});
            sty.push_back({y, -1, -1});
            while (true) {
                while (!stx.empty()) {
                    ll vtx = stx.back()[0];
                    ll par = stx.back()[1];
                    ll lo = stx.back()[2];
                    stx.pop_back();
                    auto it = adj[vtx].upper_bound(lo);
                    if (it == adj[vtx].end()) continue;
                    if (*it == par)
                        it = adj[vtx].upper_bound(par);
                    if (it == adj[vtx].end()) continue;
                    stx.push_back({vtx, par, *it});
                    stx.push_back({*it, vtx, -1});
                    break;
                }
                while (!sty.empty()) {
                    ll vtx = sty.back()[0];
                    ll par = sty.back()[1];
                    ll lo = sty.back()[2];
                    sty.pop_back();
                    auto it = adj[vtx].upper_bound(lo);
                    if (it == adj[vtx].end()) continue;
                    if (*it == par)
                        it = adj[vtx].upper_bound(par);
                    if (it == adj[vtx].end()) continue;
                    sty.push_back({vtx, par, *it});
                    sty.push_back({*it, vtx, -1});
                    break;
                }
                if (stx.empty() || sty.empty()) break;
                cnt ++;
            }
            if (stx.empty()) {
                swap(x, y);
            }
            ll szx = sz[id[x]] - cnt;
            ll szy = cnt;
            dfs(y, -1, ++pv);
            sz[id[x]] = szx;
            sz[id[y]] = szy;
            add[q-1] += szx * szy;
            cur -= szx * szy;
        }
        else {
            ll t;
            cin >> t;
            if (E) {
                t ^= prv;
            }
            ll ans = cur;
            if (q-t == 0) ans += add[q-1];
            else ans += add[q-1] - add[q-t-1];
            cout << ans << '\n';
            prv = ans;
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 28760 KB Output is correct
2 Correct 4 ms 28764 KB Output is correct
3 Correct 4 ms 28764 KB Output is correct
4 Correct 3 ms 28764 KB Output is correct
5 Correct 4 ms 28764 KB Output is correct
6 Correct 3 ms 28764 KB Output is correct
7 Correct 3 ms 28764 KB Output is correct
8 Correct 4 ms 28764 KB Output is correct
9 Correct 3 ms 28860 KB Output is correct
10 Correct 4 ms 28764 KB Output is correct
11 Correct 4 ms 28764 KB Output is correct
12 Correct 4 ms 28764 KB Output is correct
13 Correct 3 ms 28764 KB Output is correct
14 Correct 4 ms 28764 KB Output is correct
15 Correct 3 ms 28764 KB Output is correct
16 Correct 3 ms 28764 KB Output is correct
17 Correct 3 ms 28764 KB Output is correct
18 Correct 4 ms 28764 KB Output is correct
19 Correct 3 ms 28764 KB Output is correct
20 Correct 3 ms 28764 KB Output is correct
21 Correct 4 ms 28764 KB Output is correct
22 Correct 3 ms 28908 KB Output is correct
23 Correct 3 ms 28764 KB Output is correct
24 Correct 4 ms 28764 KB Output is correct
25 Correct 4 ms 28764 KB Output is correct
26 Correct 4 ms 28752 KB Output is correct
27 Correct 4 ms 28764 KB Output is correct
28 Correct 3 ms 28764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 28764 KB Output is correct
2 Correct 3 ms 28764 KB Output is correct
3 Correct 3 ms 28764 KB Output is correct
4 Correct 4 ms 28764 KB Output is correct
5 Correct 4 ms 28764 KB Output is correct
6 Correct 4 ms 28836 KB Output is correct
7 Correct 3 ms 28764 KB Output is correct
8 Correct 3 ms 28968 KB Output is correct
9 Correct 3 ms 28764 KB Output is correct
10 Correct 3 ms 28936 KB Output is correct
11 Correct 4 ms 28764 KB Output is correct
12 Correct 4 ms 28764 KB Output is correct
13 Correct 4 ms 28764 KB Output is correct
14 Correct 4 ms 28760 KB Output is correct
15 Correct 4 ms 28764 KB Output is correct
16 Correct 4 ms 28764 KB Output is correct
17 Correct 4 ms 28764 KB Output is correct
18 Correct 4 ms 28764 KB Output is correct
19 Correct 4 ms 28888 KB Output is correct
20 Correct 4 ms 28764 KB Output is correct
21 Correct 3 ms 28764 KB Output is correct
22 Correct 4 ms 28912 KB Output is correct
23 Correct 3 ms 28764 KB Output is correct
24 Correct 4 ms 28764 KB Output is correct
25 Correct 3 ms 28764 KB Output is correct
26 Correct 3 ms 28868 KB Output is correct
27 Correct 4 ms 28764 KB Output is correct
28 Correct 3 ms 28764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 28760 KB Output is correct
2 Correct 4 ms 28764 KB Output is correct
3 Correct 4 ms 28764 KB Output is correct
4 Correct 3 ms 28764 KB Output is correct
5 Correct 4 ms 28764 KB Output is correct
6 Correct 3 ms 28764 KB Output is correct
7 Correct 3 ms 28764 KB Output is correct
8 Correct 4 ms 28764 KB Output is correct
9 Correct 3 ms 28860 KB Output is correct
10 Correct 4 ms 28764 KB Output is correct
11 Correct 4 ms 28764 KB Output is correct
12 Correct 4 ms 28764 KB Output is correct
13 Correct 3 ms 28764 KB Output is correct
14 Correct 4 ms 28764 KB Output is correct
15 Correct 3 ms 28764 KB Output is correct
16 Correct 3 ms 28764 KB Output is correct
17 Correct 3 ms 28764 KB Output is correct
18 Correct 4 ms 28764 KB Output is correct
19 Correct 3 ms 28764 KB Output is correct
20 Correct 3 ms 28764 KB Output is correct
21 Correct 4 ms 28764 KB Output is correct
22 Correct 3 ms 28908 KB Output is correct
23 Correct 3 ms 28764 KB Output is correct
24 Correct 4 ms 28764 KB Output is correct
25 Correct 4 ms 28764 KB Output is correct
26 Correct 4 ms 28752 KB Output is correct
27 Correct 4 ms 28764 KB Output is correct
28 Correct 3 ms 28764 KB Output is correct
29 Correct 7 ms 29020 KB Output is correct
30 Correct 4 ms 29084 KB Output is correct
31 Correct 4 ms 28940 KB Output is correct
32 Correct 5 ms 29020 KB Output is correct
33 Correct 5 ms 29148 KB Output is correct
34 Correct 5 ms 29020 KB Output is correct
35 Correct 5 ms 29020 KB Output is correct
36 Correct 5 ms 29020 KB Output is correct
37 Correct 5 ms 29048 KB Output is correct
38 Correct 5 ms 29020 KB Output is correct
39 Correct 7 ms 29020 KB Output is correct
40 Correct 5 ms 29108 KB Output is correct
41 Correct 5 ms 29168 KB Output is correct
42 Correct 6 ms 29276 KB Output is correct
43 Correct 5 ms 29144 KB Output is correct
44 Correct 5 ms 29020 KB Output is correct
45 Correct 6 ms 29020 KB Output is correct
46 Correct 5 ms 29020 KB Output is correct
47 Correct 5 ms 29016 KB Output is correct
48 Correct 5 ms 29032 KB Output is correct
49 Correct 4 ms 29020 KB Output is correct
50 Correct 4 ms 28864 KB Output is correct
51 Correct 4 ms 29036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 28764 KB Output is correct
2 Correct 3 ms 28764 KB Output is correct
3 Correct 3 ms 28764 KB Output is correct
4 Correct 4 ms 28764 KB Output is correct
5 Correct 4 ms 28764 KB Output is correct
6 Correct 4 ms 28836 KB Output is correct
7 Correct 3 ms 28764 KB Output is correct
8 Correct 3 ms 28968 KB Output is correct
9 Correct 3 ms 28764 KB Output is correct
10 Correct 3 ms 28936 KB Output is correct
11 Correct 4 ms 28764 KB Output is correct
12 Correct 4 ms 28764 KB Output is correct
13 Correct 4 ms 28764 KB Output is correct
14 Correct 4 ms 28760 KB Output is correct
15 Correct 4 ms 28764 KB Output is correct
16 Correct 4 ms 28764 KB Output is correct
17 Correct 4 ms 28764 KB Output is correct
18 Correct 4 ms 28764 KB Output is correct
19 Correct 4 ms 28888 KB Output is correct
20 Correct 4 ms 28764 KB Output is correct
21 Correct 3 ms 28764 KB Output is correct
22 Correct 4 ms 28912 KB Output is correct
23 Correct 3 ms 28764 KB Output is correct
24 Correct 4 ms 28764 KB Output is correct
25 Correct 3 ms 28764 KB Output is correct
26 Correct 3 ms 28868 KB Output is correct
27 Correct 4 ms 28764 KB Output is correct
28 Correct 3 ms 28764 KB Output is correct
29 Correct 5 ms 29020 KB Output is correct
30 Correct 4 ms 29020 KB Output is correct
31 Correct 4 ms 28976 KB Output is correct
32 Correct 6 ms 29020 KB Output is correct
33 Correct 6 ms 29020 KB Output is correct
34 Correct 4 ms 29020 KB Output is correct
35 Correct 4 ms 29020 KB Output is correct
36 Correct 5 ms 29020 KB Output is correct
37 Correct 5 ms 29044 KB Output is correct
38 Correct 7 ms 29276 KB Output is correct
39 Correct 5 ms 29020 KB Output is correct
40 Correct 5 ms 29140 KB Output is correct
41 Correct 5 ms 29020 KB Output is correct
42 Correct 5 ms 29140 KB Output is correct
43 Correct 5 ms 29020 KB Output is correct
44 Correct 5 ms 29020 KB Output is correct
45 Correct 5 ms 29020 KB Output is correct
46 Correct 4 ms 29020 KB Output is correct
47 Correct 5 ms 29020 KB Output is correct
48 Correct 5 ms 29016 KB Output is correct
49 Correct 5 ms 29012 KB Output is correct
50 Correct 4 ms 29020 KB Output is correct
51 Correct 5 ms 29016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 28760 KB Output is correct
2 Correct 4 ms 28764 KB Output is correct
3 Correct 4 ms 28764 KB Output is correct
4 Correct 3 ms 28764 KB Output is correct
5 Correct 4 ms 28764 KB Output is correct
6 Correct 3 ms 28764 KB Output is correct
7 Correct 3 ms 28764 KB Output is correct
8 Correct 4 ms 28764 KB Output is correct
9 Correct 3 ms 28860 KB Output is correct
10 Correct 4 ms 28764 KB Output is correct
11 Correct 4 ms 28764 KB Output is correct
12 Correct 4 ms 28764 KB Output is correct
13 Correct 3 ms 28764 KB Output is correct
14 Correct 4 ms 28764 KB Output is correct
15 Correct 3 ms 28764 KB Output is correct
16 Correct 3 ms 28764 KB Output is correct
17 Correct 3 ms 28764 KB Output is correct
18 Correct 4 ms 28764 KB Output is correct
19 Correct 3 ms 28764 KB Output is correct
20 Correct 3 ms 28764 KB Output is correct
21 Correct 4 ms 28764 KB Output is correct
22 Correct 3 ms 28908 KB Output is correct
23 Correct 3 ms 28764 KB Output is correct
24 Correct 4 ms 28764 KB Output is correct
25 Correct 4 ms 28764 KB Output is correct
26 Correct 4 ms 28752 KB Output is correct
27 Correct 4 ms 28764 KB Output is correct
28 Correct 3 ms 28764 KB Output is correct
29 Correct 7 ms 29020 KB Output is correct
30 Correct 4 ms 29084 KB Output is correct
31 Correct 4 ms 28940 KB Output is correct
32 Correct 5 ms 29020 KB Output is correct
33 Correct 5 ms 29148 KB Output is correct
34 Correct 5 ms 29020 KB Output is correct
35 Correct 5 ms 29020 KB Output is correct
36 Correct 5 ms 29020 KB Output is correct
37 Correct 5 ms 29048 KB Output is correct
38 Correct 5 ms 29020 KB Output is correct
39 Correct 7 ms 29020 KB Output is correct
40 Correct 5 ms 29108 KB Output is correct
41 Correct 5 ms 29168 KB Output is correct
42 Correct 6 ms 29276 KB Output is correct
43 Correct 5 ms 29144 KB Output is correct
44 Correct 5 ms 29020 KB Output is correct
45 Correct 6 ms 29020 KB Output is correct
46 Correct 5 ms 29020 KB Output is correct
47 Correct 5 ms 29016 KB Output is correct
48 Correct 5 ms 29032 KB Output is correct
49 Correct 4 ms 29020 KB Output is correct
50 Correct 4 ms 28864 KB Output is correct
51 Correct 4 ms 29036 KB Output is correct
52 Correct 69 ms 49228 KB Output is correct
53 Correct 85 ms 51280 KB Output is correct
54 Correct 84 ms 46676 KB Output is correct
55 Correct 73 ms 50616 KB Output is correct
56 Correct 126 ms 51024 KB Output is correct
57 Correct 119 ms 47184 KB Output is correct
58 Correct 77 ms 51024 KB Output is correct
59 Correct 115 ms 51060 KB Output is correct
60 Correct 129 ms 45652 KB Output is correct
61 Correct 128 ms 48424 KB Output is correct
62 Correct 117 ms 49876 KB Output is correct
63 Correct 152 ms 44968 KB Output is correct
64 Correct 116 ms 50808 KB Output is correct
65 Correct 125 ms 50356 KB Output is correct
66 Correct 116 ms 47528 KB Output is correct
67 Correct 81 ms 51268 KB Output is correct
68 Correct 128 ms 51176 KB Output is correct
69 Correct 84 ms 51140 KB Output is correct
70 Correct 141 ms 51220 KB Output is correct
71 Correct 81 ms 47220 KB Output is correct
72 Correct 89 ms 47252 KB Output is correct
73 Correct 40 ms 39764 KB Output is correct
74 Correct 39 ms 38144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 28764 KB Output is correct
2 Correct 3 ms 28764 KB Output is correct
3 Correct 3 ms 28764 KB Output is correct
4 Correct 4 ms 28764 KB Output is correct
5 Correct 4 ms 28764 KB Output is correct
6 Correct 4 ms 28836 KB Output is correct
7 Correct 3 ms 28764 KB Output is correct
8 Correct 3 ms 28968 KB Output is correct
9 Correct 3 ms 28764 KB Output is correct
10 Correct 3 ms 28936 KB Output is correct
11 Correct 4 ms 28764 KB Output is correct
12 Correct 4 ms 28764 KB Output is correct
13 Correct 4 ms 28764 KB Output is correct
14 Correct 4 ms 28760 KB Output is correct
15 Correct 4 ms 28764 KB Output is correct
16 Correct 4 ms 28764 KB Output is correct
17 Correct 4 ms 28764 KB Output is correct
18 Correct 4 ms 28764 KB Output is correct
19 Correct 4 ms 28888 KB Output is correct
20 Correct 4 ms 28764 KB Output is correct
21 Correct 3 ms 28764 KB Output is correct
22 Correct 4 ms 28912 KB Output is correct
23 Correct 3 ms 28764 KB Output is correct
24 Correct 4 ms 28764 KB Output is correct
25 Correct 3 ms 28764 KB Output is correct
26 Correct 3 ms 28868 KB Output is correct
27 Correct 4 ms 28764 KB Output is correct
28 Correct 3 ms 28764 KB Output is correct
29 Correct 5 ms 29020 KB Output is correct
30 Correct 4 ms 29020 KB Output is correct
31 Correct 4 ms 28976 KB Output is correct
32 Correct 6 ms 29020 KB Output is correct
33 Correct 6 ms 29020 KB Output is correct
34 Correct 4 ms 29020 KB Output is correct
35 Correct 4 ms 29020 KB Output is correct
36 Correct 5 ms 29020 KB Output is correct
37 Correct 5 ms 29044 KB Output is correct
38 Correct 7 ms 29276 KB Output is correct
39 Correct 5 ms 29020 KB Output is correct
40 Correct 5 ms 29140 KB Output is correct
41 Correct 5 ms 29020 KB Output is correct
42 Correct 5 ms 29140 KB Output is correct
43 Correct 5 ms 29020 KB Output is correct
44 Correct 5 ms 29020 KB Output is correct
45 Correct 5 ms 29020 KB Output is correct
46 Correct 4 ms 29020 KB Output is correct
47 Correct 5 ms 29020 KB Output is correct
48 Correct 5 ms 29016 KB Output is correct
49 Correct 5 ms 29012 KB Output is correct
50 Correct 4 ms 29020 KB Output is correct
51 Correct 5 ms 29016 KB Output is correct
52 Correct 105 ms 50512 KB Output is correct
53 Correct 76 ms 53388 KB Output is correct
54 Correct 71 ms 47552 KB Output is correct
55 Correct 74 ms 53332 KB Output is correct
56 Correct 121 ms 52560 KB Output is correct
57 Correct 77 ms 47392 KB Output is correct
58 Correct 90 ms 53584 KB Output is correct
59 Correct 117 ms 52988 KB Output is correct
60 Correct 83 ms 47440 KB Output is correct
61 Correct 114 ms 53292 KB Output is correct
62 Correct 141 ms 53312 KB Output is correct
63 Correct 113 ms 49748 KB Output is correct
64 Correct 89 ms 53332 KB Output is correct
65 Correct 115 ms 53292 KB Output is correct
66 Correct 136 ms 49696 KB Output is correct
67 Correct 95 ms 52756 KB Output is correct
68 Correct 132 ms 51212 KB Output is correct
69 Correct 79 ms 53476 KB Output is correct
70 Correct 137 ms 52816 KB Output is correct
71 Correct 123 ms 47428 KB Output is correct
72 Correct 112 ms 47700 KB Output is correct
73 Correct 68 ms 40016 KB Output is correct
74 Correct 41 ms 38172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 28764 KB Output is correct
2 Correct 3 ms 28764 KB Output is correct
3 Correct 3 ms 28764 KB Output is correct
4 Correct 4 ms 28764 KB Output is correct
5 Correct 4 ms 28764 KB Output is correct
6 Correct 4 ms 28836 KB Output is correct
7 Correct 3 ms 28764 KB Output is correct
8 Correct 3 ms 28968 KB Output is correct
9 Correct 3 ms 28764 KB Output is correct
10 Correct 3 ms 28936 KB Output is correct
11 Correct 4 ms 28764 KB Output is correct
12 Correct 4 ms 28764 KB Output is correct
13 Correct 4 ms 28764 KB Output is correct
14 Correct 4 ms 28760 KB Output is correct
15 Correct 4 ms 28764 KB Output is correct
16 Correct 4 ms 28764 KB Output is correct
17 Correct 4 ms 28764 KB Output is correct
18 Correct 4 ms 28764 KB Output is correct
19 Correct 4 ms 28888 KB Output is correct
20 Correct 4 ms 28764 KB Output is correct
21 Correct 3 ms 28764 KB Output is correct
22 Correct 4 ms 28912 KB Output is correct
23 Correct 3 ms 28764 KB Output is correct
24 Correct 4 ms 28764 KB Output is correct
25 Correct 3 ms 28764 KB Output is correct
26 Correct 3 ms 28868 KB Output is correct
27 Correct 4 ms 28764 KB Output is correct
28 Correct 3 ms 28764 KB Output is correct
29 Correct 5 ms 29020 KB Output is correct
30 Correct 4 ms 29020 KB Output is correct
31 Correct 4 ms 28976 KB Output is correct
32 Correct 6 ms 29020 KB Output is correct
33 Correct 6 ms 29020 KB Output is correct
34 Correct 4 ms 29020 KB Output is correct
35 Correct 4 ms 29020 KB Output is correct
36 Correct 5 ms 29020 KB Output is correct
37 Correct 5 ms 29044 KB Output is correct
38 Correct 7 ms 29276 KB Output is correct
39 Correct 5 ms 29020 KB Output is correct
40 Correct 5 ms 29140 KB Output is correct
41 Correct 5 ms 29020 KB Output is correct
42 Correct 5 ms 29140 KB Output is correct
43 Correct 5 ms 29020 KB Output is correct
44 Correct 5 ms 29020 KB Output is correct
45 Correct 5 ms 29020 KB Output is correct
46 Correct 4 ms 29020 KB Output is correct
47 Correct 5 ms 29020 KB Output is correct
48 Correct 5 ms 29016 KB Output is correct
49 Correct 5 ms 29012 KB Output is correct
50 Correct 4 ms 29020 KB Output is correct
51 Correct 5 ms 29016 KB Output is correct
52 Correct 105 ms 50512 KB Output is correct
53 Correct 76 ms 53388 KB Output is correct
54 Correct 71 ms 47552 KB Output is correct
55 Correct 74 ms 53332 KB Output is correct
56 Correct 121 ms 52560 KB Output is correct
57 Correct 77 ms 47392 KB Output is correct
58 Correct 90 ms 53584 KB Output is correct
59 Correct 117 ms 52988 KB Output is correct
60 Correct 83 ms 47440 KB Output is correct
61 Correct 114 ms 53292 KB Output is correct
62 Correct 141 ms 53312 KB Output is correct
63 Correct 113 ms 49748 KB Output is correct
64 Correct 89 ms 53332 KB Output is correct
65 Correct 115 ms 53292 KB Output is correct
66 Correct 136 ms 49696 KB Output is correct
67 Correct 95 ms 52756 KB Output is correct
68 Correct 132 ms 51212 KB Output is correct
69 Correct 79 ms 53476 KB Output is correct
70 Correct 137 ms 52816 KB Output is correct
71 Correct 123 ms 47428 KB Output is correct
72 Correct 112 ms 47700 KB Output is correct
73 Correct 68 ms 40016 KB Output is correct
74 Correct 41 ms 38172 KB Output is correct
75 Runtime error 268 ms 193364 KB Execution killed with signal 11
76 Halted 0 ms 0 KB -