线段树倒序做法 60 pts 求调

P3740 [HAOI2014] 贴海报

lovely_codingcow @ 2023-12-23 11:17:56

RT

Code :

#include <iostream>
#include <algorithm>

#define int long long
#define endl '\n'
#define INF 9223372036854775807

using namespace std;

struct node {
    int l, r;
} a[1009];

int t, n, b[2009], tree[8009], tag[8009];

void push_up(int node) {
    tree[node] = min(tree[node << 1], tree[(node << 1) + 1]);
}

void push_down(int node) {
    tag[node << 1] += tag[node];
    tag[(node << 1) + 1] += tag[node];
    tree[node << 1] += tag[node];
    tree[(node << 1) + 1] += tag[node];
    tag[node] = 0;
}

void update(int node, int l, int r, int L, int R, int x) {
    if (L <= l && R >= r) {
        tag[node] += x;
        tree[node] += x;
        return;
    }
    push_down(node);
    int mid = (l + r) >> 1;
    if (L <= mid) {
        update(node << 1, l, mid, L, R, x);
    }
    if (R > mid) {
        update((node << 1) + 1, mid + 1, r, L, R, x);
    }
    push_up(node);
}

int query(int node, int l, int r, int L, int R) {
    if (L <= l && R >= r) {
        return tree[node];
    }
    push_down(node);
    int ans = INF; 
    int mid = (l + r) >> 1;
    if (L <= mid) {
        ans = min(ans, query(node << 1, l, mid, L, R));
    }
    if (R > mid) { 
        ans = min(ans, query((node << 1) + 1, mid + 1, r, L, R));
    }
    return ans;
}

signed main() {
    cin >> t >> n;
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i].l >> a[i].r;
        b[++cnt] = a[i].l, b[++cnt] = a[i].r;
    }
    sort(b + 1, b + cnt + 1);
    int len = unique(b + 1, b + cnt + 1) - b - 1;
    for (int i = 1; i <= n; i++) {
        a[i].l = lower_bound(b + 1, b + len + 1, a[i].l) - b;
        a[i].r = lower_bound(b + 1, b + len + 1, a[i].r) - b;
    }
    int ans = 0;
    for (int i = n; i >= 1; i--) {
        if (query(1, 1, len, a[i].l, a[i].r) == 0) {
            ans += 1;
        }
        update(1, 1, len, a[i].l, a[i].r, 1);
    }
    cout << ans;
}

by szydxf @ 2023-12-23 12:11:35

好像不能离散化吧


by lovely_codingcow @ 2023-12-23 13:50:30

@lovely_szy 在 OiClass A 了


|