LOJ#6034. 「雅礼集训 2017 Day2」线段游戏【李超线段树】

题目描述:

在这里插入图片描述

题目分析:

在这里插入图片描述
在这里插入图片描述

Code:

#include<bits/stdc++.h>
#define maxn 200005
#define x0 x_0
#define x1 x_1
#define y0 y_0
#define y1 y_1
using namespace std;
const int N = 1e5;
const double inf = 1e9;
int n,m;
struct node{
    int x0,y0,x1,y1;double k;
    node input(){
        scanf("%d%d%d%d",&x0,&y0,&x1,&y1),k=x0==x1?inf:(y1-y0)*1.0/(x1-x0);
        if(x0>x1) swap(x0,x1),swap(y0,y1);
        return *this;
    }
    double calc(int x){return k==inf?max(y0,y1):y0+k*(x-x0);}
    bool empty(){return !x0&&!x1&&!k;}
}t[maxn<<2];
void solve(int i,int l,int r,node p){
    if(t[i].empty()) {t[i]=p;return;}
    int mid=(l+r)>>1;
    if(t[i].calc(mid)<p.calc(mid)) swap(t[i],p);
    if(l==r) return;
    if(t[i].k>p.k) solve(i<<1,l,mid,p);
    else solve(i<<1|1,mid+1,r,p);
}
void insert(int i,int l,int r,node p){
    if(p.x1<l||r<p.x0) return;
    if(p.x0<=l&&r<=p.x1) {solve(i,l,r,p);return;}
    int mid=(l+r)>>1;
    insert(i<<1,l,mid,p),insert(i<<1|1,mid+1,r,p);
}
double query(int i,int l,int r,int x){
    double ret=t[i].empty()?-inf:t[i].calc(x);
    if(l==r) return ret;
    int mid=(l+r)>>1;
    if(x<=mid) return max(ret,query(i<<1,l,mid,x));
    else return max(ret,query(i<<1|1,mid+1,r,x));
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) insert(1,0,N,node().input());
    int op,x;double ans;
    while(m--){
        scanf("%d",&op);
        if(!op) insert(1,0,N,node().input());
        else scanf("%d",&x),printf("%.3f\n",(ans=query(1,0,N,x))>-inf?ans:0);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值