#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];
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, R = n;
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;
Lb[p[i].c] = l;
if(!f[p[i].c]) {
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 = n + 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(){
| ^~~~
fish.cpp: In function 'int main()':
fish.cpp:60:16: warning: unused variable 'R' [-Wunused-variable]
60 | int l = L, R = n;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
4 ms |
4948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Runtime error |
283 ms |
19440 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
152 ms |
12220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5076 KB |
Output is correct |
2 |
Correct |
7 ms |
5284 KB |
Output is correct |
3 |
Incorrect |
9 ms |
5204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
241 ms |
16060 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
225 ms |
19544 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
244 ms |
15928 KB |
Output is correct |
2 |
Runtime error |
275 ms |
19824 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
245 ms |
20060 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
373 ms |
20692 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
211 ms |
22228 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
322 ms |
30048 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
217 ms |
30172 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
309 ms |
30940 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |