[Luogu] 区间统计Tallest Cow

https://www.luogu.org/problemnew/show/P2879

差分 | 线段树

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
const int N = 1e4 + 10;

#define gc getchar()

struct Node {int l, r;}A[N];
int n, my, Maxh, R;
int H[N];

inline int read() {
    int x = 0; char c = gc;
    while(c < '0' || c > '9') c = gc;
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc;
    return x;
}

inline bool cmp(Node a, Node b) {
    return a.l == b.l ? a.r < b.r : a.l < b.l;
}

int main() {
    n = read();
    my = read();
    Maxh = read();
    R = read();
    for(int i = 1; i <= R; i ++) {
        int one = read(), tow = read();
        A[i].l = min(one, tow);
        A[i].r = max(one, tow);
    }
    sort(A + 1, A + R + 1, cmp);
    H[A[1].l + 1] += 1;
    H[A[1].r] -= 1;
    for(int i = 2; i <= R; i ++) {
        if(A[i].l == A[i - 1].l && A[i].r == A[i - 1].r) continue ;
        H[A[i].l + 1] += 1;
        H[A[i].r] -= 1;
    }
    int tmp = 0;
    for(int i = 1; i <= n; i ++) {
        tmp += H[i];
        cout << Maxh - tmp << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/shandongs1/p/8455213.html