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/438/problem/D
Description
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?
Input
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.
Output
For each operation 1, please print a line containing the answer. Notice that the answer may exceed the 32-bit integer.
Sample Input
5 5
1 2 3 4 5
2 3 5 4
3 3 5
1 2 5
2 1 3 3
1 1 3
Sample Output
8
5
HINT
题意
给你n个数,三个操作
1.输出[l,r]的和
2.将[l,r]中的数,对v取摸
3.把a[x]变成v
题解:
线段树
区间定值和区间和很简单
区间取摸的话,需要维护一个区间最大值,如果这个区间的最大值小于要取摸的数,那么就直接break就好了
代码
#include<iostream> #include<stdio.h> using namespace std; #define maxn 100005 struct node {int l,r;long long mx,sum; }a[maxn*4]; int d[maxn]; void build(int x,int l,int r) {a[x].l = l,a[x].r = r;if(l==r){a[x].mx = a[x].sum = d[l];return;}int mid = (l+r)/2;build(x<<1,l,mid);build(x<<1|1,mid+1,r);a[x].mx = max(a[x<<1].mx , a[x<<1|1].mx);a[x].sum = a[x<<1|1].sum + a[x<<1].sum; } void change(int x,int pos,long long val) {int l = a[x].l,r = a[x].r;if(l==r){a[x].mx = a[x].sum = val;return;}int mid = (l+r)/2;if(pos<=mid)change(x<<1,pos,val);elsechange(x<<1|1,pos,val);a[x].mx = max(a[x<<1].mx,a[x<<1|1].mx);a[x].sum = a[x<<1].sum + a[x<<1|1].sum; } void mod(int x,int l,int r,long long val) {int L = a[x].l,R = a[x].r;if(a[x].mx<val)return;if(L==R){a[x].sum%=val;a[x].mx%=val;return;}int mid = (L+R)/2;if(r<=mid)mod(x<<1,l,r,val);else if(l>mid)mod(x<<1|1,l,r,val);else mod(x<<1,l,mid,val),mod(x<<1|1,mid+1,r,val);a[x].sum = a[x<<1].sum + a[x<<1|1].sum;a[x].mx = max(a[x<<1].mx,a[x<<1|1].mx); } long long get(int x,int l,int r) {int L = a[x].l,R = a[x].r;if(L>=l&&R<=r){return a[x].sum;}int mid = (L+R)/2;long long sum1 = 0,sum2 = 0;if(r<=mid)sum1 = get(x<<1,l,r);else if(l>mid)sum2 = get(x<<1|1,l,r);else sum1 = get(x<<1,l,mid),sum2 = get(x<<1|1,mid+1,r);return sum1 + sum2; } int main() {int n,q;scanf("%d%d",&n,&q);for(int i=1;i<=n;i++)scanf("%d",&d[i]);build(1,1,n);while(q--){int op;scanf("%d",&op);if(op==1){int x,y;scanf("%d%d",&x,&y);printf("%lld\n",get(1,x,y));}if(op==2){int x,y,z;scanf("%d%d%d",&x,&y,&z);mod(1,x,y,z);}if(op==3){int x,y;scanf("%d%d",&x,&y);change(1,x,y);}} }
Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸相关推荐
- [Codeforces Round #250 (Div. 1) -D] The Child and Sequence
Codeforces传送门 洛谷传送门 题意翻译 给定数列,区间查询和,区间取模,单点修改. n,m≤105n,m≤105n,m\le 10^5 题目描述 At the children's day, ...
- Codeforces Round #250 (Div. 2) A - The Child and Homework
传送门Codeforces Round #250 (Div. 2) A - The Child and Homework 第一次做完之后交上去,过了例子.顺手就锁定了...然后一个小时之后就被HACK ...
- 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 ...
- Codeforces Round #250 (Div. 2)—A. The Child and Homework
好题啊,被HACK了.曾经做题都是人数越来越多.这次比赛 PASS人数 从2000直掉 1000人 被HACK 1000多人! ! ! ! 没见过的科技啊 1 2 4 8 这组数 被黑的 ...
- Codeforces Round #686 (Div. 3) F. Array Partition(二分+线段树)
题意:一段区间,让你分割成三段,第一段取max,第二段取min,第三段取max.问你怎么分割这个区间. 题解: 三个区间我们可以用两个点将一段区间分成三段区间. 二分:我们首先找这个题有关的单调性,我 ...
- Codeforces Round #223 (Div. 2): E. Sereja and Brackets(线段树)
题意: 给你一个括号序列和m次询问,每次询问区间[L, R]内匹配的括号个数 思路: 这道题线段树只用来维护区间最小值,所以理论上RMQ也可以,主要是要稍微推一下 设左括号为1,右括号为-1,s[]为 ...
- Codeforces Round #807 (Div. 2) E. Mark and Professor Koro(线段树二分)
E. Mark and Professor Koro 题意 给定一个长度为n的数组,有q次更新操作,每次更新会将下标为k的元素的值更新为l,数组的更新是永久的.将数组更新后假设重复做以下操作,求可以得 ...
- Codeforces Round 250(Div. 2)
layout: post title: Codeforces Round 250 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- Codeforces Round #453 (Div. 1) D. Weighting a Tree 构造 + dfs树
传送门 文章目录 题意: 思路: 题意: 给你一颗nnn个点的图,每个点都有一个点权cic_ici,要求你给每个边赋一个权值kik_iki,要求对于每个点与他相连的边的权值之和等于这个点的点权ci ...
最新文章
- 深入了解MyBatis返回值
- mysql heartbeat 高可用
- gpt 语言模型_您可以使用语言模型构建的事物的列表-不仅仅是GPT-3
- vue-cli新建的项目webpack设置涉及的大部分插件整理
- android前台进程视频教程,Android Twilio视频通话,唤醒应用程序并进入前台
- 不知道用什么图表展示数据?看这份图表选择指南就够了
- 不使用for完成一段有空格间隔的字符串,分辨长度大于等于4的单词(求各位高人修改,我表示我是菜鸟,这个算法实在太长了)...
- 大数据之_亿级分布式日志管理ELK_工作笔记001_ELK认识介绍
- JavaScript中调皮的undefined
- [转载]EXT核心API详解Ext.data(八)- Connection/Ajax/Record javascript
- 2012 定制化产品探讨(周金根).pdf
- logistic和logitraw
- JDY-10M组网 蓝牙MESH组网
- python 人体建模_Matplotlib学习---可视化人体姿态
- 程序员常用英语单词汇总
- VS2015+Qt5.9.2 调试遇到无法找到入口 无法定位程序输入点的解决办法
- Tracup|拒绝低效办公,8个Tips让你芜湖起飞,工作效率MAX
- Network 【HTTPS请求/AFN】
- arctanx麦克劳林公式推导过程_考研高数公式知识点整理
- mac vbox 共享文件夹_Mac系统VirtualBox中CentOS 7.2启用共享文件夹
热门文章
- 面试题10-二进制中1的个数
- Tsinsen A1517. 动态树 树链剖分,线段树,子树操作
- C++ Custom Control控件 向父窗体发送对应的消息
- 无缩进的XML字符串的处理
- (libgdx学习)Net的使用
- Jquery插件之ajaxForm ajaxSubmit的理解用法
- 【Erlang新手成长日记】Erlang开源项目推荐
- Sequelize-nodejs-8-Transactions
- C# DataTable常用方法总结
- “锐捷伴你行”之“300路”喊话郭德纲:有空来蹭网呀!