#include "bits/stdc++.h"
using namespace std;
struct fish {
int l;
int g;
bool operator<(const fish& oth) {
return l < oth.l;
}
};
struct Segtree {
int n;
int m;
vector<int> t;
Segtree(int n, int m) : n(n), m(m), t(2*n, 1) {}
void inc(int p) {
t[p + n] %= m;
for (t[p += n] += 1; p > 1; p >>= 1) {
t[p >> 1] = t[p] * t[p ^ 1] % m;
}
}
int query(int l, int r) {
int ans = 1;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) ans = ans * t[l++] % m;
if (r & 1) ans = ans * t[--r] % m;
}
return ans;
}
};
signed main() {
int n; cin >> n;
int k; cin >> k;
int m; cin >> m;
vector<fish> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i].l >> a[i].g; --a[i].g;
}
sort(begin(a), end(a));
vector<int> last(k);
vector<vector<int>> o(k);
for (int i = 0; i < n; ++i) {
o[a[i].g].emplace_back(a[i].l);
last[a[i].g] = i;
}
vector<int> cs;
vector<int> idx(k);
for (int i = 0; i < n; ++i) {
if (last[a[i].g] == i) {
idx[a[i].g] = cs.size();
cs.push_back(a[i].g);
}
}
Segtree st(k, m);
int ans = 0;
vector<int> frq(k);
for (int r = 0, l = 0; r < n; ++r) {
int c = a[r].g;
if (last[c] != r) continue;
while (2*a[l].l <= a[r].l) {
st.inc(idx[a[l].g]);
++frq[a[l].g];
++l;
}
int cur1 = st.query(0, idx[c]) * (frq[c] % m) % m;
int ll = idx[c], rr = k;
while (rr - ll > 1) {
int mm = (ll + rr) / 2;
if (o[mm].back() < 2 * o[c][frq[c]]) {
ll = mm;
} else {
rr = mm;
}
}
int cur2 = st.query(0, idx[c]) * st.query(idx[c]+1, ll+1) % m;
ans = (ans + cur1 + cur2) % m;
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
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 |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Incorrect |
2 ms |
308 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
318 ms |
8296 KB |
Output isn't correct |
3 |
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 |
Incorrect |
4 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
137 ms |
4740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
5 ms |
468 KB |
Output is correct |
3 |
Incorrect |
4 ms |
468 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
223 ms |
6256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
339 ms |
8940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
232 ms |
6216 KB |
Output is correct |
2 |
Incorrect |
393 ms |
8788 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
388 ms |
8840 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
416 ms |
10104 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
313 ms |
10184 KB |
Output is correct |
2 |
Correct |
444 ms |
21660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
595 ms |
17796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
457 ms |
18652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
718 ms |
21684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |