답안 #927116

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
927116 2024-02-14T09:42:30 Z berr Event Hopping (BOI22_events) C++17
100 / 100
173 ms 48772 KB
//ᓚᘏᗢ
 
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
 
struct segtree{
    int n, tl, tr;
    vector<int> st, id;
    vector<array<int, 2>> gh;

    int base = 1e9+37;

    int merge(int x, int y){
        if(y==base) return x;
        if(x==base) return y;

        if(gh[x][0]!=gh[y][0]){
            if (gh[x][0]<=gh[y][0]) return x;
            return y;

        }
        if(gh[x][1] <= gh[y][1]) return x;
        return y;
    }

    segtree(int _n, vector<array<int, 2>> _gh, vector<int> a){
        n= _n;
        gh = _gh;
        id = a;
        st.resize(4*n);
        build(1, 0, n-1);
    }

    void build(int v, int l, int r){
        if(l==r) st[v] = id[l];
        else{
            int mid = (l + r) / 2;
            build(v*2, l, mid);
            build(v*2+1, mid+1, r);

            st[v] = merge(st[v*2], st[v*2+1]);
        }
    }

    int get(int v, int l, int r){
        if(l>tr || r < tl || r<l) return base;
        else if(l>=tl && r<= tr) return st[v];
        else{
            int mid = (l+r) / 2;
            return merge(get(v*2, l, mid), get(v*2+1, mid+1, r));
        }
    }

    int get(int l, int r){
        tl = l; tr = r;
        return get(1, 0, n-1);
    }
};

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
 
    int n, q; cin >> n >> q;
 
    vector<array<int, 2>> v(n), ve;
    vector<vector<array<int, 2>>> qu(n);
    vector<array<int, 20>> gh(n), ghh(n);
    vector<int> id(n), ans(q, -1);
        
    iota(id.begin(), id.end(), 0);

    for(auto &[i, l]: v) cin >> i >> l;
        ve = v;
    sort(id.begin(), id.end(), [&](int i, int j){
        if(ve[i][1]==ve[j][1]) return ve[i][0]<ve[j][0];
        return ve[i][1]<ve[j][1];
    });

    segtree st(n, ve, id);

     for(int i=0; i<20; i++){
        for(int l=0; l<n; l++){
            gh[l][i]=-1;
        }
    }

    for(int i=0; i<n; i++){
        int l=-1;

        for(int j=18; j>=0; j--){
            int tmp = l+(1<<j);
            if(tmp<ve.size() && ve[id[tmp]][1] < v[i][0]) l = tmp;
        }
        l++;

        int r=0;

        for(int j=18; j>=0; j--){
            int tmp = r+(1<<j);
            if(tmp<ve.size() && ve[id[tmp]][1] <= v[i][1]) r = tmp;
        }   

       // if(i==3)cout<<l<<" "<<r<<"uu\n";

      //  cout<<l<<" "<<r<<"\n";
        int h = st.get(l, r);
        //cout<<i<<" "<<h<<"\n";
        if(h>=n) gh[i][0]=-1;
        else gh[i][0] = h;
    }
    for(int i=1; i<20; i++){
        for(int l=0; l<n; l++){
            if(gh[l][i-1]==-1) gh[l][i]=-1;
            else gh[l][i] = gh[gh[l][i-1]][i-1];
      
        }
    }   
    
    auto c = [&](int x, int y){
        return v[x][0] > v[y][0];
    
    };

    auto func =[&](int x, int y)->int{
        int h=0;
        if(x==y) return 0;
        if((v[y][1] <= v[x][1]&&v[y][1]>=v[x][0])) return h+(x==y?0:1);
        
        for(int j=19; j>=0; j--){

            if(c(gh[x][j], y)&&gh[x][j]!=x){
                h+=(1<<j); x=gh[x][j];
                j++;
            }

        }
        if((v[y][1] <= v[x][1]&&v[y][1]>=v[x][0])) return h+(x==y?0:1);

        if(x!=gh[x][0]&&gh[x][0]!=-1)x = gh[x][0], h++;
        
        if((v[y][1] <= v[x][1]&&v[y][1]>=v[x][0])) return h+(x==y?0:1);

        if((v[y][1] <= v[x][1]&&v[y][1]>=v[x][0])) return h+(x==y?0:1);
        return -1LL;
    };
 

 
    while(q--){
        int x, y; cin >> x >> y;
        x--; y--;
        int h = func(y, x);
        if(h==-1) cout<<"impossible";
        else cout<<h;
        cout<<"\n";        
    }


}

