bzoj5168&5029: [HAOI2014]贴海报(线段树)

14 篇文章 0 订阅

题目传送门
5029一样

解法:
离散化。
覆盖段。

代码实现:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
struct node {int l,r,lc,rc,c,lazy;}tr[1100000];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 update(int now) {
    int lc=tr[now].lc,rc=tr[now].rc;if(lc==-1)return ;
    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 k) {
    if(tr[now].lazy!=0)update(now);
    if(tr[now].l==l&&tr[now].r==r){tr[now].c=tr[now].lazy=k;return ;}
    int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;
    if(r<=mid)change(lc,l,r,k);else if(l>mid)change(rc,l,r,k);
    else {change(lc,l,mid,k);change(rc,mid+1,r,k);}
    if(tr[lc].c==tr[rc].c)tr[now].c=tr[lc].c;else tr[now].c=-1;
}
bool v[110000];
void find_sum(int now) {
    if(tr[now].lazy!=0)update(now);
    if(tr[now].c!=-1) {v[tr[now].c]=true;return ;}
    if(tr[now].lc==-1)return ;
    find_sum(tr[now].lc);find_sum(tr[now].rc);
}
struct edge {int x,id;}a[210000];int s[210000];
bool cmp(edge n1,edge n2) {return n1.x<n2.x;}
int main() {
    int n,m;scanf("%d%d",&n,&m);
    for(int i=1;i<=2*m;i++) {scanf("%d",&a[i].x);a[i].id=i;}
    sort(a+1,a+1+2*m,cmp);int tot=0;a[0].x=-1;
    for(int i=1;i<=2*m;i++) {
        if(a[i].x!=a[i-1].x) {tot++;if(a[i].x!=a[i-1].x+1)tot++;}s[a[i].id]=tot;
    }
    trlen=0;bt(1,tot);
    for(int i=1;i<=m;i++) change(1,s[2*i-1],s[2*i],i);
    memset(v,false,sizeof(v));find_sum(1);int ans=0;
    for(int i=1;i<=m;i++)if(v[i]==true)ans++;printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值