Submission #1034761

# Submission time Handle Problem Language Result Execution time Memory
1034761 2024-07-25T17:34:59 Z happy_node Event Hopping (BOI22_events) C++17
100 / 100
429 ms 65716 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MX=2e5+5;

int N,Q;

int L[MX], R[MX];
int up[MX][20];

map<int,int> id;

vector<pair<int,int>> open[MX], cl[MX];

struct segtree {
        pair<int,int> t[4*MX];
        set<pair<int,int>> S[MX];

        void upd(int v, int l, int r, int pos) {
                if(pos<l || r<pos) return;
                if(l==r) {
                        t[v]=*S[l].begin();
                        return;
                }
                int m=(l+r)/2;
                upd(v*2+0,l,m+0,pos);
                upd(v*2+1,m+1,r,pos);
                t[v]=min(t[v*2+0],t[v*2+1]);
        }

        pair<int,int> que(int v, int l, int r, int ql, int qr) {
                if(r<ql || qr<l) return {1e9,1e9};
                if(ql<=l && r<=qr) return t[v];
                int m=(l+r)/2;
                return min(que(v*2+0,l,m+0,ql,qr),que(v*2+1,m+1,r,ql,qr));
        }

        void add(int p, pair<int,int> val) {
                S[p].insert(val);
                upd(1,1,2*N,p);
        }

        void del(int p, pair<int,int> val) {
                S[p].erase(val);
                upd(1,1,2*N,p);
        }

