题解:P12518 「MSTOI-R1」Easy question
容易观察到,预处理前缀和数组
1.1 l r:答案为
2.2 l r k:答案为
3.3 l r:令
代码:
#include <bits/stdc++.h>
using namespace std;
static const int MAXN=1000000+5;
static const int MAXK=20;
static const int MOD=998244353;
static unsigned P[MAXK+1][MAXN];
static const int BSZ=1<<20;
static char ibuf[BSZ];
static int ipos,ilen;
static char obuf[BSZ];
static int opos;
inline char nextChar(){
if(ipos==ilen){
ipos=0;
ilen=fread(ibuf,1,BSZ,stdin);
if(!ilen)return EOF;
}
return ibuf[ipos++];
}
inline int readInt(){
int x=0,f=1;
char c=nextChar();
while(c!='-'&&(c<'0'||c>'9'))c=nextChar();
if(c=='-'){f=-1;c=nextChar();}
while(c>='0'&&c<='9'){x=x*10+(c-'0');c=nextChar();}
return x*f;
}
inline void writeInt(long long x,char endc){
if(x<0)x+=MOD;
char s[24];int n=0;
if(x==0)s[n++]='0';
else{
while(x){
s[n++]='0'+(x%10);
x/=10;
}
}
for(int i=n-1;i>=0;i--)obuf[opos++]=s[i];
obuf[opos++]=endc;
if(opos>BSZ-30){
fwrite(obuf,1,opos,stdout);
opos=0;
}
}
int main(){
int n=readInt();
int q=readInt();
for(int k=0;k<=MAXK;k++)P[k][0]=0;
for(int i=1;i<=n;i++){
long long ai=readInt();
ai%=MOD;
if(ai<0)ai+=MOD;
unsigned cur=(unsigned)ai;
for(int k=1;k<=MAXK;k++){
unsigned t=P[k][i-1]+cur;
if(t>=MOD)t-=MOD;
P[k][i]=t;
cur=(unsigned)((unsigned long long)cur*ai%MOD);
}
}
while(q--){
int op=readInt();
if(op==1){
int l=readInt(),r=readInt();
unsigned ans=P[1][r]+MOD-P[1][l-1];
if(ans>=MOD)ans-=MOD;
writeInt(ans,'\n');
}
else if(op==2){
int l=readInt(),r=readInt(),k=readInt();
unsigned ans=P[k][r]+MOD-P[k][l-1];
if(ans>=MOD)ans-=MOD;
writeInt(ans,'\n');
}
else{
int l=readInt(),r=readInt();
int m=r-l+1;
unsigned S1=P[1][r]+MOD-P[1][l-1];
if(S1>=MOD)S1-=MOD;
unsigned S2=P[2][r]+MOD-P[2][l-1];
if(S2>=MOD)S2-=MOD;
unsigned long long t1=(unsigned long long)m*S2%MOD;
unsigned long long t2=(unsigned long long)S1*S1%MOD;
long long ans=(long long)t1-(long long)t2;
ans=(ans%MOD+MOD)%MOD;
writeInt(ans,'\n');
}
}
if(opos)fwrite(obuf,1,opos,stdout);
return 0;
}