#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 1e5+10;
int n, q, lf[maxn], rg[maxn], sq[maxn], eq[maxn], ansq[maxn], nx[maxn];
vector<int> qrys[maxn];
void solve() {
cin >> n >> q;
vector<pair<pair<int,int>,int>> ord; // first smaller rg and smaller lf
for(int i = 1; i <= n; i++) {
cin >> lf[i] >> rg[i];
ord.pb(mp(mp(rg[i],lf[i]),i));
}
sort(all(ord));
for(int i = 1; i <= q; i++) {
cin >> sq[i] >> eq[i];
qrys[eq[i]].pb(i);
}
for(int i = 1; i <= n; i++) {
nx[i] = -1;
}
for(auto X : ord) {
int i = X.sc;
for(int j = 1; j <= n; j++) {
//from j to i
if(lf[i] <= rg[j] && rg[j] < rg[i] && j != i) {
nx[j] = i;
}
if(rg[i] == rg[j] && lf[j] < lf[i]) {
nx[j] = i;
}
}
// cout << i << endl;
for(auto id : qrys[i]) {
// cout << " " << id << endl;
int j = sq[id];
if(i == j) {
ansq[id] = 0;
break;
}
if(rg[j] == rg[i]) {
ansq[id] = 1;
break;
}
int cnt = 0;
while(j != -1) {
j = nx[j];
cnt++;
if(j == i) break;
}
if(j == -1) {
ansq[id] = -1;
}
else {
ansq[id] = cnt;
}
}
}
for(int i = 1; i <= q; i++) {
if(ansq[i] == -1) cout << "impossible" << endl;
else cout << ansq[i] << endl;
}
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Execution timed out |
1572 ms |
13768 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
5 ms |
2772 KB |
Output is correct |
4 |
Correct |
5 ms |
2772 KB |
Output is correct |
5 |
Correct |
5 ms |
2772 KB |
Output is correct |
6 |
Correct |
5 ms |
2772 KB |
Output is correct |
7 |
Correct |
5 ms |
2692 KB |
Output is correct |
8 |
Correct |
4 ms |
2772 KB |
Output is correct |
9 |
Incorrect |
3 ms |
2772 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
5 ms |
2772 KB |
Output is correct |
4 |
Correct |
5 ms |
2772 KB |
Output is correct |
5 |
Correct |
5 ms |
2772 KB |
Output is correct |
6 |
Correct |
5 ms |
2772 KB |
Output is correct |
7 |
Correct |
5 ms |
2692 KB |
Output is correct |
8 |
Correct |
4 ms |
2772 KB |
Output is correct |
9 |
Incorrect |
3 ms |
2772 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
5 ms |
2772 KB |
Output is correct |
4 |
Correct |
5 ms |
2772 KB |
Output is correct |
5 |
Correct |
5 ms |
2772 KB |
Output is correct |
6 |
Correct |
5 ms |
2772 KB |
Output is correct |
7 |
Correct |
5 ms |
2692 KB |
Output is correct |
8 |
Correct |
4 ms |
2772 KB |
Output is correct |
9 |
Incorrect |
3 ms |
2772 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1565 ms |
13768 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Execution timed out |
1572 ms |
13768 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |