题目:click
题意:贴海报每次都有一段区间值,可以覆盖掉,问最后能看到多少张海报,只要露出一点就算看到。
由于题目的数值很大,首先对数值进行离散化处理,从小到大重新编排。最后用线段树区间更新lazy的来源值,最后查询存在的个数。
也可以逆向操作,从后往前,如果这段区间被标记了那就不放入,也是一种思路。
#include<cmath>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<istream>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<queue>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3f
#define MAX_len 100005*4
using namespace std;
typedef long long ll;
#define m ((l+r)>>1)
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
int n;
struct A {
int l,r;
int lazy;
void init()
{
lazy=-1;
}
}tree[MAX_len];
int ans;
int x[10010],y[10005];
int sum[20100];
int vis[20010];
void build_tree(int p,int l1,int r1)
{
tree[p].l=l1,tree[p].r=r1;
tree[p].init();
if(l1==r1)
return ;
int mid=(l1+r1)>>1;
build_tree((p<<1),l1,mid);
build_tree((p*2+1),mid+1,r1);
}
void PushDown(int p)
{
if(tree[p].lazy!=-1)
{
tree[p*2].lazy=tree[p].lazy;
tree[p*2+1].lazy=tree[p].lazy;
tree[p].lazy=-1;
}
}
void update(int p,int l1,int r1,int val)
{
if(tree[p].l>=l1&&tree[p].r<=r1)
{
tree[p].lazy=val;
return ;
}
PushDown(p);
int mid=(tree[p].l+tree[p].r)/2;
if(l1<=mid)
{
update(p*2,l1,r1,val);
}
if(r1>mid)
{
update(p*2+1,l1,r1,val);
}
}
/*ll query(int p,int l1,int r1)
{
if(l1<=tree[p].l&&r1>=tree[p].r)
{
return tree[p].w;
}
PushDown(p);
int mid=(tree[p].l+tree[p].r)/2;
ll res=0;
if(l1<=mid)
{
res+=query(p*2,l1,r1);
}
if(r1>mid)
{
res+=query(p*2+1,l1,r1);
}
return res;
}*/
void query(int p)
{
if(tree[p].lazy!=-1&&vis[tree[p].lazy]==0)
{
ans++;
vis[tree[p].lazy]=1;
return ;
}
if(tree[p].l==tree[p].r)
return ;
PushDown(p);
query(p*2);
query(p*2+1);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
map<int,int>hh;
memset(sum,0,sizeof(sum));
memset(vis,0,sizeof(vis));
int len=0;
int n,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d %d",&x[i],&y[i]);
if(hh[x[i]]==0)
{
sum[len++]=x[i];
hh[x[i]]=1;
}
if(hh[y[i]]==0)
{
sum[len++]=y[i];
hh[y[i]]=1;
}
}
sort(sum,sum+len);
for(i=1;i<=n;i++)
{
x[i]=lower_bound(sum,sum+len,x[i])-sum+1;
y[i]=lower_bound(sum,sum+len,y[i])-sum+1;
}
build_tree(1,1,len);
for(i=1;i<=n;i++)
{
update(1,x[i],y[i],i);
}
ans=0;
query(1);
printf("%d\n",ans);
}
return 0;
}