这个题让我重新学习了加 乘 在区间的操作

题解:http://blog.csdn.net/guognib/article/details/25324025?utm_source=tuicool&utm_medium=referral

代码:也是参考上面的写的

注:确实有优先级,加只影响自己,乘会影响加,重新设定值会清零所有的标记

所以向下传的时候,乘和加的操作都晚于最后一次设定值,然后乘的操作要先于加

所以要先传递set 然后是mul 然后是add

#include <stdio.h>
#include <string.h>
#include <string>
#include <math.h>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N=1e5+;
const int mod=;
int q[][N<<],setval[N<<],add[N<<],mul[N<<];
void pushup(int rt){
for(int i=;i<;++i)
q[i][rt]=(q[i][rt<<]+q[i][rt<<|])%mod;
}
void downset(int rt,int c,int len){
int a[];a[]=c;
for(int i=;i<=;++i)
a[i]=a[i-]*a[]%mod;
for(int i=;i<;++i)
q[i][rt]=a[i+]*len%mod;
setval[rt]=c;
add[rt]=;
mul[rt]=;
}
void downadd(int rt,int c,int len){
int a[];a[]=c;
for(int i=;i<=;++i)
a[i]=a[i-]*a[]%mod;
q[][rt]=q[][rt]+a[]*len%mod+*a[]%mod*q[][rt]%mod+*a[]%mod*q[][rt]%mod;
q[][rt]=q[][rt]+a[]*len%mod+*a[]%mod*q[][rt]%mod;
q[][rt]=q[][rt]+a[]*len%mod;
for(int i=;i<;++i)q[i][rt]%=mod;
add[rt]=(add[rt]+c)%mod;
}
void downmul(int rt,int c,int len){
int a[];a[]=c;
for(int i=;i<=;++i)
a[i]=a[i-]*a[]%mod;
for(int i=;i<;++i)
q[i][rt]=(q[i][rt]*a[i+])%mod;
mul[rt]=(mul[rt]*c)%mod;
add[rt]=(add[rt]*c)%mod;
}
void down(int rt,int l,int r){
int m=(l+r)>>;
if(setval[rt]!=-){
downset(rt<<,setval[rt],m-l+);
downset(rt<<|,setval[rt],r-m);
setval[rt]=-;
}
if(mul[rt]!=){
downmul(rt<<,mul[rt],m-l+);
downmul(rt<<|,mul[rt],r-m);
mul[rt]=;
}
if(add[rt]!=){
downadd(rt<<,add[rt],m-l+);
downadd(rt<<|,add[rt],r-m);
add[rt]=;
}
}
int op,c;
void modify(int rt,int l,int r,int x,int y){
if(x<=l&&r<=y){
if(op==)downadd(rt,c,r-l+);
else if(op==)downmul(rt,c,r-l+);
else downset(rt,c,r-l+);
return;
}
int m=(l+r)>>;
down(rt,l,r);
if(x<=m)modify(rt<<,l,m,x,y);
if(y>m)modify(rt<<|,m+,r,x,y);
pushup(rt);
}
int ask(int rt,int l,int r,int x,int y){
if(x<=l&&r<=y)return q[c-][rt];
int ans=,m=(l+r)>>;
down(rt,l,r);
if(x<=m)ans=(ans+ask(rt<<,l,m,x,y))%mod;
if(y>m)ans=(ans+ask(rt<<|,m+,r,x,y))%mod;
return ans;
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
if(!n&&!m)break;
for(int i=;i<=*n;++i){
q[][i]=q[][i]=q[][i]=add[i]=;
mul[i]=;
setval[i]=-;
}
while(m--){
int x,y;
scanf("%d%d%d%d",&op,&x,&y,&c);
if(op<=)modify(,,n,x,y);
else printf("%d\n",ask(,,n,x,y));
}
}
return ;
}

