线段树【CF620E】The Child and Sequence
Description
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 mm 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,mn,m (1<=n,m<=10^{5})(1<=n,m<=105) . The second line contains nn integers, separated by space: $ a[1],a[2],...,a[n] (1<=a[i]<=10^{9}) $ — initial value of array elements.
Each of the next mm lines begins with a number typetype
.
- If type=1type=1 , there will be two integers more in the line: $ l,r (1<=l<=r<=n) $ , which correspond the operation 1.
- If type=2type=2 , there will be three integers more in the line: $ l,r,x (1<=l<=r<=n; 1<=x<=10^{9}) $ , which correspond the operation 2.
- If type=3type=3 , there will be two integers more in the line: $ k,x (1<=k<=n; 1<=x<=10^{9}) $ , 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.
题目大意:
- 给出一个序列,进行如下三种操作:
- 区间求和
- 区间每个数模 xx
- 单点修改
- n,m≤100000
裸的线段树问题.,但是问题在于如何取模。
很容易想到的是,如果区间的最大值比取模的数小,那么我们就不需要修改。
因此,我们维护区间最大值。
但是如何修改?我们需要知道其位置。
因此,我们维护最大值位置,然后单点修改即可。
每次判断区间最大值时候比取模的数小。
如果小,那我们就不用取模,所以就可以切掉这个题了!
代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#define int long long
#define R register
using namespace std;
const int gz=1e5+8;
inline void in(int &x)
{
int f=1;x=0;char s=getchar();
while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
while(isdigit(s)){x=x*10+s-'0';s=getchar();}
x*=f;
}
#define ls o<<1
#define rs o<<1|1
int tr[gz<<2],mx[gz<<2],val[gz],n,m;
inline int idmax(R int x,R int y)
{
return val[x]>val[y] ? x:y;
}
inline void up(R int o)
{
mx[o]=idmax(mx[ls],mx[rs]);
tr[o]=tr[ls]+tr[rs];
}
void build(R int o,R int l,R int r)
{
if(l==r)
{
tr[o]=val[l];
mx[o]=l;
return;
}
R int mid=(l+r)>>1;
build(ls,l,mid);
build(rs,mid+1,r);
up(o);
}
void change(R int o,R int l,R int r,R int pos,R int del)
{
if(l==r){tr[o]=val[l];return;}
R int mid=(l+r)>>1;
if(pos<=mid)change(ls,l,mid,pos,del);
else change(rs,mid+1,r,pos,del);
up(o);
}
int query(R int o,R int l,R int r,R int x,R int y)
{
if(x<=l and y>=r)return tr[o];
R int mid=(l+r)>>1,res=0;
if(x<=mid)res+=query(ls,l,mid,x,y);
if(y>mid)res+=query(rs,mid+1,r,x,y);
return res;
}
int query_max(R int o,R int l,R int r,R int x,R int y)
{
if(l==x and y==r) return mx[o];
R int mid=(l+r)>>1;
if(y<=mid) return query_max(ls,l,mid,x,y);
else if(x>mid)return query_max(rs,mid+1,r,x,y);
else return idmax(query_max(ls,l,mid,x,mid),query_max(rs,mid+1,r,mid+1,y));
}
signed main()
{
in(n);in(m);
for(R int i=1;i<=n;i++)in(val[i]);
build(1,1,n);
for(R int l,r,k,opt;m;m--)
{
in(opt);
switch(opt)
{
case 1:in(l),in(r),printf("%lld\n",query(1,1,n,l,r));break;
case 2:break;
case 3:in(l),in(r);val[l]=r;change(1,1,n,l,r);break;
}
if(opt==2)
{
in(l),in(r),in(k);
for(R int pos;;)
{
pos=query_max(1,1,n,l,r);
if(val[pos]<k)break;
val[pos]%=k;
change(1,1,n,pos,val[pos]);
}
}
}
}
线段树【CF620E】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 ...
- 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 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 - 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 Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模
D. The Child and Sequence At the children's day, the child came to Picks's house, and messed his h ...
- [CF438D]The Child and Sequence【线段树】
题目大意 区间取模,区间求和,单点修改. 分析 其实算是一道蛮简单的水题. 首先线段树非常好解决后两个操作,重点在于如何解决区间取模的操作. 一开始想到的是暴力单点修改,但是复杂度就飙到了\(mnlo ...
- cf250D. The Child and Sequence(线段树 均摊复杂度)
题意 题目链接 单点修改,区间mod,区间和 Sol 如果x > mod ,那么 x % mod < x / 2 证明: 即得易见平凡, 仿照上例显然, 留作习题答案略, 读者自证不难. ...
- CF438D The Child and Sequence(线段树)
题目链接:CF原网 洛谷 题目大意:维护一个长度为 $n$ 的正整数序列 $a$,支持单点修改,区间取模,区间求和.共 $m$ 个操作. $1\le n,m\le 10^5$.其它数均为非负整数且 ...
- 2018.07.23 codeforces 438D. The Child and Sequence(线段树)
传送门 线段树维护区间取模,单点修改,区间求和. 这题老套路了,对一个数来说,每次取模至少让它减少一半,这样每次单点修改对时间复杂度的贡献就是一个log" role="presen ...
随机推荐
- [Leetcode] Populating next right pointer in each node 填充每个节点的右指针
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- BZOJ1607 [Usaco2008 Dec]Patting Heads 轻拍牛头 【筛法】
题目 今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏. 贝茜让N(1≤N≤100000)头奶牛坐成一个圈.除了1号与N号奶牛外,i号奶牛与i-l号和i+l号奶牛相邻.N号奶牛与1号奶牛相邻 ...
- 洛谷 P2486 [SDOI2011]染色/bzoj 2243: [SDOI2011]染色 解题报告
[SDOI2011]染色 题目描述 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同 ...
- 【BZOJ 3643】Phi的反函数 数搜索
这道题是典型的数搜索,讲究把数一层一层化小,而且还有最重要的大质数剪枝. #include <cstdio> #include <cmath> typedef long lon ...
- WordPress后台edit-tags.php里无限栏目分类实现
在 WordPress 里 http://localhost/wordpress3.6.1/wp-admin/edit-tags.php?taxonomy=category 这个链接可以显示 WP 里 ...
- CentOS 7, 升级python到3.x
By francis_hao Apr 11,2017 使用源码安装方式 首先到官网https://www.python.org/downloads/source/ 下载python最新版本.当前 ...
- json 串转成 java 对象再拼接成前台 html 元素
获取商品参数 json 串,转成 java 对象,再拼接成前台 html 的Service方法 @Override public String getItemParam(Long itemId) { ...
- [poj 2274]后缀数组+最长公共子串
题目链接:http://poj.org/problem?id=2774 后缀数组真的太强大了,原本dp是O(nm)的复杂度,在这里只需要O(n+m). 做法:将两个串中间夹一个未出现过的字符接起来,然 ...
- hdu Shell Necklace 5730 分治FFT
Description Perhaps the sea‘s definition of a shell is the pearl. However, in my view, a shell neckl ...
- 程序员的那些问题---转载自veryCD
展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告 走过的路,回忆起来是那么曲折,把自己的一些心得体会分享给程序员兄弟姐妹们,虽然时代在变化,但是很可能你也会走我已经做过 ...
.
.