[BZOJ3165][HEOI2013]Segment (line segment tree without marking)

topic:

i am hyperlink

answer:

This line segment is the same as the BZOJ1568 , except that it gives a range, so you need to calculate the delta yourself.
Note that what he gives may be x0=x1 but y0!=y1, at this time, take the max for y0 and y1. . .

Code:

#include <cstdio>
#include <iostream>
using namespace std;
const int sb=1e9;
const int N=50000;
struct hh{double s,p;int id,l;}t[N*4];double maxx=0;int ans;
double Y(int x,double s,double p,int x0){return (x-x0)*p+s;}
void insert(int now,int l,int r,int lrange,int rrange,double s,double p,int id)
{   
    int mid=(l+r)>>1;
    if (lrange<=l && rrange>=r)
    {
        double y1=Y(l,s,p,lrange),y2=Y(r,s,p,lrange);
        double oy1=Y(l,t[now].s,t[now].p,t[now].l),oy2=Y(r,t[now].s,t[now].p,t[now].l);
        if (y1<=oy1 && y2<=oy2) return;
        if (y1>=oy1 && y2>=oy2) {t[now].s=s;t[now].p=p;t[now].id=id;t[now].l=lrange;return;}
        insert(now<<1,l,mid,lrange,rrange,s,p,id);
        insert(now<<1|1,mid+1,r,lrange,rrange,s,p,id);
    }
    else
    {
        if (lrange<=mid) insert(now<<1,l,mid,lrange,rrange,s,p,id);
        if (rrange>mid) insert(now<<1|1,mid+1,r,lrange,rrange,s,p,id);
    }
}
void qurry(int now,int l,int r,int x)
{
    double z=(x-t[now].l)*t[now].p+t[now].s;
    if (maxx<z || (maxx==z && ans>t[now].id)) maxx=z,ans=t[now].id;
    if (l==r) return;
    int mid=(l+r)>>1;
    if (x<=mid) qurry(now<<1,l,mid,x);
    else qurry(now<<1|1,mid+1,r,x);
}
int main()
{
    int n;scanf("%d",&n);ans=0;int bh=0;
    for (int i=1;i<=n;i++)
    {
        int id,x,x0,x1,y0,y1;scanf("%d",&id);
        if (id==1)
        {
            bh++;
            scanf("%d%d%d%d",&x0,&y0,&x1,&y1);
            x0=(x0+ans-1)%39989+1; y0=(y0+ans-1)%sb+1;
            x1=(x1+ans-1)%39989+1; y1=(y1+ans-1)%sb+1;
            if (x0>x1) swap(x0,x1),swap(y0,y1);
            double yx;
            if (x0==x1) y0=max(y0,y1),y1=y0,yx=0;
            else yx=(y1-y0)*1.0/(x1-x0)*1.0;
            insert(1,1,39989,x0,x1,y0,yx,bh);
        }else
        {
            scanf("%d",&x); x=(x+ans-1)%39989+1;
            maxx=0;ans=0;
            qurry(1,1,39989,x);printf("%d\n",ans);
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324705028&siteId=291194637