zoukankan      html  css  js  c++  java
  • hdu 4578 Transformation(线段树)

    Problem Description

    Yuanfang is puzzled with the question below: 
    There are n integers, a1, a2, …, an. The initial values of them are 0. There are four kinds of operations.
    Operation 1: Add c to each number between ax and ay inclusive. In other words, do transformation ak<---ak+c, k = x,x+1,…,y.
    Operation 2: Multiply c to each number between ax and ay inclusive. In other words, do transformation ak<---ak×c, k = x,x+1,…,y.
    Operation 3: Change the numbers between ax and ay to c, inclusive. In other words, do transformation ak<---c, k = x,x+1,…,y.
    Operation 4: Get the sum of p power among the numbers between ax and ay inclusive. In other words, get the result of axp+ax+1p+…+ay p.
    Yuanfang has no idea of how to do it. So he wants to ask you to help him. 
     

    Input

    There are no more than 10 test cases.
    For each case, the first line contains two numbers n and m, meaning that there are n integers and m operations. 1 <= n, m <= 100,000.
    Each the following m lines contains an operation. Operation 1 to 3 is in this format: "1 x y c" or "2 x y c" or "3 x y c". Operation 4 is in this format: "4 x y p". (1 <= x <= y <= n, 1 <= c <= 10,000, 1 <= p <= 3)
    The input ends with 0 0.
     

    Output

    For each operation 4, output a single integer in one line representing the result. The answer may be quite large. You just need to calculate the remainder of the answer when divided by 10007.
     

    Sample Input

    5 5
    3 3 5 7
    1 2 4 4
    4 1 5 2
    2 2 5 8
    4 3 5 3
    0 0
     

    Sample Output

    307 7489

    思路:

    题目要实现多个区间更新以及一种区间查询 我们对于次方我们可以维护三棵线段树 注意lazy标记的相互影响 以及跟新的顺序

    #include <bits/stdc++.h>
    using namespace std;
    const double pi = acos(-1.0);
    const int N = 1e5+7;
    const int inf = 0x3f3f3f3f;
    const double eps = 1e-6;
    typedef long long ll;
    const ll mod = 10007;
    struct tree{
        int l,r;
        ll add,mul,cha;
        ll sum1,sum2,sum3;
    }t[N<<2];
    void pushup(int p){
        t[p].sum1=(t[p<<1].sum1+t[p<<1|1].sum1)%mod;
        t[p].sum2=(t[p<<1].sum2+t[p<<1|1].sum2)%mod;
        t[p].sum3=(t[p<<1].sum3+t[p<<1|1].sum3)%mod;
    }
    void build(int p,int l,int r){
        t[p].sum1=t[p].sum2=t[p].sum3=t[p].add=t[p].cha=0;
        t[p].l=l; t[p].r=r;
        t[p].mul=1;
        if(l==r){
            return ;
        }
        int mid=(l+r)>>1;
        build(p<<1,l,mid);
        build(p<<1|1,mid+1,r);
    }
    void pushdown(int p,int l,int r){
        int mid=(l+r)>>1;
        if(t[p].cha){
            t[p<<1].cha=t[p].cha;
            t[p<<1].sum1=(mid-l+1)*t[p].cha%mod;
            t[p<<1].sum2=t[p<<1].sum1*t[p].cha%mod;
            t[p<<1].sum3=t[p<<1].sum2*t[p].cha%mod;
            t[p<<1|1].cha=t[p].cha;
            t[p<<1|1].sum1=(r-mid)*t[p].cha%mod;
            t[p<<1|1].sum2=t[p<<1|1].sum1*t[p].cha%mod;
            t[p<<1|1].sum3=t[p<<1|1].sum2*t[p].cha%mod;
            t[p<<1].add=t[p<<1|1].add=0;
            t[p<<1].mul=t[p<<1|1].mul=1;
            t[p].cha=0;
        }
        if(t[p].mul!=1){
            t[p<<1].mul=t[p<<1].mul*t[p].mul%mod;
            t[p<<1|1].mul=t[p<<1|1].mul*t[p].mul%mod;
            t[p<<1].sum3=t[p<<1].sum3*t[p].mul%mod*t[p].mul%mod*t[p].mul%mod;
            t[p<<1].sum2=t[p<<1].sum2*t[p].mul%mod*t[p].mul%mod;
            t[p<<1].sum1=t[p<<1].sum1*t[p].mul%mod;
            t[p<<1|1].sum3=t[p<<1|1].sum3*t[p].mul%mod*t[p].mul%mod*t[p].mul%mod;
            t[p<<1|1].sum2=t[p<<1|1].sum2*t[p].mul%mod*t[p].mul%mod;
            t[p<<1|1].sum1=t[p<<1|1].sum1*t[p].mul%mod;
            t[p<<1].add=t[p<<1].add*t[p].mul%mod;
            t[p<<1|1].add=t[p<<1|1].add*t[p].mul%mod;
            t[p].mul=1;
        }
        if(t[p].add){
            t[p<<1].add=(t[p<<1].add+t[p].add)%mod;
            t[p<<1|1].add=(t[p<<1|1].add+t[p].add)%mod;
            ll c=t[p].add*t[p].add%mod*t[p].add%mod;
            t[p<<1].sum3=(t[p<<1].sum3+(mid-l+1)*c%mod+3*t[p].add*(t[p<<1].sum1*t[p].add%mod+t[p<<1].sum2)%mod)%mod;
            t[p<<1|1].sum3=(t[p<<1|1].sum3+(r-mid)*c%mod+3*t[p].add*(t[p<<1|1].sum1*t[p].add%mod+t[p<<1|1].sum2)%mod)%mod;
            t[p<<1].sum2=(t[p<<1].sum2+2*t[p<<1].sum1*t[p].add%mod+t[p].add*t[p].add%mod*(mid-l+1)%mod)%mod;
            t[p<<1|1].sum2=(t[p<<1|1].sum2+2*t[p<<1|1].sum1*t[p].add%mod+t[p].add*t[p].add%mod*(r-mid)%mod)%mod;
            t[p<<1].sum1=(t[p<<1].sum1+(mid-l+1)*t[p].add)%mod;
            t[p<<1|1].sum1=(t[p<<1|1].sum1+(r-mid)*t[p].add)%mod;
            t[p].add=0;
        }    
    }
    void update(int p,int l,int r,ll v,int op){
        //cout<<t[p].l<<" "<<t[p].r<<endl;
        if(l<=t[p].l&&t[p].r<=r){
            if(op==3){
                t[p].cha=v;
                t[p].add=0;
                t[p].mul=1;
                t[p].sum1=(t[p].r-t[p].l+1)*v%mod;
                t[p].sum2=t[p].sum1*v%mod;
                t[p].sum3=t[p].sum2*v%mod;
            }else if(op==2){
                t[p].mul=t[p].mul*v%mod;
                t[p].add=t[p].add*v%mod;
                t[p].sum1=t[p].sum1*v%mod;
                t[p].sum2=t[p].sum2*v%mod*v%mod;
                t[p].sum3=t[p].sum3*v%mod*v%mod*v%mod;
            }else{
                t[p].add=(t[p].add+v)%mod;
                t[p].sum3=(t[p].sum3+v*v%mod*v%mod*(t[p].r-t[p].l+1)%mod+3*v*(t[p].sum1*v+t[p].sum2)%mod)%mod;
                t[p].sum2=(t[p].sum2+v*v%mod*(t[p].r-t[p].l+1)%mod+2*v*t[p].sum1%mod)%mod;
                t[p].sum1=(t[p].sum1+v*(t[p].r-t[p].l+1))%mod;    
            }
            return ;
        }
        pushdown(p,t[p].l,t[p].r);
        int mid=(t[p].l+t[p].r)>>1;
        if(l<=mid) update(p<<1,l,r,v,op);
        if(r>mid) update(p<<1|1,l,r,v,op);
        pushup(p);
    }
    ll query(int p,int l,int r,int op){
    //    cout<<p<<" "<<t[p].l<<" "<<t[p].r<<endl;
        if(l<=t[p].l&&t[p].r<=r){
            if(op==1){
                return t[p].sum1;
            }else if(op==2){
                return t[p].sum2;
            }else{
                return t[p].sum3;
            }
        }
        pushdown(p,t[p].l,t[p].r);
        int mid=(t[p].l+t[p].r)>>1;
        ll res=0;
        if(l<=mid) res=(res+query(p<<1,l,r,op))%mod;
        if(r>mid) res=(res+query(p<<1|1,l,r,op))%mod;
        return res; 
    }
    int main(){
    //    ios::sync_with_stdio(false);
    //    cin.tie(0); cout.tie(0);
        int n,m;
        while(~scanf("%d%d",&n,&m)){
            if(!n&&!m) break;
            build(1,1,n);
            for(int i=1;i<=m;i++){
                int op,x,y,c;
                scanf("%d%d%d%d",&op,&x,&y,&c);
                if(op==4){
                    printf("%lld
    ",query(1,x,y,c));
                }else{
                    update(1,x,y,c,op);
                }
            }
        }
    }
    View Code
  • 相关阅读:
    java 和.net 开发平台的感受(菜鸟级)
    结构体应用统计学生成绩
    实验十 链表
    绘制抛物线(带比例缩放)
    上下三角矩阵的输出
    结构体应用分类与索引
    笔试题之数据库
    动态规划求回文给定字符串,插入字符形成回文
    三行九个点,用4条线段连接(扩展,用3条,用1条)
    名言记录
  • 原文地址:https://www.cnblogs.com/wmj6/p/11436209.html
Copyright © 2011-2022 走看看