Submission #721889

# Submission time Handle Problem Language Result Execution time Memory
721889 2023-04-11T08:26:31 Z MDSPro Event Hopping (BOI22_events) C++17
0 / 100
57 ms 4564 KB
#include "bits/stdc++.h"
 
using namespace std;
 
struct DSU{
    int n;
    vector<int> par,r;
    DSU(int sz):n(sz){
        par.resize(n); iota(par.begin(),par.end(),0);
        r.assign(n,1);
    }

    int get(int x){
        return par[x] = (x == par[x] ? x : get(par[x]));
    }

    void unite(int a, int b){
        a = get(a);
        b = get(b);
        if(a == b) return;
        if(r[a] > r[b]) swap(a,b);
        r[b] += r[a]; par[a] = b; r[a] = 0;
    }
};

int main(){
    cin.tie(0)->sync_with_stdio(0);
 
    int n,q; cin >> n >> q;
    vector<tuple<int,int,int>> ev;
    for(int i = 0; i < n; ++i){
        int x,y; cin >> x >> y;
        ev.emplace_back(y,x,i+1);
    }
 
    sort(ev.begin(),ev.end());
 
    vector<int> nw_order(n+1);
    vector<int> left(n),right(n);
    for(int i = 0; i < n; ++i) {
        left[i] = get<1>(ev[i]);
        right[i] = get<0>(ev[i]);
        nw_order[get<2>(ev[i])] = i;
    }
    
    DSU dsu(n);
    for(int i = 0; i < n; ++i){
        for(int j = max(0,i-1); j < min(n,i+2); ++j){
            if(i == j) continue;
            if(left[j] <= right[i] && right[i] <= right[j]) dsu.unite(i,j);
        }
    }

    while(q--){
        int a,b; cin >> a >> b;
        a = nw_order[a];
        b = nw_order[b];

        if(a != b && left[b] <= right[a] && right[a] <= right[b]) cout << 1;
        else if(dsu.get(a) == dsu.get(b)) {
            if(b >= a) cout << b-a;
            else cout << "impossible"; 
        } else cout << "impossible";
        cout << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 53 ms 4052 KB Output is correct
2 Correct 54 ms 3976 KB Output is correct
3 Correct 55 ms 3992 KB Output is correct
4 Correct 51 ms 4564 KB Output is correct
5 Correct 57 ms 4432 KB Output is correct
6 Incorrect 57 ms 4292 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -