제출 #1307486

#제출 시각아이디문제언어결과실행 시간메모리
1307486DangKhoizzzzEvent Hopping (BOI22_events)C++20
100 / 100
130 ms49432 KiB
#include <bits/stdc++.h> #define fi first #define se second #define pii pair <int , int> #define arr3 array <int , 3> #define MASK(x) (1 << x) using namespace std; const int INF = 1e9; const int maxn = 2e5 + 7; const int mod = 998244353; int n , q , S[maxn] , E[maxn]; pii st[maxn][20] , jump[maxn][20]; pii get(int l , int r) { int k = __lg(r-l+1); return min(st[l][k] , st[r-MASK(k)+1][k]); } void build() { vector <int> val; for(int i = 1; i <= 2*n; i++) { for(int j = 0; j < 20; j++) st[i][j] = {INF , INF}; if(i <= n) {val.push_back(S[i]); val.push_back(E[i]);} } sort(val.begin() , val.end()); val.erase(unique(val.begin() , val.end()) , val.end()); for(int i = 1; i <= n; i++) { S[i] = upper_bound(val.begin() , val.end() , S[i]) -val.begin(); E[i] = upper_bound(val.begin() , val.end() , E[i]) -val.begin(); st[E[i]][0] = min(st[E[i]][0] , (pii){S[i] , i}); } for(int j = 1; j < 20; j++) { for(int i = 1; i + MASK(j) - 1 <= 2*n; i++) { st[i][j] = min(st[i][j-1] , st[i+MASK(j-1)][j-1]); } } for(int i = 1; i <= n; i++) jump[i][0] = get(S[i] , E[i]); for(int j = 1; j < 20; j++) { for(int i = 1; i <= n; i++) jump[i][j] = jump[jump[i][j-1].se][j-1]; } } void solve() { cin >> n >> q; for(int i = 1; i <= n; i++) cin >> S[i] >> E[i]; build(); while(q--) { int a , b; cin >> a >> b; if(a == b){ cout << 0 << '\n'; continue;} if(S[b] <= E[a] && E[a] <= E[b]) {cout << 1 << '\n'; continue;} int ans = 1 , cur = b; for(int i = 18; i >= 0; i--) { if(jump[cur][i].fi > E[a]) { ans += MASK(i); cur = jump[cur][i].se; } } ans++; cur = jump[cur][0].se; if(S[cur] <= E[a] && E[a] <= E[cur]) cout << ans << '\n'; else cout << "impossible" << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); //freopen("inp.txt", "r", stdin); freopen("out.txt", "w", stdout); solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...