This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int n, k, m, segtree[2000000];
tuple<int, int, int> f[500000];
pair<int, int> mx[500000];
int nxt[500000], ord[500000], idx[500000];
void increment(int pos, int node = 1, int l = 0, int r = k - 1) {
if (l == r) segtree[node] = (segtree[node] + 1) % m;
else {
int mid = (l + r) / 2;
if (pos > mid) increment(pos, node * 2 + 1, mid + 1, r);
else increment(pos, node * 2, l, mid);
segtree[node] = (segtree[node * 2] * segtree[node * 2 + 1]) % m;
}
}
int query(int a, int b, int node = 1, int l = 0, int r = k - 1) {
if (l > b || r < a) return 1;
if (l >= a && r <= b) return segtree[node];
int mid = (l + r) / 2;
return (query(a, b, node * 2, l, mid) * query(a, b, node * 2 + 1, mid + 1, r)) % m;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k >> m;
fill(segtree, segtree + 4 * k, 1);
for (int i = 0; i < n; i++) {
int l, t;
cin >> l >> t;
t--;
mx[t] = max(mx[t], {l, i});
nxt[t] = max(nxt[t], l);
f[i] = {l, t, i};
}
for (int i = 0; i < n; i++)
if (mx[get<1>(f[i])].first < get<0>(f[i]) * 2)
nxt[get<1>(f[i])] = min(nxt[get<1>(f[i])], get<0>(f[i]));
sort(f, f + n);
iota(ord, ord + k, 0);
sort(ord, ord + k, [](int A, int B) { return mx[A] < mx[B]; });
for (int i = 0; i < k; i++) idx[ord[i]] = i;
int ans = 0;
for (int i = 0, j = 0, l = 0; i < n; i++) {
while (get<0>(f[i]) >= get<0>(f[j]) * 2) increment(idx[get<1>(f[j++])]);
if (get<2>(f[i]) == mx[get<1>(f[i])].second) {
ans = (ans + query(0, idx[get<1>(f[i])])) % m;
while (l < k && mx[ord[l]].first < nxt[get<1>(f[i])] * 2) l++;
if (l - idx[get<1>(f[i])] > 1)
ans = (ans + query(0, idx[get<1>(f[i])] - 1) * (query(idx[get<1>(f[i])] + 1, l - 1) + m - 1)) % m;
}
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |