Codeforces Round #250 (Div. 1) D. 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 him. A lot of important things were lost, in particular the favorite sequence of Picks.
Fortunately, Picks remembers how to repair the sequence. Initially he should create an integer array a[1], a[2], ..., a[n]. Then he should perform a sequence of m operations. An operation can be one of the following:
- Print operation l, r. Picks should write down the value of
.
- Modulo operation l, r, x. Picks should perform assignment a[i] = a[i] mod x for each i (l ≤ i ≤ r).
- Set operation k, x. Picks should set the value of a[k] to x (in other words perform an assignment a[k] = x).
Can you help Picks to perform the whole sequence of operations?
The first line of input contains two integer: n, m (1 ≤ n, m ≤ 105). The second line contains n integers, separated by space:a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109) — initial value of array elements.
Each of the next m lines begins with a number type .
- If type = 1, there will be two integers more in the line: l, r (1 ≤ l ≤ r ≤ n), which correspond the operation 1.
- If type = 2, there will be three integers more in the line: l, r, x (1 ≤ l ≤ r ≤ n; 1 ≤ x ≤ 109), which correspond the operation 2.
- If type = 3, there will be two integers more in the line: k, x (1 ≤ k ≤ n; 1 ≤ x ≤ 109), which correspond the operation 3.
For each operation 1, please print a line containing the answer. Notice that the answer may exceed the 32-bit integer.
5 5
1 2 3 4 5
2 3 5 4
3 3 5
1 2 5
2 1 3 3
1 1 3
8
5 题意:
操作1:l,r 求l到r之间的区间和
操作2:l,r,c 将l到r之间的数分别取模x
操作3:l,r 讲a[l]改成r;
题解:
线段树的区间操作
对于去摸:我们设置一个区间段最大值,要取得模要是大于这个最大则之间退出
这就是优化
///
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define inf 1000000007
#define mod 1000000007
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//************************************************
const int maxn=+;
int a[maxn],n,m,q;
struct ss{
int l,r;
ll v,sum,tag;
}tr[maxn*];
void build(int k,int s,int t)
{
tr[k].l=s;tr[k].r=t;
tr[k].v=;tr[k].tag=;tr[k].sum=;
if(s==t){
tr[k].sum=a[s];
tr[k].v=a[s];
return ;
}
int mid=(s+t)>>;
build(k<<,s,mid);
build(k<<|,mid+,t);
tr[k].sum=tr[k<<].sum+tr[k<<|].sum;
tr[k].v=max(tr[k<<].v,tr[k<<|].v);
}
ll ask(int k,int s,int t)
{
if(tr[k].l==s&&tr[k].r==t){
return tr[k].sum;
}
int mid=(tr[k].l+tr[k].r)>>;
ll ret=;
if(t<=mid)ret= ask(k<<,s,t);
else if(s>mid)ret= ask(k<<|,s,t);
else {
ret=(ask(k<<,s,mid)+ask(k<<|,mid+,t));
} tr[k].sum=tr[k<<].sum+tr[k<<|].sum;
tr[k].v=max(tr[k<<].v,tr[k<<|].v);
return ret;
}
void modify(int k,int s,int t,ll c)
{
if(tr[k].v<c)return ;
if(tr[k].l==tr[k].r){
tr[k].sum%=c;
tr[k].v%=c;
return ;
}
int mid=(tr[k].l+tr[k].r)>>;
if(t<=mid)modify(k<<,s,t,c);
else if(s>mid)modify(k<<|,s,t,c);
else {
modify(k<<,s,mid,c);modify(k<<|,mid+,t,c);
}
tr[k].sum=tr[k<<].sum+tr[k<<|].sum;
tr[k].v=max(tr[k<<].v,tr[k<<|].v);
}
void update(int k,int x,int c)
{
if(tr[k].l==x&&tr[k].r==x){
tr[k].sum=c;
tr[k].v=c;
return;
}
int mid=(tr[k].l+tr[k].r)>>;
if(x<=mid)update(k<<,x,c);
else {
update(k<<|,x,c);
}
tr[k].sum=tr[k<<].sum+tr[k<<|].sum;
tr[k].v=max(tr[k<<].v,tr[k<<|].v);
}
int main(){ n=read();m=read();
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}build(,,n);int x,y;ll c;
for(int i=;i<=m;i++){
scanf("%d",&q);
if(q==){
scanf("%d%d",&x,&y);
printf("%I64d\n",ask(,x,y));
}
else if(q==){
scanf("%d%d%I64d",&x,&y,&c);
modify(,x,y,c);
}
else {
scanf("%d%d",&x,&y);
update(,x,y);
} }
return ;
}
代码
Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模的更多相关文章
- 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 ...
- 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, ...
- 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 ...
- 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 ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
- Codeforces Round #332 (Div. 2) C. Day at the Beach 线段树
C. Day at the Beach Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/599/p ...
- Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)
题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...
- Codeforces Round #225 (Div. 2) E. Propagating tree dfs序+-线段树
题目链接:点击传送 E. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp
D. Babaei and Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/D Description As you ...
随机推荐
- Zynq7000系列之芯片引脚功能综述
很多人做了很久的FPGA,知道怎么去给信号分配引脚,却对这些引脚的功能及其资源限制知之甚少:在第一章里对Zynq7000系列的系统框架进行了分析和论述,对Zynq7000系列的基本资源和概念有了大致的 ...
- VS2015 安装包缺失(联网安装失败)问题解决
Win7 x86 测试可行 * 如果前面有尝试过安装不成功, 一定要用卸载程序删除已安装的部分,否则会出乱子. 1. 或者是用虚拟光驱加载ISO, 或者是解压到硬盘上, 都没有关系. 2. 用管理员 ...
- CAD使用GetXData读数据(网页版)
主要用到函数说明: MxDrawEntity::GetXData 返回实体的扩展数据. js代码实现如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 ...
- 梦想CAD控件安卓选择集
在本示例中将使用构造选择集对被过滤对象进行过滤,该类封装了选择集及其处理函数,支持如下过滤条件. 参数类型 类型 RTDXF0 TEXT 文字 MTEXT 多行文字 CIRCLE 圆 ARC 圆弧 L ...
- 浅谈GFC
Web页面的布局,我们常见的主要有“浮动布局(float)”.“定位布局(position)”.“行内块布局(inline-block)”.“CSS3的多栏布局(Columns)”.“伸缩布局(Fle ...
- 数据库——DBUtils和连接池
第一章 DBUtils如果只使用JDBC进行开发,我们会发现冗余代码过多,为了简化JDBC开发,本案例我们讲采用apache commons组件一个成员:DBUtils.DBUtils就是JDBC的简 ...
- Python学会之后,一般能拿到多少工资?
Python在约40年前出现以来,已经有数以千计基于这项技术的网站和软件项目,Python因其独有的特点从众多开发语言中脱颖而出,深受世界各地的开发者喜爱. 随着Python的技术的流行,Python ...
- PHP下载压缩包文件
PHP 压缩文件需要用到 ZipArchive 类,Windows 环境需要打开 php_zip.dll扩展. 压缩文件 $zip = new ZipArchive(); // 打开一个zip文档,Z ...
- 还没更换RubyGems镜像?
相信用过Ruby的人都知道 gem install 命令,但是在国内该命令安装的速度甚是不稳定(你懂的),导致尝试数次便是等待数时,记得之前在安装redmine的时候便是如此,之前不懂什么意思,还以为 ...
- 第四章 模块化React和Redux应用
第四章 模块化React和Redux应用 4.1 模块化应用要点 构建一个应用的基础: 代码文件的组织结构: 确定模块的边界: Store的状态树设计. 4.2 代码文件的组织方式 4.2.1 按角色 ...