#include <bits/stdc++.h>
#define int long long
using namespace std;
int F, K, M;
pair<int, int> fish[500005]; //(length, color)
struct mulseg
{
vector<int> tree;
mulseg(int n)
{
int sz = 1 << ((int)ceil(log2(n+1))+1);
tree.resize(sz);
}
int init(int s, int e, int node, vector<int> &arr)
{
if (s == e) return tree[node] = arr[s] % M;
return tree[node] = init(s, (s+e)/2, 2*node, arr) * init((s+e)/2+1, e, 2*node+1, arr) % M;
}
void upd(int s, int e, int node, int id, int diff)
{
if (e < id || id < s) return;
if (s == e) {
tree[node] = (tree[node] + diff + M) % M;
return;
}
upd(s, (s+e)/2, 2*node, id, diff);
upd((s+e)/2+1, e, 2*node+1, id, diff);
tree[node] = tree[2*node] * tree[2*node+1] % M;
}
int query(int s, int e, int node, int l, int r)
{
if (l > r) return 1;
if (e < l || r < s) return 1;
if (l <= s && e <= r) return tree[node];
return query(s, (s+e)/2, 2*node, l, r) * query((s+e)/2+1, e, 2*node+1, l, r) % M;
}
};
vector<int> lst[500005];
pair<int, int> x[500005];
signed main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin >> F >> K >> M;
for (int i = 0; i < F; i++) {
cin >> fish[i].first >> fish[i].second;
}
sort(fish, fish + F);
vector<int> trans(1, 0);
vector<bool> vis(K+1);
for (int i = F - 1; i >= 0; i--) {
if (!vis[fish[i].second]) {
trans.push_back(fish[i].second);
vis[fish[i].second] = true;
}
}
fill(x, x + 500005, make_pair(-1, -1));
vector<int> to(K+1);
for (int i = 1; i <= K; i++) to[trans[i]] = i;
for (int i = 0; i < F; i++) fish[i].second = to[fish[i].second];
for (int i = F-1; i >= 0; i--) {
if (x[fish[i].second].second == -1) {
x[fish[i].second] = make_pair(fish[i].first, i);
}
}
for (int i = 0; i < F; i++) lst[fish[i].second].push_back(fish[i].first);
mulseg seg(K);
vector<int> cnt(K+1);
int fin = fish[F-1].first;
int pos = 0;
while (pos < F && fish[pos].first <= fin/2) pos++;
for (int i = 0; i < pos; i++) cnt[fish[i].second]++;
for (int i = 1; i <= K; i++) cnt[i]++;
seg.init(1, K, 1, cnt);
int ans = 0;
for (int i = 1; i <= K; i++) {
int now = x[i].second;
while (pos > 0 && fish[pos-1].first > fish[now].first/2) {
seg.upd(1, K, 1, fish[pos-1].second, -1);
pos--;
}
int len = 2 * *upper_bound(lst[i].begin(), lst[i].end(), fish[now].first/2);
int l = 1, r = K;
while (l < r) {
int m = l + r >> 1;
if (x[m].first >= len) l = m+1;
else r = m;
}
ans += (seg.query(1, K, 1, l, i-1) - 1 + seg.query(1, K, 1, i, i)) * seg.query(1, K, 1, i+1, K);
ans %= M;
}
assert(0 <= ans && ans < M);
cout << ans << '\n';
}
Compilation message
fish.cpp: In function 'int main()':
fish.cpp:84:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
84 | int m = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19832 KB |
Output is correct |
2 |
Correct |
11 ms |
19848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
19796 KB |
Output is correct |
2 |
Correct |
129 ms |
32120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
20020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
25608 KB |
Output is correct |
2 |
Correct |
86 ms |
29800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
20000 KB |
Output is correct |
2 |
Correct |
14 ms |
20052 KB |
Output is correct |
3 |
Correct |
11 ms |
20104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
28572 KB |
Output is correct |
2 |
Correct |
169 ms |
39192 KB |
Output is correct |
3 |
Correct |
142 ms |
39888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
33224 KB |
Output is correct |
2 |
Correct |
145 ms |
41032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
28592 KB |
Output is correct |
2 |
Correct |
156 ms |
33272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
164 ms |
32928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
200 ms |
35072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
32456 KB |
Output is correct |
2 |
Correct |
241 ms |
39644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
337 ms |
42228 KB |
Output is correct |
2 |
Correct |
273 ms |
42508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
327 ms |
42720 KB |
Output is correct |
2 |
Correct |
309 ms |
47460 KB |
Output is correct |
3 |
Correct |
348 ms |
51716 KB |
Output is correct |
4 |
Correct |
312 ms |
47424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
476 ms |
44828 KB |
Output is correct |
2 |
Correct |
539 ms |
65536 KB |
Output is correct |
3 |
Correct |
396 ms |
65536 KB |
Output is correct |
4 |
Correct |
551 ms |
65536 KB |
Output is correct |