求助 60分WA 堆做法

P3740 [HAOI2014] 贴海报

Windless_Wilds @ 2022-04-06 13:00:26

#include<bits/stdc++.h>
#define ll long long
#define rg register
#define il inline
#define MAXN 10000000
#define MAXM 1000
using namespace std;
struct poster
{
    ll r,pri;
    bool operator<(const poster& rhs) const
    {
        return pri>rhs.pri;
    }
}p[MAXM+5];
priority_queue<poster>mo;
ll pta=0,n,m,pla[MAXN+5],ans=0;
ll color[MAXN+5],rmax=0;
bool checo[MAXN+1];
int main()
{
    cin>>n>>m;
    for (rg ll i=1;i<=m;++i)
    {
        ll l;
        cin>>l>>p[++pta].r;
        rmax=max(rmax,p[pta].r);
        p[pta].pri=pta;
        if (pla[l]&&p[pta].r<p[pla[l]].r)
        {
            ++ans;
        }
        else
            pla[l]=pta;
    }
    ll cur=0;
    for ( ll i=1;i<=rmax;++i)                  //O(n+m)
    {
        if (pla[i])
        {
            ll x=pla[i];
            if (p[x].pri<p[cur].pri)
            {
                mo.push(p[x]);
            }
            else
            {
                mo.push(p[cur]);
                cur=x;
            }
        }
        if (i==p[cur].r+1)
        {
            if (mo.empty()) cur=0;
            else
            {
                while (mo.size()&&i>mo.top().r) mo.pop();
                if (mo.empty()) cur=0;
                else
                cur=mo.top().pri;
            }
        }
        color[i]=cur;
    }
    for (rg ll i=1;i<=rmax;++i)
    {
        if (color[i]&&!checo[color[i]])
        {
            ++ans;
            checo[color[i]]=1;
        }
    }
    cout<<ans;
    return 0;
}

by Windless_Wilds @ 2022-04-06 13:02:25

求调试样例


|