CF438D The Child and Sequence(线段树)
题目大意:维护一个长度为 $n$ 的正整数序列 $a$,支持单点修改,区间取模,区间求和。共 $m$ 个操作。
$1\le n,m\le 10^5$。其它数均为非负整数且 $\le 10^9$。
居然被这道水题卡了那么久……
主要难点就是取模操作。
我们发现一个数 $x$ 模 $i(1\le i\le x)$:
$i\le\lfloor\frac{x}{2}\rfloor$ 时:余数小于除数,所以答案小于 $\lfloor\frac{x}{2}\rfloor$。
$i>\lfloor\frac{x}{2}\rfloor$ 时:答案就是 $x-i$,也小于 $\lfloor\frac{x}{2}\rfloor$。
所以 $x$ 每次被取模都至少减小一半。
那……就是暴力线段树!
线段树再维护一个区间最大值,当模数大于区间最大值时,直接走人。
容易发现如果没有单点修改操作,一个叶子节点最多被暴力到 $\log a_i$ 次。
有单点修改操作?那又怎样?一共只会多 $m$ 个数而已啊!
时间复杂度 $O((n+m)\log n\log a_i)$。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=;
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline int read(){
char ch=getchar();int x=,f=;
while(ch<'' || ch>'') f|=ch=='-',ch=getchar();
while(ch>='' && ch<='') x=x*+ch-'',ch=getchar();
return f?-x:x;
}
int n,m,a[maxn],mx[maxn*];
ll sum[maxn*]; //和要开long long
inline void pushup(int o){
sum[o]=sum[o<<]+sum[o<<|];
mx[o]=max(mx[o<<],mx[o<<|]);
}
void build(int o,int l,int r){
if(l==r) return void(sum[o]=mx[o]=a[l]);
int mid=(l+r)>>;
build(lson);
build(rson);
pushup(o);
}
void modify(int o,int l,int r,int p,int v){
if(l==r) return void(sum[o]=mx[o]=v);
int mid=(l+r)>>;
if(mid>=p) modify(lson,p,v);
else modify(rson,p,v);
pushup(o);
}
ll query(int o,int l,int r,int ql,int qr){
if(l>=ql && r<=qr) return sum[o];
int mid=(l+r)>>;ll ans=;
if(mid>=ql) ans+=query(lson,ql,qr);
if(mid<qr) ans+=query(rson,ql,qr);
return ans;
} //以上基本操作,不解释。。。
void modulo(int o,int l,int r,int ql,int qr,int v){
if(mx[o]<v) return; //模数大于最大值,走人
if(l==r) return void(sum[o]=mx[o]=sum[o]%v); //到叶子结点,暴力
int mid=(l+r)>>;
if(mid>=ql) modulo(lson,ql,qr,v); //暴力下去
if(mid<qr) modulo(rson,ql,qr,v);
pushup(o);
}
int main(){
n=read();m=read();
FOR(i,,n) a[i]=read();
build(,,n);
FOR(i,,m){
int op=read(),x=read(),y=read();
switch(op){
case :printf("%lld\n",query(,,n,x,y));break;
case :modulo(,,n,x,y,read());break;
case :modify(,,n,x,y);
}
}
}
线段树
CF438D The Child and Sequence(线段树)的更多相关文章
- CF438D The Child and Sequence 线段树
给定数列,区间查询和,区间取模,单点修改. n,m小于10^5 ...当区间最值小于模数时,就直接返回就好啦~ #include<cstdio> #include<iostream& ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸
D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- Codeforces 438D The Child and Sequence - 线段树
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模
D. The Child and Sequence At the children's day, the child came to Picks's house, and messed his h ...
- cf250D. The Child and Sequence(线段树 均摊复杂度)
题意 题目链接 单点修改,区间mod,区间和 Sol 如果x > mod ,那么 x % mod < x / 2 证明: 即得易见平凡, 仿照上例显然, 留作习题答案略, 读者自证不难. ...
- CF(438D) The Child and Sequence(线段树)
题意:对数列有三种操作: Print operation l, r. Picks should write down the value of . Modulo operation l, r, x. ...
- CodeForces 438D The Child and Sequence (线段树 暴力)
传送门 题目大意: 给你一个序列,要求在序列上维护三个操作: 1)区间求和 2)区间取模 3)单点修改 这里的操作二很讨厌,取模必须模到叶子节点上,否则跑出来肯定是错的.没有操作二就是线段树水题了. ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence (线段树)
题目链接:http://codeforces.com/problemset/problem/438/D 给你n个数,m个操作,1操作是查询l到r之间的和,2操作是将l到r之间大于等于x的数xor于x, ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- [CF438D]The Child and Sequence【线段树】
题目大意 区间取模,区间求和,单点修改. 分析 其实算是一道蛮简单的水题. 首先线段树非常好解决后两个操作,重点在于如何解决区间取模的操作. 一开始想到的是暴力单点修改,但是复杂度就飙到了\(mnlo ...
随机推荐
- Ubuntu学习总结-01 安装Ubuntu
Ubuntu(友帮拓.优般图.乌班图)是一个以桌面应用为主的开源GNU/Linux操作系统,Ubuntu 是基于Debian GNU/Linux,支持x86.amd64(即x64)和ppc架构,由全球 ...
- 20155318 《网络攻防》Exp6 信息搜集与漏洞扫描
20155318 <网络攻防>Exp6 信息搜集与漏洞扫描 基础问题 哪些组织负责DNS,IP的管理. 互联网名称与数字地址分配机构,ICANN机构.其下有三个支持机构,其中地址支持组织( ...
- # 20155337《网络对抗》Web基础
20155337<网络对抗>Exp8 Web基础 实践目标 1. 实践内容 (1).Web前端HTML 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写 ...
- WPF编程,通过DoubleAnimation控制图片的透明度,将重叠的图片依次显示。
原文:WPF编程,通过DoubleAnimation控制图片的透明度,将重叠的图片依次显示. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307 ...
- VS编程,WPF中,获取鼠标相对于当前程序窗口的坐标的一种方法
原文:VS编程,WPF中,获取鼠标相对于当前程序窗口的坐标的一种方法 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/ ...
- UI自动化环境准备
1.安装专业版pycharm,只要是填写了licensed的都是专业版本的pycharm 2.python中安装好selenium包 pip install selenium 3.python中安装 ...
- 百度Hr分享,一个合格的数据工程师简历中必备技能?
如果你是一名数据科学方面的求职者,你肯定想知道在简历上写些什么才能获得面试的机会:如果你想进入这个领域,你一定想知道具备哪些技术才能成为一名有竞争力的求职者. 在本文中,我们对Indeed中一千份数据 ...
- wordpress 5.1 后台拿shell
闲着没事搭建了一套最新版wordpress 5.1,本身wordpress后台安全防御是比较差的,想尝试下后台是否可以拿shell. 再上传插件的地方可以直接上传php文件,并且该文件可以执行: sh ...
- 扩展webservice
描述: 最近一个winform项目刚开发完成.采用c/s架构.闲来把一些技术点整理下了. 做项目之前调研了客户的电脑 .客户端机子性能一般,而且都是基于xp. 这些客观存在的问题,注定了实现过程中必须 ...
- thinkphp在wamp 配置去掉url中index.php方法
http://blog.csdn.net/youmypig/article/details/45008971