news 2026/4/29 19:53:07

题解:AtCoder AT_awc0006_e Store Sales Management

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
题解:AtCoder AT_awc0006_e Store Sales Management

本文分享的必刷题目是从蓝桥云课洛谷AcWing等知名刷题平台精心挑选而来,并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构,旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。

欢迎大家订阅我的专栏:算法题解:C++与Python实现!

附上汇总贴:算法竞赛备考冲刺必刷题(C++) | 汇总


【题目来源】

AtCoder:E - Store Sales Management

【题目描述】

Takahashi works at the headquarters of a retail company that operates a chain of stores nationwide. The company runs a system that centrally manages the sales of each store.

There areN NNstores nationwide, each assigned a store number from1 11toN NN. For each storei ii( 1 ≤ i ≤ N ) (1 \leq i \leq N)(1iN), an initial sales amountS i S_iSiis recorded.

For business analysis, Takahashi needs to process a total ofQ QQqueries of the following2 22types, in the given order.

  • Type 1(1 L R): Output the total sales amount of all stores whose store numbers are betweenL LLandR RRinclusive, that is,S L + S L + 1 + ⋯ + S R S_L + S_{L+1} + \cdots + S_RSL+SL+1++SR.
  • Type 2(2 X V): Overwrite the sales amountS X S_XSXof storeX XXwithV VV. That is, setS X ← V S_X \leftarrow VSXV.

For each Type 1 query, the total sales amount should be computed with all changes from previous Type 2 queries already applied.

Please find the answer to each Type 1 query on behalf of Takahashi.

【输入】

N NNQ QQ
S 1 S_1S1S 2 S_2S2… \ldotsS N S_NSN
q u e r y 1 \mathrm{query}_1query1
q u e r y 2 \mathrm{query}_2query2
⋮ \vdots
q u e r y Q \mathrm{query}_QqueryQ

  • The first line contains the number of storesN NNand the number of queriesQ QQ, separated by a space.
  • The second line contains the initial sales amountsS 1 , S 2 , … , S N S_1, S_2, \ldots, S_NS1,S2,,SNof each store, separated by spaces.
  • The followingQ QQlines each contain one of theQ QQqueries. Each query is distinguished by the leading integer indicating its type. Thei ii-th queryq u e r y i \mathrm{query}_iqueryiis given in one of the following formats:
  • Type 1:1 L R(compute the total sales amount of stores with numbers fromL LLtoR RRinclusive)
  • Type 2:2 X V(overwrite the sales amount of storeX XXwithV VV)

【输出】

Each time a Type 1 query is processed, output the corresponding total sales amount on a single line.

【输入样例】

5 4 10 20 30 40 50 1 1 3 2 2 100 1 1 3 1 2 5

【输出样例】

60 140 220

【解题思路】

【算法标签】

#树状数组#

【代码详解】

#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintN=200005;intn,q;// n: 数组长度,q: 操作次数ints[N],tr[N];// s: 原数组,tr: 树状数组// 获取x的最低位的1intlowbit(intx){returnx&-x;}// 树状数组更新:在位置x加上cvoidadd(intx,intc){for(inti=x;i<=n;i+=lowbit(i)){tr[i]+=c;}}// 树状数组查询:求前缀和[1, x]intquery(intx){intres=0;for(inti=x;i;i-=lowbit(i)){res+=tr[i];}returnres;}signedmain(){cin>>n>>q;// 读入数组长度和操作次数for(inti=1;i<=n;i++){cin>>s[i];// 读入数组元素add(i,s[i]);// 初始化树状数组}while(q--)// 处理每个操作{intop;cin>>op;// 读入操作类型if(op==1)// 查询操作{intl,r;cin>>l>>r;// 读入查询区间// 输出区间和:前缀和[r] - 前缀和[l-1]cout<<query(r)-query(l-1)<<endl;}else// 更新操作{intx,v;cin>>x>>v;// 读入位置和新的值add(x,v-s[x]);// 更新树状数组s[x]=v;// 更新原数组}}return0;}

【运行结果】

5 4 10 20 30 40 50 1 1 3 60 2 2 100 1 1 3 140 1 2 5 220
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/29 19:48:52

HSPA+技术演进与核心创新解析

1. HSPA技术演进概述HSPA&#xff08;高速分组接入演进&#xff09;是3GPP在UMTS Release 7至Release 10中定义的关键移动通信标准演进路径。作为WCDMA技术的增强版本&#xff0c;HSPA通过一系列创新技术将理论峰值速率从Release 6的14.4Mbps提升至Release 10的168Mbps。这项技…

作者头像 李华
网站建设 2026/4/29 19:48:42

开源可持续性评分工具:模块化设计、实战部署与CI/CD集成指南

1. 项目概述&#xff1a;一个为可持续性评估而生的开源工具最近在关注可持续发展和ESG&#xff08;环境、社会、治理&#xff09;领域&#xff0c;发现很多团队在评估项目或产品的可持续性时&#xff0c;常常面临一个难题&#xff1a;如何将定性的、模糊的“可持续性”概念&…

作者头像 李华
网站建设 2026/4/29 19:48:00

AI智能体安全实践:构建基于最小权限原则的信任边界框架

1. 项目概述&#xff1a;当AI智能体需要“边界感” 最近在折腾AI智能体&#xff08;AI Agent&#xff09;项目时&#xff0c;我遇到了一个挺有意思&#xff0c;也相当棘手的问题。简单来说&#xff0c;就是如何让一个能自主调用工具、访问外部数据的AI&#xff0c;在“放飞自我…

作者头像 李华
网站建设 2026/4/29 19:47:58

构建可扩展技能生态:OpenClaw技能仓库的设计与实现

1. 项目概述&#xff1a;一个为“OpenClaw”技能生态量身定制的开源仓库最近在折腾一个名为“OpenClaw”的开源项目&#xff0c;它是一个旨在构建通用、可扩展技能执行框架的探索。在推进过程中&#xff0c;我遇到了一个几乎所有开发者都会面临的经典问题&#xff1a;如何高效地…

作者头像 李华
网站建设 2026/4/29 19:47:56

如何快速掌握Vin象棋:AI智能连线助你轻松提升棋艺

如何快速掌握Vin象棋&#xff1a;AI智能连线助你轻松提升棋艺 【免费下载链接】VinXiangQi Xiangqi syncing tool based on Yolov5 / 基于Yolov5的中国象棋连线工具 项目地址: https://gitcode.com/gh_mirrors/vi/VinXiangQi Vin象棋&#xff08;VinXiangQi&#xff09;…

作者头像 李华
网站建设 2026/4/29 19:46:53

2025届毕业生推荐的六大降重复率神器推荐

Ai论文网站排名&#xff08;开题报告、文献综述、降aigc率、降重综合对比&#xff09; TOP1. 千笔AI TOP2. aipasspaper TOP3. 清北论文 TOP4. 豆包 TOP5. kimi TOP6. deepseek 将AIGC&#xff08;人工智能生成内容&#xff09;的检测概率予以降低&#xff0c;要从文本特…

作者头像 李华