답안 #657075

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
657075 2022-11-08T19:59:39 Z Lobo Event Hopping (BOI22_events) C++17
100 / 100
315 ms 63380 KB
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

const int maxn = 2e5+10;

int n, q, idord[maxn], lf[maxn], rg[maxn], nx[maxn][25], pv[maxn][25];
pair<int,int> trmn[4*maxn],trmx[4*maxn];

void buildmx(int no, int l, int r) {
    trmx[no] = mp(-inf,0);
    if(l == r) return;
    int lc=2*no,rc=2*no+1,mid=(l+r)/2;
    buildmx(lc,l,mid);
    buildmx(rc,mid+1,r);
}
void updmx(int no, int l, int r, int pos, pair<int,int> val) {
    if(l > pos || r < pos) return;
    if(l == r) {
        trmx[no] = max(trmx[no],val);
        return;
    }
    int lc=2*no,rc=2*no+1,mid=(l+r)/2;
    updmx(lc,l,mid,pos,val);
    updmx(rc,mid+1,r,pos,val);
    trmx[no] = max(trmx[lc],trmx[rc]);
}

pair<int,int> qrymx(int no, int l, int r, int L, int R) {
    if(l > R || r < L) return mp(-inf,0);
    if(l >= L && r <= R) return trmx[no];
    int lc=2*no,rc=2*no+1,mid=(l+r)/2;
    return max(qrymx(lc,l,mid,L,R),qrymx(rc,mid+1,r,L,R));
}

void buildmn(int no, int l, int r) {
    trmn[no] = mp(inf,0);
    if(l == r) return;
    int lc=2*no,rc=2*no+1,mid=(l+r)/2;
    buildmn(lc,l,mid);
    buildmn(rc,mid+1,r);
}
void updmn(int no, int l, int r, int pos, pair<int,int> val) {
    if(l > pos || r < pos) return;
    if(l == r) {
        trmn[no] = min(trmn[no],val);
        return;
    }
    int lc=2*no,rc=2*no+1,mid=(l+r)/2;
    updmn(lc,l,mid,pos,val);
    updmn(rc,mid+1,r,pos,val);
    trmn[no] = min(trmn[lc],trmn[rc]);
}

pair<int,int> qrymn(int no, int l, int r, int L, int R) {
    if(l > R || r < L) return mp(inf,0);
    if(l >= L && r <= R) return trmn[no];
    int lc=2*no,rc=2*no+1,mid=(l+r)/2;
    return min(qrymn(lc,l,mid,L,R),qrymn(rc,mid+1,r,L,R));
}

