差分序列一般用于对区间[l,r]进行相同的线性修改(赋值)
操作方法:
1.设置一个数组a[maxn](maxn为区间的最大值)
2.对区间[l,r]的数加q, a[l]+=q,a[r+1]-=q;(注意是区间的右端点r+1修改)
3.for(int i=2;i<=maxn;i++) a[i]+=a[i-1];
例题
Master of GCD
题目描述
Hakase has n numbers in a line. At fi rst, they are all equal to 1. Besides, Hakase is interested in primes. She will choose a continuous subsequence [l, r] and a prime parameter x each time and for every l≤i≤r, she will change ai into ai*x. To simplify the problem, x will be 2 or 3. After m operations, Hakase wants to know what is the greatest common divisor of all the numbers.
输入
The first line contains an integer T (1≤T≤10) representing the number of test cases.
For each test case, the fi rst line contains two integers n (1≤n≤100000) and m (1≤m≤100000),where n refers to the length of the whole sequence and m means there are m operations.
The following m lines, each line contains three integers li (1≤li≤n), ri (1≤ri≤n), xi (xi∈{2,3} ),which are referred above.
输出
For each test case, print an integer in one line, representing the greatest common divisor of the sequence. Due to the answer might be very large, print the answer modulo 998244353.
样例输入
复制样例数据 2
5 3
1 3 2
3 5 2
1 5 3
6 3
1 2 2
5 6 2
1 6 2
样例输出
6
2
提示
For the first test case, after all operations, the numbers will be [6,6,12,6,6]. So the greatest common divisor is 6.
#include<bits/stdc++.h>
const int mod=998244353;
int in2[100005],in3[100005];
int main()
{
int n;cin>>n;
while(n--)
{
memset(in2,0,sizeof(in2));
memset(in3,0,sizeof(in3));
int m;int N;
cin>>m>>N;
int l,r,in;
while(N--)
{
cin>>l>>r>>in;
if(in==2)
{
in2[l]++,in2[r+1]--;
}
else{in3[l]++;in3[r+1]--;}
}
for(int i=2;i<=m;i++){in2[i]+=in2[i-1];in3[i]+=in3[i-1];}
int min1=100009,min2=100009;
for(int i=1;i<=m;i++){min1=min(min1,in2[i]),min2=min(min2,in3[i]);}
//for(int i=1;i<=m;i++) cout<<"i2"<<':'<<i<<' '<<in2[i]<<endl;
//for(int i=1;i<=m;i++) cout<<"i3"<<':'<<i<<' '<<in3[i]<<endl;
long ans=1;
//cout<<min1<<endl<<min2<<endl;
for(int i=0;i<min1;i++) {ans=ans*2%mod;}
for(int i=0;i<min2;i++) {ans=ans*3%mod;}
cout<<ans<<endl;
}
}