POJ 2528 Mayor‘s poster 線段樹+離散化

```#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 25010
#define Lson x<<1,l,mid
#define Rson (x<<1|1),mid+1,r

using namespace std;

struct node {
int val;
int num;
} pos[N];
bool flag;
bool d[N*3];
int p[N*3];
bool cmp2(node a,node b)
{
if (a.val<b.val) return true;
return false;
}
bool cmp(node a,node b)
{
if (a.num>b.num) return true;
if (a.num==b.num && a.val<b.val) return true;
return false;
}
void getup(int x)
{
if (d[x<<1]==true || d[x<<1|1]==true)
d[x]=true;
else
d[x]=false;
}
void build(int x,int l,int r)
{
d[x]=true;
p[x]=0;
if (l==r){
return;
}
int mid=(l+r)>>1;
build(Lson);
build(Rson);
getup(x);
}
void pushdown(int x,int l,int r)
{
if (!p[x] || l>=r) return;
d[x<<1]=d[x<<1|1]=false;
p[x<<1]=p[x<<1|1]=p[x];
p[x]=0;
}
void query(int L,int R,int x,int l,int r)
{
if (L<=l && r<=R)
{
if (d[x])
{
flag=true;
d[x]=false;
p[x]=1;
}
return;
}
if (p[x])
pushdown(x,l,r);
int mid=(l+r)>>1;
if (L>mid && d[x<<1|1]) query(L,R,Rson);
else
if (R<=mid && d[x<<1]) query(L,R,Lson);
else
if (L<=mid && R>mid){
if (d[x<<1]) query(L,R,Lson);
if (d[x<<1|1]) query(L,R,Rson);
}
getup(x);
}
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
int n;
int i,j,k;
scanf("%d",&n);
int a,b;
for (i=0;i<n;i++)
{
scanf("%d%d",&pos[i<<1].val,&pos[i<<1|1].val);
pos[i<<1].num=pos[i<<1|1].num=i;
}
sort(pos,pos+2*n,cmp2);
int cur=0,pre=-1;
for (j=0;j<2*n;j++)
{
if (pre!=pos[j].val){
pre=pos[j].val;
pos[j].val=++cur;
}
else{
pos[j].val=cur;
}
//cout<<pre<<" "<<pos[j].val<<endl;
}

build(1,1,cur);

sort(pos,pos+2*n,cmp);
int ans=0;
for (i=0;i<n;i++)
{
//cout<<pos[i<<1].val<<" "<<pos[i<<1|1].val<<endl;
flag=false;
query(pos[i<<1].val,pos[i<<1|1].val,1,1,cur);
if (flag) {ans++;}
}
printf("%d\n",ans);
}
return 0;
}```