void solve() {
    cin >> n >> q;
    vector<int> cc;
    for(int i = 1; i <= n; i++) {
        int l, r; cin >> l >> r;
        lf[i] = l;
        rg[i] = r;
        cc.pb(lf[i]);
        cc.pb(rg[i]);
    }
    sort(all(cc));
    cc.erase(unique(all(cc)),cc.end());
    int mxv = cc.size();
    for(int i = 1; i <= n; i++) {
        lf[i] = upper_bound(all(cc),lf[i])-cc.begin();
        rg[i] = upper_bound(all(cc),rg[i])-cc.begin();
    }
    // sort(all(ord));
    buildmx(1,1,mxv);
    buildmn(1,1,mxv);
    for(int i = 1; i <= n; i++) {
        updmx(1,1,mxv,lf[i],mp(rg[i],i));
        updmn(1,1,mxv,rg[i],mp(lf[i],i));
    }

    for(int i = 1; i <= n; i++) {
        auto valnx = qrymx(1,1,mxv,1,rg[i]);
        if(valnx.fr == rg[i]) nx[i][0] = 0;
        else nx[i][0] = valnx.sc;
        auto valpv = qrymn(1,1,mxv,lf[i],rg[i]);
        if(valpv.fr == lf[i]) pv[i][0] = 0;
        else pv[i][0] = valpv.sc;
    }

    for(int j = 1; j <= 20; j++) {
        for(int i = 1; i <= n; i++) {
            nx[i][j] = nx[nx[i][j-1]][j-1];
            pv[i][j] = pv[pv[i][j-1]][j-1];
        }
    }

    while(q--) {
        int s,e; cin >> s >> e;

        int ans = 0;
        // for(int j = 20; j >= 0; j--) {
        //     if(nx[s][j] != 0 && rg[nx[s][j]] < rg[e]) {
        //         ans+= (1<<j);
        //         s = nx[s][j];
        //     }
        // }
        for(int j = 20; j >= 0; j--) {
            if(pv[e][j] != 0 && lf[pv[e][j]] > lf[s]) {
                ans+= (1<<j);
                e = pv[e][j];
            }
        }
        if(s == e) {
            cout << ans << endl;
        }
        else if(lf[e] <= rg[s] && rg[s] <= rg[e]) {
            cout << ans+1 << endl;
        }
        else if(nx[s][0] != 0 && lf[e] <= rg[nx[s][0]] && rg[nx[s][0]] <= rg[e]) {
            cout << ans+2 << endl;
        }
        else if(pv[e][0] != 0 && lf[pv[e][0]] <= rg[s] && rg[s] <= rg[pv[e][0]]) {
            cout << ans+2 << endl;
        }
        else {
            cout << "impossible" << endl;
        }
    }
}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
    int tt = 1;
    // cin >> tt;
    while(tt--) {
        solve();
    }

}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 257 ms 54604 KB Output is correct
3 Correct 246 ms 54620 KB Output is correct
4 Correct 293 ms 54556 KB Output is correct
5 Correct 256 ms 54732 KB Output is correct
6 Correct 259 ms 54672 KB Output is correct
7 Correct 254 ms 54664 KB Output is correct
8 Correct 246 ms 63380 KB Output is correct
9 Correct 248 ms 63360 KB Output is correct
10 Correct 280 ms 55040 KB Output is correct
11 Correct 287 ms 63268 KB Output is correct
12 Correct 159 ms 54412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 2 ms 852 KB Output is correct
4 Correct 2 ms 852 KB Output is correct
5 Correct 1 ms 856 KB Output is correct
6 Correct 2 ms 864 KB Output is correct
7 Correct 2 ms 852 KB Output is correct
8 Correct 2 ms 852 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 2 ms 852 KB Output is correct
4 Correct 2 ms 852 KB Output is correct
5 Correct 1 ms 856 KB Output is correct
6 Correct 2 ms 864 KB Output is correct
7 Correct 2 ms 852 KB Output is correct
8 Correct 2 ms 852 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 856 KB Output is correct
13 Correct 2 ms 856 KB Output is correct
14 Correct 2 ms 852 KB Output is correct
15 Correct 2 ms 780 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 2 ms 852 KB Output is correct
18 Correct 1 ms 728 KB Output is correct
19 Correct 34 ms 4556 KB Output is correct
20 Correct 33 ms 4556 KB Output is correct
21 Correct 39 ms 4808 KB Output is correct
22 Correct 32 ms 4840 KB Output is correct
23 Correct 34 ms 5240 KB Output is correct
24 Correct 37 ms 5196 KB Output is correct
25 Correct 32 ms 4932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 2 ms 852 KB Output is correct
4 Correct 2 ms 852 KB Output is correct
5 Correct 1 ms 856 KB Output is correct
6 Correct 2 ms 864 KB Output is correct
7 Correct 2 ms 852 KB Output is correct
8 Correct 2 ms 852 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 852 KB Output is correct
13 Correct 2 ms 852 KB Output is correct
14 Correct 2 ms 848 KB Output is correct
15 Correct 2 ms 852 KB Output is correct
16 Correct 2 ms 852 KB Output is correct
17 Correct 2 ms 852 KB Output is correct
18 Correct 1 ms 728 KB Output is correct
19 Correct 216 ms 53040 KB Output is correct
20 Correct 241 ms 52020 KB Output is correct
21 Correct 228 ms 52800 KB Output is correct
22 Correct 241 ms 61124 KB Output is correct
23 Correct 236 ms 61028 KB Output is correct
24 Correct 258 ms 61096 KB Output is correct
25 Correct 69 ms 43928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 244 ms 54664 KB Output is correct
2 Correct 252 ms 54648 KB Output is correct
3 Correct 291 ms 54636 KB Output is correct
4 Correct 245 ms 63288 KB Output is correct
5 Correct 284 ms 54980 KB Output is correct
6 Correct 304 ms 63052 KB Output is correct
7 Correct 307 ms 62924 KB Output is correct
8 Correct 287 ms 63048 KB Output is correct
9 Correct 246 ms 61072 KB Output is correct
10 Correct 281 ms 62660 KB Output is correct
11 Correct 281 ms 62388 KB Output is correct
12 Correct 286 ms 62592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 257 ms 54604 KB Output is correct
3 Correct 246 ms 54620 KB Output is correct
4 Correct 293 ms 54556 KB Output is correct
5 Correct 256 ms 54732 KB Output is correct
6 Correct 259 ms 54672 KB Output is correct
7 Correct 254 ms 54664 KB Output is correct
8 Correct 246 ms 63380 KB Output is correct
9 Correct 248 ms 63360 KB Output is correct
10 Correct 280 ms 55040 KB Output is correct
11 Correct 287 ms 63268 KB Output is correct
12 Correct 159 ms 54412 KB Output is correct
13 Correct 0 ms 336 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 2 ms 852 KB Output is correct
16 Correct 2 ms 852 KB Output is correct
17 Correct 1 ms 856 KB Output is correct
18 Correct 2 ms 864 KB Output is correct
19 Correct 2 ms 852 KB Output is correct
20 Correct 2 ms 852 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 328 KB Output is correct
24 Correct 1 ms 856 KB Output is correct
25 Correct 2 ms 856 KB Output is correct
26 Correct 2 ms 852 KB Output is correct
27 Correct 2 ms 780 KB Output is correct
28 Correct 3 ms 852 KB Output is correct
29 Correct 2 ms 852 KB Output is correct
30 Correct 1 ms 728 KB Output is correct
31 Correct 34 ms 4556 KB Output is correct
32 Correct 33 ms 4556 KB Output is correct
33 Correct 39 ms 4808 KB Output is correct
34 Correct 32 ms 4840 KB Output is correct
35 Correct 34 ms 5240 KB Output is correct
36 Correct 37 ms 5196 KB Output is correct
37 Correct 32 ms 4932 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 2 ms 852 KB Output is correct
41 Correct 2 ms 852 KB Output is correct
42 Correct 2 ms 848 KB Output is correct
43 Correct 2 ms 852 KB Output is correct
44 Correct 2 ms 852 KB Output is correct
45 Correct 2 ms 852 KB Output is correct
46 Correct 1 ms 728 KB Output is correct
47 Correct 216 ms 53040 KB Output is correct
48 Correct 241 ms 52020 KB Output is correct
49 Correct 228 ms 52800 KB Output is correct
50 Correct 241 ms 61124 KB Output is correct
51 Correct 236 ms 61028 KB Output is correct
52 Correct 258 ms 61096 KB Output is correct
53 Correct 69 ms 43928 KB Output is correct
54 Correct 244 ms 54664 KB Output is correct
55 Correct 252 ms 54648 KB Output is correct
56 Correct 291 ms 54636 KB Output is correct
57 Correct 245 ms 63288 KB Output is correct
58 Correct 284 ms 54980 KB Output is correct
59 Correct 304 ms 63052 KB Output is correct
60 Correct 307 ms 62924 KB Output is correct
61 Correct 287 ms 63048 KB Output is correct
62 Correct 246 ms 61072 KB Output is correct
63 Correct 281 ms 62660 KB Output is correct
64 Correct 281 ms 62388 KB Output is correct
65 Correct 286 ms 62592 KB Output is correct
66 Correct 1 ms 340 KB Output is correct
67 Correct 241 ms 54692 KB Output is correct
68 Correct 244 ms 54632 KB Output is correct
69 Correct 294 ms 54648 KB Output is correct
70 Correct 251 ms 54660 KB Output is correct
71 Correct 249 ms 54644 KB Output is correct
72 Correct 255 ms 54664 KB Output is correct
73 Correct 248 ms 63272 KB Output is correct
74 Correct 246 ms 63324 KB Output is correct
75 Correct 315 ms 55032 KB Output is correct
76 Correct 306 ms 63204 KB Output is correct
77 Correct 157 ms 54464 KB Output is correct
78 Correct 0 ms 336 KB Output is correct
79 Correct 2 ms 852 KB Output is correct
80 Correct 1 ms 852 KB Output is correct
81 Correct 1 ms 852 KB Output is correct
82 Correct 2 ms 852 KB Output is correct
83 Correct 2 ms 852 KB Output is correct
84 Correct 2 ms 852 KB Output is correct
85 Correct 1 ms 724 KB Output is correct
86 Correct 34 ms 4592 KB Output is correct
87 Correct 32 ms 4560 KB Output is correct
88 Correct 38 ms 4872 KB Output is correct
89 Correct 31 ms 4860 KB Output is correct
90 Correct 34 ms 5296 KB Output is correct
91 Correct 31 ms 5332 KB Output is correct
92 Correct 31 ms 4912 KB Output is correct
93 Correct 211 ms 52916 KB Output is correct
94 Correct 196 ms 52004 KB Output is correct
95 Correct 222 ms 52800 KB Output is correct
96 Correct 224 ms 61112 KB Output is correct
97 Correct 235 ms 61008 KB Output is correct
98 Correct 241 ms 61044 KB Output is correct
99 Correct 74 ms 43968 KB Output is correct
100 Correct 308 ms 63112 KB Output is correct
101 Correct 309 ms 62928 KB Output is correct
102 Correct 314 ms 63128 KB Output is correct
103 Correct 277 ms 62672 KB Output is correct
104 Correct 278 ms 62424 KB Output is correct
105 Correct 292 ms 62656 KB Output is correct
106 Correct 273 ms 54052 KB Output is correct
107 Correct 295 ms 54080 KB Output is correct
108 Correct 265 ms 54784 KB Output is correct
109 Correct 263 ms 54764 KB Output is correct
110 Correct 304 ms 62876 KB Output is correct
111 Correct 310 ms 62996 KB Output is correct