P1047 校门外的树

菜鸡刷红题系列

抽象出题意:数轴的\([0,L]\)本来都是1,选定一些区域要变成0,求最后这片区域还有多少1。

讲道理这就是区间推平操作啊!

说到区间推平你想到了什么?

Chotholly!

所以我们可以用珂朵莉树非常优雅地做过去。

跑得还挺快的

代码:

#include<cstdio>
#include<set>
const int maxn = 10005;
int n, m;
struct Nodes
{
    int l, r;
    mutable int v;
    Nodes(int l, int r = -1, int v = 0): l(l), r(r), v(v){}
    bool operator < (const Nodes &rhs) const
    {
        return l < rhs.l;
    }
};
std::set<Nodes> chotholly;
#define IT std::set<Nodes>::iterator
IT split(int pos)
{
    IT it = chotholly.lower_bound(Nodes(pos));
    if(it != chotholly.end() && it->l == pos) return it;
    --it;
    int l = it->l, r = it->r, v = it->v;
    chotholly.erase(it);
    chotholly.insert(Nodes(l, pos - 1, v));
    return chotholly.insert(Nodes(pos, r, v)).first;
}
void assign(int l, int r, int x)
{
    IT itl = split(l), itr = split(r + 1);
    chotholly.erase(itl, itr);
    chotholly.insert(Nodes(l, r, x));
}
int sum(int l, int r)
{
    int ans = 0;
    IT itl = split(l), itr = split(r + 1);
    for(; itl != itr; ++itl) ans += (itl->r - itl->l + 1) * itl->v;
    return ans;
}
int main()
{
    scanf("%d%d", &n, &m);
    chotholly.insert(Nodes(0, n, 1));
    while(m--)
    {
        int l, r; scanf("%d%d", &l, &r);
        assign(l, r, 0);
    }
    printf("%d\n", sum(0, n));
    return 0;
}

转载于:https://www.cnblogs.com/Garen-Wang/p/9846536.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值