페이지

2934번: LRH 식물

https://www.acmicpc.net/problem/2934


#include<stdio.h>
const int MAX_R = 100000;
int n, bit[MAX_R + 1];
void update(int h, int x) {
    for (; h <= MAX_R; h += h&-h) bit[h] += x;
}
int query(int h) {
    int s = 0;
    for (; h; h -= h&-h) s += bit[h];
    return s;
}
int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        int l, r, ql, qr;
        scanf("%d %d", &l, &r);
        ql = query(l);
        qr = query(r);
        printf("%d\n", ql + qr);
        update(l, -ql); update(l + 1, ql);
        update(r, -qr); update(r + 1, qr);
        update(l + 1, 1);
        update(r, -1);
    }
    return 0;
}

댓글 없음 :

댓글 쓰기