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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 题解——CodeForces 438D The Child and Sequence

    题面 D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input ...

  4. 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 ...

  5. 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 ...

  6. AC日记——The Child and Sequence codeforces 250D

    D - The Child and Sequence 思路: 因为有区间取模操作所以没法用标记下传: 我们发现,当一个数小于要取模的值时就可以放弃: 凭借这个来减少更新线段树的次数: 来,上代码: # ...

  7. 438D - The Child and Sequence

    D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...

  8. 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 ...

  9. 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 ...

  10. 线段树【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 ...

随机推荐

  1. Mean Shift具体介绍

    Mean Shift,我们 翻译为“均值飘移”.其在聚类,图像平滑.图像切割和跟踪方面得到了比較广泛的应用.因为本人眼下研究跟踪方面的东西,故此主要介绍利用Mean Shift方法进行目标跟踪,从而对 ...

  2. 读TIJ -1 对象入门

    <Thinking In Java·第 1 章对象入门> 第 1 章约20页,是对面向对象的程序设计(OOP)的一个综述. 依照其前言所述: "当中包含对"什么是对象& ...

  3. Thread和Runnable差别

    继承Thread类的,我们相当于拿出三件事即三个卖票10张的任务分别分给三个窗体,他们各做各的事各卖各的票各完毕各的任务.由于MyThread继承Thread类.所以在new MyThread的时候在 ...

  4. 【Linux学习笔记】用nc实现两台主机间的文件传输(不需要输密码)

    通常,可以用scp完成两台主机间的文件传输任务,但在主机间未建立信任关系的情况下,scp每次都需要输入密码,用起来感觉不是很方便,之前这篇笔记介绍过不用输入密码执行脚本或传输文件的方法,但对于一些临时 ...

  5. spring mvc DispatcherServlet详解之三---request通过ModelAndView中获取View实例的过程

    整个spring mvc的架构如下图所示: 上篇文件讲解了DispatcherServlet第二步:通过request从Controller获取ModelAndView.现在来讲解第三步:reques ...

  6. Struts2 result type

    Struts2支持的不同类型的返回结果为: type name 说明 dispatcher 缺省类型,用来转向页面,通常处理JSP chain 转向另一个action,用来处理Action链 redi ...

  7. PHP替换数据库的换行符

    //php 有三种方法来解决 //1.使用str_replace 来替换换行 $str = str_replace(array("\r\n", "\r", &q ...

  8. VS2010在WIN7 64位系统下架设网站及路由器配置

    步骤一:安装IIS 打开[控制面板]-[程序和功能],在左侧进入[打开或关闭windows功能],按照下图选择Internet信息项目下的子选项并安装: 步骤二:配置应用程序池 打开[控制面板]-[管 ...

  9. mysql locktables

    SELECT      r.trx_id waiting_trx_id,      r.trx_mysql_thread_id waiting_thread,      TIMESTAMPDIFF(  ...

  10. iOS: 在代码中使用Autolayout (2) – intrinsicContentSize和Content Hugging Priority【转】

    原文:http://www.mgenware.com/blog/?p=491 接上文:iOS: 在代码中使用Autolayout (1) – 按比例缩放和优先级. 我们继续来看在代码中使用Autola ...