#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=1e5;
int n, q, ls[2*mxN], rs[2*mxN], ans[mxN];
ar<int, 2> segs[mxN];
vector<int> d;
vector<ar<int, 3>> qry[2*mxN];
struct ft {
int ft[2*mxN+1];
void upd(int i, int x) {
for (++i; i<=2*n; i+=i&-i)
ft[i]+=x;
}
int qry(int i) {
int r=0;
for (++i; i; i-=i&-i)
r+=ft[i];
return r;
}
} bad, distinct;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
for (int i=0; i<n; ++i) {
cin >> segs[i][0] >> segs[i][1];
d.push_back(segs[i][0]);
d.push_back(segs[i][1]);
}
sort(d.begin(), d.end());
d.resize(unique(d.begin(), d.end())-d.begin());
memset(ls, -1, sizeof(ls));
for (int i=0; i<n; ++i) {
segs[i][0]=lower_bound(d.begin(), d.end(), segs[i][0])-d.begin();
segs[i][1]=lower_bound(d.begin(), d.end(), segs[i][1])-d.begin();
//cout << "seg " << segs[i][0] << " " << segs[i][1] << endl;
if (ls[segs[i][1]]==-1||segs[i][0]<ls[segs[i][1]])
ls[segs[i][1]]=segs[i][0];
}
for (int i=0; i<q; ++i) {
int a, b;
cin >> a >> b, --a, --b;
ans[i]=-1;
if (segs[a][1]<segs[b][1])
qry[segs[b][1]].push_back({segs[a][1], segs[b][0], i});
if (segs[a][1]==segs[b][1])
ans[i]=a!=b;
}
iota(rs, rs+d.size(), 0);
vector<int> ends, bads, end_val;
for (int i=0; i<d.size(); ++i) {
distinct.upd(i, 1);
if (ls[i]!=-1) {
int last;
while(ends.size()&&ends.back()>=ls[i]) {
distinct.upd(ends.back(), -1);
ends.pop_back();
last=end_val.back();
end_val.pop_back();
}
if ((ends.empty()&&ls[i])||ends.size()&&ends.back()<ls[i]-1) {
ends.push_back(ls[i]-1);
distinct.upd(ls[i]-1, 1);
end_val.push_back(last);
}
while(bads.size()&&ls[i]<=bads.back()) {
bad.upd(bads.back(), -1);
bads.pop_back();
}
}
/*cout << i << ": ";
for (int j : end_val)
cout << j << " ";
cout << endl;*/
for (auto [s, bound, ind] : qry[i]) {
if (bad.qry(i-1)-bad.qry(s-1))
continue;
assert(ls[i]<i);
int cur=distinct.qry(i-1)-distinct.qry(s-1);
int pos=cur?end_val.back():s;
//cout << cur << " " << pos << " " << end_val.back() << endl;
ans[ind]=cur+1+(pos<bound);
}
bad.upd(i, 1);
ends.push_back(i);
end_val.push_back(i);
bads.push_back(i);
}
for (int i=0; i<q; ++i) {
if (ans[i]==-1)
cout << "impossible\n";
else
cout << ans[i] << "\n";
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:57:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for (int i=0; i<d.size(); ++i) {
| ~^~~~~~~~~
events.cpp:67:42: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
67 | if ((ends.empty()&&ls[i])||ends.size()&&ends.back()<ls[i]-1) {
| ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
95 ms |
11972 KB |
Output is correct |
3 |
Correct |
95 ms |
12492 KB |
Output is correct |
4 |
Correct |
100 ms |
12340 KB |
Output is correct |
5 |
Correct |
98 ms |
12612 KB |
Output is correct |
6 |
Correct |
98 ms |
12692 KB |
Output is correct |
7 |
Correct |
104 ms |
12684 KB |
Output is correct |
8 |
Correct |
94 ms |
14664 KB |
Output is correct |
9 |
Correct |
89 ms |
14652 KB |
Output is correct |
10 |
Correct |
102 ms |
12356 KB |
Output is correct |
11 |
Correct |
103 ms |
12360 KB |
Output is correct |
12 |
Correct |
55 ms |
12204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
3 ms |
5716 KB |
Output is correct |
3 |
Correct |
4 ms |
5844 KB |
Output is correct |
4 |
Correct |
4 ms |
5844 KB |
Output is correct |
5 |
Correct |
4 ms |
5844 KB |
Output is correct |
6 |
Incorrect |
4 ms |
5844 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
3 ms |
5716 KB |
Output is correct |
3 |
Correct |
4 ms |
5844 KB |
Output is correct |
4 |
Correct |
4 ms |
5844 KB |
Output is correct |
5 |
Correct |
4 ms |
5844 KB |
Output is correct |
6 |
Incorrect |
4 ms |
5844 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
3 ms |
5716 KB |
Output is correct |
3 |
Correct |
4 ms |
5844 KB |
Output is correct |
4 |
Correct |
4 ms |
5844 KB |
Output is correct |
5 |
Correct |
4 ms |
5844 KB |
Output is correct |
6 |
Incorrect |
4 ms |
5844 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
11976 KB |
Output is correct |
2 |
Correct |
94 ms |
12416 KB |
Output is correct |
3 |
Correct |
97 ms |
12396 KB |
Output is correct |
4 |
Correct |
91 ms |
14672 KB |
Output is correct |
5 |
Correct |
94 ms |
12380 KB |
Output is correct |
6 |
Incorrect |
111 ms |
13272 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
95 ms |
11972 KB |
Output is correct |
3 |
Correct |
95 ms |
12492 KB |
Output is correct |
4 |
Correct |
100 ms |
12340 KB |
Output is correct |
5 |
Correct |
98 ms |
12612 KB |
Output is correct |
6 |
Correct |
98 ms |
12692 KB |
Output is correct |
7 |
Correct |
104 ms |
12684 KB |
Output is correct |
8 |
Correct |
94 ms |
14664 KB |
Output is correct |
9 |
Correct |
89 ms |
14652 KB |
Output is correct |
10 |
Correct |
102 ms |
12356 KB |
Output is correct |
11 |
Correct |
103 ms |
12360 KB |
Output is correct |
12 |
Correct |
55 ms |
12204 KB |
Output is correct |
13 |
Correct |
3 ms |
5716 KB |
Output is correct |
14 |
Correct |
3 ms |
5716 KB |
Output is correct |
15 |
Correct |
4 ms |
5844 KB |
Output is correct |
16 |
Correct |
4 ms |
5844 KB |
Output is correct |
17 |
Correct |
4 ms |
5844 KB |
Output is correct |
18 |
Incorrect |
4 ms |
5844 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |