F - Clearance

news/2025/10/5 23:16:52/文章来源:https://www.cnblogs.com/onlyblues/p/19127166

F - Clearance

Problem Statement

AtCoder Inc.'s online shop currently handles $N$ products, and the stock of product $i$ is $A_i$ units remaining.  

Process the following $Q$ orders in order. The $i$-th order is as follows:

  • Buy $k_i$ units each of products $l_i,l_i+1,\dots,r_i$. For products with fewer than $k_i$ units, buy all available units. Report the total number of products bought in this order.

Note that for $i<Q$, the stock of products bought in the $i$-th order is reduced before proceeding to the $(i+1)$-th order.

Constraints

  • All input values are integers.
  • $1 \le N \le 3 \times 10^5$
  • $1 \le A_i \le 10^{15}$
  • $1 \le Q \le 3 \times 10^5$
  • $1 \le l_i \le r_i \le N$
  • $1 \le k_i \le 10^9$

Input

The input is given from Standard Input in the following format:

$N$
$A_1$ $A_2$ $\dots$ $A_N$
$Q$
$l_1$ $r_1$ $k_1$
$l_2$ $r_2$ $k_2$
$\vdots$
$l_Q$ $r_Q$ $k_Q$

Output

Output $Q$ lines.
The $i$-th line should contain the total number of products bought in the $i$-th order.


Sample Input 1

6
2 6 4 5 7 5
5
1 6 1
3 5 4
4 4 1
2 5 1
1 6 100

Sample Output 1

6
11
0
2
10

This input contains $5$ orders.

  • Initially, the stocks of the products are (from product $1$ onward) $2,6,4,5,7,5$ units.
  • The first order is $l_1 = 1, r_1 = 6, k_1 = 1$.
    • In this order, $1,1,1,1,1,1$ units of the products are bought, for a total of $6$ units.
    • After this, the stocks of the products become $1,5,3,4,6,4$ units.
  • The second order is $l_2 = 3, r_2 = 5, k_2 = 4$.
    • In this order, $0,0,3,4,4,0$ units of the products are bought, for a total of $11$ units.
    • After this, the stocks of the products become $1,5,0,0,2,4$ units.
  • The third order is $l_3 = 4, r_3 = 4, k_3 = 1$.
    • In this order, $0,0,0,0,0,0$ units of the products are bought, for a total of $0$ units.
    • After this, the stocks of the products become $1,5,0,0,2,4$ units.
  • The fourth order is $l_4 = 2, r_4 = 5, k_4 = 1$.
    • In this order, $0,1,0,0,1,0$ units of the products are bought, for a total of $2$ units.
    • After this, the stocks of the products become $1,4,0,0,1,4$ units.
  • The fifth order is $l_5 = 1, r_5 = 6, k_5 = 100$.
    • In this order, $1,4,0,0,1,4$ units of the products are bought, for a total of $10$ units.
    • After this, the stocks of the products become $0,0,0,0,0,0$ units.

 

解题思路

  退坑半个多月了,因为有其他的事情要忙,加上 8 月底到 9 月中基本打一场掉一场,被搞到心态彻底炸了。不知道为什么感觉今年水平下滑很严重,而且现在基本都不考算法,大部分都是智力题,更加玩不动了。昨天复健打了把 abc,结果连之前稍微擅长的数据结构也不会了。然后今早 lc 又继续掉分,现在玩算竞已经完全没有正反馈了,唉。可能单纯因为沉默成本的原因我现在还在坚持打吧,反正是真想彻底弃坑了。

  由于涉及到区间的查询和更新,因此容易想到用线段树来维护。问题的难点在于,当某个产品的数量不足 $k$ 时,如何处理区间内的产品。我们可以将区间 $[l,r]$ 内的产品根据剩余数量 $a_i$ 分成以下三类:数量至少为 $k$ 的产品($a_i \geq k$);数量不足 $k$ 但不为空的产品($0 < a_i < k$);空的产品($a_i = 0$)。对于第一类产品,每个产品的贡献为 $k$,更新时将其数量减少 $k$。对于第二类产品,每个产品的贡献为其现有数量 $a_i$​ ,更新时将其数量置为 $0$。对于第三类产品,贡献为 $0$,更新时无需操作。

  对于查询答案,我们可以换另外一种思路来计算。我们只考虑不为空的产品,假设这些产品每个都贡献 $k$ 个,那么总贡献为不为空的产品数乘以 $k$。另外还要考虑那些数量不足 $k$ 的产品,需要从总贡献中减去 $k-a_i$,相当于加上 $a_i-k$。而 $a_i-k$ 很容易获取,只需在线段树中对区间 $[l,r]$ 减去 $k$,如果某个产品的数量不足 $k$,那么其结果就为 $a_i-k$(不考虑区间中空的产品)。此时我们可以对整棵线段树进行多次二分依次找出小于 $0$ 的下标,从而找到数量不足 $k$ 但不为空的产品,并获取对应的 $a_i-k$。注意,在所有的操作中最多会二分线段树 $n$ 次,因为最多有 $n$ 个产品会变成空。

  那么区间中原本为空的产品怎么处理呢?我们可以再开一个线段树(或树状数组)来标记它们的状态(设为 $1$ 即可),并在每次查询时更新。具体来说,在每次操作中,我们会标记变为空的产品,同时将这些产品的数量置为正无穷,以防止对区间减去 $k$ 后得到负数。这样就可以通过区间查询获取区间内为空的产品数量(从而反推不为空的产品数量),并且在二分线段树时,只到找当前操作变成空的产品。

  AC 代码如下,时间复杂度为 $O(n+q\log{n})$:

