Submission #1038385

# Submission time Handle Problem Language Result Execution time Memory
1038385 2024-07-29T18:09:08 Z amine_aroua Joker (BOI20_joker) C++17
100 / 100
453 ms 23236 KB
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
vector<pair<int ,int>> edges;
struct DSU
{
    vector<pair<int ,int>> e;
    vector<int> col;
    bool bip;
    vector<int> rank;
    vector<vector<int>> hist;
    void init(int n)
    {
        bip = 1;
        e.assign(n , {0 , 0});
        rank.assign(n , 0);
        for(int i = 0 ; i < n ; i++)
            e[i].first = i;
    }
    pair<int ,int> get(int x)
    {
        if(e[x].first != x)
        {
            int len = e[x].second;
            pair<int ,int> p = get(e[x].first);
            (p.second+=len)%=2;
            return p;
        }
        return e[x];
    }
    bool is_bip()
    {
        return bip;
    }
    void unite(int u , int v)
    {
        if(u == v)
            return ;
        pair<int ,int> uu = get(u) , vv = get(v);
        if(uu.first == vv.first)
        {
            hist.push_back({-1 , bip});
            if(uu.second == vv.second)
            {
                bip = 0;
            }
            return ;
        }
        if(rank[uu.first] < rank[vv.first])
        {
            swap(uu , vv);
        }
        hist.push_back({vv.first , e[vv.first].first , e[vv.first].second , uu.first , rank[uu.first] == rank[vv.first]});
        e[vv.first].first = uu.first;
        e[vv.first].second = (uu.second + vv.second + 1)%2;
        rank[uu.first]+=(rank[uu.first] == rank[vv.first]);
    }
    void roll_back()
    {
        auto last = hist.back();
        hist.pop_back();
        if(last[0] == -1)
        {
            bip = last.back();
        }
        else
        {
            e[last[0]] = {last[1] , last[2]};
            rank[last[3]] -= last[4];
        }
    }
    void roll_back(int x)
    {
        while (x--)
        {
            roll_back();
        }
        
    }
    void clear()
    {
        roll_back((int)hist.size());
    }
}dsu;
int n , m , q;
vector<int> nxt;
void solve(int l , int r , int optl , int optr)
{
    if(l > r)
        return ;
    int mid = (l + r)/2;
    int ops = 0;
    for(int i = l ; i < mid ; i++)
    {
        ops++;
        dsu.unite(edges[i].first , edges[i].second);
    }
    int opt = optr;
    for(int i = optr ; i >= max(mid , optl) ; i--)
    {
        if(!dsu.is_bip())
        {
            break;
        }
        opt = i;
        if(i < m)
        {
            ops++;
            dsu.unite(edges[i].first , edges[i].second);
        }
    }
    nxt[mid] = opt;
    dsu.roll_back(ops);
    ops = 0;
    for(int i = opt + 1 ; i <= optr ;i++)
    {
        if(i < m)
        {
            dsu.unite(edges[i].first , edges[i].second);
            ops++;
        }
    }
    solve(l , mid - 1 , optl , opt);
    dsu.roll_back(ops);
    ops = 0;
    for(int i = l ; i <= mid ; i++)
    {
        ops++;
        dsu.unite(edges[i].first , edges[i].second);
    }
    solve(mid + 1 , r , opt , optr);
    dsu.roll_back(ops);
    ops = 0;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m>>q;
    nxt.assign(m , m);
    dsu.init(n);
    for(int i = 0 ; i < m ; i++)
    {
        int u , v;
        cin>>u>>v;
        u-- , v--;
        edges.push_back({u , v});
    }
    solve(0 , m - 1 , 0 , m);
    for(int i = 0 ; i< q; i++)
    {
        int l , r;
        cin>>l>>r;
        l-- , r--;
        if(nxt[l] <= r)
        {
            cout<<"NO\n";
        }
        else
        {
            cout<<"YES\n";
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 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 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 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 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 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 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 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 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 2 ms 348 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 604 KB Output is correct
33 Correct 2 ms 604 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 604 KB Output is correct
38 Correct 5 ms 616 KB Output is correct
39 Correct 2 ms 600 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 548 KB Output is correct
42 Correct 2 ms 552 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 604 KB Output is correct
49 Correct 4 ms 604 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 4 ms 604 KB Output is correct
52 Correct 2 ms 556 KB Output is correct
53 Correct 2 ms 604 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 273 ms 16072 KB Output is correct
4 Correct 424 ms 21244 KB Output is correct
5 Correct 231 ms 21444 KB Output is correct
6 Correct 274 ms 20448 KB Output is correct
7 Correct 284 ms 20208 KB Output is correct
8 Correct 305 ms 19652 KB Output is correct
9 Correct 349 ms 19040 KB Output is correct
10 Correct 453 ms 21616 KB Output is correct
11 Correct 305 ms 19392 KB Output is correct
12 Correct 335 ms 21548 KB Output is correct
13 Correct 318 ms 18872 KB Output is correct
14 Correct 302 ms 19908 KB Output is correct
15 Correct 390 ms 21184 KB Output is correct
16 Correct 428 ms 20672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 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 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 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 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 273 ms 16072 KB Output is correct
30 Correct 424 ms 21244 KB Output is correct
31 Correct 231 ms 21444 KB Output is correct
32 Correct 274 ms 20448 KB Output is correct
33 Correct 284 ms 20208 KB Output is correct
34 Correct 305 ms 19652 KB Output is correct
35 Correct 349 ms 19040 KB Output is correct
36 Correct 453 ms 21616 KB Output is correct
37 Correct 305 ms 19392 KB Output is correct
38 Correct 335 ms 21548 KB Output is correct
39 Correct 318 ms 18872 KB Output is correct
40 Correct 302 ms 19908 KB Output is correct
41 Correct 390 ms 21184 KB Output is correct
42 Correct 428 ms 20672 KB Output is correct
43 Correct 279 ms 19744 KB Output is correct
44 Correct 420 ms 21760 KB Output is correct
45 Correct 346 ms 20928 KB Output is correct
46 Correct 266 ms 21188 KB Output is correct
47 Correct 270 ms 20544 KB Output is correct
48 Correct 349 ms 19948 KB Output is correct
49 Correct 452 ms 21544 KB Output is correct
50 Correct 299 ms 20676 KB Output is correct
51 Correct 339 ms 21568 KB Output is correct
52 Correct 425 ms 21700 KB Output is correct
53 Correct 289 ms 19644 KB Output is correct
54 Correct 342 ms 20824 KB Output is correct
55 Correct 381 ms 21184 KB Output is correct
56 Correct 439 ms 22576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 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 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 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 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 2 ms 348 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 604 KB Output is correct
33 Correct 2 ms 604 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 604 KB Output is correct
38 Correct 5 ms 616 KB Output is correct
39 Correct 2 ms 600 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 548 KB Output is correct
42 Correct 2 ms 552 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 604 KB Output is correct
49 Correct 4 ms 604 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 4 ms 604 KB Output is correct
52 Correct 2 ms 556 KB Output is correct
53 Correct 2 ms 604 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
55 Correct 227 ms 15864 KB Output is correct
56 Correct 380 ms 19732 KB Output is correct
57 Correct 207 ms 19392 KB Output is correct
58 Correct 246 ms 18112 KB Output is correct
59 Correct 332 ms 18628 KB Output is correct
60 Correct 394 ms 19400 KB Output is correct
61 Correct 329 ms 17864 KB Output is correct
62 Correct 344 ms 19416 KB Output is correct
63 Correct 284 ms 17176 KB Output is correct
64 Correct 343 ms 18700 KB Output is correct
65 Correct 406 ms 19392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 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 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 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 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 2 ms 348 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 604 KB Output is correct
33 Correct 2 ms 604 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 604 KB Output is correct
38 Correct 5 ms 616 KB Output is correct
39 Correct 2 ms 600 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 548 KB Output is correct
42 Correct 2 ms 552 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 604 KB Output is correct
49 Correct 4 ms 604 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 4 ms 604 KB Output is correct
52 Correct 2 ms 556 KB Output is correct
53 Correct 2 ms 604 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
55 Correct 273 ms 16072 KB Output is correct
56 Correct 424 ms 21244 KB Output is correct
57 Correct 231 ms 21444 KB Output is correct
58 Correct 274 ms 20448 KB Output is correct
59 Correct 284 ms 20208 KB Output is correct
60 Correct 305 ms 19652 KB Output is correct
61 Correct 349 ms 19040 KB Output is correct
62 Correct 453 ms 21616 KB Output is correct
63 Correct 305 ms 19392 KB Output is correct
64 Correct 335 ms 21548 KB Output is correct
65 Correct 318 ms 18872 KB Output is correct
66 Correct 302 ms 19908 KB Output is correct
67 Correct 390 ms 21184 KB Output is correct
68 Correct 428 ms 20672 KB Output is correct
69 Correct 279 ms 19744 KB Output is correct
70 Correct 420 ms 21760 KB Output is correct
71 Correct 346 ms 20928 KB Output is correct
72 Correct 266 ms 21188 KB Output is correct
73 Correct 270 ms 20544 KB Output is correct
74 Correct 349 ms 19948 KB Output is correct
75 Correct 452 ms 21544 KB Output is correct
76 Correct 299 ms 20676 KB Output is correct
77 Correct 339 ms 21568 KB Output is correct
78 Correct 425 ms 21700 KB Output is correct
79 Correct 289 ms 19644 KB Output is correct
80 Correct 342 ms 20824 KB Output is correct
81 Correct 381 ms 21184 KB Output is correct
82 Correct 439 ms 22576 KB Output is correct
83 Correct 227 ms 15864 KB Output is correct
84 Correct 380 ms 19732 KB Output is correct
85 Correct 207 ms 19392 KB Output is correct
86 Correct 246 ms 18112 KB Output is correct
87 Correct 332 ms 18628 KB Output is correct
88 Correct 394 ms 19400 KB Output is correct
89 Correct 329 ms 17864 KB Output is correct
90 Correct 344 ms 19416 KB Output is correct
91 Correct 284 ms 17176 KB Output is correct
92 Correct 343 ms 18700 KB Output is correct
93 Correct 406 ms 19392 KB Output is correct
94 Correct 267 ms 21440 KB Output is correct
95 Correct 349 ms 21696 KB Output is correct
96 Correct 233 ms 21868 KB Output is correct
97 Correct 283 ms 20488 KB Output is correct
98 Correct 280 ms 21184 KB Output is correct
99 Correct 312 ms 21144 KB Output is correct
100 Correct 368 ms 21988 KB Output is correct
101 Correct 312 ms 20416 KB Output is correct
102 Correct 364 ms 21692 KB Output is correct
103 Correct 408 ms 21956 KB Output is correct
104 Correct 301 ms 19656 KB Output is correct
105 Correct 393 ms 21720 KB Output is correct
106 Correct 390 ms 22720 KB Output is correct
107 Correct 227 ms 23236 KB Output is correct
108 Correct 283 ms 21192 KB Output is correct
109 Correct 272 ms 21444 KB Output is correct
110 Correct 274 ms 21036 KB Output is correct
111 Correct 272 ms 21444 KB Output is correct
112 Correct 292 ms 20672 KB Output is correct
113 Correct 321 ms 20376 KB Output is correct
114 Correct 273 ms 20212 KB Output is correct
115 Correct 267 ms 20676 KB Output is correct
116 Correct 279 ms 20712 KB Output is correct