#include <bits/stdc++.h>
using namespace std;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
const int maxn = 5e5 + 20;
pair<int, int> a[maxn];
vector<int> pos[maxn];
int cnt[maxn];
int tree[maxn * 4];
int id[maxn];
int max_id[maxn];
int mod;
void build(int id, int lt, int rt) {
if (lt == rt) {
tree[id] = cnt[lt] % mod;
return;
}
int mt = (lt + rt) / 2;
build(id * 2, lt, mt);
build(id * 2 + 1, mt + 1, rt);
tree[id] = tree[id * 2] * tree[id * 2 + 1] % mod;
}
void update(int id, int lt, int rt, int pos, int val) {
if (lt == rt) {
tree[id] = val % mod;
return;
}
int mt = (lt + rt) / 2;
if (pos <= mt) {
update(id * 2, lt, mt, pos, val);
}
else {
update(id * 2 + 1, mt + 1, rt, pos, val);
}
tree[id] = tree[id * 2] * tree[id * 2 + 1] % mod;
}
int get(int id, int lt, int rt, int ql, int qr) {
if (ql > qr) {
return 1;
}
if (lt == ql && rt == qr) {
return tree[id];
}
int mt = (lt + rt) / 2;
if (qr <= mt) {
return get(id * 2, lt, mt, ql, qr);
}
else if (ql >= mt + 1) {
return get(id * 2 + 1, mt + 1, rt, ql, qr);
}
else {
return get(id * 2, lt, mt, ql, mt) * get(id * 2 + 1, mt + 1, rt, mt + 1, qr) % mod;
}
}
void just_do_it() {
int n, k;
cin >> n >> k >> mod;
for (int i = 1; i <= n; i++) {
cin >> a[i].first >> a[i].second;
}
sort(a + 1, a + n + 1);
k = 0;
for (int i = n; i >= 1; i--) {
if (!id[a[i].second]) {
id[a[i].second] = ++k;
}
a[i].second = id[a[i].second];
}
for (int i = 1; i <= n; i++) {
cnt[a[i].second]++;
pos[a[i].second].push_back(i);
}
for (int i = 1; i <= k; i++) {
cnt[i]++;
}
for (int i = n; i >= 1; i--) {
max_id[i] = max(max_id[i + 1], a[i].second);
}
build(1, 1, k);
int lt = n;
int res = 0;
for (int i = n; i >= 1; i--) {
while (lt > 0 && a[lt].first * 2 > a[i].first) {
cnt[a[lt].second]--;
update(1, 1, k, a[lt].second, cnt[a[lt].second]);
lt--;
}
if (max_id[i] != max_id[i + 1]) {
int x = max_id[i];
res += (cnt[x] - 1) % mod * get(1, 1, k, x + 1, k) % mod;
res %= mod;
int low = max_id[lower_bound(a + 1, a + n + 1, make_pair(a[pos[x][cnt[x] - 1]].first * 2, -1)) - a] + 1;
res += get(1, 1, k, low, x - 1) * get(1, 1, k, x + 1, k) % mod;
res %= mod;
}
}
cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12116 KB |
Output is correct |
2 |
Correct |
8 ms |
12244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12012 KB |
Output is correct |
2 |
Correct |
151 ms |
20444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12068 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 |
84 ms |
15640 KB |
Output is correct |
2 |
Correct |
91 ms |
16448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12116 KB |
Output is correct |
2 |
Correct |
10 ms |
12252 KB |
Output is correct |
3 |
Correct |
9 ms |
12116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
128 ms |
17560 KB |
Output is correct |
2 |
Correct |
203 ms |
20432 KB |
Output is correct |
3 |
Correct |
200 ms |
20452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
178 ms |
20708 KB |
Output is correct |
2 |
Correct |
215 ms |
21320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
123 ms |
17516 KB |
Output is correct |
2 |
Correct |
255 ms |
20684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
210 ms |
20444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
279 ms |
21784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
227 ms |
20044 KB |
Output is correct |
2 |
Correct |
347 ms |
24600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
421 ms |
25848 KB |
Output is correct |
2 |
Correct |
270 ms |
28436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
298 ms |
26692 KB |
Output is correct |
2 |
Correct |
420 ms |
32448 KB |
Output is correct |
3 |
Correct |
320 ms |
35524 KB |
Output is correct |
4 |
Correct |
358 ms |
32280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
446 ms |
28024 KB |
Output is correct |
2 |
Correct |
529 ms |
48844 KB |
Output is correct |
3 |
Correct |
462 ms |
49808 KB |
Output is correct |
4 |
Correct |
516 ms |
45812 KB |
Output is correct |