#include<bits/stdc++.h>
#define f first
#define s second
#define int long long
#define pii pair<int,int>
using namespace std;
const int N = 2e5 + 5; // !
int t[4 * N], mod, f[N];
struct fish {
int len, c;
} p[N];
int T = 0;
bool cmp(fish x, fish y) {
return (y.len > x.len);
}
void build(int u, int l, int r) {
t[u] = 1;
if(l == r) return;
int mid = (l + r) / 2;
build(2 * u, l, mid);
build(2 * u + 1, mid + 1, r);
}
void upd(int u, int id, int l, int r, int v) {
if(l > id || r < id) return;
if(l == r) {
t[u] += v;
if(t[u] < 1) t[u] = 1;
return;
}
int mid = (l + r) / 2;
if(id <= mid) upd(2 * u, id, l, mid, v); else upd(2 * u + 1, id, mid + 1, r, v);
t[u] = t[2 * u] * t[2 * u + 1] % mod;
}
int get(int u, int st, int en, int l, int r) {
if(l > en || r < st) return 1;
if(st <= l && r <= en) return t[u] % mod;
int mid = (l + r) / 2;
return get(2 * u, st, en, l, mid) * get(2 * u + 1, st, en, mid + 1, r) % mod;
}
main(){
int n, k;
cin >> n >> k >> mod;
for(int i = 1; i <= n; i++) {
cin >> p[i].len >> p[i].c;
}
build(1, 1, k);
sort(p + 1, p + n + 1, cmp);
int ans = 0, L = 0;
vector<int> cn(k + 16);
while(L < n && p[L + 1].len * 2 <= p[n].len) ++L, upd(1, p[L].c, 1, k, 1), ++cn[p[L].c];
int l = L;
for(int i = n; i >= 1; i--) {
while(l > 0 && p[l].len * 2 > p[i].len) upd(1, p[l].c, 1, k, -1), --l;
if(get(1, p[i].c, p[i].c, 1, k) == cn[p[i].c] + 1 && !f[p[i].c]) {
ans = (ans + get(1, 1, p[i].c - 1, 1, k) * get(1, p[i].c + 1, k, 1, k) % mod) % mod;
}
if(!f[p[i].c]) f[p[i].c] = i;
}
build(1, 1, k);
l = 0;
while(l < n && p[l + 1].len * 2 <= p[n].len) ++l, upd(1, p[l].c, 1, k, 1);
for(int i = n; i >= 1; i--) {
while(l > 0 && p[l].len * 2 > p[i].len) upd(1, p[l].c, 1, k, -1), --l;
if(f[p[i].c] == i) {
ans = (ans + get(1, 1, p[i].c - 1, 1, k) * get(1, p[i].c + 1, k, 1, k) % mod
* (get(1, p[i].c, p[i].c, 1, k) - (get(1, p[i].c, p[i].c, 1, k) == cn[p[i].c] + 1) + mod) % mod) % mod;
if(i == 1) T = 1;
upd(1, p[i].c, 1, k, -n);
}
}
cout << ans;
}
Compilation message
fish.cpp:40:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
40 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Runtime error |
278 ms |
16172 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
148 ms |
3328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
412 KB |
Output is correct |
2 |
Incorrect |
6 ms |
444 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
269 ms |
4960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
436 ms |
8096 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
239 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
455 ms |
7376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
511 ms |
8196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
395 ms |
6948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
572 ms |
10280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
556 ms |
10316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
471 ms |
10672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |