# include <bits/stdc++.h>
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define deb(x) cerr << #x << " = " << x << endl;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll maxn = 1e5 + 25;
const ll inf = 2e9 + 0;
const ll mod = 998244353;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
using namespace std;
int n, q, pos[maxn];
pair <pair <int, int>, int> p[maxn];
bool cmp (pair <pair <int, int>, int> a, pair <pair <int, int>, int> b)
{
if (a.ff.ss != b.ff.ss)
{
return a.ff.ss < b.ff.ss;
}
else
{
return a.ff.ff < b.ff.ff;
}
}
int t[maxn * 4];
void update (int pos, int x, int v = 1, int tl = 1, int tr = n + n)
{
if (tl == tr)
{
t[v] = max(t[v], x);
return;
}
else
{
int tm = (tl + tr) >> 1;
if (pos <= tm)
{
update(pos, x, v * 2, tl, tm);
}
else
{
update(pos, x, v * 2 +1, tm + 1, tr);
}
t[v] = max(t[v * 2], t[v * 2 + 1]);
}
}
int get (int l, int r, int v = 1, int tl = 1, int tr = n + n)
{
if (tr < l || r < tl) return -1;
if (l <= tl && tr <= r) return t[v];
int tm = (tl + tr) >> 1;
return max(get(l, r, v * 2, tl, tm), get(l, r, v * 2 + 1, tm + 1, tr));
}
int f (int id1, int id2)
{
int ans = 0;
id1 = pos[id1], id2 = pos[id2];
while (id1 < id2)
{
int pos = get(1, p[id1].ff.ss);
if (pos == -1 || id1 == pos)
{
ans = inf;
break;
}
if (pos > id2)
{
if (p[id1].ff.ss >= p[id2].ff.ff)
{
ans++;
id1 = id2;
break;
}
}
id1 = pos;
ans++;
}
if (id1 > id2)
{
ans = inf;
}
return ans;
}
void ma1n (/* SABR */)
{
memset(t, -1, sizeof(t));
cin >> n >> q;
vector <int> v;
for (int i = 1; i <= n; ++i)
{
cin >> p[i].ff.ff >> p[i].ff.ss;
p[i].ss = i;
v.pb(p[i].ff.ff);
v.pb(p[i].ff.ss);
}
sort(v.begin(), v.end());
auto ittttt = unique(v.begin(), v.end());
v.resize(distance(v.begin(), ittttt));
for (int i = 1; i <= n; ++i)
{
p[i].ff.ff = lower_bound(v.begin(), v.end(), p[i].ff.ff) - v.begin() + 1;
p[i].ff.ss = lower_bound(v.begin(), v.end(), p[i].ff.ss) - v.begin() + 1;
}
sort(p + 1, p + 1 + n, cmp);
for (int i = 1; i <= n; ++i)
{
update(p[i].ff.ff, i);
}
for (int i = 1; i <= n; ++i)
{
pos[p[i].ss] = i;
}
if (n <= 5000)
{
while (q--)
{
int id1, id2;
cin >> id1 >> id2;
int ans = f(id1, id2);
if (ans != inf)
{
cout << ans << nl;
}
else
{
cout << "impossible" << nl;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("angry.in", "r", stdin);
// freopen("angry.out", "w", stdout);
int ttt = 1;
// cin >> ttt;
for (int test = 1; test <= ttt; ++test)
{
// cout << "Case " << test << ":" << ' ';
ma1n();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Incorrect |
78 ms |
5076 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1904 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
9 ms |
1876 KB |
Output is correct |
4 |
Correct |
2 ms |
1908 KB |
Output is correct |
5 |
Incorrect |
2 ms |
1876 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1904 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
9 ms |
1876 KB |
Output is correct |
4 |
Correct |
2 ms |
1908 KB |
Output is correct |
5 |
Incorrect |
2 ms |
1876 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1904 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
9 ms |
1876 KB |
Output is correct |
4 |
Correct |
2 ms |
1908 KB |
Output is correct |
5 |
Incorrect |
2 ms |
1876 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
94 ms |
4628 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Incorrect |
78 ms |
5076 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |