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));
}
void print(int p = 1, int l = 1, int r = k){
if (l == r)
return void(printf("%d ", tree[p]));
int mid = (l+r)/2;
print(2*p, l, mid);
print(2*p+1, mid+1, r);
if (p == 1)
printf("\n");
}
namespace brute{
set<vector<int> > mys;
vector<int> comb;
void gen(int i, int spec){
if (i == 0){
//printf("now I generated a combination\n");
//for (int j = 0; j < comb.size(); j++)
// printf("%d ", comb[j]);
//printf("\n");
return void(mys.insert(comb));}
int st = 0;
if (spec == i)
st = 1;
for (int j = st; j <= cnt[i]; j++){
comb.push_back(j);
gen(i-1, spec);
comb.pop_back();
}
}
int solve(){
for (int i = 1; i <= k; i++)
cnt[i] = 0;
for (int j = 0; j < n; j++)
cnt[fish[j].se]++;
for (int i = k-1, j = n-1; i >= 0; i--){
while (j >= 0 && big[i].fi < 2*fish[j].fi)
cnt[fish[j--].se]--;
int pre = mys.size();
cnt[big[i].se]++;
gen(k, big[i].se);
cnt[big[i].se]--;
int cur = mys.size();
printf("brute: i = %d, new_combs = %d\n", i, cur-pre);
}
int sz = mys.size();
//printf("sz = %d\n", sz);
return sz%m;
}
};
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]);
//printf("i = %d, f = (%d, %d), fir = %d\n", i, big[i].fi, big[i].se, fir[i]);
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));
//printf("ins solve: i = %d, new_combs = %d\n", i, full+par);
//printf("length = %d, type = %d, par = %d, full = %d\n", big[i].fi, big[i].se, par, full);
ans = add(ans, add(full, par));
}
return ans;
}
int main(){
//freopen("fish.txt", "r", stdin);
/// 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());
//printf("brute: %d\n", brute::solve());
return 0;
}
Compilation message (stderr)
fish.cpp: In function 'int main()':
fish.cpp:178:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
178 | scanf("%d %d %d", &n, &k, &m);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fish.cpp:180:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
180 | 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... |