This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef pair<int, int> ii;
const int len = 5e5+5;
int n, k, m;
ii fish[len], big[len];
int fir[len], cnt[len], pos[len], mx[len], tree[4*len];
int add(int a, int b){
return (a+b)%m;
}
int sub(int a, int b){
return (m+a-b)%m;
}
int mul(int a, int b){
return (a*1LL*b)%m;
}
void build(int p, int l, int r){
if (l == r)
return void(tree[p] = 1);
int mid = (l+r)/2;
build(2*p, l, mid);
build(2*p+1, mid+1, r);
tree[p] = mul(tree[2*p], tree[2*p+1]);
}
void update(int p, int l, int r, int i){
if (l == r)
return void(tree[p] = add(tree[p], 1));
int mid = (l+r)/2;
if (i <= mid)
update(2*p, l, mid, i);
else
update(2*p+1, mid+1, r, i);
tree[p] = mul(tree[2*p], tree[2*p+1]);
}
int query(int p, int l, int r, int i, int j){
if (r < i || j < l)
return 1;
if (i <= l && r <= j)
return tree[p];
int mid = (l+r)/2;
return mul(query(2*p, l, mid, i, j), query(2*p+1, mid+1, r, i, j));
}
int solve(){
/// now fix big[], pos[]
for (int i = 1; i <= k; i++)
pos[i] = -1;
for (int i = n-1, j = 0; i >= 0; i--)
if (pos[fish[i].se] == -1)
big[j] = fish[i], pos[fish[i].se] = j++;
for (int i = 1; i <= k; i++)
pos[i] = k-pos[i]-1;
reverse(big, big+k);
/// fix fir[]
for (int i = 1; i <= k; i++)
cnt[i] = 0, mx[i] = -1;
for (int i = 0; i < k; i++)
fir[i] = k;
for (int i = 0, j = 0; i < k; i++){
while (big[i].fi >= 2*fish[j].fi){
cnt[fish[j].se]++;
if (cnt[fish[j].se] == mx[fish[j].se])
fir[pos[fish[j].se]] = i;
j++;
}
mx[big[i].se] = cnt[big[i].se]+1;
}
/// compute the answer
build(1, 0, k-1);
int ans = 0;
for (int i = 0, j = 0; i < k; i++){
while (big[i].fi >= 2*fish[j].fi)
update(1, 0, k-1, pos[fish[j++].se]);
int full = mul(query(1, 0, k-1, 0, i-1), query(1, 0, k-1, i+1, fir[i]-1));
int par = mul(query(1, 0, k-1, 0, i-1), sub(query(1, 0, k-1, i, i), 1));
ans = add(ans, add(full, par));
}
return ans;
}
int main(){
/// read input and sort fish[]
scanf("%d %d %d", &n, &k, &m);
for (int i = 0; i < n; i++)
scanf("%d %d", &fish[i].fi, &fish[i].se);
sort(fish, fish+n);
printf("%d\n", solve());
return 0;
}
Compilation message (stderr)
fish.cpp: In function 'int main()':
fish.cpp:110:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
110 | scanf("%d %d %d", &n, &k, &m);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fish.cpp:112:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
112 | scanf("%d %d", &fish[i].fi, &fish[i].se);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |