luogu P5471 [NOI2019] Bounce

luogu

Since a dot is connected to the rectangular edge region, can beTwo-dimensional data structure optimization even side, But will MLE. Rectangle on the maintenance of the data structure also \ (KD-Tree \) , so consider \ (KDT \) optimization even edge, the space complexity \ (m \ n-sqrt \) , not by

Further, a plurality of sides have a title (KDT \) \ point connected on the side, then the subtree of these points is updated at this point. Consider \ (Dijkstra \) process, each time out \ (DIS \ ) the smallest point, and update other point, and it can be found if other sites are currently the smallest \ (dis_x + w_i \) updated to no longer be updated, so we can every time out minimum \ (dis_x + w_i \ ) , then violence update \ (x \) corresponding to at some point as well as its sub-tree, and then they are deleted, so the point spread and the total number to be deleted are \ (O (n) \) times, it can be done space \ (O (n-) \) , time \ (O (mlogn + m \ sqrt n) \)

#include<bits/stdc++.h>
#define LL long long
#define uLL unsigned long long
#define db double

using namespace std;
const int N=70000+10,M=150000+10,inf=2109876543;
int rd()
{
    int x=0,w=1;char ch=0;
    while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    return x*w;
}
int n,m,di[N],ii=0,e[M][5];
bool cmp(int aa,int bb){return e[aa][0]<e[bb][0];}
vector<int> ee[N];
vector<int>::iterator it[N];
struct node
{
    int x[2],i;
    bool operator < (const node &bb) const {return x[ii]!=bb.x[ii]?x[ii]<bb.x[ii]:x[ii^1]<bb.x[ii^1];} 
}a[N],b[N];
int lx[N],rx[N],ly[N],ry[N],fa[N],sz[N],ch[N][2],rt;
bool ban[N];
int bui(int l,int r)
{
    if(l>r) return 0;
    int mid=(l+r)>>1;
    nth_element(b+l,b+mid,b+r+1);
    int x=b[mid].i,ndd=ii^1;
    sz[x]=1;
    ii=ndd,ch[x][0]=bui(l,mid-1),fa[ch[x][0]]=x,sz[x]+=sz[ch[x][0]];
    ii=ndd,ch[x][1]=bui(mid+1,r),fa[ch[x][1]]=x,sz[x]+=sz[ch[x][1]];
    lx[x]=min(a[x].x[0],min(lx[ch[x][0]],lx[ch[x][1]]));
    rx[x]=max(a[x].x[0],max(rx[ch[x][0]],rx[ch[x][1]]));
    ly[x]=min(a[x].x[1],min(ly[ch[x][0]],ly[ch[x][1]]));
    ry[x]=max(a[x].x[1],max(ry[ch[x][0]],ry[ch[x][1]]));
    return x;
}
struct dj
{
    int x,d;
    bool operator < (const dj &bb) const {return d>bb.d;}
};
priority_queue<dj> q2;
void updd(int x,int i,int ndi)
{
    if(!sz[x]||rx[x]<e[i][1]||lx[x]>e[i][2]||ry[x]<e[i][3]||ly[x]>e[i][4]) return;
    if(lx[x]>=e[i][1]&&rx[x]<=e[i][2]&&ly[x]>=e[i][3]&&ry[x]<=e[i][4]&&di[x]>ndi)
    {
        di[x]=ndi;
        ban[x]=1;
        int xx=x;
        while(xx) --sz[xx],xx=fa[xx];
        if(it[x]!=ee[x].end()) q2.push((dj){x,di[x]+e[*it[x]][0]});
    }
    else if(a[x].x[0]>=e[i][1]&&a[x].x[0]<=e[i][2]&&a[x].x[1]>=e[i][3]&&a[x].x[1]<=e[i][4]&&di[x]>ndi)
    {
        di[x]=ndi;
        ban[x]=1;
        int xx=x;
        while(xx) --sz[xx],xx=fa[xx];
        if(it[x]!=ee[x].end()) q2.push((dj){x,di[x]+e[*it[x]][0]});
    }
    updd(ch[x][0],i,ndi);
    updd(ch[x][1],i,ndi);
}

int main()
{
    n=rd(),m=rd(),rd(),rd();
    for(int i=1;i<=n;++i)
    {
        a[i].x[0]=rd(),a[i].x[1]=rd(),a[i].i=i;
        b[i]=a[i],di[i]=inf;
    }
    lx[0]=ly[0]=inf,rx[0]=ry[0]=-1;
    rt=bui(2,n);
    for(int i=1;i<=m;++i)
    {
        ee[rd()].push_back(i);
        for(int j=0;j<=4;++j)
            e[i][j]=rd();
    }
    for(int i=1;i<=n;++i)
        sort(ee[i].begin(),ee[i].end(),cmp),it[i]=ee[i].begin();
    q2.push((dj){1,(di[1]=0)+e[*it[1]][0]});
    while(!q2.empty())
    {
        int x=q2.top().x;
        q2.pop();
        updd(rt,*it[x],di[x]+e[*it[x]][0]);
        if((++it[x])!=ee[x].end()) q2.push((dj){x,di[x]+e[*it[x]][0]});
    }
    for(int i=2;i<=n;++i) printf("%d\n",di[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/smyjr/p/11621038.html