답안 #602329

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
602329 2022-07-22T22:17:29 Z OttoTheDino Joker (BOI20_joker) C++17
100 / 100
703 ms 26604 KB
#include<bits/stdc++.h>
using namespace std;

#define rep(i,s,e)                          for (int i = s; i <= e; ++i)
#define rrep(i,s,e)                         for (int i = s; i >= e; --i)
#define fi                                  first
#define se                                  second
#define pb                                  push_back
#define len(a)                              (int)a.size()
typedef vector<int> vi;
typedef pair<int,int> ii;

const int mx = 2e5;
vi adj[mx+1];

int par[mx+1], h[mx+1], tp[mx+1], dp[mx+1], cnt, m;
ii e[mx+1];
vector<vi> history;

ii get_par (int u) {
    if (u==par[u]) return {u, tp[u]};
    ii res = get_par (par[u]); 
    ii ans = {res.fi, res.se^tp[u]};
    return {ans};
}

bool merge (int u, int v) {
    ii paru = get_par(u), parv = get_par(v);
    if (paru.fi==parv.fi) {
        history.pb({parv.fi, parv.fi, paru.se==parv.se, 0});
        return paru.se==parv.se;
    }
    if (h[paru.fi]<h[parv.fi]) swap(paru, parv);
    history.pb({parv.fi, paru.fi, h[paru.fi]==h[parv.fi], parv.se==paru.se});
    h[paru.fi] += h[paru.fi]==h[parv.fi];
    par[parv.fi] = paru.fi; 
    tp[parv.fi] ^= parv.se==paru.se;
    return 0;
}

int rollback (int t) {
    int sum = 0;
    while (t--) {
        int a = history.back()[0], b = history.back()[1], c = history.back()[2], d = history.back()[3];
        history.pop_back();
        if (a==b) {
            sum += c;
            continue;
        }
        par[a] = a;
        h[b] -= c;
        tp[a] ^= d;
    }
    return sum;
}

