zoukankan      html  css  js  c++  java
  • hdu4578 线段树 三次方,二次方,一次方的值

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

    InputThere 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. 
    OutputFor 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

    题意:就是三种操作,一种加上一个数,一种乘上一个数,一种改为一个数,然后就是求最终一段区间每个数的几次方,相加。
    题解:就是有一个基础值,然后就是存储其三次方,二次方,一次方的值,这样便于最后的输出,这个是十分十分复杂的。
    如果在赛场上需要保持十分良好的心态才可以AC,三次方,二次方,一次方的更新,不是特别难,但是需要十分细心,以及
    良好的耐心。
     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cmath>
     5 #define lson(x) x*2
     6 #define rson(x) x*2+1
     7 using namespace std;
     8 const int MAXN=200007;
     9 const int mod=10007;
    10 typedef long long LL;
    11 int n,m,x,y,z,num;
    12 struct hh{
    13     int l,r,mid,add,mul,zhi[3];
    14 }tree[MAXN<<2];
    15 void push_up(int p)
    16 {
    17     for (int i=0;i<3;i++)
    18         tree[p].zhi[i]=(tree[lson(p)].zhi[i]+tree[rson(p)].zhi[i])%mod;
    19 }
    20 void finish_work(int p,int mul,int add)
    21 {
    22     int len=tree[p].r-tree[p].l+1;
    23     tree[p].zhi[0]=tree[p].zhi[0]*mul%mod;
    24     tree[p].zhi[1]=tree[p].zhi[1]*mul%mod*mul%mod;
    25     tree[p].zhi[2]=tree[p].zhi[2]*mul%mod*mul%mod*mul%mod;
    26     
    27     tree[p].mul=tree[p].mul*mul%mod;
    28     tree[p].add=(tree[p].add*mul%mod+add)%mod;
    29     
    30     tree[p].zhi[2]=(tree[p].zhi[2]+3*add%mod*add%mod*tree[p].zhi[0]%mod)%mod;
    31     tree[p].zhi[2]=(tree[p].zhi[2]+3*add%mod*tree[p].zhi[1]%mod)%mod;
    32     tree[p].zhi[2]=(tree[p].zhi[2]+len*add%mod*add%mod*add%mod)%mod;
    33     tree[p].zhi[1]=(tree[p].zhi[1]+2*add%mod*tree[p].zhi[0]%mod)%mod;
    34     tree[p].zhi[1]=(tree[p].zhi[1]+len*add%mod*add%mod)%mod;
    35     tree[p].zhi[0]=(tree[p].zhi[0]+len*add%mod)%mod;
    36 }
    37 void push_down(int p)
    38 {
    39     if (tree[p].l==tree[p].r) return;
    40     finish_work(lson(p),tree[p].mul,tree[p].add);
    41     finish_work(rson(p),tree[p].mul,tree[p].add);
    42     tree[p].mul=1,tree[p].add=0;
    43 }
    44 void build(int l,int r,int p)
    45 {
    46     tree[p].l=l,tree[p].r=r,tree[p].mid=(l+r)>>1,tree[p].mul=1;
    47     tree[p].add=tree[p].zhi[1]=tree[p].zhi[2]=tree[p].zhi[0]=0;
    48     if (l==r) return;
    49     build(l,tree[p].mid,lson(p));
    50     build(tree[p].mid+1,r,rson(p));
    51 }
    52 void change(int l,int r,int p,int mul,int add)
    53 {
    54     //cout<<l<<" "<<r<<" "<<p<<endl;
    55     if (l<=tree[p].l&&r>=tree[p].r) finish_work(p,mul,add);
    56     else {
    57         push_down(p);
    58         if (r<=tree[p].mid) change(l,r,lson(p),mul,add);
    59         else if (l>tree[p].mid) change(l,r,rson(p),mul,add);
    60              else{
    61                  change(l,tree[p].mid,lson(p),mul,add);
    62                  change(tree[p].mid+1,r,rson(p),mul,add);
    63              }
    64         push_up(p);     
    65     }
    66 }
    67 int query(int l,int r,int p,int num)
    68 {
    69     //cout<<l<<" "<<r<<" "<<p<<endl;
    70     if (l<=tree[p].l&&r>=tree[p].r) return tree[p].zhi[num-1];
    71     push_down(p);
    72     if (r<=tree[p].mid) return query(l,r,lson(p),num);
    73     else if (l>tree[p].mid) return query(l,r,rson(p),num);
    74     else return (query(l,tree[p].mid,lson(p),num)+query(tree[p].mid+1,r,rson(p),num))%mod; 
    75 }
    76 int main()
    77 {
    78     while (scanf("%d%d",&n,&m)&&(n+m))
    79     {
    80         build(1,n,1);
    81         for (int i=1;i<=m;i++)
    82         {
    83             scanf("%d%d%d%d",&num,&x,&y,&z);
    84             if (num==1) change(x,y,1,1,z);
    85             else if (num==2) change(x,y,1,z,0);
    86             else if (num==3) change(x,y,1,0,z);
    87             else printf("%d
    ",query(x,y,1,z)%mod);
    88         }
    89     }
    90 }


  • 相关阅读:
    如何学习区块链
    用Python从零开始创建区块链
    区块链入门
    什么是区块链
    localStorage使用总结
    整理vue学习笔记
    SCSS 教程
    vue — 创建vue项目
    软件开发的常见文档
    史上最全的CSS hack方式一览(转)
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/7535839.html
Copyright © 2011-2022 走看看