Compilation message

events.cpp: In function 'int main()':
events.cpp:95:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |             if(tmp<ve.size() && ve[id[tmp]][1] < v[i][0]) l = tmp;
      |                ~~~^~~~~~~~~~
events.cpp:103:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |             if(tmp<ve.size() && ve[id[tmp]][1] <= v[i][1]) r = tmp;
      |                ~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 126 ms 46580 KB Output is correct
3 Correct 129 ms 46420 KB Output is correct
4 Correct 165 ms 46428 KB Output is correct
5 Correct 155 ms 46508 KB Output is correct
6 Correct 134 ms 46548 KB Output is correct
7 Correct 132 ms 46492 KB Output is correct
8 Correct 126 ms 46416 KB Output is correct
9 Correct 134 ms 46420 KB Output is correct
10 Correct 142 ms 46492 KB Output is correct
11 Correct 141 ms 46428 KB Output is correct
12 Correct 88 ms 47868 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 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 760 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 860 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 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 760 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 860 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 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 29 ms 4908 KB Output is correct
20 Correct 26 ms 4956 KB Output is correct
21 Correct 32 ms 5188 KB Output is correct
22 Correct 29 ms 5204 KB Output is correct
23 Correct 24 ms 4956 KB Output is correct
24 Correct 25 ms 5204 KB Output is correct
25 Correct 24 ms 4700 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 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 760 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 114 ms 47680 KB Output is correct
20 Correct 103 ms 46856 KB Output is correct
21 Correct 115 ms 47696 KB Output is correct
22 Correct 108 ms 47680 KB Output is correct
23 Correct 116 ms 47684 KB Output is correct
24 Correct 119 ms 47668 KB Output is correct
25 Correct 46 ms 46936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 125 ms 46420 KB Output is correct
2 Correct 131 ms 46416 KB Output is correct
3 Correct 169 ms 46420 KB Output is correct
4 Correct 120 ms 46428 KB Output is correct
5 Correct 135 ms 46488 KB Output is correct
6 Correct 173 ms 46484 KB Output is correct
7 Correct 155 ms 48720 KB Output is correct
8 Correct 154 ms 48724 KB Output is correct
9 Correct 108 ms 47700 KB Output is correct
10 Correct 143 ms 48732 KB Output is correct
11 Correct 143 ms 48648 KB Output is correct
12 Correct 163 ms 48724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 126 ms 46580 KB Output is correct
3 Correct 129 ms 46420 KB Output is correct
4 Correct 165 ms 46428 KB Output is correct
5 Correct 155 ms 46508 KB Output is correct
6 Correct 134 ms 46548 KB Output is correct
7 Correct 132 ms 46492 KB Output is correct
8 Correct 126 ms 46416 KB Output is correct
9 Correct 134 ms 46420 KB Output is correct
10 Correct 142 ms 46492 KB Output is correct
11 Correct 141 ms 46428 KB Output is correct
12 Correct 88 ms 47868 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 1 ms 760 KB Output is correct
20 Correct 1 ms 724 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 1 ms 860 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 1 ms 860 KB Output is correct
31 Correct 29 ms 4908 KB Output is correct
32 Correct 26 ms 4956 KB Output is correct
33 Correct 32 ms 5188 KB Output is correct
34 Correct 29 ms 5204 KB Output is correct
35 Correct 24 ms 4956 KB Output is correct
36 Correct 25 ms 5204 KB Output is correct
37 Correct 24 ms 4700 KB Output is correct
38 Correct 0 ms 344 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 1 ms 860 KB Output is correct
41 Correct 1 ms 860 KB Output is correct
42 Correct 1 ms 724 KB Output is correct
43 Correct 1 ms 860 KB Output is correct
44 Correct 1 ms 860 KB Output is correct
45 Correct 1 ms 860 KB Output is correct
46 Correct 1 ms 724 KB Output is correct
47 Correct 114 ms 47680 KB Output is correct
48 Correct 103 ms 46856 KB Output is correct
49 Correct 115 ms 47696 KB Output is correct
50 Correct 108 ms 47680 KB Output is correct
51 Correct 116 ms 47684 KB Output is correct
52 Correct 119 ms 47668 KB Output is correct
53 Correct 46 ms 46936 KB Output is correct
54 Correct 125 ms 46420 KB Output is correct
55 Correct 131 ms 46416 KB Output is correct
56 Correct 169 ms 46420 KB Output is correct
57 Correct 120 ms 46428 KB Output is correct
58 Correct 135 ms 46488 KB Output is correct
59 Correct 173 ms 46484 KB Output is correct
60 Correct 155 ms 48720 KB Output is correct
61 Correct 154 ms 48724 KB Output is correct
62 Correct 108 ms 47700 KB Output is correct
63 Correct 143 ms 48732 KB Output is correct
64 Correct 143 ms 48648 KB Output is correct
65 Correct 163 ms 48724 KB Output is correct
66 Correct 0 ms 348 KB Output is correct
67 Correct 123 ms 48644 KB Output is correct
68 Correct 126 ms 48732 KB Output is correct
69 Correct 146 ms 48732 KB Output is correct
70 Correct 133 ms 48724 KB Output is correct
71 Correct 151 ms 48720 KB Output is correct
72 Correct 125 ms 48772 KB Output is correct
73 Correct 129 ms 48724 KB Output is correct
74 Correct 125 ms 48624 KB Output is correct
75 Correct 137 ms 48720 KB Output is correct
76 Correct 158 ms 48660 KB Output is correct
77 Correct 84 ms 47992 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 1 ms 860 KB Output is correct
80 Correct 1 ms 860 KB Output is correct
81 Correct 1 ms 860 KB Output is correct
82 Correct 1 ms 856 KB Output is correct
83 Correct 2 ms 860 KB Output is correct
84 Correct 1 ms 860 KB Output is correct
85 Correct 1 ms 724 KB Output is correct
86 Correct 30 ms 4932 KB Output is correct
87 Correct 27 ms 4956 KB Output is correct
88 Correct 31 ms 5204 KB Output is correct
89 Correct 25 ms 5212 KB Output is correct
90 Correct 24 ms 5200 KB Output is correct
91 Correct 23 ms 5212 KB Output is correct
92 Correct 30 ms 4752 KB Output is correct
93 Correct 122 ms 47688 KB Output is correct
94 Correct 104 ms 46936 KB Output is correct
95 Correct 113 ms 47696 KB Output is correct
96 Correct 108 ms 47744 KB Output is correct
97 Correct 105 ms 47596 KB Output is correct
98 Correct 122 ms 47952 KB Output is correct
99 Correct 46 ms 46928 KB Output is correct
100 Correct 162 ms 48724 KB Output is correct
101 Correct 151 ms 48720 KB Output is correct
102 Correct 156 ms 48724 KB Output is correct
103 Correct 160 ms 48676 KB Output is correct
104 Correct 139 ms 48724 KB Output is correct
105 Correct 141 ms 48720 KB Output is correct
106 Correct 137 ms 47904 KB Output is correct
107 Correct 147 ms 47952 KB Output is correct
108 Correct 150 ms 48552 KB Output is correct
109 Correct 137 ms 48468 KB Output is correct
110 Correct 146 ms 48720 KB Output is correct
111 Correct 145 ms 48640 KB Output is correct