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:

  1. Print operation \(l,r\) . Picks should write down the value of .
  2. Modulo operation \(l,r,x\) . Picks should perform assignment $ a[i]=a[i] mod x $ for each \(i (l<=i<=r)\) .
  3. 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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. [CF438D]The Child and Sequence【线段树】

    题目大意 区间取模,区间求和,单点修改. 分析 其实算是一道蛮简单的水题. 首先线段树非常好解决后两个操作,重点在于如何解决区间取模的操作. 一开始想到的是暴力单点修改,但是复杂度就飙到了\(mnlo ...

  7. cf250D. The Child and Sequence(线段树 均摊复杂度)

    题意 题目链接 单点修改,区间mod,区间和 Sol 如果x > mod ,那么 x % mod < x / 2 证明: 即得易见平凡, 仿照上例显然, 留作习题答案略, 读者自证不难. ...

  8. CF438D The Child and Sequence(线段树)

    题目链接:CF原网  洛谷 题目大意:维护一个长度为 $n$ 的正整数序列 $a$,支持单点修改,区间取模,区间求和.共 $m$ 个操作. $1\le n,m\le 10^5$.其它数均为非负整数且 ...

  9. 2018.07.23 codeforces 438D. The Child and Sequence(线段树)

    传送门 线段树维护区间取模,单点修改,区间求和. 这题老套路了,对一个数来说,每次取模至少让它减少一半,这样每次单点修改对时间复杂度的贡献就是一个log" role="presen ...

随机推荐

  1. 2017博普杯 东北大学邀请赛(B. Drink too much water)(贪心+树链剖分)

    题目地址:https://oj.neu.edu.cn/problem/1204 题目大意: 其实就是树上的线段覆盖, 给出一棵n个结点的树,然后给出树上的一些路径进行覆盖,然后要求选取最少的点,能够把 ...

  2. 一个acm过来人的心得

    刻苦的训练我打算最后稍微提一下.主要说后者:什么是有效地训练? 我想说下我的理解.        很多ACMer入门的时候,都被告知:要多做题,做个500多道就变牛了.其实,这既不是充分条件.也不会是 ...

  3. 收藏一个漂亮的Flash焦点图切换

    网上闲逛的时候发现一个Flash焦点图效果,跟喜欢,然后就下载回来,收集在这里,以便以后方便取用.这个Flash使用方法也是相当简单的,如果你喜欢,也可以从这里查看源代码下载. Flash 焦点图效果 ...

  4. 几种不同的json格式解析

    转连接: http://blog.csdn.net/whx405831799/article/details/42171191 内容很好 给服务端发送请求后,服务端会返回一连串的数据,这些数据在大部分 ...

  5. eclipse读取含有extjs的项目文件时卡死

    打开项目的.project文件,将<buildCommand>                        <name>org.eclipse.wst.jsdt.core.j ...

  6. nginx实时生成缩略图到硬盘上

    现在随着各终端的出现(手机,ipad等平板),以及各种终端的手机分辨率和尺寸都不同,现在手机用户流量都是宝,网上出现了各种各样的生成缩略图功能的架构,有使用php实时生成缩略图的,也有用nginx + ...

  7. centos 下构建lamp环境

    构建准备: 1.配置防火墙,开启80端口.3306端口 vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp - ...

  8. [bzoj1015][JSOI2008]星球大战——并查集+离线处理

    题解 给定一张图,支持删点和询问连通块个数 按操作顺序处理的话要在删除点的同时维护图的形态(即图具体的连边情况),这是几乎不可做的 我们发现,这道题可以先读入操作,把没删的点的边先连上,然后再倒序处理 ...

  9. Linux服务器中毒事件(libudev.so)

    今天机房管理人员反馈公司的某台服务器在防火墙上的连接数超限,登陆服务器时发现非常卡顿,远程登录后查看,CPU持续100%,且有一长度为10的随机字符串进程,kill掉,会重新生成另外长度为10的字符串 ...

  10. Swift 学习之二十一:?和 !(详解)

    http://blog.csdn.net/woaifen3344/article/details/30244201 Swift语言使用var定义变量,但和别的语言不同,Swift里不会自动给变量赋初始 ...