\(\\\)

\(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. freemarker导出word的一些问题

    首先,了解下freemarker导出word的流程: 参考https://www.cnblogs.com/llfy/p/9303208.html 异常一: freemarker.core.ParseE ...

  2. 洛谷 P4379 [USACO18OPEN]Lemonade Line

    P4379 [USACO18OPEN]Lemonade Line 题目描述 这是农场上一个炎热的夏日,Farmer John要给他的 NN 头奶牛发柠檬汽水了!所有的 NN 头奶牛(方便起见,编号为  ...

  3. Ubuntu 16.04修改显示字体大小(包括GNOME/Unity)

    在Ubuntu中字体都基本偏大,且和分辨率无关. Unity: 安装Unity Tweak Tool sudo apt-get install unity-tweak-tool GNOME: 打开优化 ...

  4. 在HDInsight中从Hadoop的兼容BLOB存储查询大数据的分析

    在HDInsight中从Hadoop的兼容BLOB存储查询大数据的分析 低成本的Blob存储是一个强大的.通用的Hadoop兼容Azure存储解决方式无缝集成HDInsight.通过Hadoop分布式 ...

  5. org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected be

    1.错误描写叙述 八月 14, 2015 3:03:05 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger warn 警告: Request ...

  6. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] B. Finding Team Member 排序

                                                                      B. Finding Team Member             ...

  7. Delphi中SendMessage使用说明(所有消息说明) good

    Delphi中SendMessage使用说明 SendMessage基础知识 函数功能:该函数将指定的消息发送到一个或多个窗口.此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回.而函数Po ...

  8. 容器ArrayList原理(学习)

    一.概述 动态数组,容量能动态增长,元素可以为null,用数组存储,非线程同步(vector线程同步) 每个 ArrayList 实例都有一个容量,该容量是指用来存储列表元素的数组的大小,自动增长(默 ...

  9. robo 3t 在 ubuntu下安装

    如果您尝试安装最新版本robomobo调用可以现在robo3t.或者你尝试在Ubuntu 16.04上安装,按照下面的步骤和你的robomongo安装 下载最新的robomongo tar文件 wge ...

  10. luogu 3953 逛公园

    noip2017 D1T3 逛公园 某zz选手看到数据范围直接就最短路计数了,结果写错了爆零 题目大意: N个点M条边构成的有向图,且没有自环和重边.其中1号点是起点,N号点是公园的终点,每条边有一个 ...