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(线段树)
点此看题面 大致题意: 给你一个序列,让你支持区间求和.区间取模.单点修改操作. 区间取模 区间求和和单点修改显然都很好维护吧,难的主要是区间取模. 取模标记无法叠加,因此似乎只能暴力搞? 实际上,我 ...
随机推荐
- SQLServer导入Excel,复杂操作
导入Excel 先导入的时候报错了, 提示未在本地计算机上注册"Microsoft.ACE.Oledb.12.0"提供程序.(System.Data),去网址下个软件安装就搞定了, ...
- c#中使用NetCDF存储二维数据的读写操作简单应用
[DllImport( [DllImport( [DllImport( ...
- active admin
activeadmin 1.0.0.pre4 所依赖的库 gem 'jquery-rails', '~> 4.0.4' 4.2版本会出现找不到jquery-ui 的datepicker错误 使用 ...
- 【leetcode刷题笔记】Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- SOA 面向服务架构 阅读笔记(五)
14 SOA 服务管理器 契约:契约中必须明确定义双方的责任,否则就会产生混乱. SOA可以管理端到端的流程. IT技术一直是与业务对齐的. 14.1.1 分解IT层 业务服务层 管道层 硬件层 管道 ...
- centos7环境下zookeeper的搭建步骤之单机伪集群
首先说明:这里是单机版的伪集群搭建 第一步:下载zookeeper:zookeeper的下载地址: http://mirror.bit.edu.cn/apache/zookeeper/ 第二步:安装: ...
- idea创建git分支
此时只是在本地创建好了分支,修改源代码后add,commit将本地分支提交到远程仓库 分支已创建,其它成员此时就可以从git拉分支
- 什么是OOM?如何解决OOM问题!
1.什么是OOM? 程序申请内存过大,虚拟机无法满足我们,然后自杀了.这个现象通常出现在大图片的APP开发,或者需要用到很多图片的时候.通俗来讲就是我们的APP需要申请一块内存来存放图片的时候,系统认 ...
- Jboss remote getshell (JMXInvokerServlet) vc版
#include "stdafx.h" #include <Windows.h> #include <stdio.h> #include <winht ...
- MATLAB一个数组中另一个数组的值
c = setdiff(a,b) 删掉素组a中数组b的元素 如: