HDU4578 Transformation【线段树】
<题目链接>
<转载于 >>> >
题目大意:
有一个序列,有四种操作:
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【线段树】的更多相关文章
- HDU4578 Transformation 线段树
这个题让我重新学习了加 乘 在区间的操作 题解:http://blog.csdn.net/guognib/article/details/25324025?utm_source=tuicool& ...
- 30-Transformation(HDU4578)-区间线段树(复杂)
http://acm.hdu.edu.cn/showproblem.php?pid=4578 Transformation Time Limit: 15000/8000 MS (Java/Others ...
- Transformation 线段树好题 好题 (独立写出来对线段树不容易)
Transformation Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others)T ...
- HDU 4578 Transformation --线段树,好题
题意: 给一个序列,初始全为0,然后有4种操作: 1. 给区间[L,R]所有值+c 2.给区间[L,R]所有值乘c 3.设置区间[L,R]所有值为c 4.查询[L,R]的p次方和(1<=p< ...
- hdu 4578 Transformation 线段树
没什么说的裸线段树,注意细节就好了!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> ...
- hdu 4578 Transformation 线段树多种操作裸题
自己写了一个带结构体的WA了7.8次 但是测了几组小数据都对..感觉问题应该出在模运算那里.写完这波题解去对拍一下. 以后线段树绝不写struct!一般的struct都带上l,r 但是一条线段的长度确 ...
- HDU-4578 Transformation(线段树的多种区间操作)
http://acm.hdu.edu.cn/showproblem.php?pid=4578 Time Limit: 15000/8000 MS (Java/Others) Memory Lim ...
- 【HDU4578 Transformation】线段树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4578 题意:有一个序列,有四种操作: 1:区间[l,r]内的数全部加c. 2:区间[l,r]内的数全部 ...
- HDU - 4578 Transformation(线段树区间修改)
https://cn.vjudge.net/problem/HDU-4578 题意 4种操作,区间加,区间乘,区间变为一个数,求区间的和.平方和以及立方和. 分析 明显线段树,不过很麻烦..看kuan ...
- HDU 4578 - Transformation - [加强版线段树]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4578 Problem Description Yuanfang is puzzled with the ...
随机推荐
- Oracle 口令文件:即 oracle密码文件
一:文件路径位置 [oracle@localhost db_1]$ cd $ORACLE_HOME/dbs [oracle@localhost dbs]$ ls dbsorapwPROD1 hc_or ...
- Confluence 6 使用一个主题到站点
主题被用来在你的 Confluence 站点中应用表现形式.请查看 Working with Themes 页面来查看如何应用你的整个站点和如何添加更多的主题. 希望在站点中应用主题: 进入 > ...
- 【rabbitmq】安装卸载
安装: 1. 在http://www.rabbitmq.com/install-rpm.html下载对应系统的rpm包 我下载的是rabbitmq-server-3.6.6-1.el6.noarch ...
- 【ftp】主动模式和被动模式
来自:http://blog.csdn.net/liuhelong12/article/details/50218311 原博主不让转载全文,不过下面这部分是原博主转载别人的,所以我拿过来应该没问题吧 ...
- cf219d 基础换根法
/*树形dp换根法*/ #include<bits/stdc++.h> using namespace std; #define maxn 200005 ]; int root,n,s,t ...
- ATM机模拟系统
实验要求 https://pan.baidu.com/s/1Sq5joWyVsNhY8DYiyI4UmQ 源代码 /*信1705-1 20173458 王嘉梁*/ package atm; impor ...
- python获取信息
import uuid import socket def get_mac(): mac=uuid.UUID(int = uuid.getnode()).hex[-12:] return " ...
- C++设计模式——观察者模式(转)
前言 之前做了一个性能测试的项目,就是需要对现在的产品进行性能测试,获得测试数据,然后书写测试报告,并提出合理化的改善意见.项目很简单,我们获得了一系列性能测试数据,对于数据,我们需要在Excel中制 ...
- Ubuntu16.04安装配置和使用ctags
Ubuntu16.04安装配置和使用ctags by ChrisZZ ctags可以用于在vim中的函数定义跳转.在ubuntu16.04下默认提供的ctags是很老很旧的ctags,快要发霉的版本( ...
- C/C++遍历二维数组,列优先(column-major)比行优先(row-major)慢,why?
C/C++遍历二维数组,列优先(column-major)比行优先(row-major)慢,why? 简单粗暴的答案:存在Cache机制! 稍微啰嗦一点:CPU访问内存(读/写,遍历数组的话主要是读) ...