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>
using namespace std;
typedef long long ll;
struct Fish {
int sz;
int tp;
};
bool operator < (Fish a, Fish b) {
return a.sz < b.sz;
}
const int N = (int) 5e5 + 7;
int n, k, mod;
Fish a[N];
vector<int> aux[N], oftp[N];
int caneat(int f, int s) {
int low = 0, high = (int) oftp[s].size() - 1, sol = 0;
while (low <= high) {
int mid = (low + high) / 2;
if (a[oftp[f].back()].sz >= 2 * a[oftp[s][mid]].sz) {
sol = mid + 1;
low = mid + 1;
} else {
high = mid - 1;
}
}
return sol;
}
int aux_order[N];
bool cmp(int i, int j) {
return aux[i].back() < aux[j].back();
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> k >> mod;
for (int i = 1; i <= n; i++) {
cin >> a[i].sz >> a[i].tp;
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) {
aux[a[i].tp].push_back(i);
}
n = k;
for (int i = 1; i <= n; i++) {
aux_order[i] = i;
}
sort(aux_order + 1, aux_order + n + 1, cmp);
for (int i = 1; i <= n; i++) oftp[i] = aux[aux_order[i]];
for (int i = 1; i <= n; i++) aux[i] = oftp[i];
/**
0 0 0
1 0 1
1 1 1
type 1 can eat :
type 2 can eat : 1 3
type 3 can eat : 1 2 3
**/
int print = 0;
for (int i = n; i >= 1; i--) {
/**
count all new representations that can be eaten by the fish i
aka all representations that can be eaten by fish i and not by a fish bigger than him
let's look at what restrictions the other fish impose on fish j
only the bigger fish than him impose restrictions
**/
int full = 1, semi = caneat(i, i);
for (int j = i + 1; j <= n; j++) {
if (caneat(j, i) >= caneat(i, i) + 1) {
/// I can eat only 0 j
break;
}
/// I can eat 0 j (available always)
/// I can eat any amount of fish j if I ...
full = full * (ll) (caneat(i, j) + 1) % mod;
/// assert(caneat(j, i) == caneat(i, i));
}
int sol = 1;
for (int j = 1; j < i; j++) {
sol = sol * (ll) (caneat(i, j) + 1) % mod;
}
full = full * (ll) sol % mod;
semi = semi * (ll) sol % mod;
print = (print + full) % mod;
print = (print + semi) % mod;
}
cout << print << "\n";
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... |