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\n",query(x,y,1,z)%mod);
88         }
89     }
90 }
				
时间: 2024-08-21 07:09:04

hdu4578的相关文章

HDU4578 线段树(区间更新 + 多种操作)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4578  , 线段树的区间更新 + 多种操作,好题. 虽然是比较裸的线段树,但是比较麻烦,并且有很多细节需要考虑,最后我7.3s很惊险地过了,求大神告知优化方法. 这道题坑在有三种询问:set , add , mul.所以lazy标记要有三个,如果三个标记同时出现的处理方法——当更新set操作时,就把add标记和mul标记全部取消:当更新mul操作时,如果当前节点add标记存在,就把add标记改为:a

hdu4578(线段树)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4578 题意:n个数,初始值为0,4种操作: 1.将某个区间所有值加上另一个值: 2.将区间所有值都乘上另一个值: 3.将区间所有值置为某个值: 4.查询区间中所有值的p次方和. 详细分析:http://www.cnblogs.com/GBRgbr/archive/2013/08/13/3254442.html #pragma comment(linker,"/STACK:102400000,1024

HDU4578 Transformation 线段树

这个题让我重新学习了加 乘 在区间的操作 题解:http://blog.csdn.net/guognib/article/details/25324025?utm_source=tuicool&utm_medium=referral 代码:也是参考上面的写的 注:确实有优先级,加只影响自己,乘会影响加,重新设定值会清零所有的标记 所以向下传的时候,乘和加的操作都晚于最后一次设定值,然后乘的操作要先于加 所以要先传递set 然后是mul 然后是add #include <stdio.h>

【日记】12.4

12.4日记 CDQ分治 HDU1541:给定一些(a,b),定义(a,b)的等级为满足(a2<=a&&b2<=b)的(a2,b2)的个数.输出等级为0-n-1的星星个数. 二维偏序裸题.第一位排好序,第二维树状数组即可. 注意:树状数组不可以处理下标为0的情况,因此需要先+1.或者就直接离散化.离散化的时候,注意int len=unique(a+1,a+n+1)-a-1; 线段树 HDU4578:区间加减+区间乘+区间修改+区间询问一次二次三次方和. 我tm哭了,debug了