# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
132067 |
2019-07-18T09:20:23 Z |
dragonslayerit |
Fish (IOI08_fish) |
C++14 |
|
1383 ms |
51284 KB |
#include <cstdio>
#include <algorithm>
#include <vector>
//Strategy:
//Sort fishes by size
//Relabel gems by last appearance (e.g. gem of largest fish is 0)
//count multisets of gems => count multisets of fish such that if a fish is chosen, all smaller fish with same gem are chosen
//For the actual subset of fish, some gem will be chosen to be "boosted" to the largest fish with that gem.
//Iterate over last fish in multiset (pre-boosting) and count number of valid subsets
//A multiset is bad iff no gem in the prefix is present and the largest fish's gem cannot be boosted.
int N,MOD;
int cps[500005];
int cps_size=0;
int last[500005];
int half[500005];
int prev[500005];
struct Fish{
int size,gem;
bool operator<(Fish f)const{
return size<f.size;
}
}fishes[500005];
struct Op{
int sgn;
int l,r,x;
Op(int sgn,int l,int r,int x):sgn(sgn),l(l),r(r),x(x){
}
};
std::vector<Op> ops[500005];
int st[1000010];
void pull(int i){
st[i]=1LL*st[i<<1]*st[i<<1|1]%MOD;
}
void update(int i){
for(st[i+=N]++;i>1;i>>=1){
pull(i>>1);
}
}
int query(int a,int b){
int res=1;
for(a+=N,b+=N;a<b;a>>=1,b>>=1){
if(a&1) res=1LL*res*st[a++]%MOD;
if(b&1) res=1LL*res*st[--b]%MOD;
}
return res;
}
int main(){
int K;
scanf("%d %d",&N,&K);
scanf("%d",&MOD);
for(int i=0;i<N;i++){
scanf("%d %d",&fishes[i].size,&fishes[i].gem);
fishes[i].gem--;
}
std::sort(fishes,fishes+N);
//Relabel gems by last appearance
std::fill(cps,cps+K,-1);
for(int i=N-1;i>=0;i--){
if(cps[fishes[i].gem]==-1){
cps[fishes[i].gem]=cps_size++;
}
}
for(int i=0;i<N;i++){
fishes[i].gem=cps[fishes[i].gem];
}
for(int k=0;k<K;k++){
last[k]=-1;
}
for(int i=0;i<N;i++){
prev[i]=last[fishes[i].gem];
last[fishes[i].gem]=i;
}
//half[k] is index of first fish larger than fishes[last[k]].size/2
for(int k=0;k<K;k++){
int low=-1,high=N;
while(high-low>1){
int mid=(low+high)/2;
if(fishes[mid].size*2<=fishes[last[k]].size){
low=mid;
}else{
high=mid;
}
}
half[k]=high;
}
int prefix=K-1;//largest gem # that can be "boosted"
for(int i=0;i<N;i++){
//Counting configurations where fishes[i].gem occurs exactly freq[fishes[i].gem] times
while(prefix>=0&&fishes[last[prefix]].size<2*fishes[i].size) prefix--;
//Subtract multisets where none in prefix exist to be boosted
ops[i+1].emplace_back(-1,prefix+1,K,fishes[i].gem);
//Add back multisets where none in prefix exist to be boosted but fishes[i] can be boosted
//(Must be able to take all of fishes[i].gem before i)
if(prev[i]==-1||fishes[prev[i]].size*2<=fishes[last[fishes[i].gem]].size){
//Cannot have fish whose size <=1/2*size of last fish with fishes[i].gem
ops[std::min(i,half[fishes[i].gem])].emplace_back(1,prefix+1,K,fishes[i].gem);
}
}
//Count all
ops[N].emplace_back(1,0,K,-1);
//init st
std::fill(st,st+N*2,1);
//-1 for empty set
int ans=-1;
for(int i=0;i<=N;i++){
for(Op op:ops[i]){
if(op.x>=op.l&&op.x<op.r){
ans=(ans+1LL*query(op.l,op.x)*query(op.x+1,op.r)%MOD*op.sgn)%MOD;
}else{
ans=(ans+1LL*query(op.l,op.r)*op.sgn)%MOD;
}
}
if(i==N) break;
update(fishes[i].gem);
}
printf("%d\n",(ans+MOD)%MOD);
return 0;
}
Compilation message
fish.cpp: In function 'int main()':
fish.cpp:62:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&N,&K);
~~~~~^~~~~~~~~~~~~~~
fish.cpp:63:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&MOD);
~~~~~^~~~~~~~~~~
fish.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&fishes[i].size,&fishes[i].gem);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
12024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
12024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12152 KB |
Output is correct |
2 |
Correct |
13 ms |
12152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12152 KB |
Output is correct |
2 |
Correct |
502 ms |
41748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
12152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
201 ms |
23660 KB |
Output is correct |
2 |
Correct |
239 ms |
26872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
12280 KB |
Output is correct |
2 |
Correct |
19 ms |
12520 KB |
Output is correct |
3 |
Correct |
18 ms |
12408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
334 ms |
30016 KB |
Output is correct |
2 |
Correct |
520 ms |
41848 KB |
Output is correct |
3 |
Correct |
544 ms |
41916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
534 ms |
44904 KB |
Output is correct |
2 |
Correct |
581 ms |
41848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
369 ms |
27508 KB |
Output is correct |
2 |
Correct |
614 ms |
41928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
551 ms |
39200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
657 ms |
41948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
545 ms |
36324 KB |
Output is correct |
2 |
Correct |
898 ms |
40292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
908 ms |
42208 KB |
Output is correct |
2 |
Correct |
663 ms |
33756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
812 ms |
43860 KB |
Output is correct |
2 |
Correct |
968 ms |
43544 KB |
Output is correct |
3 |
Correct |
969 ms |
49088 KB |
Output is correct |
4 |
Correct |
944 ms |
44316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1165 ms |
46768 KB |
Output is correct |
2 |
Correct |
1156 ms |
51284 KB |
Output is correct |
3 |
Correct |
1159 ms |
51256 KB |
Output is correct |
4 |
Correct |
1383 ms |
51140 KB |
Output is correct |