이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,unroll-loops")
#define ll long long
//#define int ll
#define ull unsigned ll
#define ld long double
#define rep(a) rep1(i,a)
#define rep1(i,a) rep2(i,0,a)
#define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++)
#define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--)
#define chkmin(a,b) (a=min(a,b))
#define chkmax(a,b) (a=max(a,b))
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define pb push_back
#define inf 1010000000
//#define inf 4000000000000000000
#define eps 1e-9
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define print0(a) cout << (a) << ' '
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#ifdef i_am_noob
#define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr << x << endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr << x << ", "; _do(y...);}
#else
#define bug(...) 777771449
#endif
const int mod=1000000007;
const int maxn=100005,maxm=18,maxk=7777714;
//i_am_noob
int n,k,m,l[maxm][maxn],r[maxm][maxn],spl[maxm][maxm][maxn],spr[maxm][maxm][maxn];
int queryl(int p, int l, int r){int k=__lg(r-l+1); return min(spl[p][k][l],spl[p][k][r-pow2(k)+1]);}
int queryr(int p, int l, int r){int k=__lg(r-l+1); return max(spr[p][k][l],spr[p][k][r-pow2(k)+1]);}
void orzck(){
cin >> n >> k >> m;
vector<vector<int>> addr(n+1),subr(n+1),addl(n+1),subl(n+1);
rep(m){
int u,v;
cin >> u >> v;
u--,v--;
if(u<v) addr[u].pb(v),subr[min(u+k,v)].pb(v);
else addl[u].pb(v),subl[max(u-k,v)].pb(v);
}
multiset<int> st;
rep(n){
for(auto j: addr[i]) st.insert(j);
for(auto j: subr[i]) st.erase(st.find(j));
if(st.empty()) r[0][i]=i;
else r[0][i]=*st.rbegin();
}
st.clear();
rep3(i,n-1,0){
for(auto j: addl[i]) st.insert(j);
for(auto j: subl[i]) st.erase(st.find(j));
if(st.empty()) l[0][i]=i;
else l[0][i]=*st.begin();
}
rep2(j,1,maxm+1){
rep(n) spl[j-1][0][i]=l[j-1][i],spr[j-1][0][i]=r[j-1][i];
rep2(jj,1,maxm) rep(n-pow2(jj)+1) spl[j-1][jj][i]=min(spl[j-1][jj-1][i],spl[j-1][jj-1][i+pow2(jj-1)]);
rep2(jj,1,maxm) rep(n-pow2(jj)+1) spr[j-1][jj][i]=max(spr[j-1][jj-1][i],spr[j-1][jj-1][i+pow2(jj-1)]);
if(j==maxm) break;
rep(n) l[j][i]=queryl(j-1,l[j-1][i],r[j-1][i]),r[j][i]=queryr(j-1,l[j-1][i],r[j-1][i]);
}
int q;
cin >> q;
while(q--){
int s,t;
cin >> s >> t;
s--,t--;
int res=0,curl=s,curr=s;
if(s<t){
rep3(i,maxm-1,0){
int nl=queryl(i,curl,curr),nr=queryr(i,curl,curr);
if(nr<t){
curl=nl,curr=nr;
res+=pow2(i);
}
}
res++;
}
else{
rep3(i,maxm-1,0){
int nl=queryl(i,curl,curr),nr=queryr(i,curl,curr);
if(nl>t){
curl=nl,curr=nr;
res+=pow2(i);
}
}
res++;
}
if(res>n) cout << "-1\n";
else cout << res << "\n";
}
}
signed main(){
ios_base::sync_with_stdio(0),cin.tie(0);
orzck();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |