Codeforce 438D-The Child and Sequence

第一章 Cheat Enginee(CE)使用指南 Cheat Engine核心功能详细说明(以植物大战僵尸为例) 教你修改单机游戏。 阅读详情
D. The Child and Sequence
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 m 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, 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.

Sample test(s)
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.
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
    long long l, r, s, maxx;
}num[800005];
long long n, m, key;

template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if(c=getchar(),c==EOF) return 0; //EOF
	while(c!='-'&&(c<'0'||c>'9')) c=getchar();
	sgn=(c=='-')?-1:1;
	ret=(c=='-')?0:(c-'0');
	while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
	ret*=sgn;
	return 1;
}

inline void out(long long x) {
	if(x>9) out(x/10);
	putchar(x%10+'0');
}

void build(int l,int r,int k)
{
    num[k].l = l;
    num[k].r = r;
    num[k].s = 0;
    num[k].maxx = 0;
    if(l == r)    return;
    int mi = (l+r)>>1;
    build(l,mi,k+k);
    build(mi+1,r,k+k+1);
    return;
}

void update(int l, int r, int k)
{
	if(num[k].l==num[k].r)
	{
		num[k].s = key;
		num[k].maxx = key;
		return;
	}
    int mi = (num[k].l+num[k].r)>>1;
    if(l > mi)    update(l,r,k+k+1);
    else if(r <= mi)    update(l,r,k+k);
    else
    {
        update(l,mi,k+k);
        update(mi+1,r,k+k+1);    
    }
    num[k].s = num[k+k].s + num[k+k+1].s;
    num[k].maxx = max(num[k+k].maxx,num[k+k+1].maxx);
    return;
}

void upmod(int l, int r, int k)
{
	if(num[k].maxx<key)	return;
	if(num[k].l==num[k].r)
	{
		num[k].s%=key;
		num[k].maxx = num[k].s;
		return;
	}
    int mi = (num[k].l+num[k].r)>>1;
    if(l > mi)    		upmod(l,r,k+k+1);
    else if(r <= mi)    upmod(l,r,k+k);
    else
    {
        upmod(l,mi,k+k);
        upmod(mi+1,r,k+k+1);    
    }
    num[k].s = num[k+k].s + num[k+k+1].s;
    num[k].maxx = max(num[k+k].maxx,num[k+k+1].maxx);
    return;
}

long long query(int k,int l,int r)
{
	if(num[k].l==l && num[k].r==r)
	{
	    return num[k].s;		
	}
	else
	{
		int mi = (num[k].l+num[k].r)>>1;
		if(r<=mi)		return query(k+k,l,r);
		else if(l>mi)	return query(k+k+1,l,r);
		else			return query(k+k,l,mi)+query(k+k+1,mi+1,r);
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
    int Case;
    int a, b, c;
        memset(num,0,sizeof(num));
        scan_d(n);
        scan_d(m);
        build(1,n,1);
        for(int i=1;i<=n;i++){
			scan_d(key);
            update(i,i,1);
        }
		while(m--)
		{
        	scan_d(c);
        	switch(c)
        	{
        		case 1:	
					scan_d(a);
        			scan_d(b);
        			out(query(1,a,b));
        			putchar('\n');
					break;
        		case 2:	
					scan_d(a);
					scan_d(b);
					scan_d(key);
        			upmod(a,b,1); 
        			break;
        		case 3:
        			scan_d(a);
        			scan_d(key);
        			update(a,a,1);
        			break;
        	}			
		}
    return 0;
}

另一种的线段树写法:
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <deque>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
#include <iomanip>
using namespace std;
typedef long long LL;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 100010;

LL sum[maxn<<2], ma[maxn<<2];
void PushUP(int rt) {
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void PushUP2(int rt) {
    ma[rt] = max(ma[rt<<1], ma[rt<<1|1]);
}
void build(int l,int r,int rt) {
    if (l == r) {
        //scanf("%I64d",&sum[rt]);
        cin >> sum[rt];
        ma[rt] = sum[rt];
        return ;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    PushUP(rt);
    PushUP2(rt);
}
//#define lson l , m , rt << 1
//#define rson m + 1 , r , rt << 1 | 1
void MOD(int L,int R,int l,int r,int rt, LL mod)
{
   // if(R < l  || L > r) return ;
   // if(L <= l && r <= R && ma[rt] < mod) return ;
    if(ma[rt] < mod) return ;
    if(l == r) {sum[rt] %= mod; ma[rt] = sum[rt];return ;}

    int m = (l + r) >> 1;
    if (L <= m) MOD(L, R , lson, mod);
    if (R > m)  MOD(L, R , rson, mod);
    PushUP(rt);
    PushUP2(rt);
}

void update(int p,LL add,int l,int r,int rt) {
    if (l == r) {
        //sum[rt] += add;
        sum[rt] = add;
        ma[rt] = sum[rt];
        return ;
    }
    int m = (l + r) >> 1;
    if (p <= m) update(p , add , lson);
    else update(p , add , rson);
    PushUP(rt);
    PushUP2(rt);
}
LL query(int L,int R,int l,int r,int rt) {
    if (L <= l && r <= R) {
        return sum[rt];
    }
    int m = (l + r) >> 1;
    LL ret = 0;
    if (L <= m) ret += query(L , R , lson);
    if (R > m) ret += query(L , R , rson);
    return ret;
}
int main() {

    int n, m, op, l, r, k;
    LL x, mod;
    scanf("%d%d",&n,&m);
    build(1, n, 1);
    while(m--)
    {
        scanf("%d",&op);
        if(op == 1)
        {
            //scanf("%d%d",&l, &r);
            cin >> l >> r;
            cout << query(l, r, 1, n, 1) << endl;
            //printf("%I64d\n",query(l, r, 1, n, 1));
        }
        else if(op == 2)
        {
            cin >> l >> r >> mod;
            //scanf("%d%d%I64d",&l, &r, &mod);
            MOD(l, r, 1, n, 1, mod);

        }
        else
        {
            cin >> k >> x;
            //scanf("%d%d",&k,&x);
            update(k, x, 1, n, 1);
        }
    }

    return 0;
}


Neo4j LLM知识图谱构建器使用教程 是一个开源应用程序,旨在将非结构化数据(如PDF、文档、文本、YouTube视频、网页等)转换为存储在Neo4j中的知识图谱。该项目利用大型语言模型(如OpenAI、Gemini等)从文本中提取节点、关系及其属性,并通过Langchain框架创建结构化的知识图谱。 阅读详情

相关推荐

智能车小白也能懂:手把手教你用LM358和工字电感搭建电磁循迹模块(附PCB设计避坑)

本文详细介绍了如何利用LM358运放和工字电感搭建智能车电磁循迹模块,从电感选型、电路设计到PCB布局和焊接调试,提供全流程实战指南。特别针对新手常见问题,如信号干扰、电路饱和等,给出具体解决方案和参数配置,帮助智能车比赛爱好者快速掌握电磁循迹技术。

weixin_30307921的博客 401

Codeforce 438D-The Child and Sequence 分类: ...

D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output At the children's day, the ...

weixin_34166472的博客 168

AD学习笔记(一)基础认识

文章目录AD学习笔记第一讲 AD基础认识一、两库两图(分别对应下图)1、PCB图(pcbdoc)2、原理图(schdoc)3、原理图库(schlib)4、PCB库(pcblib)二、封装1、DIP封装2、LQFP封装三、绘制整体流程1、建立工程(PrjPcb)2、建立两库两图并保存整个工程(五次)3、从库中提取绘制原理图4、原理图导入pcb图5、根据原理图布线6、pcb图交由工厂打板 AD学习笔记 第一讲 AD基础认识 一、两库两图(分别对应下图) PCB图(pcbdoc) 原理图(schdoc) PCB库

qq_52465473的博客 6686

CodeForces - 1287C Garland(贪心)

题目链接:点击查看 题目大意:原本有一个1~n的排列,现在缺少了一些数字,用0来代替,现在要求将缺少的数字重新填回去,要求代价和最少,规定代价为:若相邻两个数字的奇偶性不同,则代价为1,否则代价为0 题目分析:因为要让代价和最小,我们考虑贪心策略,对于每一段连续的0,我们可以视为一个数据结构,其中需要维护l,r,num,flag这四个变量,分别代表:左端的数的奇偶性,右端的数的奇偶性,包含了多...

Falcon的博客 902

2026年7月19日 从可插拔到共封装:技术路线研究报告

AI 算力的爆发正在重写数据中心光互连的技术路线。本报告对当前并存的四代光互连架构——

weixin_51184047的博客 584

Windows如何使用Anaconda 3进行keras环境的配置

** Windows如何使用Anaconda 3进行keras环境的配置 搭建Keras环境* Anaconda3如何安装keras,主要包括下面三步: 1.安装mingw libpython 2.安装theano 3.安装keras 若你还没有按装anaconda,赶紧下载安装吧! 下载途径有两个 (1)Anaconda版本为Anaconda 4.4.0,对应的python版本为3.6,附上An...

Lance_liangliang的博客 1601

Codeforce--165D--Beard Graph(树链剖分)

D. Beard Graph time limit per test:4 seconds memory limit per test:256 megabytes input:standard input output:standard output Let's define a non-oriented connected graph of n v

wust_cyh 346

峰值电流模式CCM BUCK的传递函数推导

电流内环闭环传递函数: Gci(s)=i^L(s)v^comp(s)=FmGid(s)1+KRiHe(s)FmGid(s)=1Ri11+s[mcL+(0.5VIN−Vo)RiVINRifSW]+s21(πfSW)2 G_{ci}(s)=\frac{\hat i_{L}(s)}{\hat v_{comp}(s)}=\frac {F_mG_{id}(s)}{1+KR_iH_e(s)F_mG_{id}(s)}=\frac{1}{R_i}\frac{1}{1+s[\frac{m_cL+(0.5V_{IN}-V_o)

c007106333的博客 2354

CodeForces 490E Restoring Increasing Sequence

题意: 一个严格递增序列  某些数字的某些位被盖住了  求  恢复后的序列 思路: 贪心  让每个数在大于前一个的基础上尽量的小 先讨论数字长度 len[i] len[i]>len[i-1] 除了第一位如果是?就填1以外  其他?全填0 len[i]==len[i-1] dfs搜索num[i]格式下大于num[i-1]的最小的数 代码: #include #include #in

House 1263

“软开关”硬技术!ZVS和ZCS到底谁更牛?看完这个例子你就懂了!

❌ ZVS 和 ZCS 是万能的?→ 不,它们都有实现条件,谐振参数选不好照样硬拐弯!❌ 只要有谐振就一定ZVS?→ 不,一定要设计合适的死区时间和负载条件才行!ZVS 是让电压先“冷静”,ZCS 是让电流先“退场”。一个是开门不撞墙,一个是关门不甩门。懂了它们,功率设计就是在跳华尔兹而不是打架!💃🔌。

weixin_43199439的博客 1236

AI大模型探索之路-认知篇1-全景透视:揭秘AIGC技术的进化之路

随着人工智能技术的突飞猛进,内容创作方式的发生了革命性的转变;从早期专业人员主导的内容生产,到现如今生成式人工智(AIGC)能创作时代;本篇文章主要讲解AIGC技术的演变历程。

寻道AI,探索AI无限可能! 1646

Codeforces 490E Restoring Increasing Sequence(贪心)

题目链接:Codeforces 490E Restoring Increasing Sequence 每个数字在尽量满足的条件下尽量下的去构造即可。 #include #include #include using namespace std; const int maxn = 1e5 + 5; const int maxm = 10; int N, len[maxn]; char

不慌不忙、不急不躁 1251

微信小程序 - request:fail errcode:10011 cronet_error_code:0 error_msg:network change, cancel all task请求报错

微信小程序请求接口,报request:fail errcode:10011是什么原因,uniapp微信小程序request:fail errcode:10011 cronet_error_code:0 error_msg:network change, cancel all task,微信小程序wx.request常规访问接口,有个别用户的接口请求频繁报错,UniApp 微信小程序uni.request请求接口报错:request:fail errcode:10011 的原因分析与解决方案,苹果ios,安卓

🏆 前端领域优质创作者 4566

codeforce438D The Child and Sequence

codeforce438D 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 ho

leihao-lester 168

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

codeforce 438D The Child and Sequence

codeforce 438D The Child and Sequence 线段树

ss25c的博客 208

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

【CodeForces 438D --- The Child and Sequence】线段树

【CodeForces 438D --- 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, i...

来日浅谈的博客 413

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

CodeForces 438D The Child and Sequence

题目链接:http://codeforces.com/problemset/problem/438/D The Child and Sequence time limit per test:4 seconds memory limit per test:256 megabytes input:standard input output:stan

Silenceneo的ACM坎坷之路(已退役) 1229

【CodeForces - 438D】:The Child and Sequence【线段树+数论】

题目: CodeForces - 438D:The Child and Sequence 题意: 给定一个序列,有三种操作:(1)区间求和(2)区间对一个数取模(3)单点修改 分析: 这个题的难点在于区间取模,并没有什么好的方法能快速维护区间和,考虑暴力取模: 首先,只有 A[x] >= mod,取模操作才有意义,那么就可以维护区间最大值,去找到需要取模的数;这样暴力的复杂度如...

KobeDuu 224

CodeForces 438D The Child and Sequence(线段树)

题目链接:http://codeforces.com/problemset/problem/438/D 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 par...

年轻过成了秃顶 222

CodeForces 438D The Child and Sequence(线段树区间取模)

### 题目大意 给出一个长度为$n$的非负整数序列$a$,需要支持以下操作: - 给出$l,r$,求$l,r$之间的数的和 - 给出$l,r,x$,将$l,r$之间的每个$a_i$都对$x$取模 - 给出$k,v$,将$a_k$的值改为$v$

tanjunming2020的博客 1309

Codeforces 438D The Child and Sequence (线段树取模操作)

题目链接:http://codeforces.com/problemset/problem/438/D #include <cmath> #include <cstdio> #include <algorithm> #include <iostream> #include <cstring> #include...

weixin_30328063的博客 128

The Child and Sequence 【CodeForces - 438D】【线段树】

题目链接   就是有三种更新的方式: 1 u v 对于所有i u&lt;=i&lt;=v,输出a[i]的和; 2 u v t 对于所有i u&lt;=i&lt;=v a[i]=a[i]%t; 3 u v 表示a[u]=v(将v赋值给a[u]); 然后,就是思路了,对于这样的一个区间更新,我们直接暴力更新到底,每次用一个剪枝,区间最大值小于模值的话,就没必要了,并且,区间和为0的话也是没必要的...

既然弱小,就只顾变强就是了 240

codeforces CF438D The Child and Sequence 线段树

$ \Rightarrow $ 戳我进CF原题 D. The Child and Sequence time limit per test: 4 seconds memory limit per test: 256 megabytes input: standard input output: standard output   At the children's day, the ...

ayf1988的博客 176

CodeForces 438D The Child and Sequence 题解

题目传送门 题目大意: 区间求和,区间取模,单点修改。 题解 跟花神游历各国很像,这题的取模也是做多几次就会变成 000,所以这题用个线段树暴力取模即可,然后每次做的时候判断区间最大值是否小于模数,如果是的话就不用往下做了。 可以证明,一个数被模最多 lognlognlogn 次就会变成0。 证明如下: 考虑取模次数最多的情况,最后肯定是变成了000,那么从 000 开始往上推。 如果取模之后变成...

a_forever_dream的博客 175

codeforces 438 D The Child and Sequence 线段树区间取模

http://codeforces.com/problemset/problem/438/D 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 particu...

xiji333的博客 214

The Child and Sequence CodeForces - 438D(线段树)

The Child and Sequence  题目链接:CodeForces - 438D 题意:一共n个数,三种操作:1  l  r :求区间[l, r]的和; 2  l   r   x :将区间[l, r]的所有数对x取模;  3   k   x:将k处的数改为x; 思路:如果区间的最大值都比要取模的数小,就不需要继续向下更新了,如果大,就看做单点更新;至于为什么不会T,恕在下太菜,不...

Sirius_han的博客 275
上一篇: Codeforces 343D Water Tree
下一篇: Least Common Ancestors
梁千仞
博客等级 码龄15年 12粉丝 71原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值