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 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.
Examples
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
10 10
6 9 6 7 6 1 10 10 9 5
1 3 9
2 7 10 9
2 5 10 8
1 4 7
3 3 7
2 7 9 9
1 2 4
1 6 6
1 5 9
3 1 10
49
15
23
1
9
Note
Consider the first testcase:
- At first, a = {1, 2, 3, 4, 5}.
- After operation 1, a = {1, 2, 3, 0, 1}.
- After operation 2, a = {1, 2, 5, 0, 1}.
- At operation 3, 2 + 5 + 0 + 1 = 8.
- After operation 4, a = {1, 2, 2, 0, 1}.
- At operation 5, 1 + 2 + 2 = 5.
题意:给出数组,有三种操作,分别是区间求和,区间取模 ,单点修改。
思路:一个点被取模,那么其大小减半,所以一个数最多被操作log次,这样的话就不难想到势能线段树。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=;
int Mx[maxn<<]; ll sum[maxn<<];
void build(int Now,int L,int R)
{
if(L==R){
scanf("%d",&Mx[Now]);
sum[Now]=Mx[Now]; return ;
}
int Mid=(L+R)>>;
build(Now<<,L,Mid); build(Now<<|,Mid+,R);
Mx[Now]=max(Mx[Now<<],Mx[Now<<|]);
sum[Now]=sum[Now<<]+sum[Now<<|];
}
ll query(int Now,int L,int R,int l,int r){
if(l<=L&&r>=R) return sum[Now];
int Mid=(L+R)>>; ll res=;
if(l<=Mid) res+=query(Now<<,L,Mid,l,r);
if(r>Mid) res+=query(Now<<|,Mid+,R,l,r);
return res;
}
void change(int Now,int L,int R,int pos,int val)
{
if(L==R){
Mx[Now]=val; sum[Now]=val; return ;
}
int Mid=(L+R)>>;
if(pos<=Mid) change(Now<<,L,Mid,pos,val);
else change(Now<<|,Mid+,R,pos,val);
Mx[Now]=max(Mx[Now<<],Mx[Now<<|]);
sum[Now]=sum[Now<<]+sum[Now<<|];
}
void modp(int Now,int L,int R,int l,int r,int P)
{
if(Mx[Now]<P) return ;
if(L==R) {
Mx[Now]%=P; sum[Now]=Mx[Now]; return ;
}
int Mid=(L+R)>>;
if(l<=Mid) modp(Now<<,L,Mid,l,r,P);
if(r>Mid) modp(Now<<|,Mid+,R,l,r,P);
Mx[Now]=max(Mx[Now<<],Mx[Now<<|]);
sum[Now]=sum[Now<<]+sum[Now<<|];
}
int main()
{
int N,M,opt,L,R,P;
scanf("%d%d",&N,&M);
build(,,N);
while(M--){
scanf("%d",&opt);
if(opt==) {
scanf("%d%d",&L,&R);
printf("%I64d\n",query(,,N,L,R));
}
else if(opt==){
scanf("%d%d%d",&L,&R,&P);
modp(,,N,L,R,P);
}
else {
scanf("%d%d",&L,&R);
change(,,N,L,R);
}
}
return ;
}
CodeForces - 438D: The Child and Sequence(势能线段树)的更多相关文章
- 2018.07.23 codeforces 438D. The Child and Sequence(线段树)
传送门 线段树维护区间取模,单点修改,区间求和. 这题老套路了,对一个数来说,每次取模至少让它减少一半,这样每次单点修改对时间复杂度的贡献就是一个log" role="presen ...
- Codeforces 438D (今日gg模拟第二题) | 线段树 考察时间复杂度的计算 -_-|||
Codeforces 438D The Child and Sequence 给出一个序列,进行如下三种操作: 区间求和 区间每个数模x 单点修改 如果没有第二个操作的话,就是一棵简单的线段树.那么如 ...
- 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 ...
- CodeForces 438D The Child and Sequence (线段树 暴力)
传送门 题目大意: 给你一个序列,要求在序列上维护三个操作: 1)区间求和 2)区间取模 3)单点修改 这里的操作二很讨厌,取模必须模到叶子节点上,否则跑出来肯定是错的.没有操作二就是线段树水题了. ...
- 题解——CodeForces 438D The Child and Sequence
题面 D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input ...
- 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 284 C. Cows and Sequence(线段树)
题目链接:http://codeforces.com/contest/284/problem/C 题意:就是给出3个操作 1)是将前i 个数加x 2)在数组最后添加一个数x 3)删除数组最后的那个数 ...
- [CF438D]The Child and Sequence【线段树】
题目大意 区间取模,区间求和,单点修改. 分析 其实算是一道蛮简单的水题. 首先线段树非常好解决后两个操作,重点在于如何解决区间取模的操作. 一开始想到的是暴力单点修改,但是复杂度就飙到了\(mnlo ...
- CF438D The Child and Sequence(线段树)
题目链接:CF原网 洛谷 题目大意:维护一个长度为 $n$ 的正整数序列 $a$,支持单点修改,区间取模,区间求和.共 $m$ 个操作. $1\le n,m\le 10^5$.其它数均为非负整数且 ...
- 【CF438D】The Child and Sequence(线段树)
点此看题面 大致题意: 给你一个序列,让你支持区间求和.区间取模.单点修改操作. 区间取模 区间求和和单点修改显然都很好维护吧,难的主要是区间取模. 取模标记无法叠加,因此似乎只能暴力搞? 实际上,我 ...
随机推荐
- Linux 关机命令详解 转自脚本之家
在linux下一些常用的关机/重启命令有shutdown.halt.reboot.及init,它们都可以达到重启系统的目的,但每个命令的内部工作过程是不同的. Linux centos重启命令: 1. ...
- C51数据类型
- 每天一个Linux命令(40)vmstat命令
vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存.进程.CPU活动进行监控. 它能够对系统的整体情况进行统计,无法对某个进程 ...
- vagrant搭建
1.在官网下载对应的vagrant版本 https://www.vagrantup.com/downloads.html (下载最新版本) https://releases.hashicorp.com ...
- github使用——如何恢复被删去文件。
首先git删除文件包括以下几种情况 删除本地文件,但是未添加到暂存区: 删除本地文件,并且把删除操作添加到了暂存区: 把暂存区的操作提交到了本地git库: 把本地git库的删除记录推送到了远程服务器g ...
- springboot中Controller没有被扫描
今天给客户开发登陆的密码加密需求,研究一下想,需要在本地搭一套环境,前台用js实现RAS加密,后台使用java解密.本是一套非常简单的环境,看最近springboot比较常用,所以想要搭一下sprin ...
- Android LCD(二):常用接口原理篇【转】
本文转载自:http://blog.csdn.net/xubin341719/article/details/9125799 关键词:Android LCD TFT TTL(RGB) LVDS E ...
- mysql慢查询设置
不同版本的mysql命令和配置不一样,以下是2个版本 修改配置文件 log-slow-queries=/alidata/mysql-log/mysql-slow.log long_query_time ...
- JavaScript -- 广告随鼠标移动, 点击一次后关闭
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- ubuntu/centos printk 终端中不能打印信息及解决办法
今天用ubuntu来调试信息,printk死活打印不出信息,即使把级别跳到<0>,即KERN_ALERT也不行,后再搜了好长时间网络, 这个地址:http://bbs.chinaunix. ...