Description
Bytetown城市要进行市长竞选,所有的选民可以畅所欲言地对竞选市长的候选人发表言论。为了统一管理,城市委 员
会为选民准备了一个张贴海报的electoral墙。张贴规则如下:
1.electoral墙是一个长度为N个单位的长方形,每个单位记为一个格子;
2.所有张贴的海报的高度必须与electoral墙的高度一致的; 3.每张海报以“A B”表示,即从第A个格子到第B个格子张贴海报;
4.后贴的海报可以覆盖前面已贴的海报或部分海报。 现在请你判断,张贴完所有海报后,在electoral墙上还可以看见多少张海报。
Input
第一行: N M 分别表示electoral墙的长度和海报个数 接下来M行: Ai Bi 表示每张海报张贴的位置
Output
输出贴完所有海报后,在electoral墙上还可以看见的海报数。 1 0<= N <= 10000000 1<=M<=1000 1<= Ai
<= Bi <=10000000 所有的数据都是整数。数据之间有一个空格
Sample Input
100 5
1 4
2 6
8 10
3 4
7 10
Sample Output
4
题解
离散化一下直接线段树??
注意
5 7
1 5
7 8
这个数据,你会发现离散出来是2 3 1 2 3 4
然后就GG了
于是一个超过1的区间离散的时候中间要多一个格子
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
struct node
{
int lc,rc,l,r,c;
int lazy;
}tr[21000];int trlen;
void bt(int l,int r)
{
int now=++trlen;
tr[now].l=l;tr[now].r=r;
tr[now].lc=tr[now].rc=-1;tr[now].c=tr[now].lazy=0;
if(l<r)
{
int mid=(l+r)/2;
tr[now].lc=trlen+1;bt(l,mid);
tr[now].rc=trlen+1;bt(mid+1,r);
}
}
void upd(int now)
{
int lc=tr[now].lc,rc=tr[now].rc;
tr[lc].c=tr[rc].c=tr[lc].lazy=tr[rc].lazy=tr[now].c;
tr[now].lazy=0;
}
void change(int now,int l,int r,int c)
{
if(tr[now].l==l && tr[now].r==r)
{
tr[now].c=tr[now].lazy=c;
return ;
}
int lc=tr[now].lc,rc=tr[now].rc;
int mid=(tr[now].l+tr[now].r)/2;
if(tr[now].lazy)upd(now);
if(r<=mid)change(lc,l,r,c);
else if(mid+1<=l)change(rc,l,r,c);
else change(lc,l,mid,c),change(rc,mid+1,r,c);
if(tr[lc].c==tr[rc].c && tr[lc].c!=-1)tr[now].c=tr[lc].c;
else tr[now].c=-1;
}
bool v[1100];
void fd(int now)
{
if(tr[now].c!=-1)
{
v[tr[now].c]=true;
return ;
}
if(tr[now].lazy)upd(now);
int lc=tr[now].lc,rc=tr[now].rc;
if(lc)fd(lc);
if(rc)fd(rc);
}
int n,m;
struct ask{int l,r,c;bool bk;ask(){bk=false;}}A[1100];
struct LSnode{int y,p,op;}w[2100];int tot;
bool cmp(LSnode n1,LSnode n2){return n1.y<n2.y;}
int main()
{
scanf("%d%d",&n,&m);
tot=0;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&A[i].l,&A[i].r);A[i].c=i;
if(A[i].l>n){A[i].bk=true;}
if(A[i].r>n)A[i].r=n;
tot++;w[tot].y=A[i].l,w[tot].p=i;w[tot].op=1;
tot++;w[tot].y=A[i].r,w[tot].p=i;w[tot].op=2;
}
sort(w+1,w+1+tot,cmp);
int tt=0,mmax=0;
for(int i=1;i<=tot;i++)
{
if(w[i].y!=w[i-1].y)tt++;
if(w[i].y!=w[i-1].y+1 && w[i].y!=w[i-1].y)tt++;
if(w[i].op==1)A[w[i].p].l=tt;
else A[w[i].p].r=tt;
}
bt(1,tt);
for(int i=1;i<=m;i++)if(A[i].bk==false)change(1,A[i].l,A[i].r,A[i].c);
memset(v,false,sizeof(v));
fd(1);
int ans=0;
for(int i=1;i<=m;i++)if(v[i])ans++;
printf("%d\n",ans);
return 0;
}