传送门

线段树维护区间取模,单点修改,区间求和。

这题老套路了,对一个数来说,每次取模至少让它减少一半,这样每次单点修改对时间复杂度的贡献就是一个log" role="presentation" style="position: relative;">loglog,所以维护区间最大值剪枝,然后每次单点暴力取模,这样的话时间复杂度为O(nlogn)" role="presentation" style="position: relative;">O(nlogn)O(nlogn)。

代码如下:

#include<bits/stdc++.h>
#define lc (p<<1)
#define rc (p<<1|1)
#define mid (T[p].l+T[p].r>>1)
#define N 100005
#define ll long long
using namespace std;
inline ll read(){
    ll ans=0;
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
    return ans;
}
inline void write(ll x){
    if(x>9)write(x/10);
    putchar((x%10)^48);
}
ll n,m,a[N];
struct Node{ll l,r,sum,maxn;}T[N<<2];
inline ll max(ll a,ll b){return a>b?a:b;}
inline void pushup(ll p){T[p].sum=T[lc].sum+T[rc].sum,T[p].maxn=max(T[lc].maxn,T[rc].maxn);}
inline void build(ll p,ll l,ll r){
    T[p].l=l,T[p].r=r;
    if(l==r){T[p].sum=T[p].maxn=a[l];return;}
    build(lc,l,mid),build(rc,mid+1,r),pushup(p);
}
inline void update(ll p,ll k,ll v){
    if(T[p].l==T[p].r){T[p].maxn=T[p].sum=v;return;}
    if(k<=mid)update(lc,k,v);
    else update(rc,k,v);
    pushup(p);
}
inline void modify(ll p,ll ql,ll qr,ll v){
    if(ql>T[p].r||qr<T[p].l||v>T[p].maxn)return;
    if(T[p].l==T[p].r){T[p].sum=T[p].maxn=T[p].sum%v;return;}
    if(qr<=mid)modify(lc,ql,qr,v);
    else if(ql>mid)modify(rc,ql,qr,v);
    else modify(lc,ql,mid,v),modify(rc,mid+1,qr,v);
    pushup(p);
}
inline ll query(ll p,ll ql,ll qr){
    if(ql>T[p].r||qr<T[p].l)return 0;
    if(ql<=T[p].l&&T[p].r<=qr)return T[p].sum;
    if(qr<=mid)return query(lc,ql,qr);
    if(ql>mid)return query(rc,ql,qr);
    return query(lc,ql,mid)+query(rc,mid+1,qr);
}
int main(){
    n=read(),m=read();
    for(ll i=1;i<=n;++i)a[i]=read();
    build(1,1,n);
    while(m--){
        ll op=read(),a=read(),b=read();
        switch(op){
            case 1:{write(query(1,a,b)),puts("");break;}
            case 2:{ll v=read();modify(1,a,b,v);break;}
            default:{update(1,a,b);break;}
        }
    }
    return 0;
}

2018.07.23 codeforces 438D. The Child and Sequence(线段树)的更多相关文章

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

  2. CodeForces 438D The Child and Sequence (线段树 暴力)

    传送门 题目大意: 给你一个序列,要求在序列上维护三个操作: 1)区间求和 2)区间取模 3)单点修改 这里的操作二很讨厌,取模必须模到叶子节点上,否则跑出来肯定是错的.没有操作二就是线段树水题了. ...

  3. CF(438D) The Child and Sequence(线段树)

    题意:对数列有三种操作: Print operation l, r. Picks should write down the value of . Modulo operation l, r, x. ...

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

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

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

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

  7. Codeforces 438D The Child and Sequence

    题意:给定一个n个数的序列,完成以下3个操作: 1.给定区间求和 2.给定区间对x取模 3.单点修改 对一个数取模,这个数至少折半.于是我们记一个最大值max,如果x>max则不做处理. #in ...

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

  9. 2018.12.15 codeforces 920F. SUM and REPLACE(线段树)

    传送门 线段树入门题. 给你一个序列:支持区间修改成自己的约数个数,区间求和. 实际上跟区间开方一个道理. 2的约数个数为2,1的约数个数为1,因此只要区间的最大值小于3就不用修改否则就暴力修改. 因 ...

随机推荐

  1. 怎样在本地windows安装和配置zookeeper

    Zookeeper是什么?有什么用? Zookeeper是一个分布式协调服务. 作用:为用户的分布式应用程序提供协调服务.  zookeeper在底层其实只提供了两个功能: 1.管理(存储,读取)用户 ...

  2. BLOB 操作

    对于数据库是BLOB类型存储数据. BLOB数据插入: Oracle提供的标准方式: 先插入一个空BLOB对象,然后Update这个空对象. 首先使用empty_blob()函数插入一个空BLOB对象 ...

  3. Java 如何产生UUID

    1.UUID 简介 UUID含义是通用唯一识别码 (Universally Unique Identifier),这是一个软件建构的标准,也是被开源软件基金会 (Open Software Found ...

  4. 快速可靠网络传输协议 KCP(转)

    KCP 是一个快速可靠协议,能以比 TCP浪费10%-20%的带宽的代价,换取平均延迟降低30%-40%,且最大延迟降低三倍的传输效果.纯算法实现,并不负责底层协议(如UDP)的收发,需要使用者自己定 ...

  5. ssh结合tar命令把远程文件拉回来或推过去(实现数据无落地推送)

    登录22后tar 压缩/var/log目录输出到标准输入通过管道传到本地22_log.tar.gz文件 ssh 192.168.0.22 "cd /var ;tar -zcvf - log& ...

  6. 关于Integer的比较,今天又犯了一个低级错误,记录下

    今天查看以前所写的代码,看到有一部分被人改了,代码如下: if (orgId != organizationUpdateReq.getOrgId()) { //orgId的类型为Integer,org ...

  7. Haskell语言学习笔记(34)System.Environment

    System.Environment getArgs :: IO [String] getProgName :: IO String getExecutablePath :: IO FilePath ...

  8. scala -- 柯里化

    柯里化 柯里化是把接受多个参数的函数,变成接受一个单一参数的函数.并且返回接受剩余参数和返回结果的新函数. 就是一个逐次消元的过程. 当把函数的元全消掉,就得到了值. 值就是零元函数. 二元函数 f( ...

  9. DataFrame查增改删

    DataFrame查增改删 查 Read 类list/ndarray数据访问方式 dates = pd.date_range(',periods=10) dates df = pd.DataFrame ...

  10. git hg提交拉取

    工作总结web_acl 535 git clone “ssh://git@outergit.yonyou.com:49622/esn_web/web_acl.git" 600 git bra ...