区间染色线段树40分

P3740 [HAOI2014] 贴海报

BIOS @ 2023-08-02 14:49:01

#include <iostream>
#include <bitset>
#include <algorithm>
#include <set>
#include <unordered_map>
using namespace std;
const int N = 3005, M = 1005;
struct node
{
    int l, r, add;
    bitset<M> color;
} tr[N << 2];
struct ques
{
    int l, r;
} e[M];
int n, m, l, r;
set<int> a;
unordered_map<int, int> ac;
void pushup(int u)
{
    tr[u].color = tr[u << 1].color | tr[u << 1 | 1].color;
}
void pushdown(node &u, node &l, node &r)
{
    if (u.add)
    {
        l.add = u.add, r.add = u.add;
        l.color = (1 << u.add), r.color = (1 << u.add);
        u.add = 0;
    }
}
void pushdown(int u)
{
    pushdown(tr[u], tr[u << 1], tr[u << 1 | 1]);
}
void build(int u, int l, int r)
{
    if (l == r)
        tr[u] = {l, r, 0, 0};
    else
    {
        tr[u].l = l, tr[u].r = r;
        int mid = l + r >> 1;
        build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
        pushup(u);
    }
}
void modify(int u, int l, int r, int k)
{
    if (tr[u].l >= l && tr[u].r <= r)
        tr[u].color = (1 << k), tr[u].add = k;
    else
    {
        pushdown(u);
        int mid = tr[u].r + tr[u].l >> 1;
        if (l <= mid)
            modify(u << 1, l, r, k);
        if (r > mid)
            modify(u << 1 | 1, l, r, k);
        pushup(u);
    }
}
int main()
{
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        cin >> e[i].l >> e[i].r;
        a.insert(e[i].l), a.insert(e[i].r), a.insert(e[i].r + 1);
    }
    int idx = 0;
    for (int t : a)
        ac[t] = ++idx;
    build(1, 1, idx);
    for (int i = 1; i <= m; i++)
        l = ac[e[i].l], r = ac[e[i].r], modify(1, l, r, i);
    cout << tr[1].color.count() << endl;
}

完全按照另一道区间染色题思路写的,只不过那道题可以不用bitset。不知道问题出在哪了,一开始是离散化有问题,后来按照题解说的把右端点+1也离散化,但还是不对


|