codeforce438D The Child and Sequence
codeforce438D 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
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
Output
8
5
Input
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
Output
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.
一.解题思路及要点
题目大意:长度为n的非负整数数列,3种操作
- 求[L,R]所有数的和。
- 将[L,R]中所有数都mod x。
- 将a[i]修改为v。
线段树的表示方法:
(1) 利用数组来表示(如果需要维护多个值则需要开多个数组)
(2)利用结构体来表示(包括l,r,val等)
区间取模的应用: 对于一个区间如果其最大值都小于mod(对mod取模),则无需操作,否则就需要暴力取模,复杂度为nlog(n)
线段树区间修改,区间查询,单点修改等操作的应用
每次修改后都需要维护区间的和与区间的最大值
t[ind]=t[ind<<1]+t[ind<<1|1];
m[ind]=max(m[ind<<1],m[ind<<1|1]);
二.完整代码
using namespace std;
#include<bits/stdc++.h>
int n,mm,type;
const int maxn=100005;
long long t[maxn<<2];//用于维护区间的和
long long m[maxn<<2];//用于维护区间最大值
long long int a[maxn];void build(long long k,long long l,long long r)
{if(l==r) {t[k]=a[l];m[k]=a[l];return;//注意需要返回并跳出函数}long long mid=(l+r)>>1;build(k<<1,l,mid);build(k<<1|1,mid+1,r); t[k]=t[k<<1]+t[k<<1|1];m[k]=max(m[k<<1],m[k<<1|1]);return ;
}void upd(long long k,long long l,long long r,long long ind,long long x)
{if(l==r){t[ind]=x;m[ind]=x;return ;//注意需要返回并跳出函数}long long mid=(l+r)>>1;if(k<=mid)upd(k,l,mid,ind<<1,x);else upd(k,mid+1,r,ind<<1|1,x);t[ind]=t[ind<<1]+t[ind<<1|1];m[ind]=max(m[ind<<1],m[ind<<1|1]);
}
long long query(long long l,long long r,long long L,long long R,long long ind)
{//注意是L>=l而不是l>=L,注意此处判断条件的理解:当前区间是所求区间的子集时直接返回t[ind]if(L>=l&&R<=r) return t[ind];long long mid=(L+R)>>1;long long ans=0;//注意这种思路的学习,利用两个if语句就将所有的情况包含进去if(l<=mid)ans+=query(l,r,L,mid,ind<<1);if(r>mid) ans+=query(l,r,mid+1,R,ind<<1|1);return ans;
}void modee(long long l,long long r ,long long L,long long R ,long long ind,long long x)
{if(l<=L&&r>=R&&m[ind]<x) return ;//注意是<x不是<=xif(L==R){t[ind]%=x;m[ind]=t[ind];return;//记住要返回}long long mid=(L+R)>>1;//注意这种思路的学习,利用两个if语句就将所有的情况包含进去if(l<=mid) modee(l,r,L,mid,ind<<1,x);if(r>mid) modee(l,r,mid+1,R,ind<<1|1,x);t[ind]=t[ind<<1]+t[ind<<1|1];m[ind]=max(m[ind<<1],m[ind<<1|1]);
}int main()
{scanf("%d%d",&n,&mm);for(long long i=1;i<=n;i++){scanf("%lld",&a[i]);}build(1,1,n);long long int l,r,x,k;while(mm--){scanf("%d",&type);if(type==1){scanf("%lld%lld",&l,&r);printf("%lld\n",query(l,r,1,n,1));}else if(type==2){scanf("%lld%lld%lld",&l,&r,&x);modee(l,r,1,n,1,x);}else if(type==3){scanf("%lld%lld",&k,&x);upd(k,1,n,1,x);}}
}
codeforce438D The Child and Sequence相关推荐
- 数据结构二之线段树Ⅱ——KiKi‘s K-Number,ball,The Child and Sequence,「雅礼集训 2017 Day1」市场,Atlantis
值域线段树+势能线段树+扫描线 KiKi's K-Number ball The Child and Sequence 「雅礼集训 2017 Day1」市场 Atlantis KiKi's K-Num ...
- codeforces438 D. The Child and Sequence
2020威海区域赛G. Caesar Cipher就用到了此思想( 今天碰到模板题了还是再写一遍吧 D. The Child and Sequence 区间取模操作模板题 有一个公式 x%p<x ...
- codeforces CF438D The Child and Sequence 线段树
$ \Rightarrow $ 戳我进CF原题 D. The Child and Sequence time limit per test: 4 seconds memory limit per te ...
- 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 438D The Child and Sequence
题目链接:http://codeforces.com/problemset/problem/438/D The Child and Sequence time limit per test:4 sec ...
- 【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 ...
- [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 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 ...
- The Child and Sequence
Codeforces Round #250 (Div. 1)D:http://codeforces.com/problemset/problem/438/D 题意:给你一个序列,然后有3种操作 1x ...
最新文章
- c++各种数据类型表示范围
- 【PHPWord】TitleTOC
- mysql等价语句是_Mysql基本语句(个人笔记)
- 【 NLS 】Steepest Descent Algorithm Iteration Procedure of TOA - Based Positioning
- 不仅能搜索还能查信息 带你了解LBS应用
- Boost:path的测试程序
- 1001: [BeiJing2006]狼抓兔子
- python php 网站_python php网站
- Springboot系列之Shiro、JWT、Redis 进行认证鉴权
- Linux下安装配置Nexus
- MSN P2P资料转载
- 【转】HttpCompress
- 前端程序员也需要知道进程和线程
- db2 linux 64位下载,Redhat6.2 64位 安装DB2V10.5
- 应用随机过程——张波
- Androi实现三个页面跳转
- 智慧医疗是什么?智慧医院包括哪些方面?
- 猜数字(Bulls and Cows)游戏
- Python中os.sep的用法
- 基于vb的mysql管理系统代码_基于VB+MySQL的简单图书管理系统
热门文章
- OSChina 周一乱弹 —— 程序员的浪漫你不懂
- python决策树代码解读_建模分析之机器学习算法(附pythonR代码)
- SpringMvc-@ExceptionHandler
- C语言速看,C语言高速入门系列(二)
- 1.12_count_sort_计数排序
- php机器人聊天对话框,仿机器人聊天窗口(热身)
- cip协议服务器,控制及信息协议(CIP)
- shell 分割字符串_谈一谈Shell中的贪婪匹配和非贪婪匹配
- 10分钟python游戏_牛得一批!10分钟用Python编写一个贪吃蛇小游戏
- axis在matlab中是什么意思_珠宝首饰上的钢印是什么意思呢?你都知道吗