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(线段树)
点此看题面 大致题意: 给你一个序列,让你支持区间求和.区间取模.单点修改操作. 区间取模 区间求和和单点修改显然都很好维护吧,难的主要是区间取模. 取模标记无法叠加,因此似乎只能暴力搞? 实际上,我 ...
随机推荐
- oracle 函数 截取 连接 替换 判断
一个处理不规范日期的函数,廖记一下吧,以免再忘. --注意全角半角 CREATE OR REPLACE function f_str2form( date_string in varchar2 ) r ...
- pyhton3 re模块
本文转自 AstralWind 的博客:Python正则表达式指南 特来收藏 1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有 ...
- Web前端开发的基本要求和认识
Web前端开发技术包括三个要素:HTML.CSS和JavaScript,但随着RIA的流行和普及,Flash/Flex.Silverlight.XML和服务器端语言也是前端开发工程师应该掌握的.Web ...
- 【leetcode刷题笔记】Text Justification
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- Docker容器技术-优化Docker镜像
一.优化Docker镜像 1.降低部署时间 一个大的Docker应用是如何影响在新Docker宿主机上的部署时间. (1)编写Dockerfile创建一个大Docker镜像 [root@bogon ~ ...
- 二、linux题型
1.[root@pyrene ~]# 这里root是当前登录用户 @分割 pyrene是主机名 -:表示当前登录环境 #:表示管理员 2.在/data下面创建一个文件oldboy. ...
- Kubernetes Controller Manager
Controller Manager 作为集群内部的管理控制中心,负责集群内的Node.Pod副本.Service Endpoint.NameSpace.ServiceAccount.Resource ...
- 开机启动顺序rc.local与chkconfig的不同
/etc/rc.local文件有如下两行/etc/init.d/mysql start/etc/init.d/keepalived start /etc/rc.local是按脚本的顺序一个启动后启动下 ...
- 算法总结之 在两个排序数组中找到第K小的数
给定两个有序数组arr1 和 arr2 ,再给定一个int K,返回所有的数中第K小的数 要求长度如果分别为 N M,时间复杂度O(log(min{M,N}),额外空间复杂度O(1) 解决此题的方法跟 ...
- Tensorflow 从零开始
1.安装pip pip是一个用于管理和安装Python包的工具,类似于LINUX 的yum命令一样! 命令(Ubuntu系统):sudo apt-get install python-pip 验证安装 ...