POJ 2528 - mayor's posts (line tree + discretization)

Title Link https://cn.vjudge.net/problem/POJ-2528

[title]
If a city wants to run for mayor, the candidates can put up posters on a wall to canvass for themselves. Everyone can put up a continuous area. Later, the posters can cover the front. Ask how many posters can be seen in the end. There are NNN candidates (N<=10000) (N<=10000) (n < = 10000), and the poster coverage of each candidate [l,r][l,r][l,r] meets the requirements of 1 & lt; = L & lt; = R & lt; = 1071 & lt; = L & lt; = R & lt; = 10 ^ 71 < = l < = R < = 107

[thinking]
Each poster has left and right two endpoints, no more than 10000000000 in total, so there will be only 20000200000000 different points at most, so the point coordinates can be discretized and then processed by line tree

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define node tree[id]
#define lson tree[id<<1]
#define rson tree[id<<1|1]
using namespace std;

const int maxn=10005;

struct Tree{
	int left,right;
	int val,lazy;
}tree[maxn<<3];

int n;
bool vis[maxn];
int le[maxn],ri[maxn];
vector<int> p;

void pushdown(int id){
	if(node.lazy!=-1 && node.left!=node.right){
		lson.lazy=node.lazy;
		lson.val=node.lazy;
		rson.lazy=node.lazy;
		rson.val=node.val;
		node.lazy=-1;
	}
}

void build(int id,int le,int ri){
	node.left=le;
	node.right=ri;
	node.val=-1;
	node.lazy=-1;
	if(le==ri) return;
	int mid=(le+ri)>>1;
	build(id<<1,le,mid);
	build(id<<1|1,mid+1,ri);
}

void update(int id,int le,int ri,int val){
	if(node.left==le && node.right==ri){
		node.val=val;
		node.lazy=val;
		return;
	}
	pushdown(id);
	int mid=(node.left+node.right)>>1;
	if(ri<=mid) update(id<<1,le,ri,val);
	else if(le>mid) update(id<<1|1,le,ri,val);
	else{
		update(id<<1,le,mid,val);
		update(id<<1|1,mid+1,ri,val);
	}
}

int query(int id,int pos){
	if(node.left==node.right){
		return node.val;
	}
	pushdown(id);
	int mid=(node.left+node.right)>>1;
	if(pos<=mid) return query(id<<1,pos);
	else return query(id<<1|1,pos);
}

int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		p.clear();
		for(int i=0;i<n;++i){
			scanf("%d%d",&le[i],&ri[i]);
			p.push_back(le[i]);
			p.push_back(ri[i]);
		}
		sort(p.begin(),p.end());
		p.erase(unique(p.begin(),p.end()),p.end());
		for(int i=0;i<n;++i){
			le[i]=lower_bound(p.begin(),p.end(),le[i])-p.begin()+1;
			ri[i]=lower_bound(p.begin(),p.end(),ri[i])-p.begin()+1;
		}
		build(1,1,2*n);
		for(int i=0;i<n;++i) update(1,le[i],ri[i],i);
		memset(vis,0,sizeof(vis));
		int ans=0;
		for(int i=1;i<=2*n;++i){
			int x=query(1,i);
			if(-1!=x && !vis[x]){
				++ans;
				vis[x]=true;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
} 

Posted by quick5pnt0 on Sat, 28 Dec 2019 10:46:49 -0800