답안 #973231

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
973231 2024-05-01T16:30:48 Z vladilius Joker (BOI20_joker) C++17
25 / 100
301 ms 23124 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
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, S, Sf;
    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;
        }
    }
    void save(){
        S = (int) hist.size();
        Sf = (int) f.size();
    }
    int get(int v){
        if (p[v] != v){
            return get(p[v]);
        }
        return v;
    }
    int op(int v){
        return v + n;
    }
    void roll_back(){
        while (hist.size() != S){
            hist.back().ff = hist.back().ss;
            hist.pop_back();
        }
        while (f.size() != Sf){
            f.pop_back();
        }
    }
    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;
    }
};

int 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);
    T.save();
    bool ind = false;
    for (int i = 1; i <= M; i++){
        ind = T.add(ed[i]);
    }
    if (!ind){
        for (int i = 1; i <= q; i++){
            cout<<"NO"<<"\n";
        }
        return 0;
    }
    T.roll_back();
    
    vector<int> last(M + 1);
    vector<bool> vis(M + 1);
    function<void(int, int, pii)> solve = [&](int l, int r, pii R){
        if (l > r) return;

        int m = (l + r) / 2;
        T.save();
        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;
        vis[m] = 1;
        T.roll_back();
        
        T.save();
        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();
        
        T.save();
        for (int i = l; i <= m; i++){
            T.add(ed[i]);
        }
        solve(m + 1, r, {last[m], R.ss});
        T.roll_back();
    };
    
    solve(1, M, {0, M});
    for (int i = 1; i <= M; i++){
        assert(vis[i] == true);
    }
    for (int i = 1; i < M; i++){
        assert(last[i] <= last[i + 1]);
    }

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

Compilation message

Joker.cpp: In member function 'void dsu::roll_back()':
Joker.cpp:37:28: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int&, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   37 |         while (hist.size() != S){
      |                ~~~~~~~~~~~~^~~~
Joker.cpp:41:25: warning: comparison of integer expressions of different signedness: 'std::vector<bool>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   41 |         while (f.size() != Sf){
      |                ~~~~~~~~~^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Incorrect 1 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Incorrect 1 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 186 ms 12172 KB Output is correct
4 Correct 54 ms 22620 KB Output is correct
5 Correct 216 ms 22548 KB Output is correct
6 Correct 191 ms 12620 KB Output is correct
7 Correct 206 ms 12620 KB Output is correct
8 Correct 290 ms 8912 KB Output is correct
9 Correct 293 ms 11860 KB Output is correct
10 Correct 281 ms 21828 KB Output is correct
11 Correct 221 ms 12120 KB Output is correct
12 Correct 234 ms 23124 KB Output is correct
13 Correct 262 ms 5360 KB Output is correct
14 Correct 277 ms 8532 KB Output is correct
15 Correct 301 ms 21576 KB Output is correct
16 Correct 298 ms 22596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Incorrect 1 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Incorrect 1 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Incorrect 1 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -