hdu 4578 Transformation [线段树 区间更新]
2014-06-23 15:24
399 查看
题意:
一个长度为n的数列a,初始全为0,对这个数列进行m个操作,操作格式如下:
op x y c
op=1: a[i] += c, i=x,x+1,...,y
op=2: a[i] *= c, i=x,x+1,...,y
op=3: a[i] = c, i=x,x+1,...,y
op=4: 输出sum=a[x]+a[x+1]+...+a[y]
思路:
考虑用线段树,对set,mul,add的处理顺序为set->mul->add。
对于一段序列a[x],a[x+1],...,a[y]记
Sigma1(a[i]) = a[x] + a[x+1] +...+ a[y]
Sigma2(a[i]) = a[x]^2 + a[x+1]^2 +...+ a[y]^2
Sigma3(a[i]) = a[x]^3 + a[x+1]^3 +...+ a[y]^3
那么,
Sigma1(n*a[i]+p)
= (n*a[x]+p) + (n*a[x+1]+p) +...+ (n*a[y]+p)
= n*(a[x]+a[x+1]+...+a[y]) + p*(y-x+1)
= n*Sigma1(a[i])
Sigma2(n*a[i]+p)
= (n*a[x]+p)^2 + (n*a[x+1]+p)^2 +...+ (n*a[y]+p)^2
= n*n*(a[x]^2+a[x+1]^2+...+a[y]^2) + 2*n*p*(a[x]+a[x+1]+...+a[y]) + p*p*(y-x+1)
= n*n*Sigma2(a[i]) + 2*n*p*Sigma1(a[i]) + p*p*(y-x+1)
Sigma3(n*a[i]+p)
= (n*a[x]+p) + (n*a[x+1]+p) +...+ (n*a[y]+p)
= n*n*n*(a[x]^3+a[x+1]^3+...+a[y]^3) + 3*n*n*p*(a[x]^2+a[x+1]^2+...+a[y]^2) + 3*n*p*p*(a[x]+a[x+1]+...+a[y]) + p*p*p*(y-x+1)
= n*n*n*Sigma3(a[i]) + 3*n*n*p*Sigma2(a[i]) + 3*n*p*p*Sigma1(a[i]) + p*p*p*(y-x+1)
这样就很容易求解了。
一个长度为n的数列a,初始全为0,对这个数列进行m个操作,操作格式如下:
op x y c
op=1: a[i] += c, i=x,x+1,...,y
op=2: a[i] *= c, i=x,x+1,...,y
op=3: a[i] = c, i=x,x+1,...,y
op=4: 输出sum=a[x]+a[x+1]+...+a[y]
思路:
考虑用线段树,对set,mul,add的处理顺序为set->mul->add。
对于一段序列a[x],a[x+1],...,a[y]记
Sigma1(a[i]) = a[x] + a[x+1] +...+ a[y]
Sigma2(a[i]) = a[x]^2 + a[x+1]^2 +...+ a[y]^2
Sigma3(a[i]) = a[x]^3 + a[x+1]^3 +...+ a[y]^3
那么,
Sigma1(n*a[i]+p)
= (n*a[x]+p) + (n*a[x+1]+p) +...+ (n*a[y]+p)
= n*(a[x]+a[x+1]+...+a[y]) + p*(y-x+1)
= n*Sigma1(a[i])
Sigma2(n*a[i]+p)
= (n*a[x]+p)^2 + (n*a[x+1]+p)^2 +...+ (n*a[y]+p)^2
= n*n*(a[x]^2+a[x+1]^2+...+a[y]^2) + 2*n*p*(a[x]+a[x+1]+...+a[y]) + p*p*(y-x+1)
= n*n*Sigma2(a[i]) + 2*n*p*Sigma1(a[i]) + p*p*(y-x+1)
Sigma3(n*a[i]+p)
= (n*a[x]+p) + (n*a[x+1]+p) +...+ (n*a[y]+p)
= n*n*n*(a[x]^3+a[x+1]^3+...+a[y]^3) + 3*n*n*p*(a[x]^2+a[x+1]^2+...+a[y]^2) + 3*n*p*p*(a[x]+a[x+1]+...+a[y]) + p*p*p*(y-x+1)
= n*n*n*Sigma3(a[i]) + 3*n*n*p*Sigma2(a[i]) + 3*n*p*p*Sigma1(a[i]) + p*p*p*(y-x+1)
这样就很容易求解了。
maintain(o): if setv then cope o with setv else if R>L then cope o with lo and ro else cope o as leaf node cope o with mulv then addv pushdown(o): if setv then push setv down if mulv then push mulv down if addv then push addv down update: if [y1,y2] contains [L,R] do update directly else pushdown if y1 <= M then update(lo) else maintain(lo) if y2 > M then update(ro) else maintain(ro) maintain(o)
相关文章推荐
- hdu 4578 Transformation(线段树区间更新)
- K - Transformation-hdu 4578(多操作混合区间更新)线段树
- 【线段树多符号区间更新】HDU - 4578 Transformation
- HDU 4578——Transformation——————【线段树区间操作、确定操作顺序】
- hdu 4578 Transformation【线段树区间&思维】
- hdu 4578 Transformation(区间线段树)
- hdu 4578 Transformation(线段树区间操作)
- hdu 4578 Transformation(线段树中级,区间和加强)
- HDU 4578(线段树区间更新)
- HDU 4578 线段树区间更新(确定区间操作的优先级)
- HDU - 4578 Transformation(线段树 区间修改)
- 线段树 hdu 1166 敌兵布阵 单点更新区间求和
- HDU-4614 Vases and Flowers 线段树区间更新
- hdu1394——线段树(单点更新 区间求和)
- HDU-1698(线段树_区间更新_java手写)
- HDU 3308【线段树-query:区间最长单调上升序列,update:结点更新,区间合并】
- HDU 1556 Color the ball 很典型的更新区间查找点的题(线段树树状数组两种解法)
- HDU 3308 LCIS 线段树区间更新
- hdu 3874 Necklace 线段树单点更新区间求和
- 线段树 hdu 1754 I Hate It 单点更新 区间求最值