1568: [JSOI2008]Blue Mary开公司

本文介绍了一种高效的数据结构——李超线段树,并通过一个具体的编程问题展示了其使用方法。该问题涉及射线插入及查询最大纵坐标,通过在根节点进行插入并采用永久化标记策略来解决。文章提供了完整的代码实现。

题目链接

题目大意:插入多条射线(起点横坐标为1),以及询问某个x值能截到的最大纵坐标

题解:李超线段树,在根插入,标记永久化,即标记不下推,每个点标记唯一

Blog1

Blog2

丢模板跑……

我的收获:李超线段树T1 get

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

#define M 50010

#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
#define root 1,n,1

int n,m;

struct Line{
    double k,b;int id;
    double getf(int x){return k*x+b;}
};

bool cmp(Line A,Line B,int x){//比较直线A,B在x的值,如果A<B返回1
    if(!A.id) return 1;
    return A.getf(x)!=B.getf(x)?A.getf(x)<B.getf(x):A.id<B.id;
}

Line t[M<<2];

void add(Line w,int l,int r,int x)
{
    if(!t[x].id) t[x]=w;
    if(cmp(t[x],w,l)) swap(t[x],w);//调整 
    if(l==r||t[x].k==w.k) return ;
    int m=(l+r)>>1;double X=(t[x].b-w.b)/(w.k-t[x].k);//求交点,X为交点横坐标,判断下放区间 
    if(X<l||X>r) return;
    if(X<=m) add(t[x],lson),t[x]=w;//判断 
    else add(w,rson);
}

void insert(int L,int R,Line w,int l,int r,int x)
{
    if(L<=l&&R>=r){add(w,l,r,x);return ;}
    int m=(l+r)>>1;
    if(L<=m) insert(L,R,w,lson);
    if(R>m) insert(L,R,w,rson);
} 

Line query(int w,int l,int r,int x)
{
    if(l==r) return t[x];
    int m=(l+r)>>1;Line tmp;
    if(w<=m) tmp=query(w,lson);
    else tmp=query(w,rson);
    return cmp(t[x],tmp,w)?tmp:t[x];
}

void work()
{
    char opt[9];int x;double S,P;
    while(m--)
    {    
        scanf("%s",opt);
        if(opt[0]=='P') {
            scanf("%lf%lf",&S,&P);Line tmp={P,S-P,1}; 
            insert(1,n,tmp,root);//在根插入k=P,b=S-P的直线 
        }
        else scanf("%d",&x),printf("%lld\n",(long long)floor((query(x,root).getf(x)/100)));
    }
}

void init(){
    cin>>m;n=50000;   
}

int main()
{
    init(); 
    work();
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值