#include <bits/stdc++.h>
#define X first
#define Y second
using namespace std;
typedef long long llint;
const int maxn = 1e6+10;
const int base = 31337;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const int logo = 19;
const int off = 1 << logo;
const int treesiz = off << 1;
int n, k, m;
pair<int, int> niz[maxn];
int cnt[maxn];
int tour[treesiz];
bool bio[maxn];
int id[maxn], fir[maxn];
int nex[maxn], las[maxn];
vector< int > v[maxn];
int mul(int a, int b) {
a %= m, b %= m;
return (a * b) % m;
}
void update(int x, int val) {
x += off;
tour[x] += val;
x /= 2;
while (x > 0) tour[x] = mul(tour[x * 2], tour[x * 2 + 1]), x /= 2;
}
int query(int a, int b, int l, int r, int node) {
if (l > b || r < a) return 1;
if (l >= a && r <= b) return tour[node];
int mid = (l + r) / 2;
return mul(query(a, b, l, mid, node * 2), query(a, b, mid + 1, r, node * 2 + 1));
}
int main() {
scanf("%d%d%d", &n, &k, &m);
for (int i = 0; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
niz[i] = {a, b};
}
sort(niz, niz+n);
reverse(niz, niz+n);
int cnt = 1;
for (int i = 0; i < n; i++) {
int col = niz[i].Y;
if (bio[col]) continue;
bio[col] = true;
fir[cnt] = i;
id[col] = cnt++;
}
for (int i = 0; i < n; i++)
niz[i].Y = id[niz[i].Y];
for (int i = 0; i < n; i++)
v[niz[i].Y].push_back(i);
for (int i = 1; i <= k; i++) {
nex[fir[i]] = n;
for (int tren : v[i]) {
if (niz[tren].X * 2 > niz[fir[i]].X) {
nex[fir[i]] = tren;
//break;
}
}
}
for (int i = 0; i < treesiz; i++) tour[i] = 1;
for (int i = 0; i < n; i++) update(niz[i].Y, 1);
int sol = 0;
int ptr = 0;
memset(bio, false, sizeof bio);
for (int i = 0; i < n; i++) {
int len = niz[i].X;
int col = niz[i].Y;
if (bio[col]) continue;
bio[col] = true;
while (ptr < n && niz[ptr].X * 2 > niz[i].X)
update(niz[ptr++].Y, -1);
//printf("debug: %d %d (%d) --> %d\n", len, col, i, ptr);
//for (int i = 1; i <= k; i++) printf("%d ", tour[i + off]); printf("\n");
update(col, -1);
sol += query(col, k + 1, 0, off - 1, 1);
update(col, 1);
sol %= m;
int lo = 0, hi = col;
while (lo < hi) {
int mid = (lo + hi + 1) / 2;
if (niz[fir[mid]].X >= 2 * niz[nex[i]].X) lo = mid;
else hi = mid - 1;
}
//printf("found: %d\n", lo);
sol += mul(query(lo + 1, col - 1, 0, off - 1, 1), query(col + 1, k + 1, 0, off - 1, 1));
sol %= m;
}
printf("%d\n", sol);
return 0;
}
Compilation message
fish.cpp: In function 'int main()':
fish.cpp:86:7: warning: unused variable 'len' [-Wunused-variable]
86 | int len = niz[i].X;
| ^~~
fish.cpp:47:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d%d%d", &n, &k, &m);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
fish.cpp:50:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
28884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
28880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
28884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
28820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
28884 KB |
Output is correct |
2 |
Correct |
15 ms |
28884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
28836 KB |
Output is correct |
2 |
Correct |
333 ms |
35440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
28884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
28880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
178 ms |
31972 KB |
Output is correct |
2 |
Correct |
181 ms |
32252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
29012 KB |
Output is correct |
2 |
Correct |
21 ms |
29012 KB |
Output is correct |
3 |
Correct |
20 ms |
29032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
215 ms |
33316 KB |
Output is correct |
2 |
Correct |
374 ms |
35384 KB |
Output is correct |
3 |
Correct |
353 ms |
35396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
400 ms |
36164 KB |
Output is correct |
2 |
Correct |
359 ms |
36044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
251 ms |
34408 KB |
Output is correct |
2 |
Correct |
387 ms |
35900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
380 ms |
35860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
439 ms |
36952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
446 ms |
34944 KB |
Output is correct |
2 |
Correct |
694 ms |
40384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
809 ms |
40468 KB |
Output is correct |
2 |
Correct |
657 ms |
38608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
739 ms |
41036 KB |
Output is correct |
2 |
Correct |
724 ms |
40232 KB |
Output is correct |
3 |
Correct |
888 ms |
43620 KB |
Output is correct |
4 |
Correct |
708 ms |
40380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1031 ms |
42856 KB |
Output is correct |
2 |
Correct |
1642 ms |
54868 KB |
Output is correct |
3 |
Correct |
1635 ms |
54796 KB |
Output is correct |
4 |
Correct |
1476 ms |
50332 KB |
Output is correct |