The Child and Sequence
Codeforces Round #250 (Div. 1)D:http://codeforces.com/problemset/problem/438/D
题意:给你一个序列,然后有3种操作 1x y.表示查询[x,y]之间的区间和,2 x y z表示把[x y]内的数%z,3x y,表示把第x个数变成y。
题解:肯定是用线段树来维护,但是一开始想不到维护什么统计量,对于区间取模,没办法用lazy标记,更新到第的话,肯定会T。后来,认为既然没办法用lazy,那么只能用别的方法来优化更新。发现每次取模之后,数都会变小,如果要取模的数比当前模数小的话,就不用取模,于是可以维护区间最大值,如果区间最大值都小于模数的话,这个区间肯定不用更新,这样来减少更新。这样的方法,其实以前也做过,就是对于一个区间内的数进行开平方操作,每个数会越开越小,到了1的时候就可以直接不开了。因此,对于线段树的区间更新来说:1如果能找到好的lazy可以标记的话,就使用lazy标记;2如果找不到就要想办法优化区间更新,让单点更新的次数变少。另外此题,自己用线段树省空间的写法,结果不熟练,一个地方写错,最后wa几发。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1e5+;
int n,m;
long long sum[N*],maxn[N*];
void pushup(int rt){
sum[rt]=sum[rt<<]+sum[rt<<|];
maxn[rt]=max(maxn[rt<<],maxn[rt<<|]);
}
void build(int l,int r,int rt){
if(l==r){
scanf("%I64d",&maxn[rt]);
sum[rt]=maxn[rt];
return;
}
int mid=(l+r)/;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
pushup(rt);
}
void update(int l,int r,int rt,int from,int to,long long mod){
if(maxn[rt]<mod)return;
if(l==r){
maxn[rt]%=mod;
sum[rt]=maxn[rt];
return;
}
int mid=(l+r)/;
if(mid>=to)update(l,mid,rt<<,from,to,mod);
else if(mid<from)update(mid+,r,rt<<|,from,to,mod);
else{
update(l,mid,rt<<,from,mid,mod);
update(mid+,r,rt<<|,mid+,to,mod);
}
pushup(rt);
}
void update2(int l,int r,int rt,int pos,long long val){
if(l==r){
maxn[rt]=val;
sum[rt]=val;
return;
}
int mid=(l+r)/;
if(mid>=pos)update2(l,mid,rt<<,pos,val);
else update2(mid+,r,rt<<|,pos,val);
pushup(rt);
}
long long query(int l,int r,int rt,int from,int to){
if(l==from&&r==to){
return sum[rt];
}
int mid=(l+r)/;
if(mid>=to)return query(l,mid,rt<<,from,to);
else if(mid<from)return query(mid+,r,rt<<|,from,to);
else {
return query(l,mid,rt<<,from,mid)+query(mid+,r,rt<<|,mid+,to); }
}
int t,t1,t4;
long long t2,t3;
int main(){
while(~scanf("%d%d",&n,&m)){
memset(sum,,sizeof(sum));
memset(maxn,,sizeof(maxn));
build(,n,);
for(int i=;i<=m;i++){
scanf("%d%d",&t,&t1);
if(t==){
scanf("%d",&t4);
printf("%I64d\n",query(,n,,t1,t4));
}
else if(t==){
scanf("%d%I64d",&t4,&t2);
update(,n,,t1,t4,t2);
}
else{
scanf("%I64d",&t2);
update2(,n,,t1,t2);
}
}
}
}
The Child and Sequence的更多相关文章
- Codeforce 438D-The Child and Sequence 分类: Brush Mode 2014-10-06 20:20 102人阅读 评论(0) 收藏
D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...
- 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
题面 D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence(线段树)
D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence
D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...
- AC日记——The Child and Sequence codeforces 250D
D - The Child and Sequence 思路: 因为有区间取模操作所以没法用标记下传: 我们发现,当一个数小于要取模的值时就可以放弃: 凭借这个来减少更新线段树的次数: 来,上代码: # ...
- 438D - The Child and Sequence
D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...
- 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 ...
- 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 ...
- 线段树【CF620E】The Child and Sequence
Description At the children's day, the child came to Picks's house, and messed his house up. Picks w ...
随机推荐
- 基于Tomcat7、Java、WebSocket的服务器推送聊天室
http://blog.csdn.net/leecho571/article/details/9707497 http://blog.fens.me/java-websocket-intro/ jav ...
- android 20 Intnet类重要的成员变量
Intnet类重要的成员变量: <intent-filter> <action android:name="android.intent.action.MAIN" ...
- 使用strace追踪多个进程
http://www.ttlsa.com/tools/use-strace-to-track-multiple-processes/ strace是Linux环境下的一款程序调试工具,用来监察一个应 ...
- 如何判断JDK是32位还是64位
第一种方法 在CMD窗口中使用java -version 命令进行查看 如果是64位的则会显示 Java HotSpot<TM>64-Bit 字样,32位的则没有类似信息. 注:这是Sun ...
- 基于Html5的兼容所有主流浏览器的在线视频播放器videoJs
在一个新的项目上需要实现在线视频播放,原本打算借助优酷的视频存储和播放,但是发现这个需要用户注册优酷账户,严重影响用户体验,于是这个方案被毙掉了.于是开始了自己开发一个在线播放器的想法,当然尽量使用已 ...
- 了解php面向对象
php 三大特性:封装.继承.多态,一直以来只知道其字,却不大了解其意思和具体使用,只是对继承有大概的了 解,优点是代码的重用性,oop概念,记得有一次我去面试,人家问我什么是oop,然后我答了很多什 ...
- HTTP协议 流程图
- spring事务管理学习
spring事务管理学习 spring的事务管理和mysql自己的事务之间的区别 参考很好介绍事务异常回滚的文章 MyBatis+Spring 事务管理 spring中的事务回滚例子 这篇文章讲解了@ ...
- Openblas编译Android NDK库的步骤
1.配置Android NDK编译工具.以下下载地址,直接放到浏览器中下载,不需要VPNlinux 32 bithttp://dl.google.com/android/ndk/android-ndk ...
- [转载]CentOS6.4+Mono3.0.7+Jexus5.2.5
本文章来自互联网,但是本人已经在VM虚拟机里面测试成功,所以分享给大家 1.更新 yum -y update 2.安装Mono源码安装需要的库 yum -y install gcc gcc-c++ a ...