\(\\\)

\(Description\)


维护长为 \(N\) 的数列,\(M\)次操作,支持单点修改,区间取模,查询区间和。

  • \(N,M\le 10^5\)

\(\\\)

\(Solution\)


线段树单点修改直接改,直接维护区间和就好。

关于取模,显然的优化是,当前节点代表区间最大值如果小于模数就停止递归。

事实上我们只需要这样做,甚至连区间取模的 tag 都不用。

因为一个数变为 \(1\) 至多需要 \(log\) 次取模,所以每个数至多被有效操作 \(log\) 次,然而修改是单点修改,所以并不会对区间暴力取模有太大的影响。

\(\\\)

\(Code\)


#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 100010
#define gc getchar
#define Rg register
#define mid ((l+r)>>1)
using namespace std;
typedef long long ll; inline ll rd(){
ll x=0; bool f=0; char c=gc();
while(!isdigit(c)){if(c=='-')f=1;c=gc();}
while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=gc();}
return f?-x:x;
} ll n,m,a[N]; struct segment{ ll root,ptr; inline ll newnode(){return ++ptr;} struct node{ll ls,rs,sum,mx;}c[N<<2]; inline void pushup(ll rt){
c[rt].mx=max(c[c[rt].ls].mx,c[c[rt].rs].mx);
c[rt].sum=c[c[rt].ls].sum+c[c[rt].rs].sum;
} void build(ll &rt,ll l,ll r){
rt=newnode();
if(l==r){
c[rt].mx=c[rt].sum=a[l];
return;
}
build(c[rt].ls,l,mid);
build(c[rt].rs,mid+1,r);
pushup(rt);
} void updata1(ll rt,ll l,ll r,ll L,ll R,ll p){
if(r<L||l>R) return;
if(l==r){
c[rt].mx=c[rt].sum=c[rt].sum%p;
return;
}
if(c[rt].mx<p) return;
if(L<=mid) updata1(c[rt].ls,l,mid,L,R,p);
if(R>mid) updata1(c[rt].rs,mid+1,r,L,R,p);
pushup(rt);
} void updata2(ll rt,ll l,ll r,ll p,ll x){
if(l==r){
c[rt].mx=c[rt].sum=x;
return;
}
if(p<=mid) updata2(c[rt].ls,l,mid,p,x);
else updata2(c[rt].rs,mid+1,r,p,x);
pushup(rt);
} ll query(ll rt,ll l,ll r,ll L,ll R){
if(r<L||l>R) return 0;
if(l>=L&&r<=R) return c[rt].sum;
ll ans=0;
if(L<=mid) ans+=query(c[rt].ls,l,mid,L,R);
if(R>mid) ans+=query(c[rt].rs,mid+1,r,L,R);
return ans;
} }tree; int main(){
n=rd(); m=rd();
for(Rg ll i=1;i<=n;++i) a[i]=rd();
tree.build(tree.root,1,n);
for(Rg ll i=1,op,l,r,x;i<=m;++i){
op=rd();
if(op==1){
l=rd(); r=rd();
printf("%I64d\n",tree.query(tree.root,1,n,l,r));
}
else if(op==2){
l=rd(); r=rd(); x=rd();
tree.updata1(tree.root,1,n,l,r,x);
}
else{
l=rd(); x=rd();
tree.updata2(tree.root,1,n,l,x);
}
}
return 0;
}

[ CodeForces 438 D ] 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

    [原题题面]传送门 [大致题意] 给定一个长度为n的非负整数序列a,你需要支持以下操作: 1:给定l,r,输出a[l]+a[l+1]+…+a[r]. 2:给定l,r,x,将a[l],a[l+1],…, ...

  3. [题解] Codeforces 438 E The Child and Binary Tree DP,多项式,生成函数

    题目 首先令\(f_i\)表示权值和为\(i\)的二叉树数量,\(f_0=1\). 转移为:\(f_k=\sum_{i=0}^n \sum_{j=0}^{k-c_i}f_j f_{k-c_i-j}\) ...

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

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

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

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

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

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

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

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

随机推荐

  1. 一步步搭建java信息管理系统00 - 前言

    开发前,先上效果图吧 信息管理系统,个人认为,以下几个因素是不可缺少的 多tab 因菜单比较多,右侧的树形一定要考虑,如果菜单还是多,那么顶部就要考虑起来了 以后想到什么,再添加吧. 看到easyui ...

  2. 手把手教你开发Chrome扩展三:关于本地存储数据

    手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单 手把手教你开发Chrome扩展二:为html添加行为 手把手教你开发Chrome扩展三:关于本地存储数据 HTML5 ...

  3. C++虚函数表剖析

    关键词:虚函数.虚表,虚表指针,动态绑定,多态 一.概述 为了实现C++的多态,C++使用了一种动态绑定的技术. 这个技术的核心是虚函数表(下文简称虚表).本文介绍虚函数表是怎样实现动态绑定的. 二. ...

  4. 字符串匹配之KMP算法(续)---还原next数组

    相信通过今天的文章,你会对KMP的认识更加深入一层,不止停留在知道怎样计算的层面上了,废话不多说,開始. 通过前面的第一篇文章,知道了怎么求next数组,相信非常多喜欢刨根问底的人就会问,我依照你的做 ...

  5. eclipse中j2ee(struts2)部署及相关问题释疑

    1.eclipse中进行web项目开发时.部署的时候和利用myeclipse部署时有非常大不同,由于在myeclipse的工具栏中有一个部署button.而且在myeclipse的preference ...

  6. Django打造大型企业官网(四)

    4.3.轮播图布局和样式 templates/news/index.html <div class="news-wrapper"> <div class=&quo ...

  7. 详细介绍Linux telnet命令的使用

    对Linux系统进行远程登录,Linux telnet命令是必须得掌握的一个知识,虽然telnet并不是唯一的远程登录的方案,但是不可否认它是最常用的,所以很有必要详细了解Linux telnet命令 ...

  8. 使用bbed改动数据

    bbed是一个强大的工具,同意我们绕过oracle直接从数据文件里改动相应的内容 ZBDBA@orcl11g>select * from emp; EMPNO ENAME JOB MGR HIR ...

  9. canvas.clipPath canvas.clipRect() 无效的原因

    今天发现有些机型不能做到像QQ 透明截图那样的功能,本来能够的.一看是部分机器所有都是灰色半透明遮挡住了,没中间的透明效果,. 并且我不是通过遮挡,我是採用 裁剪的方式,至于裁剪代码百度有相关知识,具 ...

  10. c# Action,Func,Predicate委托

    System命名空间下已经预先定义好了三中泛型委托,Action,Func和Predicate,这样我们在编程的时候,就不必要自己去定义这些委托了 Action是没有返回值的 Func是带返回值的 不 ...