For each item of the polynomial we can build a node to record its coefficients. Then the problem becomes a bare template problem.
The evaluation operation is directly O (n) engaged.
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <
Cmath> using namespace std;
#define MAXN 1000000+100 #define MAXM 100000+100 #define INF 0x3f3f3f3f #define MOD 20130426 typedef long Long Sint;
inline int getint () {int Res=0,f=1;char C; while (C=getchar (),c< ' 0 ' | |
C> ' 9 ') {if (c== '-') f=-1;}
res=c-' 0 ';
while (C=getchar (), c>= ' 0 ' &&c<= ' 9 ') res=res*10+c-' 0 ';
return res*f;
} int tr[maxn][2],fa[maxn],root,n,m;
Sint V[maxn],t[maxn][2],ans,bas;
int siz[maxn],tot,t1,t2;
inline void pushup (int rt) {siz[rt]=siz[tr[rt][0]]+siz[tr[rt][1]]+1;} inline void update (int x,sint Xx,sint yy) {
if (!x) return;
v[x]= (v[x]*yy+xx)%mod;
((T[X][0]*=YY) +=xx)%=mod;
(T[X][1]*=YY)%=mod;
} inline void pushdown (int x) {if (!x) return;
if (!t[x][0]&&t[x][1]==1) return;
Update (tr[x][0],t[x][0],t[x][1]);
Update (tr[x][1],t[x][0],t[x][1]);
T[x][0]=0;t[x][1]=1; } inline void RotaTe (int x,int &rt) {int l,r,z,y;
Y=fa[x];z=fa[y];
if (x==tr[y][0]) l=0;
else l=1;r=1^l;
if (rt==y) rt=x;
else {if (tr[z][0]==y) tr[z][0]=x;
else tr[z][1]=x;
} fa[x]=z;fa[y]=x;fa[tr[x][r]]=y;
Tr[y][l]=tr[x][r];tr[x][r]=y;
Pushup (y);p ushup (x);
} inline void Build (int l,int r,int f) {if (l>r) return;
int mid= (L+R) >>1;
fa[mid]=f;tr[f][mid>f]=mid;
Siz[mid]=1;t[mid][0]=0;t[mid][1]=1;
if (l==r) return;
Build (L,mid-1,mid); build (Mid+1,r,mid);
Pushup (mid);
} inline void splay (int x,int &rt) {int y,z;
while (X!=RT) {y=fa[x];z=fa[y];
if (Y!=RT) {if ((tr[y][0]==x) ^ (tr[z][0]==y)) rotate (X,RT);
else rotate (Y,RT);
} rotate (X,RT);
}} int Select (int now,int rk) {pushdown (now);
int l=tr[now][0],r=tr[now][1];
int rak=siz[l]+1;
if (RAK==RK) return now;
if (RAK>RK) return select (L,RK); else return selECT (R,rk-rak);
} inline void Calc (int x) {if (!x) return;
Pushdown (x);
Calc (tr[x][1]);
if (x!=1) ans= (ans*bas+v[x])%mod;
Calc (tr[x][0]);
} inline void Split (int l,int R) {t1=select (root,l); T2=select (ROOT,R);
Splay (T1,root); splay (t2,tr[t1][1]);
} int main () {build (1,maxm,0);
tot=maxm;root= (1+MAXM) >>1;
int Q=getint (); Char CH[MAXN];
while (q--) {scanf ("%s", ch);
if (ch[0]== ' Q ') {ans=0;bas=getint ()%mod;
Calc (root);
cout<<ans<<endl;
} else {int x=getint () +1,y=getint () +1;
if (ch[3]== ' x ') {split (y,y+2);
Pushdown (T1), pushdown (T2);
V[t2]+=v[tr[t2][0]];
fa[tr[t2][0]]=tr[t2][0]=0;
Pushup (T2);p ushup (T1);
Split (x,x+1);
Tr[t2][0]=++tot;siz[tot]=1; V[tot]=0;fa[tot]=t2;t[tot][1]=1;
Pushup (T2);p ushup (T1);
} else if (ch[0]== ' m ') {split (x,y+2);
Sint Xx=0,yy=getint ()%mod;
Update (TR[T2][0],XX,YY);
Pushup (T2);p ushup (T1);
} else {split (x,y+2);
int xx=getint ()%mod,yy=1;
Update (TR[T2][0],XX,YY);
Pushup (T2);p ushup (T1);
}}} return 0; }