CF438D The Child and Sequence
CF438D 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 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]a[k] to xx (in other words perform an assignment a[k]=xa[k]=x ).
Can you help Picks to perform the whole sequence of operations?
输入格式
The first line of input contains two integer: n,m (1<=n,m<=10^{5}) . The second line contains n 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 type
.
- If type=1 , there will be two integers more in the line: l,r (1<=l<=r<=n)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<=10^{9}) , which correspond the operation 2.
- If type=3 , there will be two integers more in the line: k,x (1<=k<=n; 1<=x<=10^{9}) , which correspond the operation 3.
输出格式
For each operation 1, please print a line containing the answer. Notice that the answer may exceed the 32-bit integer.
题意翻译
给定数列,区间查询和,区间取模,单点修改。
n,m ≤105。
思路
线段树
题目要求:1.查询区间和 2.对给定区间取模 3.单点修改
考虑到
但是 时间复杂度很高
考虑开一个数组
可以极大优化时间复杂度
并且,这是第一次接触单点修改,注意单点修改的方法即可
小tips:非常有意思
CPP
#include <bits/stdc++.h>
#define int long long
//#define mid (l+r)>>1
using namespace std;
const int N=1e5+10;
int n,m;
int a[N];
int tree[N<<2],mx[N<<2];
inline int max(int a,int b) {
return a>b?a:b;
}
inline int ls(int x) {
return x<<1;
}
inline int rs(int x) {
return x<<1|1;
}
inline void pushup(int rt) {
tree[rt]=tree[ls(rt)]+tree[rs(rt)];
mx[rt]=max(mx[ls(rt)],mx[rs(rt)]);
}
/*
inline void build(int rt,int l,int r) {
if(l==r) {
tree[rt]=mx[rt]=a[l];
return;
}
build(ls(rt),l,mid);
build(rs(rt),mid+1,r);
pushup(rt);
}
*/
inline void build(int rt,int l,int r) {
if(l==r) {
tree[rt]=mx[rt]=a[l];
return;
}
int mid=l+r>>1;
build(ls(rt),l,mid);
build(rs(rt),mid+1,r);
pushup(rt);
}
inline void m_mod(int rt,int l,int r,int s,int t,int x) {
if(mx[rt]<x) return;
//if(t<l||s>r) return;
if(l==r) {
mx[rt]%=x;
tree[rt]%=x;
return;
}
int mid=l+r>>1;
if(s<=mid) m_mod(ls(rt),l,mid,s,t,x);
if(t>mid) m_mod(rs(rt),mid+1,r,s,t,x);
pushup(rt);
}
inline void m_pos(int rt,int l,int r,int pos,int val) {
if(l==r) {
tree[rt]=mx[rt]=val;
return;
}
int mid=l+r>>1;
if(pos<=mid) m_pos(ls(rt),l,mid,pos,val);
else m_pos(rs(rt),mid+1,r,pos,val);
pushup(rt);
}
inline int query(int rt,int l,int r,int s,int t) {
if(t<l||s>r) return 0;
if(s<=l&&r<=t) return tree[rt];
int mid=l+r>>1;
int ans=0;
if(s<=mid) ans+=query(ls(rt),l,mid,s,t);
if(t>mid) ans+=query(rs(rt),mid+1,r,s,t);
return ans;
//return query(ls(rt),l,mid,s,t)+query(rs(rt),mid+1,r,s,t);
}
inline int read() {
int x, f = 1;
char c;
while (!((c = getchar()) >= '0' && c <= '9')) if (c == '-') f = -1;
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9') (x *= 10) += c - '0';
return x * f;
}
inline void write(int x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 ^ 48);
}
signed main() {
n=read(),m=read();
for(int i=1; i<=n; i++) a[i]=read();
build(1,1,n);
while(m--) {
int opt=read();
if(opt==1) {
int l=read(),r=read();
int ans=query(1,1,n,l,r);
write(ans);
putchar('\n');
}
if(opt==2) {
int l=read(),r=read(),x=read();
m_mod(1,1,n,l,r,x);
}
if(opt==3) {
int k=read(),x=read();
m_pos(1,1,n,k,x);
}
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】