#include <bits/stdc++.h>
using namespace std;typedef long long LL;const int N = 3e5 + 5;
const LL INF = 0x3f3f3f3f3f3f3f3f;int n, m;
LL a[N];
struct Node {int l, r;LL mn, add;
}tr1[N * 4];
LL tr2[N];void build(int u, int l, int r) {tr1[u] = {l, r};if (l == r) {tr1[u].mn = a[l];}else {int mid = l + r >> 1;build(u << 1, l, mid);build(u << 1 | 1, mid + 1, r);tr1[u].mn = min(tr1[u << 1].mn, tr1[u << 1 | 1].mn);}
}void upd(int u, LL c) {tr1[u].mn += c;tr1[u].add += c;
}void pushdown(int u) {if (!tr1[u].add) return;upd(u << 1, tr1[u].add);upd(u << 1 | 1, tr1[u].add);tr1[u].add = 0;
}void modify(int u, int l, int r, LL c) {if (tr1[u].l >= l && tr1[u].r <= r) {upd(u, c);}else {pushdown(u);int mid = tr1[u].l + tr1[u].r >> 1;if (l <= mid) modify(u << 1, l, r, c);if (r >= mid + 1) modify(u << 1 | 1, l, r, c);tr1[u].mn = min(tr1[u << 1].mn, tr1[u << 1 | 1].mn);}
}LL query(int u, int l, int r) {if (tr1[u].l >= l && tr1[u].r <= r) return tr1[u].mn;pushdown(u);int mid = tr1[u].l + tr1[u].r >> 1;if (r <= mid) return query(u << 1, l, r);if (l >= mid + 1) return query(u << 1 | 1, l, r);return min(query(u << 1, l, r), query(u << 1 | 1, l, r));
}int query1(int u) {if (tr1[u].l == tr1[u].r) return tr1[u].l;pushdown(u);if (tr1[u << 1].mn < 0) return query1(u << 1);return query1(u << 1 | 1);
}int lowbit(int x) {return x & -x;
}void add(int x) {for (int i = x; i <= n; i += lowbit(i)) {tr2[i]++;}
}int query2(int x) {int ret = 0;for (int i = x; i; i -= lowbit(i)) {ret += tr2[i];}return ret;
}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);cin >> n;for (int i = 1; i <= n; i++) {cin >> a[i];}build(1, 1, n);cin >> m;while (m--) {int l, r, c;cin >> l >> r >> c;LL ret = (r - l + 1ll - query2(r) + query2(l - 1)) * c;modify(1, l, r, -c);while (tr1[1].mn < 0) {int x = query1(1);ret += query(1, x, x);modify(1, x, x, INF);add(x);}cout << ret << '\n';}return 0;
}

 

参考资料

  Editorial - AtCoder Beginner Contest 426:https://atcoder.jp/contests/abc426/editorial/14148

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/928838.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

P9234 [蓝桥杯 2023 省 A] 买瓜

难度 算法s 日期 题目链接普及+/提高 折半搜索、剪枝 2025-07-20 https://luogu.com.cn/problem/P9234我觉得此题没有参考价值。 本题解聚焦于剪枝优化上。如果只是简单地使用折半搜索,很容易 \(\text{TLE}\)。我感觉…

P1044 [NOIP 2003 普及组] 栈

难度 算法s 日期 题目链接普及− DP 2025-05-21 https://luogu.com.cn/problem/P1044因为本人 DP 很差,故撰此文巩固一下。 本文是对 P1044 [NOIP 2003 普及组] 栈 题解 的详细解释。 写的很直白了。。。几乎没有理解…

餐饮环境评估在哪个网站做网站开发一定要用框架吗

1.Kafka中的ISR、AR又代表什么&#xff1f;ISR&#xff1a;与leader保持同步的follower集合AR&#xff1a;分区的所有副本2.Kafka中的HW、LEO等分别代表什么&#xff1f;LEO&#xff1a;没个副本的最后条消息的offsetHW&#xff1a;一个分区中所有副本最小的offset3.Kafka中是怎…

宁波小型建网站公司新网站 seo

