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:

  1. Print operation l,r . Picks should write down the value of img.
  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]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 img.

  • 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.单点修改

考虑到 109 内的数字,理想状况下最多进行 log(n) 次取模就可以实现

但是 时间复杂度很高

考虑开一个数组 mx[i] 表示该段的最大数值,如果大于模数,那么进行取模操作

可以极大优化时间复杂度

并且,这是第一次接触单点修改,注意单点修改的方法即可


小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;
}
posted @   Pass1on_W  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示