void go (int l1, int l2, int r1, int r2) {
    if (r1==r2) {
        rep (i,l1,l2) dp[i] = r1;
        return;
    }

    int lm = (l1+l2)/2, rm = r2, steps = 0;
    rep (i,l1,lm) {
        if (i) {
            cnt += merge(e[i].fi, e[i].se);
            steps++;
        }
    }

    bool suc = 0;

    if (cnt) {
        suc = 1;
        goto nxt;
    }

    while (true) {
        if (rm<=m) {
            cnt += merge(e[rm].fi, e[rm].se);
            steps++;
        }
        if (cnt) {
            suc = 1;
            break;
        }
        if (rm==lm+1) break;
        --rm;
    }

    nxt:

    cnt -= rollback(steps);

    if (!suc) {
        //don't need to do anything with dp[l1]...dp[lm]
        if (l2>=lm+1) {
            steps = 0;
            rep (i,l1,lm) {
                if (i) {
                    cnt += merge(e[i].fi, e[i].se);
                    steps++;
                }
            }
            go(lm+1, l2, lm+2, r2); 
            cnt -= rollback(lm-l1);
        }
        return;
    }

    dp[lm] = rm;

    if (lm-1>=l1) {
        steps = 0;
        rep (i,rm+1,r2) {
            if (i<=m) {
                cnt += merge(e[i].fi, e[i].se); 
                steps++;
            }
        }
        go (l1, lm-1, r1, rm);
        cnt -= rollback(steps);
    }

    if (l2>=lm+1) {
        steps = 0;
        rep (i,l1,lm) {
            if (i) {
                cnt += merge(e[i].fi, e[i].se);
                steps++;
            }
        }
        go (lm+1,l2,max(lm+2,r1),r2);
        cnt -= rollback(steps);
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n, q; cin >>n >> m >> q;

    rep (i,1,n) {
        par[i] = i;
        h[i] = 1;
    }

    rep (i,1,m) cin >> e[i].fi >> e[i].se;


    go (0,m,1,m+1);

    while (q--) {
        int l, r; cin >> l >> r;
        cout << (r<dp[l-1]?"YES":"NO") << "\n";
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 5028 KB Output is correct
3 Correct 3 ms 5028 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 5048 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5024 KB Output is correct
15 Correct 3 ms 5020 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5020 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 4 ms 5024 KB Output is correct
21 Correct 4 ms 4948 KB Output is correct
22 Correct 5 ms 5076 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 4948 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 5028 KB Output is correct
3 Correct 3 ms 5028 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 5048 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5024 KB Output is correct
15 Correct 3 ms 5020 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5020 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 4 ms 5024 KB Output is correct
21 Correct 4 ms 4948 KB Output is correct
22 Correct 5 ms 5076 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 4948 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 3 ms 5168 KB Output is correct
30 Correct 5 ms 5204 KB Output is correct
31 Correct 6 ms 5204 KB Output is correct
32 Correct 5 ms 5160 KB Output is correct
33 Correct 6 ms 5204 KB Output is correct
34 Correct 7 ms 5204 KB Output is correct
35 Correct 7 ms 5228 KB Output is correct
36 Correct 7 ms 5168 KB Output is correct
37 Correct 8 ms 5244 KB Output is correct
38 Correct 4 ms 5204 KB Output is correct
39 Correct 6 ms 5204 KB Output is correct
40 Correct 5 ms 5164 KB Output is correct
41 Correct 4 ms 5204 KB Output is correct
42 Correct 7 ms 5204 KB Output is correct
43 Correct 5 ms 5204 KB Output is correct
44 Correct 5 ms 5204 KB Output is correct
45 Correct 5 ms 5232 KB Output is correct
46 Correct 5 ms 5164 KB Output is correct
47 Correct 5 ms 5204 KB Output is correct
48 Correct 6 ms 5204 KB Output is correct
49 Correct 5 ms 5168 KB Output is correct
50 Correct 7 ms 5216 KB Output is correct
51 Correct 7 ms 5204 KB Output is correct
52 Correct 6 ms 5224 KB Output is correct
53 Correct 6 ms 5216 KB Output is correct
54 Correct 5 ms 5168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 5028 KB Output is correct
3 Correct 329 ms 20028 KB Output is correct
4 Correct 129 ms 20420 KB Output is correct
5 Correct 459 ms 21056 KB Output is correct
6 Correct 345 ms 20036 KB Output is correct
7 Correct 378 ms 20252 KB Output is correct
8 Correct 391 ms 19772 KB Output is correct
9 Correct 490 ms 20168 KB Output is correct
10 Correct 703 ms 21436 KB Output is correct
11 Correct 476 ms 20044 KB Output is correct
12 Correct 550 ms 21072 KB Output is correct
13 Correct 422 ms 19388 KB Output is correct
14 Correct 451 ms 19900 KB Output is correct
15 Correct 563 ms 20788 KB Output is correct
16 Correct 666 ms 21736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 5028 KB Output is correct
3 Correct 3 ms 5028 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 5048 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5024 KB Output is correct
15 Correct 3 ms 5020 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5020 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 4 ms 5024 KB Output is correct
21 Correct 4 ms 4948 KB Output is correct
22 Correct 5 ms 5076 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 4948 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 329 ms 20028 KB Output is correct
30 Correct 129 ms 20420 KB Output is correct
31 Correct 459 ms 21056 KB Output is correct
32 Correct 345 ms 20036 KB Output is correct
33 Correct 378 ms 20252 KB Output is correct
34 Correct 391 ms 19772 KB Output is correct
35 Correct 490 ms 20168 KB Output is correct
36 Correct 703 ms 21436 KB Output is correct
37 Correct 476 ms 20044 KB Output is correct
38 Correct 550 ms 21072 KB Output is correct
39 Correct 422 ms 19388 KB Output is correct
40 Correct 451 ms 19900 KB Output is correct
41 Correct 563 ms 20788 KB Output is correct
42 Correct 666 ms 21736 KB Output is correct
43 Correct 444 ms 24376 KB Output is correct
44 Correct 159 ms 24492 KB Output is correct
45 Correct 693 ms 25588 KB Output is correct
46 Correct 397 ms 24664 KB Output is correct
47 Correct 377 ms 24428 KB Output is correct
48 Correct 453 ms 24124 KB Output is correct
49 Correct 537 ms 25424 KB Output is correct
50 Correct 387 ms 24172 KB Output is correct
51 Correct 439 ms 24972 KB Output is correct
52 Correct 451 ms 25592 KB Output is correct
53 Correct 397 ms 23468 KB Output is correct
54 Correct 420 ms 24228 KB Output is correct
55 Correct 535 ms 25020 KB Output is correct
56 Correct 529 ms 25656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 5028 KB Output is correct
3 Correct 3 ms 5028 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 5048 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5024 KB Output is correct
15 Correct 3 ms 5020 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5020 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 4 ms 5024 KB Output is correct
21 Correct 4 ms 4948 KB Output is correct
22 Correct 5 ms 5076 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 4948 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 3 ms 5168 KB Output is correct
30 Correct 5 ms 5204 KB Output is correct
31 Correct 6 ms 5204 KB Output is correct
32 Correct 5 ms 5160 KB Output is correct
33 Correct 6 ms 5204 KB Output is correct
34 Correct 7 ms 5204 KB Output is correct
35 Correct 7 ms 5228 KB Output is correct
36 Correct 7 ms 5168 KB Output is correct
37 Correct 8 ms 5244 KB Output is correct
38 Correct 4 ms 5204 KB Output is correct
39 Correct 6 ms 5204 KB Output is correct
40 Correct 5 ms 5164 KB Output is correct
41 Correct 4 ms 5204 KB Output is correct
42 Correct 7 ms 5204 KB Output is correct
43 Correct 5 ms 5204 KB Output is correct
44 Correct 5 ms 5204 KB Output is correct
45 Correct 5 ms 5232 KB Output is correct
46 Correct 5 ms 5164 KB Output is correct
47 Correct 5 ms 5204 KB Output is correct
48 Correct 6 ms 5204 KB Output is correct
49 Correct 5 ms 5168 KB Output is correct
50 Correct 7 ms 5216 KB Output is correct
51 Correct 7 ms 5204 KB Output is correct
52 Correct 6 ms 5224 KB Output is correct
53 Correct 6 ms 5216 KB Output is correct
54 Correct 5 ms 5168 KB Output is correct
55 Correct 330 ms 21824 KB Output is correct
56 Correct 106 ms 22540 KB Output is correct
57 Correct 246 ms 22852 KB Output is correct
58 Correct 303 ms 21752 KB Output is correct
59 Correct 410 ms 21776 KB Output is correct
60 Correct 473 ms 22660 KB Output is correct
61 Correct 383 ms 21804 KB Output is correct
62 Correct 444 ms 22864 KB Output is correct
63 Correct 327 ms 21060 KB Output is correct
64 Correct 418 ms 22152 KB Output is correct
65 Correct 483 ms 23024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 5028 KB Output is correct
3 Correct 3 ms 5028 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 5048 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5024 KB Output is correct
15 Correct 3 ms 5020 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5020 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 4 ms 5024 KB Output is correct
21 Correct 4 ms 4948 KB Output is correct
22 Correct 5 ms 5076 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 4948 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 3 ms 5168 KB Output is correct
30 Correct 5 ms 5204 KB Output is correct
31 Correct 6 ms 5204 KB Output is correct
32 Correct 5 ms 5160 KB Output is correct
33 Correct 6 ms 5204 KB Output is correct
34 Correct 7 ms 5204 KB Output is correct
35 Correct 7 ms 5228 KB Output is correct
36 Correct 7 ms 5168 KB Output is correct
37 Correct 8 ms 5244 KB Output is correct
38 Correct 4 ms 5204 KB Output is correct
39 Correct 6 ms 5204 KB Output is correct
40 Correct 5 ms 5164 KB Output is correct
41 Correct 4 ms 5204 KB Output is correct
42 Correct 7 ms 5204 KB Output is correct
43 Correct 5 ms 5204 KB Output is correct
44 Correct 5 ms 5204 KB Output is correct
45 Correct 5 ms 5232 KB Output is correct
46 Correct 5 ms 5164 KB Output is correct
47 Correct 5 ms 5204 KB Output is correct
48 Correct 6 ms 5204 KB Output is correct
49 Correct 5 ms 5168 KB Output is correct
50 Correct 7 ms 5216 KB Output is correct
51 Correct 7 ms 5204 KB Output is correct
52 Correct 6 ms 5224 KB Output is correct
53 Correct 6 ms 5216 KB Output is correct
54 Correct 5 ms 5168 KB Output is correct
55 Correct 329 ms 20028 KB Output is correct
56 Correct 129 ms 20420 KB Output is correct
57 Correct 459 ms 21056 KB Output is correct
58 Correct 345 ms 20036 KB Output is correct
59 Correct 378 ms 20252 KB Output is correct
60 Correct 391 ms 19772 KB Output is correct
61 Correct 490 ms 20168 KB Output is correct
62 Correct 703 ms 21436 KB Output is correct
63 Correct 476 ms 20044 KB Output is correct
64 Correct 550 ms 21072 KB Output is correct
65 Correct 422 ms 19388 KB Output is correct
66 Correct 451 ms 19900 KB Output is correct
67 Correct 563 ms 20788 KB Output is correct
68 Correct 666 ms 21736 KB Output is correct
69 Correct 444 ms 24376 KB Output is correct
70 Correct 159 ms 24492 KB Output is correct
71 Correct 693 ms 25588 KB Output is correct
72 Correct 397 ms 24664 KB Output is correct
73 Correct 377 ms 24428 KB Output is correct
74 Correct 453 ms 24124 KB Output is correct
75 Correct 537 ms 25424 KB Output is correct
76 Correct 387 ms 24172 KB Output is correct
77 Correct 439 ms 24972 KB Output is correct
78 Correct 451 ms 25592 KB Output is correct
79 Correct 397 ms 23468 KB Output is correct
80 Correct 420 ms 24228 KB Output is correct
81 Correct 535 ms 25020 KB Output is correct
82 Correct 529 ms 25656 KB Output is correct
83 Correct 330 ms 21824 KB Output is correct
84 Correct 106 ms 22540 KB Output is correct
85 Correct 246 ms 22852 KB Output is correct
86 Correct 303 ms 21752 KB Output is correct
87 Correct 410 ms 21776 KB Output is correct
88 Correct 473 ms 22660 KB Output is correct
89 Correct 383 ms 21804 KB Output is correct
90 Correct 444 ms 22864 KB Output is correct
91 Correct 327 ms 21060 KB Output is correct
92 Correct 418 ms 22152 KB Output is correct
93 Correct 483 ms 23024 KB Output is correct
94 Correct 339 ms 25212 KB Output is correct
95 Correct 416 ms 26372 KB Output is correct
96 Correct 292 ms 26180 KB Output is correct
97 Correct 327 ms 25140 KB Output is correct
98 Correct 348 ms 25172 KB Output is correct
99 Correct 397 ms 24628 KB Output is correct
100 Correct 492 ms 26492 KB Output is correct
101 Correct 429 ms 24912 KB Output is correct
102 Correct 420 ms 25704 KB Output is correct
103 Correct 479 ms 26544 KB Output is correct
104 Correct 370 ms 24408 KB Output is correct
105 Correct 489 ms 25496 KB Output is correct
106 Correct 521 ms 26184 KB Output is correct
107 Correct 221 ms 26604 KB Output is correct
108 Correct 317 ms 24868 KB Output is correct
109 Correct 350 ms 25048 KB Output is correct
110 Correct 336 ms 24860 KB Output is correct
111 Correct 345 ms 24900 KB Output is correct
112 Correct 355 ms 24836 KB Output is correct
113 Correct 340 ms 24928 KB Output is correct
114 Correct 344 ms 24888 KB Output is correct
115 Correct 367 ms 24872 KB Output is correct
116 Correct 367 ms 24824 KB Output is correct