1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| struct SegTree { #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 ll a[maxn << 2]; int tag[maxn << 2]; void pushup(int rt) { a[rt] = min(a[rt << 1], a[rt << 1 | 1]); } void push(int rt,int x) { a[rt] += x; tag[rt] += x; } void pushdown(int rt) { if(!tag[rt]) return; push(rt << 1, tag[rt]); push(rt << 1 | 1, tag[rt]); tag[rt] = 0; } void build(int l = 1,int r = n,int rt = 1) { if(l==r) { a[rt] = 0; return; } int mid = (l + r) >> 1; build(lson), build(rson); pushup(rt); } void update(int L, int R, int x, int l = 1, int r = n,int rt = 1) { if(L<=l&&r<=R) { push(rt, x); return; } int mid = (l + r) >> 1; pushdown(rt); if(L<=m) update(L, R, x, lson); if(R>m) update(L, R, x, rson); pushup(rt); } ll query(int L, int R, int l = 1, int r = n,int rt = 1) { if(L<=l&&r<=R) { return a[rt]; } int m = (l + r) >> 1; pushdown(rt); if(R<=m) return query(L, R, lson); if(L>m) return query(L, R, rson); return min(query(L, R, lson), query(L, R, rson)); } } f;
|