hdu 4578 Transformation(线段树+多种操作)
来源:互联网 发布:数位板绘画软件 编辑:程序博客网 时间:2024/04/30 17:32
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
思路:
这道题有三种询问: add(加) , mul(乘),same(赋值)。所以lazy标记要有三个,
如果三个标记同时出现的处理方法——当更新same操作时,就把add标记和mul标记全部取消;
当更新mul操作时,如果当前节点add标记存在,就把add标记改为:add * mul。
这样的话就可以在PushDown()操作中先执行same,然后mul,最后add。
麻烦的是有三种询问:和 , 平方和 , 立方和。对于same和mul操作来说,这三种询问都比较好弄。
对于add操作,和的话就比较好弄,按照正常方法就可以;
平方和这样来推:
立方和这样推:
代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define LL(x) (x<<1)#define RR(x) (x<<1|1)const int maxn=1e5+10;const int mod=10007;struct Segtree{ int le,ri,add,mul,same,sum[4]; int mid() { return (le+ri)>>1; }} tree[maxn<<2];int len,tmp1,tmp2,tmp3,sum1,sum2,sum3;int n,m;void Build(int rt,int le,int ri){ tree[rt].le=le,tree[rt].ri=ri; tree[rt].add=tree[rt].mul=tree[rt].same=0; tree[rt].sum[1]=tree[rt].sum[2]=tree[rt].sum[3]=0; if(le==ri) return ; int mid=tree[rt].mid(); Build(LL(rt),le,mid); Build(RR(rt),mid+1,ri);}void fun_add(int rt,int num){ tree[rt].add=(tree[rt].add+num)%mod; sum1=tree[rt].sum[1],sum2=tree[rt].sum[2],sum3=tree[rt].sum[3]; len=tree[rt].ri-tree[rt].le+1; tmp1=(num*len)%mod; tree[rt].sum[1]=(sum1+tmp1)%mod; tmp2=(num*num)%mod; tree[rt].sum[2]=(sum2+(2*sum1*num)%mod+(tmp2*len)%mod)%mod;//(a+num)^2*len=a^2*len+2*a*num*len+num*num*len tmp3=(tmp2*num)%mod; tree[rt].sum[3]=(sum3+(3*sum2*num)%mod+(3*sum1*tmp2)%mod+(tmp3*len)%mod)%mod;//同理}void fun_mul(int rt,int num){ if(tree[rt].mul) tree[rt].mul=(tree[rt].mul*num)%mod; else tree[rt].mul=num; tree[rt].add=(tree[rt].add*num)%mod; sum1=tree[rt].sum[1],sum2=tree[rt].sum[2],sum3=tree[rt].sum[3]; tmp1=num; tree[rt].sum[1]=(sum1*tmp1)%mod; tmp2=(tmp1*num)%mod; tree[rt].sum[2]=(sum2*tmp2)%mod; tmp3=(tmp2*num)%mod; tree[rt].sum[3]=(sum3*tmp3)%mod;}void fun_same(int rt,int num){ tree[rt].same=num; tree[rt].add=tree[rt].mul=0; len=tree[rt].ri-tree[rt].le+1; tmp1=num; tree[rt].sum[1]=(tmp1*len)%mod; tmp2=(tmp1*num)%mod; tree[rt].sum[2]=(tmp2*len)%mod; tmp3=(tmp2*num)%mod; tree[rt].sum[3]=(tmp3*len)%mod;}void Pushdown(int rt){ if(tree[rt].same) { fun_same(LL(rt),tree[rt].same); fun_same(RR(rt),tree[rt].same); tree[rt].same=0; } if(tree[rt].mul) { fun_mul(LL(rt),tree[rt].mul); fun_mul(RR(rt),tree[rt].mul); tree[rt].mul=0; } if(tree[rt].add) { fun_add(LL(rt),tree[rt].add); fun_add(RR(rt),tree[rt].add); tree[rt].add=0; }}void Pushup(int rt){ tree[rt].sum[1]=(tree[LL(rt)].sum[1]+tree[RR(rt)].sum[1])%mod; tree[rt].sum[2]=(tree[LL(rt)].sum[2]+tree[RR(rt)].sum[2])%mod; tree[rt].sum[3]=(tree[LL(rt)].sum[3]+tree[RR(rt)].sum[3])%mod;}void Update(int rt,int le,int ri,int num,int op){ if(le<=tree[rt].le&&tree[rt].ri<=ri) { if(op==1) fun_add(rt,num); else if(op==2) fun_mul(rt,num); else fun_same(rt,num); return ; } Pushdown(rt); int mid=tree[rt].mid(); if(le<=mid) Update(LL(rt),le,ri,num,op); if(ri>mid) Update(RR(rt),le,ri,num,op); Pushup(rt);}int Query(int rt,int le,int ri,int p){ if(le<=tree[rt].le&&tree[rt].ri<=ri) return tree[rt].sum[p]; Pushdown(rt); int mid=tree[rt].mid(),ans=0; if(le<=mid) ans=(ans+Query(LL(rt),le,ri,p))%mod; if(ri>mid) ans=(ans+Query(RR(rt),le,ri,p))%mod; Pushup(rt); return ans;}int main(){ while(scanf("%d%d",&n,&m),n||m) { Build(1,1,n); int op,x,y,c; while(m--) { scanf("%d%d%d%d",&op,&x,&y,&c); if(op<=3) Update(1,x,y,c,op); else printf("%d\n",Query(1,x,y,c)); } } return 0;}
- hdu 4578 Transformation(线段树+多种操作)
- hdu 4578 Transformation(线段树)
- HDU 4578-Transformation(线段树)
- HDU - 4578 -Transformation(线段树)
- HDU 4578 Transformation (线段树)
- hdu 4578 Transformation 线段树
- HDU 4578 Transformation (线段树)
- hdu-4578-Transformation-线段树
- HDU 4578 Transformation 线段树
- HDU 4578 Transformation[线段树]
- hdu 4578 Transformation (线段树)
- hdu 4578 Transformation 线段树
- hdu 4578 Transformation 线段树
- HDU 4578 Transformation 线段树
- hdu 4578 Transformation(线段树中级,区间和加强)
- hdu 4578 Transformation(线段树区间更新)
- hdu 4578 Transformation(区间线段树)
- hdu 4578 Transformation [线段树 区间更新]
- [置顶]基于51单片机+DAC0832的信号发生器
- Python安装包常见错误
- [置顶]Python基本语法初试
- Python中读取txt文本出现“ 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence”的解决办法
- [置顶]树莓派B+使用心得
- hdu 4578 Transformation(线段树+多种操作)
- [置顶]SFTP服务的使用!!
- [置顶]eNSP仿真学习,网络入门!
- mongodb 添加用户报错TypeError:db.addUser is not a function (mongodb3.4.1)
- common-pool2连接池详解与使用
- 喷水装置(二)
- mysql的alter用法
- 创建节点的三种方法
- jsp九大内置对象和四个作用域