码迷,mamicode.com
首页 > 其他好文 > 详细

qqq

时间:2018-07-25 17:02:44      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:can   ons   date   update   upd   clu   name   char   --   

#include <bits/stdc++.h>
using namespace std;
#define IO ios_base::sync_with_stdio(0),cin.tie(0)
#define mem(a,b) memset(a,b,sizeof(a))
#define FIN freopen("in.txt","r",stdin)
#define ls (id<<1)
#define rs ((id<<1)|1)
#define mid ((l+r)>>1)
typedef long long LL;

const int maxn = 1e5+5;

int n, q, a[maxn], b[maxn];

struct Node {
    LL minx, valb, sum_zero, lazy, cnt;
} node[maxn*4];

void pushdown(int id, int l, int r) {
    node[ls].lazy += node[id].lazy;
    node[rs].lazy += node[id].lazy;
    node[ls].minx -= node[id].lazy;
    node[rs].minx -= node[id].lazy;
    node[id].lazy = 0;
}
void pushup(int id, int l, int r) {
    node[id].sum_zero = node[ls].sum_zero + node[rs].sum_zero;
    node[id].minx = min(node[ls].minx, node[rs].minx);
}
void build(int id, int l, int r) {
    node[id].lazy = node[id].sum_zero = 0;
    if (l == r) {
        node[id].minx = node[id].valb = b[l];
        node[id].sum_zero = 0;
        node[id].cnt = 1;
        return;
    }
    build(ls, l, mid);
    build(rs, mid+1, r);
    pushup(id, l, r);
}
void update_zero(int id, int l, int r, int ql, int qr) {
    if (node[id].minx > 0 || l > r) return;
    if (l == r) {
        node[id].sum_zero++;
        node[id].cnt++;
        node[id].minx = node[id].valb*node[id].cnt;
        node[id].lazy = 0;
        return ;
    }
    if (node[id].lazy > 0) pushdown(id, l, r);
    if (qr <= mid) update_zero(ls, l, mid, ql, qr);
    else if (ql > mid) update_zero(rs, mid+1, r, ql, qr);
    else {
        update_zero(ls, l, mid, ql, mid);
        update_zero(rs, mid+1, r, mid+1, qr);
    }
    pushup(id, l, r);
}
void update(int id, int l, int r, int ql, int qr) {
    if (l > r) return;
    if (l == ql && r == qr) {
        node[id].lazy++;
        node[id].minx -= node[id].lazy;
        return;
    }
    if (node[id].minx <= 0) update_zero(1, l, r, ql, qr);
    if (node[id].lazy > 0) pushdown(id, l, r);
    if (qr <= mid) update(ls, l, mid, ql, qr);
    else if (ql > mid) update(rs, mid+1, r, ql, qr);
    else {
        update(ls, l, mid, ql, mid);
        update(rs, mid+1, r, mid+1, qr);
    }
    pushup(id, l, r);
}

LL query(int id, int l, int r, int ql, int qr) {
    if (l > r) return 0;
    if (l == ql && r == qr) return node[id].sum_zero;
    if (node[id].minx <= 0) {
        update_zero(1, l, r, ql, qr);
    }
    if (node[id].lazy > 0) pushdown(id, l, r);
    if (qr <= mid) return query(ls, l, mid, ql, qr);
    else if (ql > mid) return query(rs, mid+1, r, ql, qr);
    else {
        return query(ls, l, mid, ql, mid)+query(rs, mid+1, r, mid+1, qr);
    }
}

int main() {
    FIN;
    while (scanf("%d %d", &n, &q) != EOF) {
        for (int i = 1; i <= n; ++i) scanf("%d", &b[i]);
        build(1, 1, n);
        while (q--) {
            char op[10];
            int l, r;
            scanf("%s %d %d", op, &l, &r);
            if (op[0] == a) {
                update(1, 1, n, l, r);
            } else {
                LL ans = query(1, 1, n, l, r);
                printf("%lld\n", ans);
            }
        }
    }
    return 0;
}
/*
1
1
2
4
4
6
*/

 

qqq

标签:can   ons   date   update   upd   clu   name   char   --   

原文地址:https://www.cnblogs.com/Jstyle-continue/p/9366753.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!