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 ...
随机推荐
- docker 查看容器详细
http://www.docker.org.cn/book/docker/checking-running-image-12.html
- java数组使用技巧
参考网上文章,总结了一下java数组使用技巧,如下: package com.beijing.array; import java.nio.ByteBuffer; import java.util.A ...
- linux调度器系列
http://blog.csdn.net/wudongxu/article/category/791519
- Qt 学习之路:深入 Qt5 信号槽新语法
在前面的章节(信号槽和自定义信号槽)中,我们详细介绍了有关 Qt 5 的信号槽新语法.由于这次改动很大,许多以前看起来不是问题的问题接踵而来,因此,我们用单独的一章重新介绍一些 Qt 5 的信号槽新语 ...
- 使用 Acegi 保护 Java 应用程序
第 1 部分: 架构概览和安全过滤器 Acegi Security System 是一种功能强大并易于使用的替代性方案,使您不必再为 Java 企业应用程序编写大量的安全代码.虽然它专门针对使用 Sp ...
- MySQL(10):实体、实体表和外键(foreign key)
1.实体 数据库管理系统中的各种用于数据管理方便而设定的各种数据管理对象,如:数据库表.视图.存储过程等都是数据库实体.广义上讲,这些对象中所存储的数据也是数据库实体.因为它们也是确切存 ...
- 详解SQL Server 2005 Express下的事件探查器
安装Visual Studio 2008会有附带的SQL Server 2005 Express版 我们开发一般都用那个都不单独安装SQL Server的 大家都知道express版的sql是没有 事 ...
- anjularjs slider控件替代方案
做项目需要一个slider控件,找了很久没有找到合适的指令集,无意间看到可以直接用range替代,不过样式有点丑. <label> <input type="range&q ...
- 【svn】server建立以及svn使用
安装好VisualSVN Server后[安装过程看这里],运行VisualSVN Server Manger,下面是启动界面: 好的,下面我来添加一个代码库[Repository],如下图: 按上图 ...
- JS获取屏幕各种高
<script language="javascript"> var h = ""; h += " 网页可见区域宽:"+ doc ...