#include <bits/stdc++.h>
#define fr(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, a, b) for (int i = (a); i >= (b); --i)
#define fe(x, y) for (auto& x : y)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define pw(x) (1LL << (x))
using namespace std;
mt19937_64 rng(228);
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key
template <typename T>
bool umn(T& a, T b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
bool umx(T& a, T b) {
return a < b ? a = b, 1 : 0;
}
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
using ve = vector<T>;
const int N = 1e5 + 5;
int n, q;
pii a[N];
ve<int> order;
int to[N];
int d[N];
ve<pii> Q[N];
int ans[N];
bool subtask1 = 1;
pii get(int v) {
if (to[v] == v) {
return {v, 0};
} else {
pii p = get(to[v]);
p.se += d[v];
to[v] = p.fi;
d[v] = p.se;
return {p.fi, p.se};
}
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
ios::sync_with_stdio(0);
cin.tie(0);
#endif
cin >> n >> q;
subtask1 &= n > 5000 || q > 100;
fr (i, 1, n) {
cin >> a[i].fi >> a[i].se;
order.pb(i);
}
fr (i, 1, q) {
int s, e;
cin >> s >> e;
if (s == e) {
ans[i] = 0;
continue;
}
if (a[s].se == a[e].se) {
ans[i] = 1;
continue;
}
if (a[s].se > a[e].se) {
ans[i] = -1;
continue;
}
Q[e].pb({s, i});
}
sort(all(order), [](int i, int j) {
return a[i].se < a[j].se || (a[i].se == a[j].se && a[i].fi < a[j].fi);
});
fr (i, 1, n) {
to[i] = i;
d[i] = 0;
}
fr (idx, 0, sz(order) - 1) {
int i = order[idx];
// cout << a[i].fi << " " << a[i].se << "\n";
rf (ptr, idx - 1, 0) {
int j = order[ptr];
// subtask1 &= to[j] == j;
if (a[j].se < a[i].fi) {
break;
}
to[j] = i;
d[j] = 1;
}
fe (cur, Q[i]) {
if (subtask1) {
auto p = get(cur.fi);
if (p.fi != i) {
ans[cur.se] = -1;
} else {
ans[cur.se] = p.se;
}
} else {
int v = cur.fi;
int sum = 0;
while (to[v] != v) {
sum += d[v];
v = to[v];
}
if (v == i) {
ans[cur.se] = sum;
} else {
ans[cur.se] = -1;
}
}
}
}
fr (i, 1, q) {
if (ans[i] == -1) {
cout << "impossible\n";
} else {
cout << ans[i] << "\n";
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
54 ms |
9672 KB |
Output is correct |
3 |
Correct |
55 ms |
9824 KB |
Output is correct |
4 |
Correct |
58 ms |
7156 KB |
Output is correct |
5 |
Correct |
57 ms |
9572 KB |
Output is correct |
6 |
Correct |
64 ms |
9212 KB |
Output is correct |
7 |
Correct |
58 ms |
9220 KB |
Output is correct |
8 |
Correct |
47 ms |
7116 KB |
Output is correct |
9 |
Correct |
47 ms |
7184 KB |
Output is correct |
10 |
Correct |
63 ms |
7016 KB |
Output is correct |
11 |
Correct |
75 ms |
7152 KB |
Output is correct |
12 |
Correct |
44 ms |
6964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
3 ms |
2644 KB |
Output is correct |
9 |
Correct |
3 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
3 ms |
2644 KB |
Output is correct |
9 |
Correct |
3 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
3 ms |
2644 KB |
Output is correct |
17 |
Correct |
3 ms |
2644 KB |
Output is correct |
18 |
Correct |
3 ms |
2644 KB |
Output is correct |
19 |
Incorrect |
42 ms |
4792 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
3 ms |
2644 KB |
Output is correct |
9 |
Correct |
3 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
3 ms |
2696 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
3 ms |
2644 KB |
Output is correct |
16 |
Correct |
3 ms |
2644 KB |
Output is correct |
17 |
Correct |
3 ms |
2644 KB |
Output is correct |
18 |
Correct |
4 ms |
2644 KB |
Output is correct |
19 |
Correct |
43 ms |
7812 KB |
Output is correct |
20 |
Correct |
30 ms |
4960 KB |
Output is correct |
21 |
Execution timed out |
1575 ms |
4660 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
9548 KB |
Output is correct |
2 |
Correct |
60 ms |
9800 KB |
Output is correct |
3 |
Correct |
59 ms |
7144 KB |
Output is correct |
4 |
Correct |
51 ms |
7148 KB |
Output is correct |
5 |
Correct |
62 ms |
7096 KB |
Output is correct |
6 |
Incorrect |
77 ms |
6828 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
54 ms |
9672 KB |
Output is correct |
3 |
Correct |
55 ms |
9824 KB |
Output is correct |
4 |
Correct |
58 ms |
7156 KB |
Output is correct |
5 |
Correct |
57 ms |
9572 KB |
Output is correct |
6 |
Correct |
64 ms |
9212 KB |
Output is correct |
7 |
Correct |
58 ms |
9220 KB |
Output is correct |
8 |
Correct |
47 ms |
7116 KB |
Output is correct |
9 |
Correct |
47 ms |
7184 KB |
Output is correct |
10 |
Correct |
63 ms |
7016 KB |
Output is correct |
11 |
Correct |
75 ms |
7152 KB |
Output is correct |
12 |
Correct |
44 ms |
6964 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
3 ms |
2644 KB |
Output is correct |
21 |
Correct |
3 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
1 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
3 ms |
2644 KB |
Output is correct |
29 |
Correct |
3 ms |
2644 KB |
Output is correct |
30 |
Correct |
3 ms |
2644 KB |
Output is correct |
31 |
Incorrect |
42 ms |
4792 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |