#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
typedef long double ld;
using namespace std;
const int maxn = 2e5 + 5, logn = 18, inf = 1e9 + 5;
//const int maxn = 16, logn = 18, inf = 1e9 + 5;
vector<pair<int, int> > st(maxn * 2, { inf, inf });
int mini(int l, int r)
{
pair<int, int> p = { inf, inf };
for (l += maxn, r += maxn + 1; l < r; l >>= 1, r >>= 1)
{
if (l & 1) p = min(st[l++], p);
if (r & 1) p = min(st[--r], p);
}
return p.second;
}
vector<vector<int> > l(logn, vector<int>(maxn, -1));
struct usek
{
int l, r;
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
vector<usek> u(n);
vector<int> s;
for (int i = 0; i < n; i++)
{
cin >> u[i].l >> u[i].r;
s.push_back(u[i].l);
s.push_back(u[i].r);
}
sort(s.begin(), s.end());
for (int i = 0; i < n; i++)
{
u[i].l = lower_bound(s.begin(), s.end(), u[i].l) - s.begin();
u[i].r = lower_bound(s.begin(), s.end(), u[i].r) - s.begin();
st[maxn + u[i].r] = min(st[maxn + u[i].r], {u[i].l, i});
}
for (int i = maxn - 1; i > 0; i--) st[i] = min(st[i << 1], st[(i << 1) | 1]);
while (q--)
{
int s, e;
cin >> s >> e;
s--, e--;
//cout << " ";
if (s == e)
{
cout << "0\n";
continue;
}
if (u[s].r > u[e].r)
{
cout << "impossible\n";
continue;
}
if (u[e].l <= u[s].r)
{
cout << "1\n";
continue;
}
int ans = 1;
bool bad = false;
while (u[e].l > u[s].r)
{
int mi = mini(u[e].l, u[e].r);
if (mi == e)
{
bad = true;
break;
}
ans++;
e = mi;
}
if (!bad) cout << ans << "\n";
else cout << "impossible\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
18260 KB |
Output is correct |
2 |
Execution timed out |
1580 ms |
20184 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
18260 KB |
Output is correct |
2 |
Correct |
9 ms |
18260 KB |
Output is correct |
3 |
Correct |
9 ms |
18260 KB |
Output is correct |
4 |
Correct |
10 ms |
18272 KB |
Output is correct |
5 |
Correct |
11 ms |
18236 KB |
Output is correct |
6 |
Correct |
9 ms |
18260 KB |
Output is correct |
7 |
Correct |
10 ms |
18260 KB |
Output is correct |
8 |
Correct |
11 ms |
18264 KB |
Output is correct |
9 |
Correct |
9 ms |
18260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
18260 KB |
Output is correct |
2 |
Correct |
9 ms |
18260 KB |
Output is correct |
3 |
Correct |
9 ms |
18260 KB |
Output is correct |
4 |
Correct |
10 ms |
18272 KB |
Output is correct |
5 |
Correct |
11 ms |
18236 KB |
Output is correct |
6 |
Correct |
9 ms |
18260 KB |
Output is correct |
7 |
Correct |
10 ms |
18260 KB |
Output is correct |
8 |
Correct |
11 ms |
18264 KB |
Output is correct |
9 |
Correct |
9 ms |
18260 KB |
Output is correct |
10 |
Correct |
9 ms |
18356 KB |
Output is correct |
11 |
Correct |
8 ms |
18260 KB |
Output is correct |
12 |
Correct |
9 ms |
18260 KB |
Output is correct |
13 |
Correct |
9 ms |
18260 KB |
Output is correct |
14 |
Correct |
10 ms |
18268 KB |
Output is correct |
15 |
Correct |
9 ms |
18260 KB |
Output is correct |
16 |
Correct |
9 ms |
18248 KB |
Output is correct |
17 |
Correct |
9 ms |
18260 KB |
Output is correct |
18 |
Correct |
12 ms |
18260 KB |
Output is correct |
19 |
Correct |
1445 ms |
19228 KB |
Output is correct |
20 |
Execution timed out |
1587 ms |
18560 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
18260 KB |
Output is correct |
2 |
Correct |
9 ms |
18260 KB |
Output is correct |
3 |
Correct |
9 ms |
18260 KB |
Output is correct |
4 |
Correct |
10 ms |
18272 KB |
Output is correct |
5 |
Correct |
11 ms |
18236 KB |
Output is correct |
6 |
Correct |
9 ms |
18260 KB |
Output is correct |
7 |
Correct |
10 ms |
18260 KB |
Output is correct |
8 |
Correct |
11 ms |
18264 KB |
Output is correct |
9 |
Correct |
9 ms |
18260 KB |
Output is correct |
10 |
Correct |
8 ms |
18268 KB |
Output is correct |
11 |
Correct |
10 ms |
18304 KB |
Output is correct |
12 |
Correct |
12 ms |
18260 KB |
Output is correct |
13 |
Correct |
11 ms |
18312 KB |
Output is correct |
14 |
Correct |
13 ms |
18260 KB |
Output is correct |
15 |
Correct |
13 ms |
18344 KB |
Output is correct |
16 |
Correct |
10 ms |
18260 KB |
Output is correct |
17 |
Correct |
10 ms |
18260 KB |
Output is correct |
18 |
Correct |
9 ms |
18276 KB |
Output is correct |
19 |
Correct |
248 ms |
22148 KB |
Output is correct |
20 |
Correct |
103 ms |
21332 KB |
Output is correct |
21 |
Correct |
80 ms |
22012 KB |
Output is correct |
22 |
Correct |
81 ms |
22272 KB |
Output is correct |
23 |
Correct |
72 ms |
22076 KB |
Output is correct |
24 |
Correct |
75 ms |
22120 KB |
Output is correct |
25 |
Correct |
44 ms |
21424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1585 ms |
20184 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
18260 KB |
Output is correct |
2 |
Execution timed out |
1580 ms |
20184 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |