답안 #973354

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
973354 2024-05-01T19:54:00 Z vladilius Joker (BOI20_joker) C++17
100 / 100
429 ms 23628 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int unsigned int
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second

struct dsu{
    vector<pair<int&, int>> hist;
    vector<int> sz, p;
    vector<bool> f;
    int n;
    dsu(int ns){
        n = ns;
        p.resize(2 * n + 1);
        sz.resize(2 * n + 1);
        for (int i = 1; i <= 2 * n; i++){
            p[i] = i;
            sz[i] = 1;
        }
    }
    int get(int v){
        if (p[v] != v){
            return get(p[v]);
        }
        return v;
    }
    int op(int v){
        return v + n;
    }
    void unite(int x, int y){
        x = get(x); y = get(y);
        if (x == y) return;
        if (sz[x] > sz[y]) swap(x, y);
        hist.pb({p[x], p[x]});
        hist.pb({sz[y], sz[y]});
        p[x] = y;
        sz[y] += sz[x];
    }
    bool add(pii P){
        auto [x, y] = P;
        if (!x) return 0;
        unite(x, op(y));
        unite(y, op(x));
        bool k = ((!f.empty() && f.back()) | (get(x) == get(y)));
        f.pb(k);
        return k;
    }
    void roll_back(int S, int Sf){
        while (hist.size() != S){
            hist.back().ff = hist.back().ss;
            hist.pop_back();
        }
        while (f.size() != Sf){
            f.pop_back();
        }
    }
};

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n, M, q; cin>>n>>M>>q;
    vector<pii> ed = {{0, 0}};
    for (int i = 1; i <= M; i++){
        int u, v; cin>>u>>v;
        ed.pb({u, v});
    }
    dsu T(n);
    for (int i = 1; i <= M; i++){
        T.add(ed[i]);
    }
    if (!T.f.back()){
        for (int i = 1; i <= q; i++){
            cout<<"NO"<<"\n";
        }
        return 0;
    }
    T.roll_back(0, 0);
    
    vector<int> last(M + 1);
    function<void(int, int, pii)> solve = [&](int l, int r, pii R){
        if (l > r) return;

        int m = (l + r) / 2;
        int S = (int) T.hist.size(), Sf = (int) T.f.size();
        for (int i = l; i < m; i++){
            T.add(ed[i]);
        }
        
        int i = R.ss;
        while (T.f.empty() || !T.f.back()){
            T.add(ed[i--]);
        }
        last[m] = i;
        T.roll_back(S, Sf);
        
        S = (int) T.hist.size(); Sf = (int) T.f.size();
        for (int i = last[m] + 1; i <= R.ss; i++){
            T.add(ed[i]);
        }
        solve(l, m - 1, {R.ff, last[m]});
        T.roll_back(S, Sf);
        
        S = (int) T.hist.size(); Sf = (int) T.f.size();
        for (int i = l; i <= m; i++){
            T.add(ed[i]);
        }
        solve(m + 1, r, {last[m], R.ss});
        T.roll_back(S, Sf);
    };
    
    solve(1, M, {0, M});

    while (q--){
        int l, r; cin>>l>>r;
        cout<<((r <= last[l]) ? "YES" : "NO")<<"\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 416 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 420 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 416 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 420 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 612 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 3 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 600 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 616 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 348 KB Output is correct
44 Correct 2 ms 616 KB Output is correct
45 Correct 2 ms 600 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 3 ms 600 KB Output is correct
49 Correct 2 ms 600 KB Output is correct
50 Correct 4 ms 604 KB Output is correct
51 Correct 2 ms 596 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 604 KB Output is correct
54 Correct 2 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 227 ms 13640 KB Output is correct
4 Correct 47 ms 21568 KB Output is correct
5 Correct 232 ms 22220 KB Output is correct
6 Correct 266 ms 12620 KB Output is correct
7 Correct 282 ms 12364 KB Output is correct
8 Correct 286 ms 9040 KB Output is correct
9 Correct 313 ms 12620 KB Output is correct
10 Correct 368 ms 23112 KB Output is correct
11 Correct 280 ms 12364 KB Output is correct
12 Correct 335 ms 22312 KB Output is correct
13 Correct 307 ms 5192 KB Output is correct
14 Correct 284 ms 8088 KB Output is correct
15 Correct 382 ms 20800 KB Output is correct
16 Correct 383 ms 22076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 416 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 420 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 227 ms 13640 KB Output is correct
30 Correct 47 ms 21568 KB Output is correct
31 Correct 232 ms 22220 KB Output is correct
32 Correct 266 ms 12620 KB Output is correct
33 Correct 282 ms 12364 KB Output is correct
34 Correct 286 ms 9040 KB Output is correct
35 Correct 313 ms 12620 KB Output is correct
36 Correct 368 ms 23112 KB Output is correct
37 Correct 280 ms 12364 KB Output is correct
38 Correct 335 ms 22312 KB Output is correct
39 Correct 307 ms 5192 KB Output is correct
40 Correct 284 ms 8088 KB Output is correct
41 Correct 382 ms 20800 KB Output is correct
42 Correct 383 ms 22076 KB Output is correct
43 Correct 230 ms 13392 KB Output is correct
44 Correct 57 ms 22608 KB Output is correct
45 Correct 379 ms 23108 KB Output is correct
46 Correct 266 ms 12116 KB Output is correct
47 Correct 264 ms 13640 KB Output is correct
48 Correct 347 ms 12876 KB Output is correct
49 Correct 429 ms 23628 KB Output is correct
50 Correct 252 ms 12628 KB Output is correct
51 Correct 293 ms 22348 KB Output is correct
52 Correct 340 ms 22356 KB Output is correct
53 Correct 290 ms 5332 KB Output is correct
54 Correct 349 ms 11596 KB Output is correct
55 Correct 380 ms 21060 KB Output is correct
56 Correct 405 ms 22860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 416 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 420 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 612 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 3 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 600 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 616 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 348 KB Output is correct
44 Correct 2 ms 616 KB Output is correct
45 Correct 2 ms 600 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 3 ms 600 KB Output is correct
49 Correct 2 ms 600 KB Output is correct
50 Correct 4 ms 604 KB Output is correct
51 Correct 2 ms 596 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 604 KB Output is correct
54 Correct 2 ms 600 KB Output is correct
55 Correct 223 ms 13124 KB Output is correct
56 Correct 50 ms 22560 KB Output is correct
57 Correct 207 ms 22952 KB Output is correct
58 Correct 255 ms 12304 KB Output is correct
59 Correct 304 ms 13648 KB Output is correct
60 Correct 388 ms 22028 KB Output is correct
61 Correct 271 ms 12376 KB Output is correct
62 Correct 295 ms 22716 KB Output is correct
63 Correct 245 ms 8276 KB Output is correct
64 Correct 352 ms 13908 KB Output is correct
65 Correct 377 ms 23584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 416 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 420 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 612 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 3 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 600 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 616 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 348 KB Output is correct
44 Correct 2 ms 616 KB Output is correct
45 Correct 2 ms 600 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 3 ms 600 KB Output is correct
49 Correct 2 ms 600 KB Output is correct
50 Correct 4 ms 604 KB Output is correct
51 Correct 2 ms 596 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 604 KB Output is correct
54 Correct 2 ms 600 KB Output is correct
55 Correct 227 ms 13640 KB Output is correct
56 Correct 47 ms 21568 KB Output is correct
57 Correct 232 ms 22220 KB Output is correct
58 Correct 266 ms 12620 KB Output is correct
59 Correct 282 ms 12364 KB Output is correct
60 Correct 286 ms 9040 KB Output is correct
61 Correct 313 ms 12620 KB Output is correct
62 Correct 368 ms 23112 KB Output is correct
63 Correct 280 ms 12364 KB Output is correct
64 Correct 335 ms 22312 KB Output is correct
65 Correct 307 ms 5192 KB Output is correct
66 Correct 284 ms 8088 KB Output is correct
67 Correct 382 ms 20800 KB Output is correct
68 Correct 383 ms 22076 KB Output is correct
69 Correct 230 ms 13392 KB Output is correct
70 Correct 57 ms 22608 KB Output is correct
71 Correct 379 ms 23108 KB Output is correct
72 Correct 266 ms 12116 KB Output is correct
73 Correct 264 ms 13640 KB Output is correct
74 Correct 347 ms 12876 KB Output is correct
75 Correct 429 ms 23628 KB Output is correct
76 Correct 252 ms 12628 KB Output is correct
77 Correct 293 ms 22348 KB Output is correct
78 Correct 340 ms 22356 KB Output is correct
79 Correct 290 ms 5332 KB Output is correct
80 Correct 349 ms 11596 KB Output is correct
81 Correct 380 ms 21060 KB Output is correct
82 Correct 405 ms 22860 KB Output is correct
83 Correct 223 ms 13124 KB Output is correct
84 Correct 50 ms 22560 KB Output is correct
85 Correct 207 ms 22952 KB Output is correct
86 Correct 255 ms 12304 KB Output is correct
87 Correct 304 ms 13648 KB Output is correct
88 Correct 388 ms 22028 KB Output is correct
89 Correct 271 ms 12376 KB Output is correct
90 Correct 295 ms 22716 KB Output is correct
91 Correct 245 ms 8276 KB Output is correct
92 Correct 352 ms 13908 KB Output is correct
93 Correct 377 ms 23584 KB Output is correct
94 Correct 255 ms 12368 KB Output is correct
95 Correct 335 ms 23112 KB Output is correct
96 Correct 249 ms 21328 KB Output is correct
97 Correct 271 ms 13136 KB Output is correct
98 Correct 277 ms 13652 KB Output is correct
99 Correct 309 ms 8256 KB Output is correct
100 Correct 386 ms 22340 KB Output is correct
101 Correct 281 ms 13140 KB Output is correct
102 Correct 335 ms 21776 KB Output is correct
103 Correct 360 ms 22860 KB Output is correct
104 Correct 294 ms 8524 KB Output is correct
105 Correct 361 ms 13656 KB Output is correct
106 Correct 389 ms 22164 KB Output is correct
107 Correct 250 ms 22852 KB Output is correct
108 Correct 228 ms 13640 KB Output is correct
109 Correct 246 ms 12620 KB Output is correct
110 Correct 233 ms 13140 KB Output is correct
111 Correct 236 ms 13900 KB Output is correct
112 Correct 245 ms 13140 KB Output is correct
113 Correct 233 ms 12112 KB Output is correct
114 Correct 273 ms 12212 KB Output is correct
115 Correct 231 ms 13384 KB Output is correct
116 Correct 244 ms 12372 KB Output is correct