[Question Solution] LuoGu1438: Boring Sequence

The original question portal
Add arithmetic sequence to interval, single-point summation, you can use tree array to maintain
Open two tree arrays, the first maintains the sum, the second maintains the delta deltad e l t a
Magic changed the way of adding violence to arithmetic sequence, using a tree array to add onlylogn lognl o g n places, but I need to savedelta deltad e l t a , when summing, the cumulative sum andk ∗ deltak*deltakd e l t a , thiskkk is thexxthrequirement in the actual senseDifference between x number and current

After understanding the specific operation, you will know how to update
update (l, k, d), update (r + 1, − k − (r − l + 1) ∗ d, − d) update(l,k,d) ,update(r+1,-k-(r-l+1)*d,-d)update(l,k,d),update(r+1,k(rl+1)d,d)

Code:

#include <bits/stdc++.h>
#define maxn 100010
using namespace std;
int n, m, a[maxn], tree[maxn][2];

inline int read(){
    
    
	int s = 0, w = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
	for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
	return s * w;
}

int lowbit(int x){
    
     return x & -x; }
void update(int x, int k, int d){
    
     for (; x <= n; x += lowbit(x)) tree[x][0] += k, tree[x][1] += d, k += d * lowbit(x); }
int query(int x){
    
     int s = 0, p = x; for (; x; x -= lowbit(x)) s += tree[x][0] + (p - x) * tree[x][1]; return s; }

int main(){
    
    
	n = read(), m = read();
	for (int i = 1; i <= n; ++i) a[i] = read();
	while (m--){
    
    
		int opt = read();
		if (opt == 1){
    
    
			int l = read(), r = read(), k = read(), d = read();
			update(l, k, d), update(r + 1, -k - (r - l + 1) * d, -d);
		} else{
    
    
			int x = read();
			printf("%d\n", query(x) + a[x]);
		}
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/ModestCoder_/article/details/108477543