Vases and Flowers(线段树)
发布日期:2021-05-15 00:24:31 浏览次数:19 分类:精选文章

本文共 6316 字,大约阅读时间需要 21 分钟。

���������������������������������������n������������n���������������������������������������0���n-1������������������������������������

  • ������������������������x���������������������y���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Can not put any one.������

  • ������������������������[l, r]������������������������������������������������������������������������������������������������������������������

  • ������������

    ���������������������������������������������������������������������������������������������O(log n)������������������������������������������������������������������������������

    ������������������������������������������������������������������

    ������������������

    • ���������������������[x, n)������������������������������
    • ���������������������y���������������������������
      • ������������������������������������������������������������������������������������������
      • ���y������������������������������������������������������������������������������
    • ������������������������������������������������������������

    ������������������

    • ������������������������������������������������������������������������������������������������������������������������������
    • ������������������������������

    AC������������

    #include 
    using namespace std;
    const int N = 5 * 10^4 + 10;
    struct node {
    int l, r; // ������������������
    int cou; // ���������������������������������
    int lazy; // ������������-1���������
    };
    void pushdown(node& op, int lazy) {
    // ���������������
    if (op.l == op.r) return;
    op.cou = lazy * (op.r - op.l + 1);
    op.lazy = lazy;
    // ���������������������������
    }
    void pushup(int x) {
    // ������������������������������������
    int mid = t[x].l + t[x].r > 1 ? t[x].l + (t[x].r - t[x].l + 1) / 2 : t[x].r - 1;
    t[x].cou = t[x<<1].cou + t[x<<1|1].cou;
    }
    void build(int l, int r, int x) {
    if (l == r) {
    t[x].l = l, t[x].r = r, t[x].cou = 1, t[x].lazy = -1;
    return;
    }
    int mid = l + r > 1 ? l + (r - l + 1) / 2 : r - 1;
    build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1);
    pushup(x);
    }
    void modify(int l, int r, int c, int x) {
    if (c <= 0 || t[x].cou == 0) return;
    if (l <= t[x].l && r >= t[x].r) {
    if (t[x].l == t[x].r) {
    // ������������������������������������������������ck
    if (c >= t[x].cou) {
    L = min(L, t[x].l), R = max(R, t[x].r);
    t[x].cou = 0;
    pushdown(t[x], 0);
    return;
    }
    }
    if (c <= t[x].cou) {
    L = min(L, t[x].l), R = max(R, t[x].r);
    t[x].cou -= c;
    pushdown(t[x], 0);
    return;
    }
    // ������������������������������
    }
    pushdown(t[x]);
    int mid = t[x].l + t[x].r > 1 ? t[x].l + (t[x].r - t[x].l + 1) / 2 : t[x].r - 1;
    if (l <= mid) modify(l, r, c, x << 1);
    if (r > mid) modify(l, r, c, x << 1 | 1);
    pushup(x);
    }
    int ask(int l, int r, int x) {
    if (l > t[x].r || r < t[x].l) return 0;
    if (l <= t[x].l && r >= t[x].r) {
    return t[x].cou;
    }
    pushdown(t[x]);
    int mid = t[x].l + (t[x].r - t[x].l + 1) / 2;
    int res = 0;
    if (l <= mid || r > mid) { // ������������������������
    res += ask(l, r, x << 1);
    }
    if (l <= t[x].r && r >= t[x].r) {
    res += ask(l, r, x << 1 | 1);
    }
    return res;
    }
    void main() {
    const int DEBUG = 1;
    int T = 2;
    while (T--) {
    int n, m;
    fifscanf(stdin, "%d %d", &n, &m);
    build(1, n);
    while (m--) {
    int op, x, y;
    fifscanf(stdin, "%d %d %d", &op, &x, &y);
    if (op == 1) {
    // operation 1: put y flowers at x
    int temp = y;
    L = n+1, R = 0;
    modify(x, n, y, 1);
    if (y == 0) {
    puts("Can not put any one.");
    if (DEBUG) cout << "Y=0, L, R=0" << endl;
    } else if (temp != y) {
    // ���������������
    cout << L << " and " << R << endl;
    if (DEBUG) cout << "Y=1, L, R" << endl;
    } else {
    cout << "Can not put any one." << endl;
    if (DEBUG) cout << "Y= couldn't" << endl;
    }
    } else {
    // operation 2: clear [x, y], output count
    int l = x+1, r = y+1;
    // modify to clear
    int empty = ask(l, r, 1);
    int cnt = (r - l + 1) - empty;
    cout << cnt << endl;
    if (DEBUG) {
    cout << "������������[" << l << ", " << r << "]" << endl
    << "���������������:" << empty << endl
    << "���������������:" << cnt << endl;
    }
    }
    }
    cout << endl;
    }
    }

    ������������

  • ������������������������������������������������������������������������������������������������������������������������������

  • build������������������������������������������������������������������������

  • pushdown������������������������������������������������������������������������������

  • pushup���������������������������������������������������

  • modify���������������������������������������������������������������������������L���R���

  • ask������������������������������������������

  • ���������������������������������������������������������������������������������

  • ������������

    • ������������1��� ���������n=5, m=1, 1 0 3 ���������������������������0���������������������������2��� ������������������0������1���������������1������1���������������2������1���������������3���������������2������������

    • ������������2��� ���������n=5, m=1, 2 2 1 ���������Can not put any one. ������������������2������1���������������������������������������������

    • ������������3��� ���������n=5, m=2, 1 0 4 2 1 3 ��������� 2 3 ���������������������������������������������������������������

    ������

    ���������������������������������������������������������������������������������������������������

    上一篇:约会安排(线段树)
    下一篇:Transformation(线段树)

    发表评论

    最新留言

    感谢大佬
    [***.8.128.20]2025年04月25日 19时52分22秒