3323: [Scoi2013]多项式的运算

Description

某天,mzry1992 一边思考着一个项目问题一边在高速公路上骑着摩托车。一个光头踢了他一脚,摩托车损坏,而他也被送进校医院打吊针。现在该项目的截止日期将近,他不得不请你来帮助他完成这个项目。该项目的目的是维护一个动态的关于x 的无穷多项式F(x) = a0 * x^0 + a1 * x^1 + a2 * x^2 + ... ,这个多项式初始时对于所有i有ai = 0。
操作者可以进行四种操作:
1. 将x^L 到x^R 这些项的系数乘上某个定值v
2. 将x^L 到x^R 这些项的系数加上某个定值v
 
3. 将x^L 到x^R 这些项乘上x变量
4. 将某个定值v代入多项式F(x),并输出代入后多项式的值,之后多项式还原为代入前的状况
经过观察,项目组发现使用者的操作集中在前三种,第四种操作不会出现超过10次。mzry1992 负责这个项目的核心代码,你能帮他实现么。

Input

输入的第一行有一个整数n 代表操作的个数。
接下来n 行,每行一个操作,格式如下:
mul L R v 代表第一种操作
add L R v 代表第二种操作
mulx L R 代表第三种操作
query v 代表第四种操作

对于30% 的数据:N <= 5000,0 <= L <= R <= 5000,0 <= v <= 10^9
另有20% 的数据:N <= 10^5,0 <= L <= R <= 10^5,0 <= v <= 10^9,没有mulx 操作
剩下的50% 的数据:N <= 10^5,0 <= L <= R <= 10^5,0 <= v <= 10^9

Output

对于每个query 操作,输出对应的答案,结果可能较大,需要模上20130426。

Sample Input

6
add 0 1 7
query 1
mul 0 1 7
query 2
mulx 0 1
query 3

Sample Output

14
147
588
Hint
操作一之后,多项式为F(x) = 7x + 7。
操作三之后,多项式为F(x) = 49x + 49。
操作五之后,多项式为F(x) = 49x^2 + 49x。
 
 
操作1,2很简单,splay,线段树都可以完成,操作4只有10个,暴力就好了,现在来看操作三,实际上就是把一段区间向后移一位,那么直接用splay维护,
在l后面加一个0节点,然后把r和r+1合并即可。。。
  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<cmath>
  4 #include<cstring>
  5 #include<algorithm>
  6 #include<iostream>
  7 #include<vector>
  8 #include<map>
  9 #include<set>
 10 #include<queue>
 11 #include<string>
 12 #define inf 1000000000
 13 #define maxn 1000000+100
 14 #define maxm 100000+100
 15 #define eps 1e-10
 16 #define ll long long
 17 #define pa pair<int,int>
 18 #define for0(i,n) for(int i=0;i<=(n);i++)
 19 #define for1(i,n) for(int i=1;i<=(n);i++)
 20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
 21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
 22 #define mod 20130426
 23 using namespace std;
 24 inline int read()
 25 {
 26     int x=0,f=1;char ch=getchar();
 27     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 28     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
 29     return x*f;
 30 }
 31 int q,rt,t1,t2,tot,s[maxn],c[maxn][2],fa[maxn];
 32 ll v[maxn],t[maxn][2],ans,base;
 33 inline void pushup(int x)
 34 {
 35  s[x]=s[c[x][0]]+s[c[x][1]]+1;
 36 }
 37 inline void rotate(int x,int &k)
 38 {
 39  int y=fa[x],z=fa[y],l=c[y][1]==x,r=l^1;
 40  if(y!=k)c[z][c[z][1]==y]=x;else k=x;
 41  fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
 42  c[y][l]=c[x][r];c[x][r]=y;
 43  pushup(y);pushup(x);
 44 }
 45 inline void splay(int x,int &k)
 46 {
 47  while(x!=k)
 48  {
 49   int y=fa[x],z=fa[y];
 50   if(y!=k)
 51   {
 52    if(c[z][0]==y^c[y][0]==x)rotate(x,k);else rotate(y,k);
 53   }
 54   rotate(x,k);
 55  }
 56 }
 57 inline void update(int x,ll xx,ll yy)
 58 {
 59  if(!x)return;
 60  (v[x]=v[x]*yy+xx)%=mod;
 61  ((t[x][0]*=yy)+=xx)%=mod;
 62  (t[x][1]*=yy)%=mod;
 63 }
 64 inline void pushdown(int x)
 65 {
 66  if(!x)return;
 67  if(!t[x][0]&&t[x][1]==1)return;
 68  update(c[x][0],t[x][0],t[x][1]);
 69  update(c[x][1],t[x][0],t[x][1]);
 70  t[x][0]=0;t[x][1]=1;
 71 }
 72 inline int find(int x,int k)
 73 {
 74  pushdown(x);
 75  int l=c[x][0],r=c[x][1];
 76  if(s[l]+1==k)return x;
 77  else if(s[l]>=k)return find(l,k);
 78  else return find(r,k-s[l]-1);
 79 }
 80 inline void split(int l,int r)
 81 {
 82  t1=find(rt,l);t2=find(rt,r);
 83  splay(t1,rt);splay(t2,c[t1][1]);
 84 }
 85 inline void calc(int x)
 86 {
 87  if(!x)return;
 88  pushdown(x);
 89  calc(c[x][1]);
 90  if(x!=1)ans=(ans*base+v[x])%mod;
 91  calc(c[x][0]);
 92 }
 93 inline void build(int l,int r,int f)
 94 {
 95  if(l>r)return;
 96  int x=(l+r)>>1;
 97  fa[x]=f;c[f][x>f]=x;
 98  s[x]=1;t[x][0]=0;t[x][1]=1;
 99  if(l==r)return;
100  build(l,x-1,x);build(x+1,r,x);
101  pushup(x);
102 } 
103 int main()
104 {
105     freopen("input.txt","r",stdin);
106     freopen("output.txt","w",stdout);
107     build(1,maxm,0);tot=maxm;rt=(1+maxm)>>1;
108     q=read();char ch[maxn];
109     while(q--)
110     {
111      scanf("%s",ch);
112         if(ch[0]=='q')
113         {
114          ans=0;base=read()%mod;
115          calc(rt);
116             cout<<ans<<endl;
117         }
118         else 
119         {
120          int x=read()+1,y=read()+1;
121          if(ch[3]=='x')
122          {
123           split(y,y+3);
124           int z=c[t2][0],zz=c[z][0]+c[z][1];
125           pushdown(t1);pushdown(t2);pushdown(z);
126                 v[z]+=v[zz];s[z]=1;
127                 fa[zz]=c[z][0]=c[z][1]=0;
128                 pushup(t2);pushup(t1);
129                 split(x,x+1);
130                 c[t2][0]=++tot;s[tot]=1;v[tot]=0;fa[tot]=t2;t[tot][1]=1;
131                 pushup(t2);pushup(t1);
132          }
133          else if(ch[0]=='m')
134          {
135           split(x,y+2);
136           ll xx=0,yy=read()%mod;
137           update(c[t2][0],xx,yy);
138           pushup(t2);pushup(t1);
139          }
140          else 
141          {
142           split(x,y+2);
143           ll xx=read()%mod,yy=1;
144           update(c[t2][0],xx,yy);
145           pushup(t2);pushup(t1);
146          }
147         }
148  }
149     return 0;
150 }
View Code

 

posted @ 2016-07-09 22:19  HTWX  阅读(298)  评论(0编辑  收藏  举报