求调,样例2及sub0过不去
查看原帖
求调,样例2及sub0过不去
437398
EastIsRed楼主2024/4/13 16:57
#include<cstdio>
#include<map>
#include<set>
#include<algorithm>
int c,n,m,q;
struct node{
    int l,r;
    int val;
}tr[800086];
inline void push_up(int now)
{
    tr[now].val=tr[now<<1].val+tr[now<<1|1].val;
}
void build(int now,int l,int r)
{
    tr[now].l=l;
    tr[now].r=r;
    tr[now].val=0;
    if(l!=r)
    {
        int mid=(tr[now].l+tr[now].r)>>1;
        build(now<<1,l,mid);
        build(now<<1|1,mid+1,r);
    }
}
void change(int now,int pos,int val)
{
    if(tr[now].l==tr[now].r)
    {
        tr[now].val=val;
        return;
    }
    int mid=(tr[now].l+tr[now].r)>>1;
    if(pos<=mid)
        change(now<<1,pos,val);
    else change(now<<1|1,pos,val);
    push_up(now);
}
int query(int now,int l,int r)
{
    if(l<=tr[now].l&&tr[now].r<=r)
        return tr[now].val;
    int mid=(tr[now].l+tr[now].r)>>1;
    if(r<=mid)
        return query(now<<1,l,r);
    else if(mid<l)
        return query(now<<1|1,l,r);
    else return query(now<<1,l,r)+query(now<<1|1,l,r);
}
std::map<int,int>y_mp;
int y_mp_cnt;
std::set<std::pair<int,int> >cro_ss;
struct straight_point{
    int x,y;
    bool mode;
    bool operator < (const straight_point& rhs)const
    {
        return x<rhs.x;
    }
}sps[200086];
int sps_tot;
struct vertical{
    int x,y1,y2;
    bool operator < (const vertical& rhs)const
    {
        return x<rhs.x;
    }
}vcs[100086];
int vcs_tot;
struct oper{
    int t,x1,y1,x2,y2;
}opers[100086];
long long res;
int third[100086],t_tot;
int main()
{
    //freopen("P9478_1.in","r",stdin);
    //freopen("P9478.out","w",stdout);
    scanf("%d%d%d%d",&c,&n,&m,&q);
    for(int i=1;i<=q;i++)
    {
        scanf("%d%d%d%d%d",
        &opers[i].t,&opers[i].x1,&opers[i].y1,&opers[i].x2,&opers[i].y2);
        if(opers[i].t==3)
            third[++t_tot]=i;
    }
    for(int i=2;i<=t_tot;i++)
        for(int j=1;j<i;j++)
            if(opers[third[i]].t==3&&opers[third[j]].t==3
             &&opers[third[i]].y2-opers[third[i]].x2
               ==opers[third[j]].y2-opers[third[j]].x2)
            {
                int l1=opers[third[i]].x1,r1=opers[third[i]].x2;
                int l2=opers[third[j]].x1,r2=opers[third[j]].x2;
                if(l1<=r2||l2<=r1)
                    opers[third[j]].x1=std::min(l1,l2),opers[third[j]].x2=std::max(r1,r2),
                    opers[third[j]].y1=std::min(opers[third[j]].y1,opers[third[i]].y1),
                    opers[third[j]].y2=std::max(opers[third[j]].y2,opers[third[i]].y2),
                    opers[third[i]].t=0;
                break;
            }
    for(int i=1;i<=q;i++)
        if(opers[i].t==1)
        {
            sps[++sps_tot]=straight_point{opers[i].x1,opers[i].y1,false};
            sps[++sps_tot]=straight_point{opers[i].x2+1,opers[i].y2,true};
            y_mp[opers[i].y1]=0;
            res+=opers[i].x2-opers[i].x1+1;
        }
        else if(opers[i].t==2)
        {
            vcs[++vcs_tot]=vertical{opers[i].x1,opers[i].y1,opers[i].y2};
            y_mp[opers[i].y1]=0;
            y_mp[opers[i].y2]=0;
            res+=opers[i].y2-opers[i].y1+1;
        }
        else if(opers[i].t==3)
        {
            int b=opers[i].y2-opers[i].x2;
            for(int j=1;j<=q;j++)
                if(opers[j].t==1)
                {
                    int x=opers[j].y1-b;
                    if(x>=opers[j].x1&&x<=opers[j].x2)
                        cro_ss.insert(std::make_pair(x,opers[j].y1));
                }
                else if(opers[j].t==2)
                {
                    int y=opers[j].x1+b;
                    if(y>=opers[j].y1&&y<=opers[j].y2)
                        cro_ss.insert(std::make_pair(opers[j].x1,y));
                }
            res+=opers[i].x2-opers[i].x1+1;
        }
    res-=cro_ss.size();
    for(std::map<int,int>::iterator iter=y_mp.begin();iter!=y_mp.end();iter++)
        iter->second=++y_mp_cnt;
    build(1,1,y_mp_cnt);
    std::sort(sps+1,sps+1+sps_tot);
    std::sort(vcs+1,vcs+1+vcs_tot);
    for(int i=1,j=1;i<=vcs_tot;i++)
    {
        while(j<=sps_tot&&sps[j].x<=vcs[i].x)
        {
            if(!sps[j].mode)
                change(1,y_mp[sps[j].y],1);
            else change(1,y_mp[sps[j].y],0);
            j++;
        }
        res-=query(1,y_mp[vcs[i].y1],y_mp[vcs[i].y2]);
    }
    printf("%lld",res);
    return 0;
}
2024/4/13 16:57
加载中...