效果图 在uniapp微信小程序/手机h5网页网站/安卓app/苹果app/支付宝小程序/nvue等(全平台完美兼容)开发中,实现uniApp各端都兼容的 “刻度尺(横格尺 | 尺子)” 手势左右两侧拖动、手指滑动刻度尺功能,水平刻度尺,支持自定义尺子颜色、大小、刻度、滑动时的步进值、最大…

P1080 [NOIP 2012 提高组] 国王游戏

难度 算法s 日期 题目链接普及+/提高 贪心、邻项排序 2025-07-25 https://luogu.com.cn/problem/有 \(n\) 个大臣和一个国王,每个大臣左、右手各有一个数,分别记为 \(a_i,b_i\),国王手上也有数,记为 \(a,b\)。现在…

音响没声音

联想拯救者x9000p笔记本(win11)之前总来不来就没音(只能听到特定几个音调),然后隔一段时间重启莫名其妙就好了,以为是静电。 这次又故障了,就把如图关了,就解决了,查了驱动也都是最新,网上有人说这个会消杂音…

P1654 OSU!

难度 算法s 日期 题目链接提高+/省选− 数学期望、递推 2025-07-21~22 https://luogu.com.cn/problem/P1654似乎是一道蛮经典的期望递推题。 洛谷上的题解看不懂,老师讲的也很简略。只有自己推起来感觉很复杂。。。心…

响应式网站几个断点毕节市城乡建设局网站

文章目录 年费和小额账户管理费减免政策&#xff1a;每家银行均可有一张借记卡享受双免政策减免政策&#xff1a;代发工资、低保、社保、医保、失业保险、养老金、退休金、住房公积金等账户减免政策&#xff1a;二、三类电子账户一类卡、二类卡和三类卡二、三类电子账户不收取年…

做网站网络合同石河子网站制作

问题如下;Java代码中的方法是&#xff1a;Rule foo(){return sequence(foo(), x());}这将引发解析循环,当然应该避免;但是,这是合法的&#xff1a;Rule foo(){return sequence(x(), foo());}现在,代码中的其他地方我可以访问RuleMethod,这是一个扩展MethodNode的类,因此我可以访…

10/5

今日学习了Java,背诵了英语单词,明日继续

10/4

今日学习了Java,背诵了英语单词,明日继续

DynamoDB十年演进:云原生数据库的技术革新

本文深入探讨了Amazon DynamoDB云原生数据库的十年发展历程,从Dynamo研究论文到完全托管服务的演进,涵盖了分布式系统架构设计、弹性扩展机制、毫秒级性能优化等核心技术突破,以及全球表、事务支持等创新功能的发展…

惠州网站建设英语怎么快速搭建网站

当用C语言来实现猜数字游戏时&#xff0c;我们可以设计一个简单的游戏规则&#xff1a;计算机随机生成一个1到100之间的整数&#xff0c;玩家需要通过猜测来猜出这个数字。游戏会根据玩家猜测的数字与目标数字的大小关系给出提示&#xff0c;直到玩家猜中为止。 下面是一个用C…

详细介绍:【python】uv管理器

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

企业微网站开发锛网站

现在&#xff0c;人工智能的发展处于跳跃式阶段&#xff0c;我们也对AI在大型数据集的应用进展感到吃惊。更重要的是&#xff0c;那些我们没有跟踪的数十亿张照片或餐厅的评论并没有被遗漏掉&#xff1a;迁移学习技术让收集数据变得更加“容易”。另外&#xff0c;得益于PyTorc…

深圳网络做网站app开发费用大概多少

22/06/2005 12:22 FPOracle从8.1.6开始提供分析函数&#xff0c;分析函数用于计算基于组的某种聚合值&#xff0c;它和聚合函数的不同之处是对于每个组返回多行&#xff0c;而聚合函数对于每个组只返回一行。下面例子中使用的表来自Oracle自带的HR用户下的表&#xff0c;如果没…

NotImplementedError: Cannot convert a symbolic Tensor (lstm/strided_slice:0) to a numpy array.

NotImplementedError: Cannot convert a symbolic Tensor (lstm/strided_slice:0) to a numpy array. This error may indicate that youre trying to pass a Tensor to a NumPy call, which is not supportednumpy …

porting perf性能观测工具

1.cd sdk/kernel/linux/linux-6.1.83/tools/perf 2.make ARCH=arm64 CROSS_COMPILE=aarch64-none-linux-gnu- NO_LIBPYTHON=1 NO_LIBPERL=1 NO_LIBBPF=1 3.在当前路径下生成perf 4.copy到板子/tmp目录下 scp ./per…

Windows常用快捷指令

Windows常用快捷指令 总览shift+数字键:输入数字键上的特殊符号 shift+Ctrl:切换输入法(台式电脑) shift+空格:切换输入法(笔记本电脑) Ctrl+C:复制 Ctrl+V:粘贴 Ctrl+X:剪切 Ctrl+Z:撤销 Ctrl+Y:重做 Ctr…