#include "bits/stdc++.h"
using namespace std;
template<typename T>
using V = vector<T>;
using vi = V<int>;
using pi = pair<int,int>;
#define f first
#define s second
#define F0R(i, n) for(int i = 0; i < n;i++)
#define FOR(i,a, n) for(int i = a; i < n;i++)
#define pb push_back
#define all(x) begin(x), end(x)
#define each(x, y) for(auto &x : y)
constexpr int INF = 1e9 + 2;
pi binarySearch(V<pi> &stack, int point) {
auto it = std::upper_bound(stack.begin(), stack.end(), make_pair(point,INF)); // Point to before or at point
if (it == stack.begin()) return {0,0};
it--;
return {it - stack.begin() + 1, it->s};
}
int getImp(V<pi> &stack) {
return (stack.empty() ? 0 : stack.back().s);
}
int main() {
int n, q; cin >> n >> q;
V<pair<pi, int>> edges;
map<int, V<pi>> queries;
V<int> ans(q, -1);
F0R(i, n) {
int a, b; cin >> a >> b;
edges.pb({{b, a},i});
}
F0R(i, q) {
int a, b; cin >> a >> b;
a--;
b--;
queries[b].emplace_back(edges[a].f.f,i);
}
sort(all(edges));
V<pi> stack; // Point, possible prefix
// Self bug?
each(edgeDATA, edges) {
auto [edge, id] = edgeDATA;
swap(edge.f, edge.s);
pi last = {-1,-1};
while (!stack.empty() && stack.back().f >= edge.f) {
last = stack.back();
stack.pop_back();
}
if (last.f != -1) stack.push_back(last);
stack.emplace_back(edge.s, getImp(stack) + (last.f == -1 ? 1 : 0));
each(starts, queries[id]) {
auto data = binarySearch(stack, starts.f);
if (getImp(stack) - data.s > 0) continue;
if (starts.f > edge.s) continue;
ans[starts.s] = stack.size() - data.f;
}
}
F0R(i, q) {
if (ans[i] == -1) cout << "impossible\n";
else cout << ans[i] << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
137 ms |
15056 KB |
Output is correct |
3 |
Correct |
136 ms |
15220 KB |
Output is correct |
4 |
Correct |
171 ms |
16228 KB |
Output is correct |
5 |
Correct |
146 ms |
15300 KB |
Output is correct |
6 |
Correct |
138 ms |
15216 KB |
Output is correct |
7 |
Correct |
170 ms |
15300 KB |
Output is correct |
8 |
Correct |
127 ms |
15816 KB |
Output is correct |
9 |
Correct |
136 ms |
15840 KB |
Output is correct |
10 |
Correct |
158 ms |
16580 KB |
Output is correct |
11 |
Incorrect |
167 ms |
16324 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Incorrect |
1 ms |
544 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Incorrect |
1 ms |
544 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Incorrect |
1 ms |
544 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
124 ms |
15048 KB |
Output is correct |
2 |
Correct |
142 ms |
15288 KB |
Output is correct |
3 |
Correct |
151 ms |
16296 KB |
Output is correct |
4 |
Correct |
125 ms |
15812 KB |
Output is correct |
5 |
Correct |
164 ms |
16580 KB |
Output is correct |
6 |
Incorrect |
154 ms |
15556 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
137 ms |
15056 KB |
Output is correct |
3 |
Correct |
136 ms |
15220 KB |
Output is correct |
4 |
Correct |
171 ms |
16228 KB |
Output is correct |
5 |
Correct |
146 ms |
15300 KB |
Output is correct |
6 |
Correct |
138 ms |
15216 KB |
Output is correct |
7 |
Correct |
170 ms |
15300 KB |
Output is correct |
8 |
Correct |
127 ms |
15816 KB |
Output is correct |
9 |
Correct |
136 ms |
15840 KB |
Output is correct |
10 |
Correct |
158 ms |
16580 KB |
Output is correct |
11 |
Incorrect |
167 ms |
16324 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |