#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 = 5e5 + 5; // !
int t[4 * N], mod, f[N];
vector<int> x[N];
int id[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 K = k;
for(int i = n; i >= 1; i--) {
if(!f[p[i].c]) {
f[p[i].c] = K--;
}
}
for(int i = 1; i <= n; i++) p[i].c = f[p[i].c], x[p[i].c].push_back(i);
for(int i = 1; i <= k; i++) f[i] = 0;
int ans = 0, L = 0;
while(L < n && p[L + 1].len * 2 <= p[n].len) ++L, upd(1, p[L].c, 1, k, 1);
int l = L;
vector<int> Lb(n + 2);
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]) {
Lb[p[i].c] = l;
int X = x[p[i].c][get(1, p[i].c, p[i].c, 1, k) - 1];
int l = p[i].c + 1, r = k, id = k + 1;
while(l <= r) {
int mid = (l + r) / 2;
if(Lb[mid] >= X) id = mid, r = mid - 1;
else l = mid + 1;
}
ans = (ans + get(1, 1, p[i].c - 1, 1, k) * get(1, p[i].c + 1, id - 1, 1, k) % mod) % mod;
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) - 1 + mod) % mod) % mod;
if(i == 1) T = 1;
upd(1, p[i].c, 1, k, -n);
}
}
cout << ans;
}
/*
5
3
7
2 2
5 1
8 3
4 1
2 3
5 3 7
2 2
2 3
4 1
5 1
8 3
*/
Compilation message
fish.cpp:42:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
42 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
11976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
11988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
11988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
11976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12056 KB |
Output is correct |
2 |
Correct |
296 ms |
28276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
140 ms |
19392 KB |
Output is correct |
2 |
Correct |
178 ms |
20848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12116 KB |
Output is correct |
2 |
Incorrect |
11 ms |
12244 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
232 ms |
23156 KB |
Output is correct |
2 |
Incorrect |
438 ms |
28600 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
446 ms |
30204 KB |
Output is correct |
2 |
Correct |
397 ms |
30048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
221 ms |
23116 KB |
Output is correct |
2 |
Incorrect |
422 ms |
29172 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
439 ms |
28372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
459 ms |
30888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
391 ms |
27452 KB |
Output is correct |
2 |
Correct |
539 ms |
41840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
746 ms |
35496 KB |
Output is correct |
2 |
Correct |
581 ms |
34676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
762 ms |
36032 KB |
Output is correct |
2 |
Correct |
681 ms |
41988 KB |
Output is correct |
3 |
Correct |
821 ms |
44720 KB |
Output is correct |
4 |
Correct |
716 ms |
41932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1119 ms |
37820 KB |
Output is correct |
2 |
Correct |
1330 ms |
58792 KB |
Output is correct |
3 |
Correct |
965 ms |
59788 KB |
Output is correct |
4 |
Correct |
1280 ms |
55980 KB |
Output is correct |