Submission #594705

# Submission time Handle Problem Language Result Execution time Memory
594705 2022-07-12T20:34:50 Z Bench0310 Fish (IOI08_fish) C++17
100 / 100
828 ms 28856 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int mod;
int add(int a,int b){return a+b-(a+b>=mod?mod:0);}
int sub(int a,int b){return a-b+(a-b<0?mod:0);}
int mul(int a,int b){return (a*b)%mod;}

const int N=500005;
int tree[4*N];
array<int,2> fish[N];
int newid[N];
bool islast[N];
int lenlast[N];
int lenprv[N];
int nxtlast[N];
int lenone[N];

void update(int idx,int l,int r,int pos,int x)
{
    if(l==r) tree[idx]=add(tree[idx],x);
    else
    {
        int m=(l+r)/2;
        if(pos<=m) update(2*idx,l,m,pos,x);
        else update(2*idx+1,m+1,r,pos,x);
        tree[idx]=mul(tree[2*idx],tree[2*idx+1]);
    }
}

int query(int idx,int l,int r,int ql,int qr)
{
    if(ql>qr) return 1;
    if(l==ql&&r==qr) return tree[idx];
    int m=(l+r)/2;
    return mul(query(2*idx,l,m,ql,min(qr,m)),query(2*idx+1,m+1,r,max(ql,m+1),qr));
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n,m;
    cin >> n >> m >> mod;
    for(int i=0;i<n;i++) for(int j=0;j<2;j++) cin >> fish[i][j];
    sort(fish,fish+n);
    int tcnt=m;
    for(int i=n-1;i>=0;i--)
    {
        if(newid[fish[i][1]]==0)
        {
            newid[fish[i][1]]=tcnt--;
            islast[i]=1;
        }
    }
    for(int i=0;i<n;i++) fish[i][1]=newid[fish[i][1]];
    for(int i=n-1;i>=0;i--)
    {
        auto [sz,g]=fish[i];
        if(islast[i]) lenlast[g]=sz;
        if(2*sz>lenlast[g]) lenprv[g]=sz;
        if(i+1<n) nxtlast[i]=nxtlast[i+1];
        if(islast[i]) nxtlast[i]=i;
    }
    for(int i=0;i<n;i++)
    {
        auto [sz,g]=fish[i];
        if(2*sz<=lenlast[g]) lenone[g]=sz;
    }
    for(int i=0;i<4*N;i++) tree[i]=1;
    auto getlim=[&](int sz)
    {
        int p=lower_bound(fish,fish+n,array<int,2>{2*sz,0})-fish;
        if(p==n) return m+1;
        return fish[nxtlast[p]][1];
    };
    auto getp=[&](int l,int r)->int{return query(1,1,m,l,r);};
    int res=0;
    int idx=0;
    for(int i=0;i<n;i++)
    {
        auto [sz,g]=fish[i];
        if(!islast[i]) continue;
        while(2*fish[idx][0]<=sz) update(1,1,m,fish[idx++][1],1);
        int one=max(getlim(lenone[g]),g+1);
        int two=getlim(lenprv[g]);
        res=add(res,mul(getp(1,g-1),getp(g+1,two-1)));
        update(1,1,m,g,mod-1);
        res=add(res,getp(1,one-1));
        update(1,1,m,g,1);
    }
    cout << res << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8148 KB Output is correct
2 Correct 158 ms 20016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 8148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 10528 KB Output is correct
2 Correct 83 ms 14172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 8148 KB Output is correct
2 Correct 6 ms 8276 KB Output is correct
3 Correct 8 ms 8276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 102 ms 11648 KB Output is correct
2 Correct 189 ms 20768 KB Output is correct
3 Correct 181 ms 20788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 178 ms 14340 KB Output is correct
2 Correct 177 ms 21056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 12036 KB Output is correct
2 Correct 184 ms 21096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 172 ms 13900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 198 ms 14460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 195 ms 13708 KB Output is correct
2 Correct 235 ms 23404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 378 ms 16116 KB Output is correct
2 Correct 312 ms 18880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 363 ms 15848 KB Output is correct
2 Correct 328 ms 23836 KB Output is correct
3 Correct 442 ms 23464 KB Output is correct
4 Correct 356 ms 23776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 564 ms 17616 KB Output is correct
2 Correct 828 ms 27600 KB Output is correct
3 Correct 644 ms 28588 KB Output is correct
4 Correct 739 ms 28856 KB Output is correct