P4097 [HEOI2013]Segment 李超线段树

传送门

文章目录

题意:

实现以下两个操作:
( 1 ) (1) (1)在平面上加入一条线段。记第 i i i条被插入的线段的标号为 i i i
( 2 ) (2) (2)给定一个数 k k k,询问与直线 x = k x=k x=k相交的线段中,交点纵坐标最大的线段的编号。
n ≤ 1 e 5 , k , x 0 , x 1 ≤ 39989 , 1 ≤ y 0 , y 1 ≤ 1 e 9 n\le 1e5,k,x_0,x_1\le39989,1\le y_0,y_1 \le 1e9 n1e5,k,x0,x139989,1y0,y11e9

思路:

复习一下李超线段树。
李超树是用来维护一次函数,将线段划分为若干个段,即线段树的每一个段都维护一个最优线,他在当前段的 m i d mid mid处的位置是最大值,我注意李超树线段不能向上 p u s h u p pushup pushup。当然我们如果要查询区间最小值或者最大值怎么办呢?我们总不能从 [ x 1 , x 2 ] [x_1,x_2] [x1,x2]跑一遍,每个点都查询一遍吧?这样显然是不科学的,我们可以发现一次函数是个单调函数,我们在插入的时候顺便维护以下最小值就好了,最小值一定在当前区间的端点处。

下面我们考虑这个题,这个题不是维护单点最大值是多少,而是问最大值的线段编号,我们只需要再加一个变量维护以下编号即可,注意修改一下查询函数以及判断当前区间是否存在最优段。

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=1000010,mod=1e9,INF=0x3f3f3f3f;
const double eps=1e-6;

int n,m;
struct Node {
	int l,r,flag,id; 
	double k,b;
	double calc(const int pos) const {
		return k*pos+b;
	}
	int cross(const Node &x) const {
		return floor((b-x.b)/(x.k-k));
	}
}tr[N<<2];
struct Query {
	int id;
	double val;
};

void build(int u,int l,int r) {
	tr[u]={l,r,0,0,0,0};
	if(l==r) return;
	int mid=(l+r)>>1;
	build(L,l,mid); build(R,mid+1,r);
}

void modify(int u,int l,int r,Node k) {
	if(k.l<=l&&k.r>=r) {
		if(!tr[u].flag) { tr[u]=k; }
		else if(k.calc(l)-tr[u].calc(l)>eps&&k.calc(r)-tr[u].calc(r)>eps) tr[u]=k;   
		else if(k.calc(l)-tr[u].calc(l)>eps||k.calc(r)-tr[u].calc(r)>eps) {
			int mid=(l+r)>>1;
			if(k.calc(mid)-tr[u].calc(mid)>eps) swap(tr[u],k);
			else if(fabs(k.calc(mid)-tr[u].calc(mid))<eps) {
				if(tr[u].id>k.id) swap(tr[u],k);
			}
			if(k.cross(tr[u])-mid<-eps) modify(L,l,mid,k);
			else modify(R,mid+1,r,k);
		}
	}
	else {
		int mid=(l+r)>>1;
		if(k.l<=mid) modify(L,l,mid,k);
		if(k.r>mid) modify(R,mid+1,r,k);
	}
}


Query query(int u,int l,int r,int x,int flag) {
	//if(!tr[u].flag) return {0,0}; 
	if(l==r) return {tr[u].id,tr[u].calc(x)};
	int mid=(l+r)>>1;
	double ans=tr[u].calc(x);
	int id=tr[u].id;
	if(x<=mid) {
		Query cmp=query(L,l,mid,x,flag);
		if(cmp.id==0) return {id,ans};
		if(ans>cmp.val) return {id,ans};
		else if(ans==cmp.val) return {min(id,cmp.id),ans};
		else return cmp;  
	}
	else {
		Query cmp=query(R,mid+1,r,x,flag);
		if(cmp.id==0) return {id,ans};
		if(ans>cmp.val) return {id,ans};
		else if(ans==cmp.val) return {min(id,cmp.id),ans};
		else return cmp;  
	}
}


/*
double query(int u,int l,int r,int x) {
	//if(l==1&&r==1) printf("%.2f %.2f\n",tr[u].k,tr[u].b);
	if(l==r) return tr[u].calc(x);
	int mid=(l+r)>>1;
	double ans=tr[u].calc(x);
	if(x<=mid) return max(ans,query(L,l,mid,x));
	else return max(ans,query(R,mid+1,r,x));
}
*/


int main() {
//	ios::sync_with_stdio(false);
//	cin.tie(0);
	
	scanf("%d",&n);
	build(1,1,50000);
	int ans=0,id=0;
	while(n--) {
		int op; scanf("%d",&op);
		if(op==0) {
			int x; scanf("%d",&x);
			x=(x+ans-1)%39989+1;
			Query now=query(1,1,50000,x,1);
			printf("%d\n",ans=now.id);
		}
		else {
			id++;
			int x1,x2,y1,y2;
			scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
			x1=(x1+ans-1)%39989+1; x2=(x2+ans-1)%39989+1;
			y1=(y1+ans-1)%mod+1;   y2=(y2+ans-1)%mod+1;
			if(x1>x2) swap(x1,x2),swap(y1,y2);
			if(x1==x2) modify(1,1,50000,{x1,x2,1,id,0,(double)max(y1,y2)});
			else {
				double k=double(y1-y2)/(x1-x2);
				double b=y1-k*x1;
				modify(1,1,50000,{x1,x2,1,id,k,b});
			}
		}
	}
	



	return 0;
}
/*

*/




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值