# Find the number of k small O (n) -Stinthia fast row SCAR

2022-12-26   ES

Title: Obviously a thread tree topic, but what is required is a [l]*len+a [l+1]*(len-)+a [l+2]*(len-2)++(len-2)+(len-2)+ ……+a [r], so the following formula is pushed according to this:

can try to push this formula. So we maintain the two lines of the tree, SUM1 preserves the normal array interval, and SUM2 saves the array of A [i]*i array.

When the query range is [L, R], the answer is SUM2 [L, R]-(N-R)*Sum1 [L, R]

code and thinking reference on:https://blog.csdn.net/ccsu_cat/article/details/82594389

#include <cStdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

/*
Maintain the normal interval and SUM1 maintenance normal interval and SUM2 maintenance (N-I+1)*A [i] interval and
The answer is SMU2 [L, R]-(N-R) Sum1 [L, R]
The formula can push it yourself
*/
const into maxn = 1E5+10;
TypeDef long long ll;
LL SUM1 [Maxn*4], SUM2 [maxn*4], a [maxn]; // line segment tree open 4x space
int n;
void build (int o, int L, int R)
{{
if (l == r)
{{
sum1 [o] = a [l];
sum2 [o] = a [l]*(n-l+1);
Return;
}
int ls = o*2, rs = o*2+1, m = (l+r)/2; // left and right sub -nodes and intermediate nodes
Build (ls, l, m);
Build (RS, M+1, R);
SUM1 [O] = SUM1 [LS]+SUM1 [RS];
SUM2 [O] = SUM2 [LS]+SUM2 [RS];
}

// Query SUM1
ll query1 (int o, int L, int R, int QL, int QR)
{{
if (l> = ql && r <= qr) Return Sum1 [o];
int ls = 2*o, rs = 2*o+1, m = (l+r)/2;
ll res = 0;
if (ql <= m) res+= query1 (ls, l, m, ql, qr);
if (qr> m) res+= query1 (RS, M+1, R, QL, QR);
Return res;
}
// Query SUM2
ll query2 (int o, int L, int R, int QL, int QR)
{{
if (l> = ql && r <= qr) Return Sum2 [o];
int ls = 2*o, rs = 2*o+1, m = (l+r)/2;
ll res = 0;
if (ql <= m) res+= query2 (ls, l, m, ql, qr);
if (qr> m) res+= query2 (RS, M+1, R, QL, QL);
Return res;
}
//renew
void update (int o, int l, int r, int k, ll v)
{{
if (l == r)
{{
sum1 [o] = v;
sum2 [o] = v*(n-l+1);
Return;
}
int ls = o*2, rs = o*2+1, m = (l+r)/2;
if (k <= m) update (ls, l, m, k, v);
Else Update (RS, M+1, R, K, V);
SUM1 [O] = SUM1 [LS]+SUM1 [RS];
SUM2 [O] = SUM2 [LS]+SUM2 [RS];
}

int Main ()
{{
int Q;
While (Scanf ("%D%D", & N, & Q)! = EOF)
{{
for (int i = 1; i <= n; i ++)
{{
scanf ("%lld", & a [i]);
}
Build (1,1, n);
While (q--)
{{
int x;
scanf ("%d", & x);
if (x == 1)
{{
int L, R;
scanf ("%d%d", & l, & r);
ll temp1 = query1 (1,1, n, l, r);
ll temp2 = query2 (1,1, n, l, r);
ll temp3 = n-r;
Printf ("%LLD \ N", Temp2-Temp3*Temp1);
}
else
{{
int K;
ll v;
scanf ("%d%lld", & k, & v);
update (1,1, n, k, v);
}
}
}
Return 0;
}

source