Submission #1120957

#TimeUsernameProblemLanguageResultExecution timeMemory
1120957WansurEvent Hopping (BOI22_events)C++17
70 / 100
1126 ms320684 KiB
#include <bits/stdc++.h> #define f first #define s second #define ent '\n' #define int long long typedef long long ll; using namespace std; const int maxn = 2e5 + 12; const int mod = 1e9 + 7; vector<int> g[maxn]; int d[maxn]; int up[20][maxn]; int sup[20][maxn]; int ans[5005][5005]; int l[maxn], r[maxn]; int n, q; void solve5000(vector<pair<int, int>> u) { for(int j = 1; j <= n; j++) { pair<int, int> mx = {-1, -1}; for(auto [x, pos] : u) { if(pos < 0) { if(r[-pos] > r[j]) continue; mx = max(mx, {r[-pos], -pos}); } else { if(r[pos] > r[j]) continue; g[mx.second].push_back(pos); } } queue<int> q; for(int i = 1; i <= n; i++) { ans[i][j] = 1e9; } ans[j][j] = 0; q.push(j); for(int i = 1; i <= n; i++) { if(r[i] >= l[j] && r[i] <= r[j] && i != j) { q.push(i); ans[i][j] = 1; } } while(q.size()) { int v = q.front(); q.pop(); for(int to : g[v]) { if(ans[to][j] > ans[v][j] + 1) { ans[to][j] = ans[v][j] + 1; q.push(to); } } } for(int i = 1; i <= n; i++) { g[i].clear(); } } while(q--) { int i, j; cin >> i >> j; if(ans[i][j] > n) { cout << "impossible\n"; } else { cout << ans[i][j] << ent; } } } void solve() { cin >> n >> q; vector<pair<int, int>> u; for(int i = 1; i <= n; i++) { cin >> l[i] >> r[i]; u.push_back({l[i], -i}); u.push_back({r[i], i}); } sort(u.begin(), u.end()); if(n <= 5000) { solve5000(u); return; } if(q > 100) { pair<int, int> mx = {-1, -1}, smx = mx; for(auto [x, pos] : u) { if(pos < 0) { pair<int, int> tx = {r[-pos], -pos}; if(tx > mx) { smx = mx; mx = tx; } else if(tx > smx) { smx = tx; } } else { up[0][pos] = mx.second; sup[0][pos] = smx.second; } } for(int k = 1; k < 20; k++) { for(int i = 1; i <= n; i++) { up[k][i] = up[k - 1][up[k - 1][i]]; sup[k][i] = sup[k - 1][sup[k - 1][i]]; } } while(q--) { int i, j; cin >> i >> j; int ans = 0; for(int k = 19; k >= 0; k--) { if(up[k][i] != 0 && r[up[k][i]] < l[j]) { i = up[k][i]; ans += (1 << k); } } if(i == j) { cout << "0\n"; } else if(r[i] >= l[j] && r[i] <= r[j]) { cout << "1\n"; } else if(up[0][i] == 0 || r[up[0][i]] < l[j] || ans > n || r[i] > r[j]) { cout << "impossible\n"; } else if(r[up[0][i]] > r[j]) { for(int k = 19; k >= 0; k--) { if(sup[k][i] != 0 && r[up[k][i]] < l[j]) { i = sup[k][i]; ans += (1 << k); } } ans++; i = sup[0][i]; if(i != j) { if(l[j] <= r[i] && r[i] <= r[j]) { ans++; } else { cout << "impossible\n"; continue; } } cout << ans << ent; } else { i = up[0][i]; ans++; ans += (i != j); cout << ans << ent; } } return; } while(q--) { int i, j; cin >> i >> j; if(r[i] > r[j]) { cout << "impossible\n"; continue; } if(i == j) { cout << "0\n"; continue; } int ans = 0; pair<int, int> mx = {-1, -1}; for(auto [x, pos] : u) { if(pos < 0) { if(r[-pos] > r[j]) continue; mx = max(mx, {r[-pos], -pos}); } else { up[0][pos] = mx.second; } } for(int _ = 0; _ <= n && r[i] < l[j]; _++) { ans++; i = up[0][i]; } if(ans > n) { cout << "impossible\n"; } else { if(i != j) ans++; cout << ans << ent; } } } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while(t--){ solve(); } }
#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...