zoukankan      html  css  js  c++  java
  • 【洛谷P4097】Segment 李超线段树

    题目大意:维护一个二维平面,给定若干条线段,支持询问任意整数横坐标处对应的纵坐标最靠上的线段的 id,相同高度取 id 值较小的,强制在线。

    题解:初步学习了李超线段树。李超线段树的核心思想在于通过标记永久化的方式来维护斜率。

    代码如下

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=1e5+10;
    const double eps=1e-6;
    
    inline int read(){
    	int x=0,f=1;char ch;
    	do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch));
    	do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
    	return f*x;
    }
    
    int n,lastans,cnt;
    struct line{double k,b;int id;};
    struct node{
    	#define ls(x) t[x].lc
    	#define rs(x) t[x].rc
    	int lc,rc;bool tag;line s;
    }t[maxn];
    int tot,root;
    void pushdown(int o,int l,int r,line now){
    	if(!t[o].tag){t[o].s=now,t[o].tag=1;return;}
    	double l1=now.k*l+now.b,r1=now.k*r+now.b;
    	double l2=t[o].s.k*l+t[o].s.b,r2=t[o].s.k*r+t[o].s.b;
    	if(l2>=l1&&r2>=r1)return;
    	else if(l1>=l2&&r1>=r2){t[o].s=now;return;}
    	else{
    		double pos=(now.b-t[o].s.b)/(t[o].s.k-now.k);
    		int mid=l+r>>1;
    		if(pos<=mid)pushdown(ls(o),l,mid,r1>r2?t[o].s:now);
    		else pushdown(rs(o),mid+1,r,r1>r2?now:t[o].s);
    		if((l1>l2&&pos>mid)||(pos<=mid&&r1>r2))t[o].s=now;
    	}
    }
    int build(int l,int r){
    	int x=++tot;
    	if(l==r)return x;
    	int mid=l+r>>1;
    	ls(x)=build(l,mid),rs(x)=build(mid+1,r);
    	return x;
    }
    void modify(int o,int l,int r,int x,int y,line now){
    	if(l==x&&r==y){pushdown(o,l,r,now);return;}
    	int mid=l+r>>1;
    	if(y<=mid)modify(ls(o),l,mid,x,y,now);
    	else if(x>mid)modify(rs(o),mid+1,r,x,y,now);
    	else modify(ls(o),l,mid,x,mid,now),modify(rs(o),mid+1,r,mid+1,y,now);
    }
    line query(int o,int l,int r,int pos){
    	if(l==r)return t[o].tag?t[o].s:line{0,0,0};
    	int mid=l+r>>1;
    	line res=pos<=mid?query(ls(o),l,mid,pos):query(rs(o),mid+1,r,pos);
    	if(!t[o].tag)return res;
    	double v1=t[o].s.k*pos+t[o].s.b,v2=res.k*pos+res.b;
    	if(!res.id||v1>v2||(fabs(v1-v2)<eps&&t[o].s.id<res.id))res=t[o].s;
    	return res;
    }
    
    void solve(){
    	n=read();
    	root=build(1,39989);
    	while(n--){
    		int opt=read();
    		if(opt==0){
    			int pos=(read()+lastans-1)%39989+1;
    			printf("%d
    ",lastans=query(root,1,39989,pos).id);
    		}else{
    			int x0=read(),y0=read(),x1=read(),y1=read();
    			x0 = (x0 + lastans - 1) % 39989 + 1;
                x1 = (x1 + lastans - 1) % 39989 + 1;
                y0 = (y0 + lastans - 1) % (int) (1e9) + 1;
                y1 = (y1 + lastans - 1) % (int) (1e9) + 1;
    			if(x0==x1)modify(root,1,39989,x0,x1,line{0.0,max(y0,y1),++cnt});
    			else{
    				if(x0>x1)swap(x0,x1),swap(y0,y1);
    				double k=(double)(y0-y1)/(x0-x1),b=(double)y0-k*x0;
    				modify(root,1,39989,x0,x1,line{k,b,++cnt});
    			}
    		}
    	}
    }
    
    int main(){
    	solve();
    	return 0;
    }
    
  • 相关阅读:
    家长如何助力孩子适应小学生活
    一年级线上家长会
    gdb常用调试命令
    二叉树-后序遍历
    机器人
    Oracle创建只读账号的详细步骤
    ORACLE RAC日常运维-调整RAC+DG环境redo大小
    Redis 延迟分析
    oracle dataguard 重启步骤
    catalog start with + switch database to copy的妙用
  • 原文地址:https://www.cnblogs.com/wzj-xhjbk/p/10434892.html
Copyright © 2011-2022 走看看