题目链接

题目大意:

给定一个长度为n的序列,要求能够执行m次下列操作:

1.查询区间[l,r]的和

2.将区间[l,r]的每一个数%=mod

3.修改第x个数为y

操作1,3都是线段树的基本操作,线段树详细知识可以看看这篇大牛的文章   https://www.cnblogs.com/TheRoadToTheGold/p/6254255.html

操作二取模如果一个个模那么复杂度为O(n*m),直接T,我们可以在线段树中同时维护一个最大值,如果一个区间的最大值比取模值要小,就可以不取模,否则的话递归左右子树,继续将每个区间

的最大值与取模值进行比较,一直不断缩小到单点,使需要取模的单点才取模,这样就不会T了

代码如下:

 #include <bits/stdc++.h>
using namespace std;
const int inf=<<;
typedef long long ll;
const double pi=acos(-);
const int mod=1e9+;
const int maxn=1e5+;
int n,m;ll ans;
struct node{
int l,r,maxx;ll v;
}tree[*maxn];
void modify(int k,int x,int y,int z){
if(tree[k].maxx<z) return ;
if(tree[k].l==tree[k].r){
tree[k].v%=z;tree[k].maxx=tree[k].v;
return ;
}
int m=(tree[k].l+tree[k].r)/;
if(x<=m) modify(*k,x,y,z);
if(y>m) modify(*k+,x,y,z);
tree[k].v=tree[k*].v+tree[*k+].v;
tree[k].maxx=max(tree[k*].maxx,tree[k*+].maxx);
}
void build(int l,int r,int k){
//important
tree[k].l=l,tree[k].r=r;
if(l==r){
scanf("%d",&tree[k].v);
tree[k].maxx=tree[k].v;
return ;
}
int m=(l+r)>>;
build(l,m,k*);
build(m+,r,k*+);
tree[k].v=tree[*k].v+tree[*k+].v;
tree[k].maxx=max(tree[*k].maxx,tree[*k+].maxx);
}
void sum(int k,int x,int y){
if(tree[k].l>=x&&tree[k].r<=y){
ans+=tree[k].v; //cout<<111<<endl;
return ;
}
int m=(tree[k].l+tree[k].r)/;
if(x<=m) sum(k*,x,y);
if(y>m) sum(k*+,x,y);
}
void change(int k,int x,int y){
if(tree[k].l==tree[k].r){
tree[k].v=y,tree[k].maxx=y;return;
}
int m=(tree[k].l+tree[k].r)/;
if(x<=m) change(k*,x,y);
else change(k*+,x,y);
tree[k].v=tree[*k].v+tree[*k+].v;
tree[k].maxx=max(tree[*k].maxx,tree[*k+].maxx);
}
int main(){
scanf("%d%d",&n,&m);
build(,n,);
while(m--){
int type,x,y,z;scanf("%d%d%d",&type,&x,&y);
if(type==){
ans=;
sum(,x,y);
printf("%lld\n",ans);
}
else if(type==){
scanf("%d",&z);
modify(,x,y,z);
}
else if(type==){
change(,x,y);
}
}
return ;
}

CF438D 线段树 区间求和,区间求膜,单点更新的更多相关文章

  1. HDU 3308 线段树 最长连续上升子序列 单点更新 区间查询

    题意: T个测试数据 n个数 q个查询 n个数 ( 下标从0开始) Q u v 查询 [u, v ] 区间最长连续上升子序列 U u v 把u位置改成v #include<iostream> ...

  2. 线段树 区间开方区间求和 & 区间赋值、加、查询

    本文同步发表于 https://www.zybuluo.com/Gary-Ying/note/1288518 线段树的小应用 -- 维护区间开方区间求和 题目传送门 约定: sum(i,j) 表示区间 ...

  3. POJ 3468 A Simple Problem with Integers(线段树模板之区间增减更新 区间求和查询)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 140120 ...

  4. HDU 3577Fast Arrangement(线段树模板之区间增减更新 区间求和查询)

    Fast Arrangement Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  5. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  6. hdu 4288 离线线段树+间隔求和

    Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  7. Codevs-4919 线段树练习4(区间加上一个值并求摸个区间整除k的数的个数,线段树+数组维护)

    给你N个数,有两种操作 1:给区间[a,b]内的所有数都增加X 2:询问区间[a,b]能被7整除的个数 输入描述 Input Description 第一行一个正整数n,接下来n行n个整数,再接下来一 ...

  8. HDU 1698 Just a Hook (线段树模板题-区间求和)

    Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...

  9. cf1132G 线段树解分区间LIS(一种全新的线段树解LIS思路)+单调栈

    /* 给定n个数的数列,要求枚举长为k的区间,求出每个区间的最长上升子序列长度 首先考虑给定n个数的数列的LIS求法:从左往右枚举第i点作为最大点的贡献, 那么往左找到第一个比a[i]大的数,设这个数 ...

随机推荐

  1. 如何卸载旧版本的dotnet core

    How to remove the .NET Core Runtime and SDK https://docs.microsoft.com/en-us/dotnet/core/versions/re ...

  2. Linux/shell: remove adjacent similar patterns

    cat > temp004AA1abcAA2AA3abcAA4abcAA5AA6 awk 'BEGIN {pre=0; str="";} { if(NR==1){     i ...

  3. office完全卸载

    第一步:先暂停office服务,再通过 控制面板--卸载程序  --卸载office应用 第二步:通过office_move(自己命名的工具)软件卸载  工具分享:https://pan.baidu. ...

  4. LuoguP2161 [SHOI2009]会场预约

    题目地址 题目链接 题解 用fhqtreap对区间进行维护. 可以注意到的是,对于当前存在的预约,他们一定是升序排列的(有重叠的都被删了). 那么就可以用按照位置分裂的fhqtreap搞了(预约无论按 ...

  5. 分布式强化学习基础概念(Distributional RL )

    分布式强化学习基础概念(Distributional RL) from: https://mtomassoli.github.io/2017/12/08/distributional_rl/ 1. Q ...

  6. NLP--- How to install the tool NLTK in Ubuntu ?

    NLP--- How to install the tool NLTK in Ubuntu ? 1. open the website of NLTK and download it.  https: ...

  7. Tutorial: Implementation of Siamese Network on Caffe, Torch, Tensorflow

    Tutorial: Implementation of Siamese Network with Caffe, Theano, PyTorch, Tensorflow  Updated on 2018 ...

  8. (转)Understanding, generalisation, and transfer learning in deep neural networks

    Understanding, generalisation, and transfer learning in deep neural networks FEBRUARY 27, 2017   Thi ...

  9. 函数嵌套函数传递this值

    <button onclick="demo()(this)">test</button> function demo(){ return function ...

  10. 自定义Exception——实战篇

    public class EntityConfigurationException : Exception { public EntityConfigurationException(string m ...