bzoj3323 the operation of "scoi2013" polynomial

Source: Internet
Author: User
Tags mul split time limit

3323: operation of [Scoi2013] polynomialTime Limit:12 Sec Memory limit:64 MB
submit:232 solved:69
[Submit] [Status] [Discuss]DescriptionOne day, mzry1992 was thinking about a project and riding a motorbike on the freeway. A bald head kicked him in the leg, the motorcycle was damaged, and he was sent to the school hospital to play the needle. Now that the deadline for the project is near, he has to ask you to help him finish the project. The purpose of the project is to maintain a dynamic infinite polynomial f (x) = a0 * x^0 + A1 * x^1 + A2 * x^2 + ..., this polynomial initially for all I have AI = 0.  The operator can perform four operations: 1.  Multiply the coefficients of these items x^l to x^r by a certain value of V 2.  Add the coefficients of these items x^l to X^r plus a certain value of V 3.  Multiply the x^l to x^r by the X variable 4. Put a certain value V into the polynomial f (x), and output the value of the post-generation polynomial, then the polynomial reverts to the pre-generation condition after observation, the project group found that the user's operation is concentrated in the first three kinds, the fourth operation will not appear more than 10 times. MZRY1992 is responsible for the core code of this project, can you help him to achieve it?InputThe first line of input has an integer n representing the number of operations.
Next n rows, one action per line, in the following format:
Mul L R v represents the first operation
The Add L R v represents the second type of operation
Mulx L R represents a third operation
Query V represents the fourth operation

For 30% data: N <= 5000,0 <= L <= R <= 5000,0 <= v <= 10^9
20% Additional data: N <= 10^5,0 <= L <= R <= 10^5,0 <= v <= 10^9, no Mulx operation
Remaining 50% data: N <= 10^5,0 <= L <= R <= 10^5,0 <= v <= 10^9OutputFor each query operation, the corresponding answer, the result may be large, need to modulo 20130426.Sample Input6
Add 0 1 7
Query 1
Mul 0 1 7
Query 2
Mulx 0 1
Query 3
Sample Output14
147
588
Hint
After operation one, the polynomial is f (x) = 7x + 7.
After operation three, the polynomial is f (x) = 49x + 49.
After operation five, the polynomial is f (x) = 49x^2 + 49x.HINT





Should the uploader request, this series of questions are not disclosed, if there are objections, this site will be deleted.















It is also a question of splay maintenance intervals.



First to second operation: Splau maintenance interval plus or multiply a number, only need to hit a mark on it.



The third operation: first a[r] and a[r+1] merge, and then insert a number 0 before a[l], where you use the interval to insert, delete, or modify a number.



Fourth operation: Because the number of occurrences is up to 10 times, it can be solved by brute force.



Note: to pushdown from top to bottom before each operation, proceed from bottom to top pushup. (Similar line tree)











#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define LL long long
#define pa pair<int,int>
#define MAXN 200005
#define MAXM 100005
#define INF 1000000000
#define mod 20130426
using namespace std;
int rt,n,m,tot,t1,t2,c[MAXN][2],fa[MAXN];
LL v[MAXN],s[MAXN],t[MAXN][2],ans,base;
int read()
{
    int ret=0,flag=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') flag=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();}
    return ret*flag;
}
inline void pushup(int k)
{
    s[k]=s[c[k][0]]+s[c[k][1]]+1;
}
inline void rotate(int x,int &k)
{
    int y=fa[x],z=fa[y],l=(c[y][1]==x),r=l^1;
    if (y!=k) c[z][c[z][1]==y]=x;else k=x;
    fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
    c[y][l]=c[x][r];c[x][r]=y;
    pushup(y);pushup(x);
}
inline void splay(int x,int &k)
{
    while (x!=k)
    {
        int y=fa[x],z=fa[y];
        if (y!=k){if(c[y][0]==x^c[z][0]==y)rotate(x,k);else rotate(y,k);}
        rotate(x,k);
    }
}
inline void update(int k,LL x,LL y)
{
    if (!k) return;
    v[k]=(v[k]*y+x)%mod;
    t[k][0]=(t[k][0]*y+x)%mod;
    t[k][1]=(t[k][1]*y)%mod;
}
inline void pushdown(int k)
{
    if (!k) return;
    if (!t[k][0]&&t[k][1]==1) return;
    update(c[k][0],t[k][0],t[k][1]);
    update(c[k][1],t[k][0],t[k][1]);
    t[k][0]=0;t[k][1]=1;
}
inline int find(int k,int rank)
{
    pushdown(k);
    if (s[c[k][0]]+1==rank) return k;
    else if (s[c[k][0]]>=rank) return find(c[k][0],rank);
    else return find(c[k][1],rank-s[c[k][0]]-1);
}
inline void split(int l,int r)
{
    t1=find(rt,l);t2=find(rt,r);
    splay(t1,rt);splay(t2,c[rt][1]);
}
inline void getans(int k)
{
    if (!k) return;
    pushdown(k);
    getans(c[k][1]);
    if (k!=1) ans=(ans*base+v[k])%mod;
    getans(c[k][0]);
}
inline void build(int l,int r,int f)
{
    if (l>r) return;
    int mid=(l+r)>>1;
    fa[mid]=f;c[f][mid>f]=mid;
    s[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 solvemul()
{
    int x=read()+1,y=read()+1;
    split(x,y+2);
    LL ad=0,mu=read()%mod;
    update(c[t2][0],ad,mu);
    pushup(t2);pushup(t1);
}
inline void solveadd()
{
    int x=read()+1,y=read()+1;
    split(x,y+2);
    LL ad=read()%mod,mu=1;
    update(c[t2][0],ad,mu);
    pushup(t2);pushup(t1);
}
inline void solvemulx()
{
    int x=read()+1,y=read()+1;
    split(y,y+3);
    int z=c[t2][0],zz=c[z][0]+c[z][1];
    pushdown(t1);pushdown(t2);pushdown(z);
    v[z]=(v[z]+v[zz])%mod;s[z]=1;
    fa[zz]=c[z][0]=c[z][1]=0;
    pushup(t2);pushup(t1);
    split(x,x+1);
    c[t2][0]=++tot;s[tot]=1;fa[tot]=t2;v[tot]=t[tot][0]=0;t[tot][1]=1;
    pushup(t2);pushup(t1);
}
inline void query()
{
    ans=0;base=read()%mod;
    getans(rt);
    printf("%lld\n",ans);
}
int main()
{
    build(1,MAXM,0);tot=MAXM;rt=(1+MAXM)>>1;
    m=read();char ch[10];
    while(m--)
    {
        scanf("%s",ch);
        if (strlen(ch)==3)
        {
            if (ch[0]=='m') solvemul();
            else solveadd();
        }
        else if (ch[3]=='x') solvemulx();
        else query();
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.