bzoj4311 向量(线段树分治+凸包)

45 篇文章 1 订阅
42 篇文章 3 订阅

题目链接

分析:
我们按照时间种一棵线段树
每一个向量出现在一段时间里,我们就可以区间添加这个向量
在每个线段树的每个结点中用当前有的向量维护一个上凸壳

为什么要维护上凸壳呢?
题目要求最大点积
我们知道,点积的公式: x1x2+y1y2 x 1 x 2 + y 1 y 2
已知 x1,y1 x 1 , y 1 ,上式就可以转化成一个类似线性规划的东西
显然最优解在凸包上

在添加点的之前,我们按照x坐标排序

这样插入可以方便我们对于凸包的维护:
因为插入的时候x坐标升序,在维护的时候只用插入这一个结点即可
不用再排序,重新维护一次上凸壳(想一下朴素的凸包求法)

因为这棵线段树维护的东西比较奇怪
push会比较难受,所以我们可以标记永久化
查询的时候不要忘了一路维护最大值

因为是单点查询+标记永久化,所以不需要update

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#define ll long long

using namespace std;

const int N=2000010;
int n,m,cnt;
struct point{
    ll x,y;
    point(ll xx=0,ll yy=0)
    {
        x=xx; y=yy;
    }
};
point ch[N];
struct po{
    int s,t;
    point pos;
    bool operator <(const po &a) const
    {
        return pos.x<a.pos.x;
    }
};
po a[N],ques[N];
vector<point> t[N<<2];
ll ans=0;

point operator -(const point &a,const point &b) {return point(a.x-b.x,a.y-b.y);}
ll Cross(point A,point B) {return A.x*B.y-A.y*B.x;}
ll Dot(point A,point B) {return A.x*B.x+A.y*B.y;}

void TuB(int x,point p)
{
    int i=t[x].size()-1;
    while (i>0&&Cross(p-t[x][i-1],t[x][i]-t[x][i-1])<=0) i--,t[x].pop_back();
    t[x].push_back(p);
}

void insert(int bh,int l,int r,int x,int y,point p)
{
    if (l>=x&&r<=y)
    {
        TuB(bh,p);               //维护凸包 
        return; 
    } 
    int mid=(l+r)>>1;
    if (x<=mid) insert(bh<<1,l,mid,x,y,p);
    if (y>mid) insert(bh<<1|1,mid+1,r,x,y,p);
}

void getmin(int x,point p)       //三分 
{
    int L=0,R=t[x].size()-1;
    while (L<=R)
    {
        if (R-L<=2)
        {
            if (R-L==0) ans=max(ans,Dot(p,t[x][L]));
            if (R-L==1) ans=max(ans,max(Dot(p,t[x][L]),Dot(p,t[x][R])));
            if (R-L==2) ans=max(ans,max(Dot(p,t[x][L]),
                            max(Dot(p,t[x][L+1]),Dot(p,t[x][R]))));
            break;
        }
        int m1=L+(R-L)/3;
        int m2=R-(R-L)/3;
        if (Dot(t[x][m1],p)>Dot(t[x][m2],p))
            R=m2;
        else L=m1;
    }
}

void ask(int bh,int l,int r,int x,point p)
{
    getmin(bh,p);
    if (l==r) return;
    int mid=(l+r)>>1;
    if (x<=mid) ask(bh<<1l,l,mid,x,p);
    else ask(bh<<1|1,mid+1,r,x,p);
}

int main()
{
    scanf("%d",&n);
    int opt,x,y;
    for (int i=1;i<=n;i++)
    {
        scanf("%d",&opt);
        if (opt==1) {
            scanf("%d%d",&x,&y);
            ++m;
            a[m].pos=point((ll)x,(ll)y); 
            a[m].s=i;                     //起始时间 
        } else if (opt==2) {
            scanf("%d",&x);
            a[x].t=i;                     //终止时间 
        } else {
            scanf("%d%d",&x,&y);
            ques[++cnt].s=i;     
            ques[cnt].pos=point((ll)x,(ll)y);
        }
    }
    sort(a+1,a+1+m);                      //按照x排序 
    for (int i=1;i<=m;i++) {
        if (!a[i].t) a[i].t=n;
        insert(1,1,n,a[i].s,a[i].t,a[i].pos);
    }
    for (int i=1;i<=cnt;i++)
    {
        ans=0;
        ask(1,1,n,ques[i].s,ques[i].pos);
        printf("%lld\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值