This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define X first
#define Y second
using namespace std;
int n, q, a, b;
pair<int, int> p[100005];
vector< pair<int, int> > v;
set< pair<int, int> > s;
set< pair<int, int> > :: iterator it;
vector<int> sus[100005];
vector<int> roots;
int dub[100005];
int prt[20][100005];
bool cmp(pair<int, int> p1, pair<int, int> p2) {
if (p1.X == p2.X) {
if (p[p1.Y].X == p1.X) return true;
else return false;
}
return p1.X < p2.X;
}
void rek(int x, int d) {
dub[x] = d;
for (int i = 0; i < sus[x].size(); i++) {
prt[0][sus[x][i]] = x;
rek(sus[x][i], d+1);
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
for (int i = 0; i < n; i++) {
cin >> p[i].X >> p[i].Y;
v.push_back({p[i].X, i});
v.push_back({p[i].Y, i});
}
sort(v.begin(), v.end(), cmp);
for (int i = 0; i < v.size(); i++) {
int ps = i;
while (ps < v.size() && v[i].X == v[ps].X) ps++;
for (int j = i; j < ps; j++) {
if (v[j].X == p[v[j].Y].X) {
s.insert({p[v[j].Y].Y, v[j].Y});
//cout << "dodaj " << p[v[i].Y].Y << " " << v[i].Y << endl;
}
else {
it = s.end();
it--;
//cout << v[i].Y+1 << " -> " << (it -> second)+1 << endl;
if (v[j].Y != (it -> second)) sus[(it -> second)+1].push_back(v[j].Y+1);
else roots.push_back(v[j].Y+1);
//cout << "makni " << v[i].X << " " << v[i].Y << endl;
}
}
for (int j = i; j < ps; j++) {
if (v[j].X != p[v[j].Y].X) s.erase(v[j]);
}
i = ps-1;
}
for (int i = 0; i < roots.size(); i++) {
rek(roots[i], 1);
}
for (int i = 1; i < 20; i++) {
for (int j = 1; j <= n; j++) {
prt[i][j] = prt[i-1][prt[i-1][j]];
}
}
for (int i = 0; i < q; i++) {
cin >> a >> b;
int mini = dub[a]-dub[b];
for (int j = 19; j >= 0; j--) {
if (dub[prt[j][a]] >= dub[b]) a = prt[j][a];
}
if (a == b) cout << mini << "\n";
else cout << "impossible\n";
}
return 0;
}
Compilation message (stderr)
events.cpp: In function 'void rek(int, int)':
events.cpp:27:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for (int i = 0; i < sus[x].size(); i++) {
| ~~^~~~~~~~~~~~~~~
events.cpp: In function 'int main()':
events.cpp:47:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
events.cpp:49:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | while (ps < v.size() && v[i].X == v[ps].X) ps++;
| ~~~^~~~~~~~~~
events.cpp:72:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i = 0; i < roots.size(); i++) {
| ~~^~~~~~~~~~~~~~
# | 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... |