题目大意:插入多条射线(起点横坐标为1),以及询问某个x值能截到的最大纵坐标
题解:李超线段树,在根插入,标记永久化,即标记不下推,每个点标记唯一
丢模板跑……
我的收获:李超线段树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;
}
本文介绍了一种高效的数据结构——李超线段树,并通过一个具体的编程问题展示了其使用方法。该问题涉及射线插入及查询最大纵坐标,通过在根节点进行插入并采用永久化标记策略来解决。文章提供了完整的代码实现。
370

被折叠的 条评论
为什么被折叠?



