<题目链接>

<转载于 >>> >

题目大意:

有一个序列,有四种操作:

1:区间[l,r]内的数全部加c。

2:区间[l,r]内的数全部乘c。

3:区间[l,r]内的数全部初始为c。

4:询问区间[l,r]内所有数的P次方之和。

解题分析:

不可能全部查询的节点,最好的情况就是查询到一段[l,r],这段区间内所有的值都相等,那么返回(r-l+1)*val 的值即可。只要标记区间内的所有数是否相同,并记录下区间内所有数相同的区间的值即可。每次询问时查询到区间内所有值相同的区间即可。

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; typedef long long ll;
const int mod=;
const int M =1e5+; #define Lson rt<<1,l,mid
#define Rson rt<<1|1,mid+1,r
int n,m;
bool cnt[M<<];
ll tr[M<<];
void Pushdown(int rt){
if(cnt[rt]){
cnt[rt<<]=cnt[rt<<|]=true;
tr[rt<<]=tr[rt<<|]=tr[rt];
}
}
void Pushup(int rt){
if(!cnt[rt<<]||!cnt[rt<<|])cnt[rt]=false;
else if(tr[rt<<]!=tr[rt<<|])cnt[rt]=false;
else cnt[rt]=true,tr[rt]=tr[rt<<];
}
void update(int rt,int l,int r,int L,int R,int c,int type){
if(cnt[rt]&&L<=l&&r<=R){
if(type==)tr[rt]=(tr[rt]+c)%mod; //分三种情况进行操作
else if(type==)tr[rt]=(tr[rt]*c)%mod;
else tr[rt]=c;
return;
}
Pushdown(rt);
int mid=(l+r)>>;
if(L<=mid)update(Lson,L,R,c,type);
if(R>mid)update(Rson,L,R,c,type);
Pushup(rt);
}
int query(int rt,int l,int r,int L,int R,int c){
if(cnt[rt]&&L<=l&&r<=R){
ll res=;for(int i=;i<=c;i++)res*=tr[rt]; //得到tr[rt]的c次方(因为c<=3,所以可以不用快速幂)
res=res*(r-l+); //乘上区间长度
return res%mod;
}
Pushdown(rt);
int mid=(l+r)>>;
ll ans=;
if(L<=mid)ans+=query(Lson,L,R,c);
if(R>mid)ans+=query(Rson,L,R,c);
return ans%mod;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF,n||m){
memset(tr,,sizeof(tr));
memset(cnt,true,sizeof(cnt));
while(m--){
int op,x,y,c;
scanf("%d%d%d%d",&op,&x,&y,&c);
if(op>=&&op<=)update(,,n,x,y,c,op);
else printf("%d\n",query(,,n,x,y,c));
}
}
return ;
}

2018-09-25

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

  1. HDU4578 Transformation 线段树

    这个题让我重新学习了加 乘 在区间的操作 题解:http://blog.csdn.net/guognib/article/details/25324025?utm_source=tuicool& ...

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

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

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

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

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

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

  5. hdu 4578 Transformation 线段树

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

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

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

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

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

  8. 【HDU4578 Transformation】线段树

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

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

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

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

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

随机推荐

  1. 什么是orm思想?

    什么是orm思想? 1.hibernate使用orm思想对数据库进行crud操作 2.在web阶段学习javabean更正确的叫法是:实体类 3.orm: object   relational   ...

  2. 基于vue-cli3的vue项目移动端样式适配,lib-flexible和postcss-px2rem

    1,安装 flexible和 postcss-px2rem(命令行安装) npm install lib-flexible --save npm install postcss-px2rem --sa ...

  3. D3.js 使用缩放zoom时节点无法拖动,只能整体移动的问题

    .on("dragstart", function() { d3.event.sourceEvent.stopPropagation(); }) https://stackover ...

  4. json的转换操作

    toJSON 把JS对象{ 'x': 2, 'y': 3 }转为JSON对象格式的字符串   不能转化字符串 比如"{ 'x': 2, 'y': 3 }" 可以转格式不标准的jso ...

  5. 从认识面向对象到构造函数的标准写法(构造函数的继承、多态、ECMA6中新代替语法class) - 下

    笔记一个包含:认识面向对象.构造函数的封装.继承.多态.ECMA6中新代替语法class 下:包括构造函数的继承.多态.ECMA6中新代替语法class 构造函数的继承 从父一级延续下来的属性和功能( ...

  6. Python使用re模块实现正则表达式操作

    Python提供了re模块,用于实现正则表达式的操作.在实现时,可以使用re模块提供的方法(如search().match().findall()等)进行字符串处理,也可以先使用re模块的compil ...

  7. JS实现继承的几种方式(转)

    转自:幻天芒的博客 前言 JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一.那么如何在JS中实现继承呢?让我们拭目以待. JS继承的实现方式 既然要实现继承,那么首先我们得有一个父类,代码如 ...

  8. 【技巧汇总】eclipse中如何跳转到指定行

    技巧汇总 持续更新ing eclipse中如何跳转到指定行 ctrl+L

  9. skyline添加wfs服务时,弹出错误“no layers were found”!

    1.问题描述: 使用TerraExplorer Pro添加ArcGIS Server 10.2发布的WFS服务图层时,弹出如下错误: 2.错误原因: 发布wfs服务前,图层数据源的空间参考未设置,不能 ...

  10. HTTP.sys远程执行代码漏洞

    远程执行代码漏洞存在于 HTTP 协议堆栈 (HTTP.sys) 中,当 HTTP.sys 未正确分析经特殊设计的 HTTP 请求时会导致此漏洞. http://bbs.safedog.cn/thre ...