답안 #656588

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
656588 2022-11-08T02:10:32 Z Lobo Event Hopping (BOI22_events) C++17
100 / 100
341 ms 65816 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<pair<pair<int,int>,int>> ord;
    vector<int> cc;
    for(int i = 1; i <= n; i++) {
        int l, r; cin >> l >> r;
        ord.pb(mp(mp(r,l),i));
        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++) {
        nx[i][0] = 0;

        // for(int j = 1; j <= n; j++) {
        //     if(lf[j] <= rg[i] && rg[j] > rg[i]) {
        //         if(nx[i][0] == 0 || rg[j] > rg[nx[i][0]]) nx[i][0] = j;
        //     }
        // }
        auto valnx = qrymx(1,1,mxv,1,rg[i]);
        if(valnx.fr == rg[i]) nx[i][0] = 0;
        else nx[i][0] = valnx.sc;
        pv[i][0] = 0;
        // for(int j = 1; j <= n; j++) {
        //     if(j == i || !(lf[i] <= rg[j] && rg[j] <= rg[i]) || !(lf[j] < lf[i])) continue;
        //     if(pv[i][0] == 0 || lf[j] < lf[pv[i][0]]) pv[i][0] = j;
        // }
        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;

        // cout << i << " " << nx[i][0] << " " << pv[i][0] << endl;
    }

    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 237 ms 53976 KB Output is correct
