1、如何处理乘法以及计算平方和
2、首先分析对于区间内每个数+k,对区间平方和的影响:
∑(xi+k)² = ∑xi² + 2*k*∑xi + ∑k²
因此我们只要将该区间的平方和 += 该区间的和*2*k + 该区间长度*k
tree2[p] += 2*tree1[p]*num + num*num*(r-l+1);
对于乘k对于区间和、区间平方和影响
区间和*k = ∑每个元素*k,因此区间和直接乘k即可
区间平方和*k² = ∑(每个元素*k)²,因此区间平方和直接乘k²即可
3、两个标记,lazy1表示加,lazy2表示乘,如何Pushdown
首先对于两个标记是先加还是先乘很难搞清,统一设为(xi+p1)*p2的形式,即lazy1要随着lazy2更新,每次对该区间进行乘操作时,lazy1也跟着*k,
- lazy1[p] *= num;
- lazy2[p] *=num;
这时进行pushdown:
先更新标记:
- lazy1[p*2] += lazy1[p];
- lazy1[p*2+1] += lazy1[p];
-
- lazy2[p*2] *= lazy2[p];
- lazy2[p*2+1] *= lazy2[p];
∑((xi+p1)*p2)² = ∑xi² * p2² + 2 * p2² * p1 * ∑xi + ∑p2² * p1²
又因为p1即lazy1标记随着lazy2(p2)标记实时更新,因此此时lazy1[p] = p1*p2;
说明:∑xi为区间和
- tree2[p*2] *= lazy2[p]*lazy2[p];
- tree2[p*2] += 2*tree1[p*2]*lazy1[p]*lazy2[p] + lazy1[p]*lazy1[p]*(mid-l+1);
- tree2[p*2+1] *= lazy2[p]*lazy2[p];
- tree2[p*2+1] += 2*tree1[p*2+1]*lazy1[p]*lazy2[p] + lazy1[p]*lazy1[p]*(r-(mid+1)+1);
同样的分析对于区间和
∑((xi+p1)*p2) = ∑xi * p2 + 2*∑p2 * p1
- tree1[p*2] *= lazy2[p];
- tree1[p*2] += (mid-l+1)*lazy1[p];
- tree1[p*2+1] *= lazy2[p];
- tree1[p*2+1] += (r-(mid+1)+1)*lazy1[p];
注意这里两个区间的更新不可以调换,因为区间平方和要利用区间和,因此区间平方和要先计算,如果调换,那么就会重复计算
- # include
- using namespace std;
- typedef long long ll;
- const int N = 10000+10;
- int n, m;
- ll a[N];
- ll tree1[N*4], tree2[N*4], lazy1[N*4], lazy2[N*4];
- void build(int p, int l, int r)
- {
- if (l == r)
- {
- tree1[p] = a[l];
- tree2[p] = a[l]*a[l];
- return ;
- }
- int mid = (l+r)/2;
- build(2*p, l, mid);
- build(2*p+1, mid+1, r);
- tree1[p] = tree1[p*2] + tree1[p*2+1];
- tree2[p] = tree2[p*2] + tree2[p*2+1];
- }
- void pushdown(int p, int l, int r)
- {
- lazy1[p*2] += lazy1[p];
- lazy1[p*2+1] += lazy1[p];
-
- lazy2[p*2] *= lazy2[p];
- lazy2[p*2+1] *= lazy2[p];
-
- int mid = (l+r)/2;
-
- tree2[p*2] *= lazy2[p]*lazy2[p];
- tree2[p*2] += 2*tree1[p*2]*lazy1[p]*lazy2[p] + lazy1[p]*lazy1[p]*(mid-l+1);
- tree2[p*2+1] *= lazy2[p]*lazy2[p];
- tree2[p*2+1] += 2*tree1[p*2+1]*lazy1[p]*lazy2[p] + lazy1[p]*lazy1[p]*(r-(mid+1)+1);
-
- tree1[p*2] *= lazy2[p];
- tree1[p*2] += (mid-l+1)*lazy1[p];
- tree1[p*2+1] *= lazy2[p];
- tree1[p*2+1] += (r-(mid+1)+1)*lazy1[p];
-
- lazy1[p] = 0;
- lazy2[p] = 1;
- }
- void change1(int p, int l, int r, int x, int y, ll num)//+num
- {
- if (x<=l && y>=r)
- {
- tree2[p] += 2*tree1[p]*num + num*num*(r-l+1);
- tree1[p] += num*(r-l+1);
- lazy1[p] += num;
- return ;
- }
- if (lazy1[p]!=0 || lazy2[p]!=1)
- pushdown(p, l, r);
- int mid = (l+r)/2;
- if (x<=mid) change1(2*p, l, mid, x, y, num);
- if (y>mid) change1(2*p+1, mid+1, r, x, y, num);
- tree1[p] = tree1[p*2] + tree1[p*2+1];
- tree2[p] = tree2[p*2] + tree2[p*2+1];
- }
- void change2(int p, int l, int r, int x, int y, ll num)//*num
- {
- if (x<=l && y>=r)
- {
- tree2[p] *= num*num;
- tree1[p] *= num;//单个影响
- lazy1[p] *= num;
- lazy2[p] *=num;
- return ;
- }
- if (lazy1[p]!=0 || lazy2[p]!=1)
- pushdown(p, l, r);
- int mid = (l+r)/2;
- if (x<=mid) change2(2*p, l, mid, x, y, num);
- if (y>mid) change2(2*p+1, mid+1, r, x, y, num);
- tree1[p] = tree1[p*2] + tree1[p*2+1];
- tree2[p] = tree2[p*2] + tree2[p*2+1];
- }
- ll cal1(int p, int l, int r, int x, int y)//求和
- {
- if (x<=l && y>=r) return tree1[p];
- if (lazy1[p]!=0 || lazy2[p]!=1)
- pushdown(p, l, r);
- int mid = (l+r)/2;
- // ll ans = 0;
- // if (x<=mid) ans += cal1(2*p, l, mid, x, y);
- // if (y>mid) ans += cal1(2*p+1, mid+1, r, x, y);
- // return ans;
- if (y<=mid) return cal1(2*p, l, mid, x, y);
- if (x>mid) return cal1(2*p+1, mid+1, r, x, y);
- return cal1(2*p, l, mid, x, mid) + cal1(2*p+1, mid+1, r, mid+1, y);
- }
- ll cal2(int p, int l, int r, int x, int y)//求平方和
- {
- if (x<=l && y>=r) return tree2[p];
- if (lazy1[p]!=0 || lazy2[p]!=1)
- pushdown(p, l, r);
- int mid = (l+r)/2;
- // ll ans = 0;
- // if (x<=mid) ans += cal2(2*p, l, mid, x, y);
- // if (y>mid) ans += cal2(2*p+1, mid+1, r, x, y);
- // return ans;
- if (y<=mid) return cal2(2*p, l, mid, x, y);
- if (x>mid) return cal2(2*p+1, mid+1, r, x, y);
- return cal2(2*p, l, mid, x, mid) + cal2(2*p+1, mid+1, r, mid+1, y);
- }
- int main()
- {
- for (int i=0; i
4; i++) - lazy2[i] = 1;
- memset(lazy1, 0, sizeof(lazy1));
- cin>>n>>m;
- for (int i=1; i<=n; i++)
- cin>>a[i];
- build(1, 1, n);
- for (int i=1; i<=m; i++)
- {
- int q;
- cin>>q;
- if (q == 1)
- {
- int l, r;
- cin>>l>>r;
- cout<<cal1(1, 1, n, l, r)<
- }
- if (q == 2)
- {
- int l, r;
- cin>>l>>r;
- cout<<cal2(1, 1, n, l, r)<
- }
- if (q == 3)
- {
- int l, r;
- ll x;
- cin>>l>>r>>x;
- change2(1, 1, n, l, r, x);
- }
- if (q == 4)
- {
- int l, r;
- ll x;
- cin>>l>>r>>x;
- change1(1, 1, n, l, r, x);
- }
- }
-
-
- return 0;
- }
-
相关阅读:
关于Redis集群的一些问题的理解
创新之力迸发无限想象,联想创新开放日触见未来科技
【前端进阶】-TypeScript高级类型 | 泛型约束、泛型接口、泛型工具类型
【羊了个羊】背后的计算机网络原理
【车载以太网测试从入门到精通】——DoIP BootLoader刷写测试(含CAPL源码)
LeetCode220726_52、腐烂的橘子
Python字符串操作:str.format
DM8表空间备份还原
用python统计文本字符出现的次数
创新实战|高转化网站首页设计的10大关键要素
-
原文地址:https://blog.csdn.net/m0_60531106/article/details/127820828