#include <bits/stdc++.h> using namespace std; const int maxn = 1e5+10; #define ls (rt<<1) #define rs (rt<<1|1) #define mid ((l+r)>>1) typedef long long ll; typedef long long ll; int n, q; int a[maxn], b[maxn]; struct Node { int minx, valb, sum_zero, lazy; }node[maxn*4]; void push_up(int rt, int l, int r){ node[rt].minx =min(node[ls].minx, node[rs].minx); node[rt].sum_zero = node[ls].sum_zero+node[rs].sum_zero; } void push_down(int rt, int l, int r){ node[ls].lazy += node[rt].lazy; node[rs].lazy += node[rt].lazy; node[ls].minx -= node[rt].lazy; node[rs].minx -= node[rt].lazy; node[rt].lazy = 0; return; } void build(int rt, int l ,int r){ node[rt].lazy = node[rt].sum_zero = 0; if(l == r){ node[rt].minx = node[rt].valb = b[l]; return ; } build(ls, l, mid); build(rs, mid+1, r); push_up(rt, l, r); } void update(int rt, int l, int r, int ql ,int qr){ if(l>r) return; if(node[rt].minx>1&&l == ql&&r == qr){ node[rt].lazy += 1; node[rt].minx -= 1; return ; } if(l == r&&node[rt].minx == 1){ node[rt].sum_zero += 1; node[rt].minx = node[rt].valb; node[rt].lazy = 0; return; } if(node[rt].lazy>0) push_down(rt, l, r); if(mid>=qr) 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); } push_up(rt, l, r); } ll query(int rt, int l, int r, int ql, int qr){ if(l>r) return 0; if(l == ql&&r == qr) return node[rt].sum_zero; if(node[rt].minx==0) update(rt, l, r, ql, qr); 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(){ ios::sync_with_stdio(false); while(cin>>n>>q){ for(int i=1; i<=n; i++) cin>>b[i]; char op[10]; build(1, 1, n); int l, r; for(int i=0; i<q; i++){ cin>>op>>l>>r; if(op[0] == 'a'){ update(1, 1, n, l, r); } else{ cout<<query(1, 1, n, l, r)<<endl; } } } return 0; }
|