题解:AtCoder AT_awc0006_e Store Sales Management
本文分享的必刷题目是从蓝桥云课、洛谷、AcWing等知名刷题平台精心挑选而来并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。欢迎大家订阅我的专栏算法题解C与Python实现附上汇总贴算法竞赛备考冲刺必刷题C | 汇总【题目来源】AtCoderE - 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)(1≤i≤N), 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_{L1} \cdots S_RSLSL1⋯SR.Type 2(2 X V): Overwrite the sales amountS X S_XSXof storeX XXwithV VV. That is, setS X ← V S_X \leftarrow VSX←V.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 QQS 1 S_1S1S 2 S_2S2… \ldots…S N S_NSNq u e r y 1 \mathrm{query}_1query1q u e r y 2 \mathrm{query}_2query2⋮ \vdots⋮q u e r y Q \mathrm{query}_QqueryQThe 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【解题思路】【算法标签】#树状数组#【代码详解】#includebits/stdc.husingnamespacestd;#defineintlonglongconstintN200005;intn,q;// n: 数组长度q: 操作次数ints[N],tr[N];// s: 原数组tr: 树状数组// 获取x的最低位的1intlowbit(intx){returnx-x;}// 树状数组更新在位置x加上cvoidadd(intx,intc){for(intix;in;ilowbit(i)){tr[i]c;}}// 树状数组查询求前缀和[1, x]intquery(intx){intres0;for(intix;i;i-lowbit(i)){restr[i];}returnres;}signedmain(){cinnq;// 读入数组长度和操作次数for(inti1;in;i){cins[i];// 读入数组元素add(i,s[i]);// 初始化树状数组}while(q--)// 处理每个操作{intop;cinop;// 读入操作类型if(op1)// 查询操作{intl,r;cinlr;// 读入查询区间// 输出区间和前缀和[r] - 前缀和[l-1]coutquery(r)-query(l-1)endl;}else// 更新操作{intx,v;cinxv;// 读入位置和新的值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
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2566502.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!