poj3468 A Simple Problem with Integers
普通序列,区间更新,区间查询。
区间更新:
每个结点的sum信息,表示以这个结点为根的所有子树结点的和,包括根结点。
void add(int a, int b, int c) { if(a > b) swap(a, b); Node *left, *mid, *right, *o; split(ss.root, a, left, o); split(o, b-a+1, mid, right); mid->v += c; mid->sum += (i64)c*mid->s; mid->lazy += c; ss.root = merge(merge(left, mid), right); }
区间求和:
这个题需要用i64,v与延迟标记lazy,以及sum都应该是i64类型。。
void query_sum(int a, int b) { Node *left, *mid, *right, *o; split(ss.root, a, left, o); split(o, b-a+1, mid, right); printf ( "%lld\n", mid->sum ); ss.root = merge(merge(left, mid), right); }
maintain()和pushdown():
特别注意一下sum。。
void maintain() { s = ch[0]->s + ch[1]->s + 1; sum = v; if ( ch[0] != null ) sum += ch[0]->sum; if ( ch[1] != null ) sum += ch[1]->sum; } void pushdown() { if(lazy) { ch[0]->v += lazy; ch[0]->sum += lazy*ch[0]->s; ch[0]->lazy += lazy; ch[1]->v += lazy; ch[1]->sum += lazy*ch[1]->s; ch[1]->lazy += lazy; } lazy = 0; }