        void prep() {
                for(int i=1;i<MX;i++) {
                        S[i].insert({1e9,1e9});
                }
                for(int i=1;i<4*MX;i++) {
                        t[i]={1e9,1e9};
                }
        }
} st;

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

        cin>>N>>Q;        

        for(int i=1;i<=N;i++) {
                cin>>L[i]>>R[i];
                id[L[i]]=0;
                id[R[i]]=0;
        }

        int cnt=1;
        for(auto &[x,y]:id) y=cnt++;

        for(int i=1;i<=N;i++) {
                L[i]=id[L[i]];
                R[i]=id[R[i]];
                open[R[i]].push_back({L[i],i});
                cl[L[i]].push_back({R[i],i});
        }

        st.prep();

        for(int r=2*N;r>=1;r--) {
                for(auto [l,id]:open[r]) {
                        st.add(r,make_pair(l,id));
                }

                for(auto [R,id]:cl[r]) {
                        st.del(R,make_pair(r,id));
                        int qry=st.que(1,1,2*N,r,R).second;
                        if(qry!=1e9) up[id][0]=qry;
                        st.add(R,make_pair(r,id));
                }

                for(auto [R,id]:cl[r]) {
                        st.del(R,make_pair(r,id));
                }
        }

        for(int k=0;k+1<20;k++) {
                for(int i=1;i<=N;i++) {
                        if(up[i][k]!=0 && up[up[i][k]][k]!=up[i][k])
                                up[i][k+1]=up[up[i][k]][k];
                }
        }

        for(int i=0;i<Q;i++) {
                int s,e;
                cin>>s>>e;

                if(s==e) {
                        cout<<0<<'\n';
                        continue;
                }

                int cur=e, res=0;

                for(int k=19;k>=0;k--) {
                        if(up[cur][k]!=0 && L[up[cur][k]]>R[s]) {
                                cur=up[cur][k];
                                res+=1<<k;
                        }
                }

                if(L[cur]<=R[s] && R[s]<=R[cur]) {
                        res++;
                        cout<<res<<'\n';
                        continue;
                } 

                cur=up[cur][0];
                res++;

                if(L[cur]<=R[s] && R[s]<=R[cur]) {
                        res++;
                        cout<<res<<'\n';
                } else {
                        cout<<"impossible"<<'\n';
                }
        }
}
# Verdict Execution time Memory Grader output
1 Correct 21 ms 34904 KB Output is correct
2 Correct 198 ms 58196 KB Output is correct
3 Correct 217 ms 57940 KB Output is correct
4 Correct 213 ms 57936 KB Output is correct
5 Correct 260 ms 58556 KB Output is correct
6 Correct 223 ms 58964 KB Output is correct
7 Correct 257 ms 59220 KB Output is correct
8 Correct 245 ms 55380 KB Output is correct
9 Correct 230 ms 55444 KB Output is correct
10 Correct 225 ms 58456 KB Output is correct
11 Correct 267 ms 60244 KB Output is correct
12 Correct 138 ms 54608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 34908 KB Output is correct
2 Correct 20 ms 34908 KB Output is correct
3 Correct 20 ms 34908 KB Output is correct
4 Correct 21 ms 34952 KB Output is correct
5 Correct 20 ms 35020 KB Output is correct
6 Correct 27 ms 34904 KB Output is correct
7 Correct 22 ms 35160 KB Output is correct
8 Correct 21 ms 35164 KB Output is correct
9 Correct 20 ms 34908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 34908 KB Output is correct
2 Correct 20 ms 34908 KB Output is correct
3 Correct 20 ms 34908 KB Output is correct
4 Correct 21 ms 34952 KB Output is correct
5 Correct 20 ms 35020 KB Output is correct
6 Correct 27 ms 34904 KB Output is correct
7 Correct 22 ms 35160 KB Output is correct
8 Correct 21 ms 35164 KB Output is correct
9 Correct 20 ms 34908 KB Output is correct
10 Correct 21 ms 34828 KB Output is correct
11 Correct 20 ms 34904 KB Output is correct
12 Correct 21 ms 34908 KB Output is correct
13 Correct 22 ms 34908 KB Output is correct
14 Correct 20 ms 34908 KB Output is correct
15 Correct 20 ms 34904 KB Output is correct
16 Correct 22 ms 35160 KB Output is correct
17 Correct 21 ms 35164 KB Output is correct
18 Correct 25 ms 34900 KB Output is correct
19 Correct 45 ms 37408 KB Output is correct
20 Correct 43 ms 37200 KB Output is correct
21 Correct 48 ms 37468 KB Output is correct
22 Correct 46 ms 37532 KB Output is correct
23 Correct 45 ms 37788 KB Output is correct
24 Correct 49 ms 37972 KB Output is correct
25 Correct 43 ms 37200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 34908 KB Output is correct
2 Correct 20 ms 34908 KB Output is correct
3 Correct 20 ms 34908 KB Output is correct
4 Correct 21 ms 34952 KB Output is correct
5 Correct 20 ms 35020 KB Output is correct
6 Correct 27 ms 34904 KB Output is correct
7 Correct 22 ms 35160 KB Output is correct
8 Correct 21 ms 35164 KB Output is correct
9 Correct 20 ms 34908 KB Output is correct
10 Correct 20 ms 34728 KB Output is correct
11 Correct 20 ms 34900 KB Output is correct
12 Correct 21 ms 34904 KB Output is correct
13 Correct 21 ms 34908 KB Output is correct
14 Correct 22 ms 35016 KB Output is correct
15 Correct 22 ms 34900 KB Output is correct
16 Correct 21 ms 35164 KB Output is correct
17 Correct 21 ms 35160 KB Output is correct
18 Correct 20 ms 34908 KB Output is correct
19 Correct 191 ms 56372 KB Output is correct
20 Correct 188 ms 55380 KB Output is correct
21 Correct 227 ms 54612 KB Output is correct
22 Correct 207 ms 57940 KB Output is correct
23 Correct 242 ms 65620 KB Output is correct
24 Correct 289 ms 60912 KB Output is correct
25 Correct 132 ms 50876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 205 ms 58192 KB Output is correct
2 Correct 232 ms 57940 KB Output is correct
3 Correct 233 ms 58036 KB Output is correct
4 Correct 233 ms 55516 KB Output is correct
5 Correct 232 ms 58548 KB Output is correct
6 Correct 280 ms 62800 KB Output is correct
7 Correct 283 ms 62800 KB Output is correct
8 Correct 277 ms 63060 KB Output is correct
9 Correct 227 ms 65540 KB Output is correct
10 Correct 266 ms 62624 KB Output is correct
11 Correct 287 ms 63448 KB Output is correct
12 Correct 300 ms 62548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 34904 KB Output is correct
2 Correct 198 ms 58196 KB Output is correct
3 Correct 217 ms 57940 KB Output is correct
4 Correct 213 ms 57936 KB Output is correct
5 Correct 260 ms 58556 KB Output is correct
6 Correct 223 ms 58964 KB Output is correct
7 Correct 257 ms 59220 KB Output is correct
8 Correct 245 ms 55380 KB Output is correct
9 Correct 230 ms 55444 KB Output is correct
10 Correct 225 ms 58456 KB Output is correct
11 Correct 267 ms 60244 KB Output is correct
12 Correct 138 ms 54608 KB Output is correct
13 Correct 20 ms 34908 KB Output is correct
14 Correct 20 ms 34908 KB Output is correct
15 Correct 20 ms 34908 KB Output is correct
16 Correct 21 ms 34952 KB Output is correct
17 Correct 20 ms 35020 KB Output is correct
18 Correct 27 ms 34904 KB Output is correct
19 Correct 22 ms 35160 KB Output is correct
20 Correct 21 ms 35164 KB Output is correct
21 Correct 20 ms 34908 KB Output is correct
22 Correct 21 ms 34828 KB Output is correct
23 Correct 20 ms 34904 KB Output is correct
24 Correct 21 ms 34908 KB Output is correct
25 Correct 22 ms 34908 KB Output is correct
26 Correct 20 ms 34908 KB Output is correct
27 Correct 20 ms 34904 KB Output is correct
28 Correct 22 ms 35160 KB Output is correct
29 Correct 21 ms 35164 KB Output is correct
30 Correct 25 ms 34900 KB Output is correct
31 Correct 45 ms 37408 KB Output is correct
32 Correct 43 ms 37200 KB Output is correct
33 Correct 48 ms 37468 KB Output is correct
34 Correct 46 ms 37532 KB Output is correct
35 Correct 45 ms 37788 KB Output is correct
36 Correct 49 ms 37972 KB Output is correct
37 Correct 43 ms 37200 KB Output is correct
38 Correct 20 ms 34728 KB Output is correct
39 Correct 20 ms 34900 KB Output is correct
40 Correct 21 ms 34904 KB Output is correct
41 Correct 21 ms 34908 KB Output is correct
42 Correct 22 ms 35016 KB Output is correct
43 Correct 22 ms 34900 KB Output is correct
44 Correct 21 ms 35164 KB Output is correct
45 Correct 21 ms 35160 KB Output is correct
46 Correct 20 ms 34908 KB Output is correct
47 Correct 191 ms 56372 KB Output is correct
48 Correct 188 ms 55380 KB Output is correct
49 Correct 227 ms 54612 KB Output is correct
50 Correct 207 ms 57940 KB Output is correct
51 Correct 242 ms 65620 KB Output is correct
52 Correct 289 ms 60912 KB Output is correct
53 Correct 132 ms 50876 KB Output is correct
54 Correct 205 ms 58192 KB Output is correct
55 Correct 232 ms 57940 KB Output is correct
56 Correct 233 ms 58036 KB Output is correct
57 Correct 233 ms 55516 KB Output is correct
58 Correct 232 ms 58548 KB Output is correct
59 Correct 280 ms 62800 KB Output is correct
60 Correct 283 ms 62800 KB Output is correct
61 Correct 277 ms 63060 KB Output is correct
62 Correct 227 ms 65540 KB Output is correct
63 Correct 266 ms 62624 KB Output is correct
64 Correct 287 ms 63448 KB Output is correct
65 Correct 300 ms 62548 KB Output is correct
66 Correct 20 ms 34908 KB Output is correct
67 Correct 214 ms 58164 KB Output is correct
68 Correct 216 ms 57940 KB Output is correct
69 Correct 240 ms 58092 KB Output is correct
70 Correct 218 ms 58452 KB Output is correct
71 Correct 224 ms 58964 KB Output is correct
72 Correct 214 ms 59244 KB Output is correct
73 Correct 217 ms 55380 KB Output is correct
74 Correct 225 ms 55312 KB Output is correct
75 Correct 236 ms 58448 KB Output is correct
76 Correct 235 ms 60244 KB Output is correct
77 Correct 170 ms 54776 KB Output is correct
78 Correct 21 ms 34904 KB Output is correct
79 Correct 22 ms 34908 KB Output is correct
80 Correct 21 ms 34904 KB Output is correct
81 Correct 21 ms 34904 KB Output is correct
82 Correct 21 ms 34908 KB Output is correct
83 Correct 23 ms 35160 KB Output is correct
84 Correct 21 ms 35164 KB Output is correct
85 Correct 22 ms 34908 KB Output is correct
86 Correct 50 ms 37524 KB Output is correct
87 Correct 43 ms 37212 KB Output is correct
88 Correct 48 ms 37460 KB Output is correct
89 Correct 42 ms 37460 KB Output is correct
90 Correct 44 ms 37792 KB Output is correct
91 Correct 42 ms 37968 KB Output is correct
92 Correct 43 ms 37204 KB Output is correct
93 Correct 193 ms 56404 KB Output is correct
94 Correct 195 ms 55380 KB Output is correct
95 Correct 223 ms 54596 KB Output is correct
96 Correct 219 ms 57940 KB Output is correct
97 Correct 235 ms 65716 KB Output is correct
98 Correct 266 ms 60888 KB Output is correct
99 Correct 132 ms 50912 KB Output is correct
100 Correct 323 ms 62988 KB Output is correct
101 Correct 338 ms 62772 KB Output is correct
102 Correct 372 ms 62928 KB Output is correct
103 Correct 328 ms 62564 KB Output is correct
104 Correct 376 ms 63520 KB Output is correct
105 Correct 332 ms 62600 KB Output is correct
106 Correct 221 ms 56032 KB Output is correct
107 Correct 266 ms 57428 KB Output is correct
108 Correct 282 ms 56660 KB Output is correct
109 Correct 254 ms 56580 KB Output is correct
110 Correct 404 ms 65048 KB Output is correct
111 Correct 429 ms 65212 KB Output is correct