This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
#define lpos pos*2
#define rpos pos*2+1
const int SIZE = 1e5 + 5;
const int H = __lg(SIZE) + 1;
int n, q;
pair<int, int> a[SIZE], p[SIZE];
vector<int> lis;
pair<int, int> node[4 * SIZE];
int to[SIZE][H + 1];
void build(int pos, int l, int r) {
if (l == r) {
node[pos] = a[l];
return;
}
int mid = (l + r) / 2;
build(lpos, l, mid);
build(rpos, mid + 1, r);
node[pos] = min(node[lpos], node[rpos]);
}
pair<int, int> que(int pos, int l, int r, int L, int R) {
if (l == L && r == R) return node[pos];
int mid = (L + R) / 2;
if (r <= mid) return que(lpos, l, r, L, mid);
else if (l > mid) return que(rpos, l, r, mid + 1, R);
return min(que(lpos, l, mid, L, mid), que(rpos, mid + 1, r, mid + 1, R));
}
void solve() {
cin >> n >> q;
FOR (i, 1, n) {
auto &[l, r] = p[i];
cin >> l >> r;
lis.pb(r);
}
sort(lis.begin(), lis.end());
lis.erase(unique(lis.begin(), lis.end()), lis.end());
fill(a + 1, a + n + 1, make_pair(INT_MAX, 0));
FOR (i, 1, n) {
int pos = lower_bound(lis.begin(), lis.end(), p[i].S) - lis.begin() + 1;
a[pos] = min(a[pos], {p[i].F, i});
}
build(1, 1, n);
FOR (i, 1, n) {
int l = lower_bound(lis.begin(), lis.end(), p[i].F) - lis.begin() + 1;
int r = lower_bound(lis.begin(), lis.end(), p[i].S) - lis.begin() + 1;
to[i][0] = que(1, l, r, 1, n).S;
debug(i, to[i][0]);
}
FOR (j, 1, H) FOR (i, 1, n) to[i][j] = to[to[i][j - 1]][j - 1];
while (q--) {
int x, y, ans = 1;
cin >> x >> y;
if (x == y || (p[y].F <= p[x].S && p[x].S <= p[y].S)) {
cout << (x != y) << '\n';
continue;
}
for (int i = H; ~i; i--) if (p[x].S < p[to[y][i]].F) {
ans += 1 << i;
y = to[y][i];
debug(ans, y);
}
y = to[y][0];
debug(ans, y);
if (p[x].S < p[y].F || p[x].S > p[y].S) cout << "impossible\n";
else cout << ans + (p[x].S >= p[y].F) << '\n';
}
}
int main() {
Waimai;
solve();
}
Compilation message (stderr)
events.cpp: In function 'void solve()':
events.cpp:12:20: warning: statement has no effect [-Wunused-value]
12 | #define debug(...) 7122
| ^~~~
events.cpp:73:9: note: in expansion of macro 'debug'
73 | debug(i, to[i][0]);
| ^~~~~
events.cpp:12:20: warning: statement has no effect [-Wunused-value]
12 | #define debug(...) 7122
| ^~~~
events.cpp:86:13: note: in expansion of macro 'debug'
86 | debug(ans, y);
| ^~~~~
events.cpp:12:20: warning: statement has no effect [-Wunused-value]
12 | #define debug(...) 7122
| ^~~~
events.cpp:89:9: note: in expansion of macro 'debug'
89 | debug(ans, y);
| ^~~~~
# | 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... |