#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define sz(x) (x).size()
#define l first
#define r second
#define boo bool
using namespace std;
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
int n, q;
vector<pii> highway;
vector<pii> segm;
vector<int> rez;
vector<pii> bruteqs;
const int nmax = 2e5 + 5;
int occ[nmax];
boo intersect(pii a, pii b) {
return !(a.r < b.l || b.r < a.l);
}
boo intersecc(pii a, pii b) {
return a.l <= b.r && b.r <= a.r;
}
#define ends adsjkhajksdhjkakjcss
vector<tii> qs[nmax];
vector<int> ends[nmax];
#define lsb(x) (x & -x)
struct AIB {
int tree[nmax];
int len;
void init(int nlen) {
len = nlen;
memset(tree, 0, sizeof(tree[0]) * (len + 1));
}
void upd(int p, int x = 1) {
while(p <= len)
tree[p] += x,
p += lsb(p);
}
int query(int p) {
int sum = 0;
while(p > 0)
sum += tree[p],
p -= lsb(p);
return sum;
}
int query(int l, int r) { return query(r) - query(l - 1); }
};
namespace Driver {
void init(vector<int> V, vector<tii> aggr) {
vector<int> ind(sz(aggr));
iota(all(ind), 0);
//for(auto &x : ind) x /= 2;
sort(all(ind), [&](auto a, auto b) { return aggr[a] < aggr[b]; });
vector<pii> cpsegm = segm;
sort(all(cpsegm));
AIB aib;
aib.init(2 * n);
vector<int> tmprez(q, 0);
auto advance = [&](int& ptr) {
int atr = V[ind[ptr] / 2];
auto [line, l, r] = aggr[ind[ptr]];
if(r < 0) {
r = -r;
tmprez[atr] = -aib.query(l, r);
}
else {
tmprez[atr] += aib.query(l, r);
if(tmprez[atr] == 0)
rez[atr];
else
rez[atr]--;
}
ptr++;
};
int ptr = 0;
for(auto [x, y] : cpsegm) {
while(ptr < sz(ind) && get<0>(aggr[ind[ptr]]) < x)
advance(ptr);
aib.upd(y);
}
while(ptr < sz(ind)) {
advance(ptr);
}
}
}
int main() {
cin >> n >> q;
segm.resize(n);
vector<int> ind(n);
iota(all(ind), 0);
map<int,int> nrm;
for(auto &[a, b] : segm) cin >> a >> b, nrm[a], nrm[b];
int cnt = 1; for(auto &[a, b] : nrm) b = cnt++;
for(auto &[a, b] : segm) tie(a, b) = pii{nrm[a], nrm[b]}, ends[b].push_back(a);
bruteqs.resize(q);
for(auto &[a, b] : bruteqs) {
cin >> a >> b;
--a;
--b;
if(segm[a].r > segm[b].r)
rez.push_back(0);
else if(segm[a] == segm[b])
rez.push_back(-1);
else if(intersect(segm[a], segm[b]))
rez.push_back(1);
else {
rez.push_back(0);
qs[segm[b].r].emplace_back(segm[a].r, segm[b].l, rez.size() - 1);
}
}
vector<pii> st;
vector<int> sum;
st.emplace_back(-1, -1);
sum.emplace_back(0);
vector<int> V;
vector<tii> aggr;
for(int i = 1; i < cnt; i++) {
int mn = i;
for(auto x : ends[i]) mn = min(x, mn);
//cerr << '\n';
//cerr << mn << '\n';
//cerr << '\n';
sort(all(qs[i]));
for(auto [a, b, id] : qs[i]) {
while(!st.empty() && (b <= st.back().l || (st.size() >= 2 && st.rbegin()[1].r >= b)))
sum.pop_back(),
st.pop_back();
if(b > st.back().r)
sum.emplace_back(sum.back() + 1);
else
sum.emplace_back(sum.back());
st.emplace_back(b, i);
int lim = 0;
for(int i = 16; i >= 0; i--) {
if(lim + (1 << i) >= st.size()) continue;
if(st[lim + (1 << i)].l <= a)
lim += (1 << i);
}
if(sum.back() - sum[lim]) {
//for(auto [a, b] : st) cerr << a << ' ' << b << '\n';
//cerr << a << ' ' << b << ' ' << id << '\n';
//cerr << sum.back() << ' ' << sum[lim] << '\n' << lim << '\n';
rez[id] = 0;
}
else {
if(lim == st.size() - 2) {
rez[id] = 2;
}
else {
int l1, l2, r1, r2;
tie(l1, r1) = st[lim];
tie(l2, r2) = st[lim + 2];
aggr.emplace_back(l1 - 1, l2, r2);
aggr.emplace_back(r1, l2, r2);
V.emplace_back(id);
rez[id] = st.size() - lim;
}
}
}
while(!st.empty() && (mn <= st.back().l || (st.size() >= 2 && st.rbegin()[1].r >= mn)))
sum.pop_back(),
st.pop_back();
if(mn > st.back().r)
sum.emplace_back(sum.back() + 1);
else
sum.emplace_back(sum.back());
st.emplace_back(mn, i);
}
Driver::init(V, aggr);
for(auto x : rez) {
if(x == 0) cout << "impossible\n";
else if(x == -1) cout << "0\n";
else cout << x << '\n';
}
}
Compilation message
events.cpp: In function 'void Driver::init(std::vector<int>, std::vector<std::tuple<int, int, int> >)':
events.cpp:87:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | while(ptr < sz(ind) && get<0>(aggr[ind[ptr]]) < x)
| ^
events.cpp:91:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | while(ptr < sz(ind)) {
| ^
events.cpp: In function 'int main()':
events.cpp:152: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]
152 | if(lim + (1 << i) >= st.size()) continue;
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~
events.cpp:163:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
163 | if(lim == st.size() - 2) {
| ~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
10468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
10452 KB |
Output is correct |
2 |
Incorrect |
7 ms |
10452 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
10452 KB |
Output is correct |
2 |
Incorrect |
7 ms |
10452 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
10452 KB |
Output is correct |
2 |
Incorrect |
7 ms |
10452 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
298 ms |
30940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
10468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |