#include <bits/stdc++.h>
using namespace std;
// ordered set whith s.order_of_key(x) method, which returns rank of element x in set s
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
*/
// pair printing
template <class T, class U>
ostream& operator<<(ostream& out, const pair<T, U> &par) {out << "(" << par.first << "; " << par.second << ")"; return out;}
// set printing
template <class T>
ostream& operator<<(ostream& out, const set<T> &cont) { out << "{"; for(const auto &x:cont) out << x << ", "; out << "}"; return out; }
// map printing
template <class T, class U>
ostream& operator<<(ostream& out, const map<T, U> &cont) {out << "{"; for(const auto &x:cont) out << x << ", "; out << "}"; return out; }
// unordered_set printing
template <class T>
ostream& operator<<(ostream& out, const unordered_set<T> &cont) {out << "{";for(const auto &x:cont) out << x << ", ";out << "}";return out;}
// unordered_map printing
template <class T, class U>
ostream& operator<<(ostream& out, const unordered_map<T, U> &cont) {out << "{";for(const auto &x:cont) out << x << ", ";out << "}";return out;}
// vector printing
template<class T>
ostream& operator<<(ostream& out, const vector<T> &cont){ out << "["; for (const auto &x:cont) out << x << ", "; out << "]"; return out;}
#define print(x) cout << (x) << endl;
#define dmp(x) cerr << #x << " = " << x << endl
#define dmpn(x) cerr << #x << " = " << x << "; "
#define dmpl(x) cerr << "Line " << __LINE__ << ": " << #x << " = " << x << endl
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define pb push_back
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define contains(s,x) ((s).find(x) != (s).end())
int N, Q;
struct interval {
int index = -1;
int s = -1, e = -1;
int pindex = -1;
};
bool compare(interval &a, interval &b) {
return a.s < b.s;
}
bool compare2(interval &a, interval &b) {
return a.e < b.e;
}
ostream& operator<<(ostream& out, const interval &i) {
out << "{" << i.s << "; " << i.e << "}"; return out;
}
void solve() {
cin >> N >> Q;
vector<interval> v, iv;
for (int i = 0; i < N; i++) {
int a, b;
cin >> a >> b;
v.pb({ i, a, b });
iv.pb({ i, a, b });
}
sort(all(v), compare);
interval next = v[0];
int i = 1;
while (i < N && v[i - 1].s == v[i].s) {
if (v[i].e > next.e) next = v[i];
i++;
}
vector<interval> p = { next };
vector<int> borders;
while (i < N) {
// dmp(v[i]);
next = v[i];
int j = i + 1;
while (j < N && p.back().e >= v[j].s) {
if (v[j].e > next.e) next = v[j];
j++;
}
if (next.s > p.back().e) {
borders.pb(p.back().e);
borders.pb(v[j - 1].s);
}
if (next.e > p.back().e) p.pb(next);
i = j;
}
for (int i = 0; i < sz(p); i++) {
p[i].pindex = i;
iv[p[i].index].pindex = i;
}
for (int i = 0; i < N; i++) {
v[i].pindex = iv[v[i].index].pindex;
}
// dmp(borders);
sort(all(v), compare2);
vector<int> best(N);
int j = 0;
for (int i = 0; i < N; i++) {
if (j < sz(p) - 1 && p[j + 1].s <= v[i].e) j++;
best[v[i].index] = j;
}
// dmp(p);
// dmp(best);
while (Q--) {
int s, e;
cin >> s >> e;
s--;
e--;
int l = upper_bound(all(borders), v[s].e) - begin(borders);
int r = (--upper_bound(all(borders), v[e].s)) - begin(borders);
// dmpn(l); dmp(r);
// medzi intervalmi je medzera
if (r > l) {
cout << "impossible" << endl;
// prvy interval sa konci neskor ako druhy
} else if (iv[s].e > iv[e].e) {
cout << "impossible" << endl;
// druhy interval sa zacina skor ako sa prvy skonci
} else if (iv[s].e >= iv[e].s) {
cout << 1 << endl;
} else {
int a = p[best[iv[s].index]].pindex - (iv[s].index != best[iv[s].index]);
int b = p[best[iv[e].index]].pindex + (iv[e].index != best[iv[e].index]);
// dmpn(a); dmp(b);
cout << b - a - 1 << endl;
}
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
152 ms |
9108 KB |
Output is correct |
2 |
Incorrect |
150 ms |
9056 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |