POJ-2528 Mayor's posters (线段树区间更新+离散化)

时间:2023-12-29 22:52:14

题目分析:线段树区间更新+离散化

代码如下:

# include<iostream>
# include<cstdio>
# include<queue>
# include<vector>
# include<list>
# include<map>
# include<set>
# include<cstdlib>
# include<string>
# include<cstring>
# include<algorithm>
using namespace std;
# define LL long long const int N=10000;
const int INF=1<<30;
const double oo=1e20;
const double eps=1e-20; int x[N+5],y[N+5];
vector<int>v;
map<int,int>mp;
int lazy[N*8+5];
int tr[N*8+5];
set<int>s; void pushDown(int o,int l,int r)
{
if(lazy[o]!=-1){
lazy[o<<1]=lazy[o<<1|1]=lazy[o];
tr[o<<1]=tr[o<<1|1]=lazy[o];
lazy[o]=-1;
}
} void build(int o,int l,int r)
{
tr[o]=-1;
lazy[o]=-1;
if(l==r) return ;
int mid=l+(r-l)/2;
build(o<<1,l,mid);
build(o<<1|1,mid+1,r);
} void update(int o,int l,int r,int L,int R,int val)
{
if(L<=l&&r<=R){
tr[o]=lazy[o]=val;
}else{
pushDown(o,l,r);
int mid=l+(r-l)/2;
if(L<=mid) update(o<<1,l,mid,L,R,val);
if(R>mid) update(o<<1|1,mid+1,r,L,R,val);
}
} void query(int o,int l,int r)
{
if(l==r){
if(tr[o]!=-1) s.insert(tr[o]);
}else{
pushDown(o,l,r);
int mid=l+(r-l)/2;
query(o<<1,l,mid);
query(o<<1|1,mid+1,r);
}
} int solve(int m,int n)
{
build(1,1,m);
for(int i=0;i<n;++i){
update(1,1,m,mp[x[i]],mp[y[i]],i);
}
s.clear();
query(1,1,m);
return s.size();
} int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
mp.clear();
v.clear();
scanf("%d",&n);
for(int i=0;i<n;++i){
scanf("%d%d",x+i,y+i);
v.push_back(x[i]);
v.push_back(y[i]);
}
sort(v.begin(),v.end());
int len=unique(v.begin(),v.end())-v.begin();
for(int i=0;i<len;++i)
mp[v[i]]=i+1;
printf("%d\n",solve(len,n));
}
return 0;
}