标签: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 */
标签:can ons date update upd clu name char --
原文地址:https://www.cnblogs.com/Jstyle-continue/p/9366753.html