[template] zhinai sauce's static array maintenance problem polynomial (multiple difference array - polynomial processing)

Posted by Ruiser on Fri, 25 Feb 2022 09:12:05 +0100

subject

Original question link

Problem description

thinking

The advantage of differential array is that it can move the whole body by pulling one hair. By processing one position, the values of subsequent positions can be changed.

The operation we are facing at this time is to process the interval, and the processing is not simply adding a value, but to [ l , r ] [l,r] In [l,r] a [ i ] a[i] a[i] plus f ( i ) f(i) f(i), f ( i ) f(i) f(i) is a maximum order not exceeding 5 5 Polynomial of 5.

1. Introduction

In topic Little W's candy In, we need to deal with the square difference array, that is, for the position l l l and each position on its right need to add i 2 i^2 i2, in fact, this is also a special polynomial. We do it by 3 3 Third difference:
[ 1 , 4 , 9 , 16 , 25 ] ⟹ [ 1 , 3 , 5 , 7 , 9 ] ⟹ [ 1 , 2 , 2 , 2 , 2 ] ⟹ [ 1 , 1 , 0 , 0 , 0 ] [1,4,9,16,25]\Longrightarrow[1,3,5,7,9]\Longrightarrow[1,2,2,2,2]\Longrightarrow[1,1,0,0,0] [1,4,9,16,25]⟹[1,3,5,7,9]⟹[1,2,2,2,2]⟹[1,1,0,0,0]

In mathematics, there is a conclusion: the highest order item is n n n times n n n-order polynomial n + 1 n+1 The remainder of n+1 order difference is constant (non-zero term is finite).

According to this theorem, we add a specific constant to a specific position on a sequence, and then n + 1 n+1 n+1 prefixes and operations, you can [ l , + ∞ ) [l,+\infty) In [l, + ∞) a [ i ] a[i] a[i] plus the highest order item is n n n times n n n-order polynomial f ( i ) f(i) f(i).

It is worth noting that if we take the highest order item as n n n times n n n-order polynomial n + 2 n+2 n+2 order difference, what kind of result will we get?
Still using the above example, make another difference, and we will get the sequence [ 1 , 0 , − 1 , 0 , 0 ] [1,0,-1,0,0] [1,0, − 1,0,0], this sequence is still a non-linear sequence 0 0 0 finite sequences.

At this time, there is another problem, that is, this non-linear problem 0 0 In what range may the length of 0-term finite sequence be, we take f ( x ) = x 5 + x 4 + x 3 + x 2 + x + 6 f(x)=x^5+x^4+x^3+x^2+x+6 f(x)=x5+x4+x3+x2+x+6 let's see

6 6 After six differences 6 6 6 items, so we set it at the beginning 10 10 10 bits can launch this sequence.

2. Analysis

However, the operation in the above topic does not operate on all the elements on the right, but on the interval [ l , r ] [l,r] [l,r] internal elements, so we need to [ r + 1 , + ∞ ) [r+1,+\infty) Each element in [r+1, + ∞) is corrected, that is, each element of this paragraph is added − f ( i ) -f(i) − f(i), it is still possible to − f ( i ) -f(i) − f(i) is regarded as a polynomial, and a non polynomial is obtained through multiple differences 0 0 0 finite sequences.

code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod =1e9+7;
const int N =1e5+7;
#define fir(i, a, b) for (int i = (a); i <= (b); i++)
#define rif(i, a, b) for (int i = (a); i >= (b); i--)
ll t,n,m,q,a[N],l,r,k,c[15],p[2][15];
ll f(ll x, ll a[],ll k){
	ll res = 0;
	ll base = 1;
	fir(i,0,k){
		(res+=base*c[i])%=mod;
		(base*=x)%=mod;
	}
	return res;
}
ll g(ll x,ll a[],ll k,ll l,ll r){
	return (mod-f(x+r-l+1,a,k))%mod;
}
void P(ll a[], int len, int cnt){
	while(cnt--){
		fir(i,1,len){
			(a[i]+=a[i-1])%=mod;
		}
	}
}
void D(ll a[], int len, int cnt){
	while(cnt--){
		rif(i,len,0){
			a[i]=(a[i]-a[i-1]+mod)%mod;
		}
	}
}
int main(){
	scanf("%lld %lld %lld", &n, &m, &q);
	fir(i,1,n)scanf("%lld", &a[i]);
	D(a,n,6);
	while(m--){
		cin>>l>>r>>k;
		rif(i,k,0)scanf("%lld", &c[i]);
		fir(i,1,10){
			p[0][i]=f(i,c,k);
			p[1][i]=g(i,c,k,l,r);
		}
		D(p[0],10,6);
		D(p[1],10,6);
		fir(i,1,10){
			(a[l+i-1]+=p[0][i])%=mod;
			(a[r+i]+=p[1][i])%=mod;
		}
	}
	P(a,n,7);
	while(q--){
		scanf("%lld %lld", &l, &r);
		printf("%lld\n",((a[r]-a[l-1])%mod+mod)%mod);
	}
}

Topics: Algorithm data structure linear algebra