Submission #529497

#TimeUsernameProblemLanguageResultExecution timeMemory
529497wiwihoRailway Trip 2 (JOI22_ho_t4)C++14
11 / 100
2091 ms15188 KiB
#include <bits/stdc++.h> #define iter(a) a.begin(), a.end() #define mp make_pair #define F first #define S second #define eb emplace_back #define lsort(a) sort(iter(a)) #define gsort(a) sort(iter(a), greater<>()) using namespace std; typedef long long ll; typedef double ld; using pii = pair<int, int>; ll iceil(ll a, ll b){ return (a + b - 1) / b; } int n, K, m; vector<int> lb, rb, d; void solve(){ int s, t; cin >> s >> t; multimap<pii, int> tmp; for(int i = 1; i <= m; i++){ int l = lb[i], r = rb[i], dir = d[i]; int tl, tr; if(dir == 0){ tl = l; tr = min(l + K - 1, r - 1); } else{ tl = max(l + 1, r - K + 1); tr = r; } //cerr << i << " " << tl << " " << tr << "\n"; tmp.insert(mp(mp(tl, tr), i)); } int l = s, r = s; auto upd = [&](){ //cerr << "upd " << l << " " << r << "\n"; //for(auto i : tmp) cerr << i.F.F << " " << i.F.S << " " << i.S << "\n"; auto it = tmp.lower_bound(mp(l, 0)); //if(it != tmp.end()) cerr << it->F.F << " " << it->F.S << "\n"; while(it != tmp.begin() && prev(it)->F.S >= l) it--; int nl = l, nr = r; while(it != tmp.end() && it->F.F <= r){ //cerr << "go " << it->F.F << " " << it->F.S << "\n"; int id = it->S; if(d[id] == 0){ nr = max(nr, rb[id]); } else{ nl = min(nl, lb[id]); } it = tmp.erase(it); } l = nl; r = nr; }; int ans = 0; while(t < l || r < t){ int lstl = l, lstr = r; upd(); ans++; if(l == lstl && r == lstr) break; } //cerr << "ok " << l << " " << r << "\n"; if(t < l || r < t) cout << "-1\n"; else cout << ans << "\n"; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> K >> m; lb.resize(m + 1); rb.resize(m + 1); d.resize(m + 1); for(int i = 1; i <= m; i++){ cin >> lb[i] >> rb[i]; if(lb[i] > rb[i]) swap(lb[i], rb[i]), d[i] = 1; } int q; cin >> q; while(q--) 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...