제출 #955362

#제출 시각아이디문제언어결과실행 시간메모리
955362amine_arouaRailway Trip 2 (JOI22_ho_t4)C++17
100 / 100
708 ms121440 KiB
#include <bits/stdc++.h> //#pragma GCC optimize("O3") //#pragma GCC optimize("unroll-loops") using namespace std; #define int long long #define vi vector<int> #define vl vector<long long> #define vii vector<pair<int,int>> #define vll vector<pair<long long,long long>> #define pb push_back #define ll long long #define ld long double #define nl '\n' #define boost ios::sync_with_stdio(false) #define mp make_pair #define se second #define fi first #define fore(i, y) for(int i = 0; i < y; i++) #define forr(i,x,y) for(int i = x;i<=y;i++) #define forn(i,y,x) for(int i = y; i >= x; i--) #define all(v) v.begin(),v.end() #define sz(v) (int)v.size() #define clr(v,k) memset(v,k,sizeof(v)) #define rall(v) v.rbegin() , v.rend() #define pii pair<int,int> #define pll pair<ll , ll> const ll MOD = 1e9 + 7; const ll INF = 1e18 + 1; ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (gcd) ll lcm(ll a , ll b) {return a * (b / gcd(a , b));} // least common multiple (lcm) // HERE IS THE SOLUTION vi mxTree , mnTree ; int n; pii comb(pii a , pii b) { return {min(a.fi , b.fi) , max(a.se , b.se)}; } void build() { while(__builtin_popcount(n) != 1) n++; mxTree.assign(2*n , 0); mnTree.assign(2*n , n + 1); } void upd(int node , int L , int R , int l , int r , int x , bool type) { if(l <= L && R <= r) { if(!type) mxTree[node] = max(mxTree[node] , x); else mnTree[node] = min(mnTree[node] , x); return ; } if(l > R || L > r) return ; int mid = (L + R)/2; upd(2*node , L , mid , l , r , x , type); upd(2*node + 1 , mid + 1 , R , l , r , x , type); } int getMax(int i) { int ans = i; for(int j = (i + n); j>= 1;j/=2) { ans = max(ans , mxTree[j]); } return ans; } int getMin(int i) { int ans = i; for(int j = (i + n); j>= 1;j/=2) { ans = min(ans , mnTree[j]); } return ans; } struct { vii tree; int lim; void init(int N , vii a) { lim = N + 1; while(__builtin_popcount(lim) != 1) { a.pb({INF , -INF}); lim++; } tree.resize(2*lim); forr(i , 0 , lim - 1) { tree[i + lim] = a[i]; } forn(i , lim - 1 ,0) { tree[i] = comb(tree[2*i] , tree[2*i +1 ]); } } pii query(int node , int L , int R , int l , int r) { if(l <= L && R <= r) { return tree[node]; } if(l > R || L > r) return {INF , -INF}; int mid = (L + R)/2; return comb(query(2*node , L , mid , l , r) , query(2*node+ 1 , mid + 1 , R , l , r)); } pii query(int l , int r) { return query(1 , 0 , lim - 1 , l , r); } } tree[20]; signed main() { boost; cin.tie(0); cout.tie(0); int N , K , M; cin>>N>>K>>M; n = N +1; build(); vector<vector<pii>> adj(20, vii(N + 1)); forr(i , 1 , N) { adj[0][i] = {i , i}; } fore(i , M) { int A , B; cin>>A>>B; if(A < B) { upd(1 , 0 , n - 1 , A , min(B - 1 , A + K - 1) , B , 0); } else { upd(1 , 0 , n - 1 , max(B + 1 , A - K + 1) , A , B , 1); } } forr(i , 1 , N) { adj[0][i].first = getMin(i); adj[0][i].second = getMax(i); } forr(lg , 1 , 19) { tree[lg - 1].init(N , adj[lg - 1]); forr(i , 1 , N) { adj[lg][i] = tree[lg - 1].query(adj[lg - 1][i].first , adj[lg- 1][i].second); } } int Q; cin>>Q; while(Q--) { int S , T; cin>>S>>T; int ans = 0; pii cur = {S , S}; forn(i , 18 , 0) { pii ncur = tree[i].query(cur.first , cur.second); if(ncur.first <= T && T <= ncur.second) continue; cur = ncur ; ans+=(1<<i); } if(ans >= 524287) cout<<-1<<nl; else cout<<ans + 1<<nl; } }
#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...