이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
set<int> s;
map<int, int> idx;
map<pair<int, int>, int> toEvent;
vector<pair<int, int>> p;
vector<int> mini;
struct SegTree {
int lx, rx;
int val = 1e9, id = -1;
SegTree *ls, *rs;
SegTree(int l, int r) {
lx = l;
rx = r;
if(rx - lx > 1) {
int mid = (lx + rx) / 2;
ls = new SegTree(lx, mid);
rs = new SegTree(mid, rx);
val = min(ls->val, rs->val);
if(val == ls->val) id = ls->id;
if(val == rs->val) id = rs->id;
}
else {
val = mini[lx];
id = lx;
}
}
pair<int, int> getMin(int l, int r) {
if(l <= lx && rx <= r) return {val, id};
if(r <= lx || rx <= l) return {1e9, -1};
return min(ls->getMin(l, r), rs->getMin(l, r));
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
int g = 1;
while(g <= n) g *= 2;
p.resize(n);
mini.resize(g, 1e9);
for(int i = 0; i < n; i++) {
cin >> p[i].first >> p[i].second;
s.insert(p[i].first);
s.insert(p[i].second);
}
for(int g : s) {
idx[g] = idx.size();
}
for(int i = 0; i < n; i++) {
p[i].first = idx[p[i].first];
p[i].second = idx[p[i].second];
int x, y;
tie(x, y) = p[i];
mini[y] = min(mini[y], x);
toEvent[p[i]] = i;
}
SegTree st(0, g);
int a = 5e8, b = 0;
while(a--) {
b++;
}
while(q--) {
int x, y;
cin >> x >> y;
x--; y--;
int ans = 0;
while(p[x] != p[y]) {
if(p[y].first <= p[x].second && p[x].second <= p[y].second) {
ans++;
break;
}
pair<int, int> pa = st.getMin(p[y].first, p[y].second + 1);
if(pa.second == -1 || pa == p[y]) {
ans = -1;
break;
}
ans++;
y = toEvent[pa];
}
if(ans == -1) {
cout << "impossible\n";
continue;
}
else {
cout << ans << '\n';
}
}
}
# | 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... |