POJ 2528 Mayor's posters

题目分析

贴海报就看成染色,后面染上的颜色会覆盖之前的颜色。于是就转化成了线段树区间更新,大家可以看到题目给出的范围为10000000,但是只有10000个点,所以需要进行离散化即可,离散化最初用的map,直接tle了,于是看别人的博客改成二分查找了。

#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10005;
#define mid (L+R)/2
#define lson o<<1, L, mid
#define rson o<<1|1, mid+1, R
vector <int> vec;
struct Node
{
    int left,right;
}node[maxn];

int color[maxn<<3];
int vis[maxn<<1];

void pushdown(int o)
{
    if(color[o])
    {
        color[o<<1] = color[o];
        color[o<<1|1] = color[o];
        color[o] = 0;
    }
}

void update(int o,int L,int R,int l,int r,int cnt) //更新
{
    if(l <= L && R <= r)
    {
        color[o] = cnt;
        return ;
    }
    pushdown(o);
    if(l <= mid) update(lson,l,r,cnt);
    if(r > mid) update(rson,l,r,cnt);
}

int query(int o,int L,int R)
{
    if(L == R)    //这一段有颜色
    {
        if(vis[color[o]])
            return 0;
        else        //如果没有出现过这种颜色,也就是出现了新的板报
        {
            vis[color[o]] = 1;
            return 1;
        }
    }
    pushdown(o);
    int ret = 0;
    ret += query(lson);
    ret += query(rson);
    return ret;
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n;
        scanf("%d", &n);
        vec.clear();
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d", &node[i].left, &node[i].right);
            vec.push_back(node[i].left);
            vec.push_back(node[i].right);
        }
        sort(vec.begin(), vec.end());
        vec.erase(unique(vec.begin(),vec.end()), vec.end());  //删除重复值
        int len = vec.size();
        memset(color, 0, sizeof(color));  
        for(int i = 0; i < n; i++)
        {
            int l = lower_bound(vec.begin(),vec.end(),node[i].left) - vec.begin() + 1;
            int r = lower_bound(vec.begin(),vec.end(),node[i].right) - vec.begin() + 1;
            update(1, 1, len, l, r, i+1);
        }
        memset(vis, 0 ,sizeof(vis));
        printf("%d\n", query(1,1,len));
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值