HDU4578 Transformation 线段树的更多相关文章

  1. 30-Transformation(HDU4578)-区间线段树(复杂)

    http://acm.hdu.edu.cn/showproblem.php?pid=4578 Transformation Time Limit: 15000/8000 MS (Java/Others ...

  2. Transformation 线段树好题 好题 (独立写出来对线段树不容易)

    Transformation Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 65535/65536 K (Java/Others)T ...

  3. HDU 4578 Transformation --线段树,好题

    题意: 给一个序列,初始全为0,然后有4种操作: 1. 给区间[L,R]所有值+c 2.给区间[L,R]所有值乘c 3.设置区间[L,R]所有值为c 4.查询[L,R]的p次方和(1<=p< ...

  4. hdu 4578 Transformation 线段树

    没什么说的裸线段树,注意细节就好了!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> ...

  5. hdu 4578 Transformation 线段树多种操作裸题

    自己写了一个带结构体的WA了7.8次 但是测了几组小数据都对..感觉问题应该出在模运算那里.写完这波题解去对拍一下. 以后线段树绝不写struct!一般的struct都带上l,r 但是一条线段的长度确 ...

  6. HDU-4578 Transformation(线段树的多种区间操作)

    http://acm.hdu.edu.cn/showproblem.php?pid=4578 Time Limit: 15000/8000 MS (Java/Others)    Memory Lim ...

  7. 【HDU4578 Transformation】线段树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4578 题意:有一个序列,有四种操作: 1:区间[l,r]内的数全部加c. 2:区间[l,r]内的数全部 ...

  8. HDU - 4578 Transformation(线段树区间修改)

    https://cn.vjudge.net/problem/HDU-4578 题意 4种操作,区间加,区间乘,区间变为一个数,求区间的和.平方和以及立方和. 分析 明显线段树,不过很麻烦..看kuan ...

  9. HDU 4578 - Transformation - [加强版线段树]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4578 Problem Description Yuanfang is puzzled with the ...

随机推荐

  1. Runtime 实现 动态添加属性

    利用动态加载为对象添加一个 block 点击属性; .h 文件 #import <UIKit/UIKit.h> @interface UIView (Tap) /** * 动态添加手势 * ...

  2. iOS 状态栏管理

    iOS 7 以前:状态栏由 UIApplication 管理 1.隐藏状态栏 : application.statusBarHidden = NO; 2.设置状态栏样式 : application.s ...

  3. 1874 素数和最大 - Wikioi

    题目描述 Description 有一天萌萌哒Sevenkplus在跟素数们玩>_<...他玩着玩着突然想到一个问题!就是这样的:    从1到n这n个自然数中,选出一些数使得它们之间两两 ...

  4. Makefile教程

    Makefile学习教程: 跟我一起写 Makefile 0 Makefile概述 0.1 关于程序的编译和链接 1 Makefile 介绍 1.1 Makefile的规则 1.2 一个示例 1.3 ...

  5. [itint5]摆放窗口

    http://www.itint5.com/oj/#47 一种做法是:把矩形所占的方格都设为-1,就是个最大子矩阵和问题.复杂度o(w^2*h)或o(w*h^2),空间W*H猜想应用场景是:电脑屏幕上 ...

  6. [itint5]环形最大连续子段和

    http://www.itint5.com/oj/#9 一开始有了个n*n的算法,就是把原来的数组*2,由环形的展开成数组.然后调用n次最大子段和的方法.超时. 后来看到个O(n)的算法,就是如果不跨 ...

  7. Mac与Linux的一个巨大不同

    就是Mac仍处在桌面市场的商业圈里,尽管它的市场很小,但是正版率却很高,而且还有专门的Mac Store提供付费下载. Linux在桌面市场几乎没有份额,也不会有人会去买它的应用软件,基本上只存在于服 ...

  8. /storage/sdcard, /sdcard, /mnt/sdcard 三者的区别

    原文地址: /storage/sdcard, /sdcard, /mnt/sdcard 三者的区别 - petercao - 博客园 http://www.cnblogs.com/bluestorm/ ...

  9. POJ3295——Tautology

    Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...

  10. node.js模块之util模块

    util提供了各种使用的工具.require('util') to access them. Util.format(format,[..]) Returns a formatted string u ...