【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 moperations. 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.
题目大意:
给一个序列
支持3种操作
1 u v 对于所有i u<=i<=v,输出a[i]的和
2 u v t 对于所有i u<=i<=v a[i]=a[i]%t
3 u v 表示a[u]=v(将v赋值给a[u])
n,q<=1e5 a[i],t,v<=1e9
解题报告:
这题需要维护最大值的,不能直接判断是否sum=0了就返回。因为我操作可能给的模数很大,如果我给1e5次这个大模数操作,那就n^2logn了,肯定炸,但是用判断最大值的的方法就可以取消一大部分操作使复杂度降回来。
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
struct TREE {int l,r;ll sum;ll maxx;
} tree[MAX<<2];
ll a[MAX];
int n,m;
void pushup(int cur) {tree[cur].sum = tree[cur*2].sum + tree[cur*2+1].sum;tree[cur].maxx = max(tree[cur*2].maxx,tree[cur*2+1].maxx);
}
void build(int l,int r,int cur) {tree[cur].l = l,tree[cur].r = r;if(l == r) {tree[cur].sum = tree[cur].maxx = a[r];return ;}int m = (l+r)>>1;build(l,m,cur*2);build(m+1,r,cur*2+1);pushup(cur);
}
void update(int pl,int pr,ll val,int cur) {if(tree[cur].maxx < val) return ;if(tree[cur].l == tree[cur].r) {tree[cur].sum %=val;tree[cur].maxx %= val;return ;}if(pl <= tree[cur*2].r) update(pl,pr,val,cur*2);if(pr >= tree[cur*2+1].l) update(pl,pr,val,cur*2+1);pushup(cur);
}
void update2(int tar,ll val,int cur) {if(tree[cur].l == tree[cur].r) {tree[cur].sum = tree[cur].maxx = val;return ;}if(tar <= tree[cur*2].r) update2(tar,val,cur*2);if(tar >= tree[cur*2+1].l) update2(tar,val,cur*2+1);pushup(cur);
}
ll qSum(int pl,int pr,int cur) {if(pl <= tree[cur].l && pr >= tree[cur].r) return tree[cur].sum;ll res = 0;if(pl <= tree[cur*2].r) res += qSum(pl,pr,cur*2);if(pr >= tree[cur*2+1].l) res += qSum(pl,pr,cur*2+1); return res;
}
int main()
{cin>>n>>m;for(int i = 1; i<=n; i++) cin>>a[i];build(1,n,1);while(m--) {int op,u,v;ll t;scanf("%d",&op);if(op == 1) {scanf("%d%d",&u,&v);printf("%lld\n",qSum(u,v,1));}if(op == 2) {scanf("%d%d%lld",&u,&v,&t);if(u>v) swap(u,v);update(u,v,t,1);}if(op == 3) {scanf("%d%lld",&u,&t);update2(u,t,1);}}return 0 ;
}
/*
16:25 - 16:38
*/
【CodeForces - 438D】The Child and Sequence(线段树区间取模操作)相关推荐
- 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 ...
- 【bzoj4355】Play with sequence 线段树区间最值操作
题目描述 维护一个长度为N的序列a,现在有三种操作: 1)给出参数U,V,C,将a[U],a[U+1],...,a[V-1],a[V]都赋值为C. 2)给出参数U,V,C,对于区间[U,V]里的每个数 ...
- codeforces CF438D The Child and Sequence 线段树
$ \Rightarrow $ 戳我进CF原题 D. The Child and Sequence time limit per test: 4 seconds memory limit per te ...
- 【bzoj4695】最假女选手 线段树区间最值操作
题目描述 给定一个长度为 N 序列,编号从 1 到 N .要求支持下面几种操作: 1.给一个区间[L,R] 加上一个数x 2.把一个区间[L,R] 里小于x 的数变成x 3.把一个区间[L,R] ...
- 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
题目链接:http://codeforces.com/problemset/problem/438/D The Child and Sequence time limit per test:4 sec ...
- CodeForces - 272C Dima and Staircase (线段树区间更新)
题意: 见以下样例,给出 5 个区间,每个区间的高度已知.一共 4 次操作.每次操作都是从最左边开始向下垒一个宽为 w 高为h 的木块,过程见下图. 问每次垒木块的高度是多少? Input 5 1 2 ...
- Gorgeous Sequence线段树区间跟新
http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?pid=1018&ojid=0&cid=12578&hide= ...
- CodeForces - 1539F Strange Array(线段树区间合并)
题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,规定位置 iii 的贡献是:设 x=a[i]x=a[i]x=a[i],选择一个包含 iii 的区间 [l,r][l,r][l,r],将其中 ...
最新文章
- 微机原理—定时计数控制接口
- 大盘过4000,注意风险~
- 王高利:TCP Wrappers访问控制(hosts.allow,hosts.deny)
- 六、springcloud之配置中心Config
- 【文末赠书】牛顿:伸向未知量的利爪
- 软件测试技术qtp,51Testing独家连载:(四十一)精通QTP——自动化测试技术领航
- [Unity] Animation Blend Tree 中混合值变化时部分骨骼错误旋转 360 度的解决办法:将 Humanoid 改成 Generic
- java读取sh脚本_linux环境下java读取sh脚本并执行
- C++语言运算符重载
- c/c++教程 - 1.8 函数 形参 实参 值传递 声明调用 函数的分文件编写
- 码农们的聚餐,会复杂到什么程度?
- 7.MongoDB之读策略readPreference
- javascript 视频进度条制作
- 用什么材料作电磁屏蔽呢?
- Unity动画系统-配置Avatar
- python的多线程
- IDEA导入JUnit测试类
- pureftpd mysql 语句_FTP服务器之pure-ftpd常用指令详解
- SpringBoot之事务处理:隔离级别与传播行为
- 16进制转字符串字符串转16进制
热门文章
- [Leetcode][第99题][JAVA][恢复二叉搜索树][中序遍历]
- [Leetcode][第32题][JAVA][最长有效括号][动态规划][栈][正向逆向结合]
- [Leedcode][JAVA][第202题][快乐数]
- matlab如何找出最小的数据,读取数据并找出全部数据的最大值和最小值
- matlab窗函数带通滤波器,Matlab结合窗函数法设计数字带通FIR滤波器
- linux下anaconda3安装教程,Ubuntu18.04 安装 Anaconda3的教程详解
- C语言#define宏定义可能注意不到的地方
- C#中的变量类型(值类型、引用类型)
- pythonvim编辑教程_Pycharm学习教程(6) Pycharm作为Vim编辑器使用
- c++源码矢量图形编辑器_下一代代码编辑器的设想