100WA求调(线段树染色

P3740 [HAOI2014] 贴海报

Unknown__ @ 2024-03-28 23:21:26

#include <iostream>
#include <algorithm>
#define int long long
#define ls (s << 1)
#define rs (s << 1 | 1)
using namespace std;
struct node
{
    int l, r, col;
} T[801111];
pair<int, int> act[301111];
int n, m, A[201111], pcnt, len, ans, cnt;
bool book[201111];
void pushup(int s)
{
    if (T[ls].col != T[rs].col)
    {
        T[s].col = -1;
        return;
    }
    T[s].col = T[ls].col;
    return;
}
void build(int s, int l, int r)
{
    T[s] = (node){l, r, -2};
    if (l == r)
        return;
    int mid = (l + r) >> 1;
    build(ls, l, mid);
    build(rs, mid + 1, r);
    //pushup(s);
}
void pushdown(int s)
{
    if (T[s].col == -1)
        return;
    T[ls].col = T[rs].col = T[s].col;
}
void modify(int rt, int l, int r, int k)
{
    if (l >= T[rt].r || r <= T[rt].l)
        return;
    if (l <= T[rt].l && T[rt].r <= r)
    {
        T[rt].col = k;
        return;
    }
    pushdown(rt);
    modify(rt << 1, l, r, k);
    modify(rt << 1 | 1, l, r, k);
    pushup(rt);
}
void query(int s)
{
    if (T[s].col > 0)
    {
        book[T[s].col] = 1;
        return;
    }
    if (T[s].l == T[s].r)
        return;
    pushdown(s);
    query(ls);
    query(rs);
    pushup(s);
}
signed main()
{
    cin >> m >> n;
    int i;
    for (i = 1; i <= n; i++)
    {
        int a, b;
        cin >> a >> b;
        A[++len] = a;
        A[++len] = b;
        act[i] = make_pair(a, b);
        // modify(1,a,b,i);
    }
    sort(A + 1, A + len + 1);
    pcnt = unique(A + 1, A + len + 1) - A - 1;
    A[0] = -1;
    cnt = pcnt;
    for (i = pcnt; i >= 1; i--)
        if (A[i - 1] != A[i] - 1)
            A[++cnt] = A[i - 1] + 1;
    sort(A + 1, A + cnt + 1);
    build(1, 1, cnt);
    for (i = 1; i <= n; i++)
    {
        int l, r;
        l = lower_bound(A + 1, A + cnt + 1, act[i].first) - A;
        r = lower_bound(A + 1, A + cnt + 1, act[i].second) - A;
        modify(1, l, r, i);
    }
    query(1);
    for (i = 1; i <= n; i++)
        if (book[i])
            ans++;
    cout << ans << endl;
}

已经和题解几乎一样了(大悲


by _Ubuntu_ @ 2024-07-04 16:50:12

@NG_Jack 没细看不过,感觉离散化肯定有问题,hack

10 3
1 10
1 5
9 10

|