+ Scan line segment tree - SOJ # 999 Monument

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_42557561/article/details/102765927

Portal


Analysis

Each rectangle ( x 1 , y 1 , x 2 , y 2 ) (x1,y1,x2,y2) is split into two segments ( x 1 , y 1 , y 2 ) (x1,y1,y2) ( x 2 , y 1 , y 2 ) (x2,y1,y2)
maintains two pointers, the abscissa is incremented by scanning the past
encountered a line segment when the corresponding operation proceeds
Specifically:
the subscript y coordinate considered, maintaining a segment tree
then corresponds to the longest interval is not in the query covered length
due to solving the problem, we need to tag downstream.
Because every time the whole range of inquiry


Code
#include<bits/stdc++.h>
#define in read()
#define re register
using namespace std;
inline int read(){
	char ch;int f=1,res=0;
	while((ch=getchar())<'0'||ch>'9') if(ch=='-') f=-1;
	while(ch>='0'&&ch<='9'){
		res=(res<<1)+(res<<3)+(ch^48);
		ch=getchar();
	}
	return f==1?res:-res;
}
const int N=1000009;
struct Seg{int l,r,ls,rs,mx,lzy;}seg[N<<2];
#define lc (k<<1)
#define rc (k<<1|1)
void build(int k,int l,int r){
	seg[k].l=l;seg[k].r=r;seg[k].ls=seg[k].rs=seg[k].mx=r-l+1;seg[k].lzy=0;
	if(l==r) return;
	int mid=l+r>>1;
	build(lc,l,mid);build(rc,mid+1,r);
}
void pushnow(int k){
	if(seg[k].lzy){seg[k].ls=seg[k].rs=seg[k].mx=0;return;}
	if(seg[k].l==seg[k].r) {seg[k].ls=seg[k].rs=seg[k].mx=1;return;}
	seg[k].ls=(seg[lc].ls==seg[lc].r-seg[lc].l+1)?seg[lc].ls+seg[rc].ls:seg[lc].ls;
	seg[k].rs=(seg[rc].rs==seg[rc].r-seg[rc].l+1)?seg[rc].rs+seg[lc].rs:seg[rc].rs;
	seg[k].mx=max(max(seg[lc].mx,seg[rc].mx),seg[lc].rs+seg[rc].ls);
	return;
}
void modify(int k,int x,int y,int val){
	if(x<=seg[k].l&&seg[k].r<=y){
		seg[k].lzy+=val;
		pushnow(k);
		return ;
	}
	int mid=seg[k].l+seg[k].r>>1;
	if(y<=mid) modify(lc,x,y,val);
	else if(x>mid) modify(rc,x,y,val);
	else modify(lc,x,y,val),modify(rc,x,y,val);
	pushnow(k);
}
int x[2][N],y[2][N];
vector<int> add[N],del[N];
int main(){
	int n,m,p;
	n=in;m=in;p=in;
	for(re int i=1;i<=p;++i){
		x[0][i]=in;y[0][i]=in;x[1][i]=in;y[1][i]=in;
		add[x[0][i]].push_back(i);
		del[x[1][i]].push_back(i);		
	}
	build(1,1,m);
	int ans=0;
	for(int l=1,r=1;r<=n;++r){
		for(re int k=0;k<add[r].size();++k) modify(1,y[0][add[r][k]],y[1][add[r][k]],1);
		ans=max(ans,min(seg[1].mx,r-l+1));
		while(seg[1].mx<r-l+1){
			for(re int k=0;k<del[l].size();++k) modify(1,y[0][del[l][k]],y[1][del[l][k]],-1);
			l++;
		}
	}
	cout<<ans;
	return 0;
} 

Guess you like

Origin blog.csdn.net/weixin_42557561/article/details/102765927