其他分享
首页 > 其他分享> > 【题解】Luogu P4097 [HEOI2013]Segment

【题解】Luogu P4097 [HEOI2013]Segment

作者:互联网

原题传送门

这珂以说是李超线段树的模板题

按着题意写就行了,时间复杂度为\(O(n\log^2n)\)

#include <bits/stdc++.h>
#define N 40005
#define db double
#define getchar nc
using namespace std;
inline char nc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
    register int x=0,f=1;register char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return x*f;
}
inline void write(register int x)
{
    if(!x)putchar('0');if(x<0)x=-x,putchar('-');
    static int sta[20];register int tot=0;
    while(x)sta[tot++]=x%10,x/=10;
    while(tot)putchar(sta[--tot]+48);
}
inline db Max(register db a,register db b)
{
    return a>b?a:b;
}
struct node{
    db k,b;
    int id;
    node(register int ax=0,register int ay=0,register int bx=0,register int by=0,register int ID=0)
    {
        id=ID;
        if(ax==bx)
            k=0,b=Max(ay,by);
        else
            k=(db)(ay-by)/(ax-bx),b=(db)ay-k*ax;    
    }
    inline db getv(register int x)
    {
        return k*x+b;
    }
};
inline bool cmp(register node a,register node b,register int x)
{
    if(!a.id)
        return 1;
    return a.getv(x)!=b.getv(x)?a.getv(x)<b.getv(x):a.id<b.id;
}
node tr[N<<2];
inline void insert(register int x,register int l,register int r,register node v)
{
    if(!tr[x].id)
        tr[x]=v;
    if(cmp(tr[x],v,l))
        swap(tr[x],v);
    if(l==r||tr[x].k==v.k)
        return;
    int mid=l+r>>1;
    db X=(tr[x].b-v.b)/(v.k-tr[x].k);
    if(X<l||X>r)
        return;
    if(X<=mid)
        insert(x<<1,l,mid,tr[x]),tr[x]=v;
    else
        insert(x<<1|1,mid+1,r,v); 
}
inline void Insert(register int x,register int l,register int r,register int L,register int R,register node v)
{
    if(L<=l&&r<=R)
    {
        insert(x,l,r,v);
        return;
    }
    int mid=l+r>>1;
    if(L<=mid)
        Insert(x<<1,l,mid,L,R,v);
    if(R>mid)
        Insert(x<<1|1,mid+1,r,L,R,v);
}
inline node query(register int x,register int l,register int r,register int pos)
{
    if(l==r)
        return tr[x];
    int mid=l+r>>1;
    node tmp;
    if(pos<=mid)
        tmp=query(x<<1,l,mid,pos);
    else
        tmp=query(x<<1|1,mid+1,r,pos);
    return cmp(tr[x],tmp,pos)?tmp:tr[x];
}
int n,m,lans=0,cnt=0;
#define p1 39989
#define p2 1000000000
int main()
{
    m=read(),n=40000;
    while(m--)
    {
        int opt=read();
        if(opt==0)
        {
            int x=read();
            x=(x+lans-1)%p1+1;
            lans=query(1,1,n,x).id;
            write(lans),puts("");
        }
        else
        {
            int ax=read(),ay=read(),bx=read(),by=read();
            ax=(ax+lans-1)%p1+1,bx=(bx+lans-1)%p1+1;
            ay=(ay+lans-1)%p2+1,by=(by+lans-1)%p2+1;
            if(ax>bx)
            {
                ax^=bx^=ax^=bx;
                ay^=by^=ay^=by;
            }
            Insert(1,1,n,ax,bx,node(ax,ay,bx,by,++cnt));
        }
    }
    return 0;
}

标签:node,bx,int,题解,register,HEOI2013,ay,ax,Segment
来源: https://www.cnblogs.com/yzhang-rp-inf/p/10349127.html