#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cassert>
using namespace std;
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], aux_inv_order[N];
bool cmp(int i, int j) {
return aux[i].back() < aux[j].back();
}
const int TM = 1048576 + 7;
int tree[TM];
void upd(int v, int tl, int tr, int i, int x) {
if (tr < i || i < tl) {
return;
}
if (tl == tr) {
tree[v] = x % mod;
return;
}
int tm = (tl + tr) / 2;
upd(2 * v, tl, tm, i, x);
upd(2 * v + 1, tm + 1, tr, i, x);
tree[v] = tree[2 * v] * tree[2 * v + 1] % mod;
}
int get(int v, int tl, int tr, int l, int r) {
if (tr < l || r < tl) {
return 1;
}
if (l <= tl && tr <= r) {
return tree[v];
}
int tm = (tl + tr) / 2;
return get(2 * v, tl, tm, l, r) * get(2 * v + 1, tm + 1, tr, l, r) % mod;
}
int nxt;
void upd(int i, int x) {
upd(1, 1, nxt, i, x);
}
int get(int l, int r) {
return get(1, 1, nxt, l, r);
}
int thesol[N], frq[N], lc[N], rc[N], full[N], semi[N];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
for (int i = 1; i < TM; i++) {
tree[i] = 1;
}
/// freopen ("input", "r", stdin);
cin >> n >> k >> mod;
int init_n = n;
for (int i = 1; i <= n; i++) {
cin >> a[i].sz >> a[i].tp;
}
bool debug = (a[1].tp == 374545);
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;
}
nxt = 1;
while (nxt < n) nxt *= 2;
sort(aux_order + 1, aux_order + n + 1, cmp);
for (int i = 1; i <= n; i++) aux_inv_order[aux_order[i]] = i;
for (int i = 1; i <= n; i++) {
oftp[i].resize((int) aux[aux_order[i]].size());
for (int j = 0; j < (int) oftp[i].size(); j++) {
oftp[i].push_back(aux[aux_order[i]][j]);
}
/// oftp[i].resize(aux[aux_order[i]].begin(), aux[aux_order[i]].end());
}
for (int i = 1; i <= init_n; i++) a[i].tp = aux_inv_order[a[i].tp];
int go = 1, print = 0;
/// if (debug) exit(0);
for (int i = 1; i <= n; i++) {
while (2 * a[go].sz <= a[oftp[i].back()].sz) {
frq[a[go].tp]++;
upd(a[go].tp, frq[a[go].tp] + 1);
go++;
}
thesol[i] = get(1, i - 1);
full[i] = 1;
semi[i] = caneat(i, i) % mod;
int last = i, low = i + 1, high = n;
while (low <= high) {
int j = (low + high) / 2;
if (caneat(j, i) == caneat(i, i)) {
last = j;
low = j + 1;
} else {
high = j - 1;
}
}
full[i] = get(i + 1, last);
full[i] = full[i] * thesol[i] % mod;
semi[i] = semi[i] * thesol[i] % mod;
print = (print + full[i]) % mod;
print = (print + semi[i]) % mod;
}
cout << print << "\n";
return 0;
}
/**
**/
Compilation message
fish.cpp: In function 'int main()':
fish.cpp:98:8: warning: unused variable 'debug' [-Wunused-variable]
98 | bool debug = (a[1].tp == 374545);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
35 ms |
56976 KB |
Execution killed with signal 11 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
38 ms |
56976 KB |
Execution killed with signal 11 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
33 ms |
56952 KB |
Execution killed with signal 11 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
34 ms |
56892 KB |
Execution killed with signal 11 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
36 ms |
56964 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
34 ms |
56964 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
42 ms |
56972 KB |
Execution killed with signal 11 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
35 ms |
57164 KB |
Execution killed with signal 11 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
84 ms |
65392 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
46 ms |
57304 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
115 ms |
65536 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
130 ms |
65536 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
115 ms |
65536 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
162 ms |
65536 KB |
Execution killed with signal 11 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
176 ms |
65536 KB |
Execution killed with signal 11 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
176 ms |
65536 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
245 ms |
65536 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
199 ms |
65536 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
318 ms |
65536 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |