#include<bits/stdc++.h>
#define taskname "D"
using namespace std;
const int INF = 1e9;
template<class T>void minimize(T& a, T b){
if(a > b){
a = b;
}
}
template<class T>void maximize(T& a, T b){
if(a < b){
a = b;
}
}
int n, k, m, q;
namespace sub1{
void solve(){
vector<vector<int>>d(n + 1, vector<int>(n + 1, INF));
for(int i = 1; i <= n; i++){
d[i][i] = 0;
}
for(int i = 0; i < m; i++){
int a, b;
cin >> a >> b;
if(a < b){
for(int i = min(b, a + k) - 1; i >= a; i--){
for(int j = b; j > i; j--){
d[i][j] = 1;
}
}
}
else{
for(int i = max(b, a - k) + 1; i <= a; i++){
for(int j = b; j < i; j++){
d[i][j] = 1;
}
}
}
}
for(int k = 1; k <= n; k++){
for(int u = 1; u <= n; u++){
for(int v = 1; v <= n; v++){
minimize(d[u][v], d[u][k] + d[k][v]);
}
}
}
cin >> q;
for(int _ = 0; _ < q; _++){
int s, t;
cin >> s >> t;
cout << (d[s][t] == INF ? -1 : d[s][t]) << "\n";
}
}
}
namespace sub2{
void solve(){
vector<int>left(n + 1), right(n + 1);
for(int i = 1; i <= n; i++){
left[i] = right[i] = i;
}
for(int i = 0; i < m; i++){
int a, b;
cin >> a >> b;
if(a < b){
for(int i = min(b, a + k) - 1; i >= a; i--){
maximize(right[i], b);
}
}
else{
for(int i = max(b, a - k) + 1; i <= a; i++){
minimize(left[i], b);
}
}
}
cin >> q;
for(int _ = 0; _ < q; _++){
int s, t;
cin >> s >> t;
int l = s, r = s, next_l = left[s], next_r = right[s], ans = -1;
for(int i = 1; l != next_l || r != next_r; i++){
int temp_next_l = next_l, temp_next_r = next_r;
for(int j = temp_next_l; j < l; j++){
minimize(next_l, left[j]);
maximize(next_r, right[j]);
}
for(int j = temp_next_r; j > r; j--){
minimize(next_l, left[j]);
maximize(next_r, right[j]);
}
r = temp_next_r;
if((l = temp_next_l) <= t && r >= t){
ans = i;
break;
}
}
cout << ans << "\n";
}
}
}
namespace sub3456{
void solve(){
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
cin >> n >> k >> m;
if(max(n, m) <= 300){
sub1::solve();
}
else if(max(n, m) <= 2000){
sub2::solve();
}
else{
sub3456::solve();
}
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:108:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |