码迷,mamicode.com
首页 > 其他好文 > 详细

P1047 校门外的树

时间:2018-10-25 00:23:05      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:include   printf   iterator   cpp   ret   iter   end   erase   max   

菜鸡刷红题系列

抽象出题意:数轴的\([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;
}

P1047 校门外的树

标签:include   printf   iterator   cpp   ret   iter   end   erase   max   

原文地址:https://www.cnblogs.com/Garen-Wang/p/9846536.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!