3 Correct 258 ms 57100 KB Output is correct
4 Correct 313 ms 57104 KB Output is correct
5 Correct 260 ms 57108 KB Output is correct
6 Correct 266 ms 57260 KB Output is correct
7 Correct 276 ms 57200 KB Output is correct
8 Correct 258 ms 65768 KB Output is correct
9 Correct 247 ms 65712 KB Output is correct
10 Correct 308 ms 57480 KB Output is correct
11 Correct 315 ms 65632 KB Output is correct
12 Correct 165 ms 56824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 852 KB Output is correct
4 Correct 2 ms 852 KB Output is correct
5 Correct 1 ms 852 KB Output is correct
6 Correct 3 ms 852 KB Output is correct
7 Correct 2 ms 852 KB Output is correct
8 Correct 2 ms 940 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 852 KB Output is correct
4 Correct 2 ms 852 KB Output is correct
5 Correct 1 ms 852 KB Output is correct
6 Correct 3 ms 852 KB Output is correct
7 Correct 2 ms 852 KB Output is correct
8 Correct 2 ms 940 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 340 KB Output is correct
12 Correct 1 ms 852 KB Output is correct
13 Correct 2 ms 852 KB Output is correct
14 Correct 2 ms 852 KB Output is correct
15 Correct 1 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 724 KB Output is correct
19 Correct 37 ms 3676 KB Output is correct
20 Correct 39 ms 3744 KB Output is correct
21 Correct 43 ms 3880 KB Output is correct
22 Correct 37 ms 3960 KB Output is correct
23 Correct 39 ms 4364 KB Output is correct
24 Correct 35 ms 4392 KB Output is correct
25 Correct 33 ms 3944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 852 KB Output is correct
4 Correct 2 ms 852 KB Output is correct
5 Correct 1 ms 852 KB Output is correct
6 Correct 3 ms 852 KB Output is correct
7 Correct 2 ms 852 KB Output is correct
8 Correct 2 ms 940 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 340 KB Output is correct
12 Correct 1 ms 852 KB Output is correct
13 Correct 2 ms 852 KB Output is correct
14 Correct 1 ms 852 KB Output is correct
15 Correct 2 ms 852 KB Output is correct
16 Correct 2 ms 928 KB Output is correct
17 Correct 2 ms 852 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 212 ms 53380 KB Output is correct
20 Correct 207 ms 54496 KB Output is correct
21 Correct 229 ms 55240 KB Output is correct
22 Correct 227 ms 63516 KB Output is correct
23 Correct 252 ms 63756 KB Output is correct
24 Correct 248 ms 63560 KB Output is correct
25 Correct 71 ms 46456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 246 ms 53888 KB Output is correct
2 Correct 261 ms 54464 KB Output is correct
3 Correct 303 ms 54444 KB Output is correct
4 Correct 256 ms 63376 KB Output is correct
5 Correct 315 ms 54868 KB Output is correct
6 Correct 341 ms 62872 KB Output is correct
7 Correct 332 ms 62788 KB Output is correct
8 Correct 320 ms 62876 KB Output is correct
9 Correct 255 ms 62072 KB Output is correct
10 Correct 296 ms 62664 KB Output is correct
11 Correct 275 ms 62196 KB Output is correct
12 Correct 296 ms 62396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 237 ms 53976 KB Output is correct
3 Correct 258 ms 57100 KB Output is correct
4 Correct 313 ms 57104 KB Output is correct
5 Correct 260 ms 57108 KB Output is correct
6 Correct 266 ms 57260 KB Output is correct
7 Correct 276 ms 57200 KB Output is correct
8 Correct 258 ms 65768 KB Output is correct
9 Correct 247 ms 65712 KB Output is correct
10 Correct 308 ms 57480 KB Output is correct
11 Correct 315 ms 65632 KB Output is correct
12 Correct 165 ms 56824 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 852 KB Output is correct
16 Correct 2 ms 852 KB Output is correct
17 Correct 1 ms 852 KB Output is correct
18 Correct 3 ms 852 KB Output is correct
19 Correct 2 ms 852 KB Output is correct
20 Correct 2 ms 940 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 340 KB Output is correct
24 Correct 1 ms 852 KB Output is correct
25 Correct 2 ms 852 KB Output is correct
26 Correct 2 ms 852 KB Output is correct
27 Correct 1 ms 852 KB Output is correct
28 Correct 2 ms 852 KB Output is correct
29 Correct 2 ms 852 KB Output is correct
30 Correct 1 ms 724 KB Output is correct
31 Correct 37 ms 3676 KB Output is correct
32 Correct 39 ms 3744 KB Output is correct
33 Correct 43 ms 3880 KB Output is correct
34 Correct 37 ms 3960 KB Output is correct
35 Correct 39 ms 4364 KB Output is correct
36 Correct 35 ms 4392 KB Output is correct
37 Correct 33 ms 3944 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 852 KB Output is correct
41 Correct 2 ms 852 KB Output is correct
42 Correct 1 ms 852 KB Output is correct
43 Correct 2 ms 852 KB Output is correct
44 Correct 2 ms 928 KB Output is correct
45 Correct 2 ms 852 KB Output is correct
46 Correct 1 ms 724 KB Output is correct
47 Correct 212 ms 53380 KB Output is correct
48 Correct 207 ms 54496 KB Output is correct
49 Correct 229 ms 55240 KB Output is correct
50 Correct 227 ms 63516 KB Output is correct
51 Correct 252 ms 63756 KB Output is correct
52 Correct 248 ms 63560 KB Output is correct
53 Correct 71 ms 46456 KB Output is correct
54 Correct 246 ms 53888 KB Output is correct
55 Correct 261 ms 54464 KB Output is correct
56 Correct 303 ms 54444 KB Output is correct
57 Correct 256 ms 63376 KB Output is correct
58 Correct 315 ms 54868 KB Output is correct
59 Correct 341 ms 62872 KB Output is correct
60 Correct 332 ms 62788 KB Output is correct
61 Correct 320 ms 62876 KB Output is correct
62 Correct 255 ms 62072 KB Output is correct
63 Correct 296 ms 62664 KB Output is correct
64 Correct 275 ms 62196 KB Output is correct
65 Correct 296 ms 62396 KB Output is correct
66 Correct 1 ms 340 KB Output is correct
67 Correct 241 ms 57108 KB Output is correct
68 Correct 261 ms 56984 KB Output is correct
69 Correct 314 ms 57076 KB Output is correct
70 Correct 263 ms 57132 KB Output is correct
71 Correct 274 ms 57248 KB Output is correct
72 Correct 261 ms 57088 KB Output is correct
73 Correct 271 ms 65816 KB Output is correct
74 Correct 250 ms 65796 KB Output is correct
75 Correct 301 ms 57444 KB Output is correct
76 Correct 301 ms 65724 KB Output is correct
77 Correct 162 ms 56708 KB Output is correct
78 Correct 1 ms 340 KB Output is correct
79 Correct 2 ms 920 KB Output is correct
80 Correct 2 ms 856 KB Output is correct
81 Correct 2 ms 852 KB Output is correct
82 Correct 2 ms 852 KB Output is correct
83 Correct 2 ms 924 KB Output is correct
84 Correct 2 ms 980 KB Output is correct
85 Correct 1 ms 852 KB Output is correct
86 Correct 39 ms 4712 KB Output is correct
87 Correct 36 ms 4680 KB Output is correct
88 Correct 42 ms 4904 KB Output is correct
89 Correct 36 ms 4952 KB Output is correct
90 Correct 37 ms 5388 KB Output is correct
91 Correct 39 ms 5320 KB Output is correct
92 Correct 34 ms 5040 KB Output is correct
93 Correct 215 ms 55472 KB Output is correct
94 Correct 207 ms 54528 KB Output is correct
95 Correct 229 ms 55376 KB Output is correct
96 Correct 226 ms 63432 KB Output is correct
97 Correct 234 ms 63500 KB Output is correct
98 Correct 253 ms 63432 KB Output is correct
99 Correct 73 ms 46444 KB Output is correct
100 Correct 321 ms 65424 KB Output is correct
101 Correct 311 ms 65396 KB Output is correct
102 Correct 310 ms 65736 KB Output is correct
103 Correct 301 ms 64996 KB Output is correct
104 Correct 274 ms 64820 KB Output is correct
105 Correct 289 ms 65132 KB Output is correct
106 Correct 286 ms 56520 KB Output is correct
107 Correct 301 ms 56456 KB Output is correct
108 Correct 292 ms 57192 KB Output is correct
109 Correct 275 ms 57108 KB Output is correct
110 Correct 325 ms 65396 KB Output is correct
111 Correct 313 ms 65336 KB Output is correct