Submission #959659

#TimeUsernameProblemLanguageResultExecution timeMemory
959659happy_nodeRailway Trip 2 (JOI22_ho_t4)C++17
52 / 100
1106 ms64668 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MX=2e5+5, MK=18; int N,K,M,Q; int L[MX],R[MX], A[MX], B[MX]; struct segtree { pair<int,int> t[4 * MX]; // I define t[v] to be the range within reach from v by using 2^k steps pair<int,int> merge(pair<int,int> a, pair<int, int> b) { int l=min(a.first,b.first); int r=max(a.second,b.second); return make_pair(l,r); } void update(int v, int l, int r, int pos, pair<int,int> val) { if(l > pos || r < pos) return; if(l == r) { t[v] = val; return; } int mid = (l + r) >> 1; update(v * 2 + 0, l, mid + 0, pos, val); update(v * 2 + 1, mid + 1, r, pos, val); t[v] = merge(t[v * 2 + 0], t[v * 2 + 1]); } pair<int,int> query(int v, int l, int r, int ql, int qr) { if(ql > r || qr < l) return make_pair(1e9,0); if(ql <= l && qr >= r) return t[v]; int mid = (l + r) >> 1; return merge(query(v * 2 + 0, l, mid + 0, ql, qr), query(v * 2 + 1, mid + 1, r, ql, qr)); } void reset() { for(int i=0;i<4*MX;i++) t[i]=make_pair(1e9,0); } } ST[MK]; vector<int> opens[MX], closes[MX]; int main() { cin.tie(0); ios_base::sync_with_stdio(0); cin>>N>>K>>M; for(int i=1;i<=M;i++) { cin>>A[i]>>B[i]; if(A[i]<B[i]) { opens[A[i]].push_back(B[i]); closes[min(A[i]+K-1,B[i]-1)].push_back(B[i]); } } multiset<int> st; for(int i=1;i<=N;i++) { for(auto j:opens[i]) { st.insert(j); } if(st.size()) R[i]=*st.rbegin(); else R[i]=i; for(auto j:closes[i]) { st.find(st.erase(j)); } } for(int i=1;i<=N;i++) { opens[i].clear(); closes[i].clear(); } for(int i=1;i<=M;i++) { if(A[i]>B[i]) { opens[A[i]].push_back(B[i]); closes[max(A[i]-K+1,B[i]+1)].push_back(B[i]); } } st.clear(); for(int i=N;i>=1;i--) { for(auto j:opens[i]) { st.insert(j); } if(st.size()) L[i]=*st.begin(); else L[i]=i; for(auto j:closes[i]) { st.find(st.erase(j)); } } for(int i=1;i<=N;i++) { ST[0].update(1,1,N,i,make_pair(L[i],R[i])); } for(int k=1;k<MK;k++) { for(int i=1;i<=N;i++) { auto [l,r]=ST[k-1].query(1,1,N,i,i); auto [nxtL,nxtR]=ST[k-1].query(1,1,N,l,r); ST[k].update(1,1,N,i,make_pair(nxtL,nxtR)); } } cin>>Q; for(int i=1;i<=Q;i++) { int s,t; cin>>s>>t; int curL=s,curR=s,ans=0; for(int k=MK-1;k>=0;k--) { int nxtL=ST[k].query(1,1,N,curL,curR).first; int nxtR=ST[k].query(1,1,N,curL,curR).second; if(!(nxtL<=t&&t<=nxtR)) { ans+=1<<k; curL=nxtL; curR=nxtR; } } auto [nxtL,nxtR]=ST[0].query(1,1,N,curL,curR); if(nxtL<=t&&t<=nxtR) { cout<<ans+1<<'\n'; } else { cout<<-1<<'\n'; } } }
#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...