跳到正文

题解

题解:P13744 [NWERC 2024] Flowing Fountain

PDAFX约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年10月24日
收录
2026年8月28日
更新
2026年11月14日
篇幅
约 2 分钟
题目
P13744

版本

  1. v1当前

题目大意

有两种操作:

1. 往盆子里加啤酒,每一层啤酒会往容量更大的比它更低的层级流。

2. 查询盆子中啤酒数量。

令 axa_x 表示目前啤酒容量。

首先使用单调栈,求出往下流的层级,然后建一个类似于链表的结构。

每一次添加啤酒时,顺着链表一层一层往下模拟倾倒,直到无法溢出为止,然后同时修改下层链表。

查询直接输出 axa_x 即可。

时间复杂度 O(n)O(n)。

C++
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N=3e5+5;
int n,q,u,v,top,a[N],c[N],nxt[N],stk[N];
char opt;
int update(int x,int y)
{
	if(!x)return 0;
	if(a[x]+y<c[x])return a[x]+=y,x;
	y+=a[x]-c[x],a[x]=c[x];
	return nxt[x]=update(nxt[x],y);
}
int main()
{
	freopen("flowing.in","r",stdin);
	freopen("flowing.out","w",stdout);
	cin.tie(0)->sync_with_stdio(0);
	cin>>n>>q;
	for(int i=1;i<=n;i++)
	{
		cin>>c[i];
		while(top&&c[stk[top]]<c[i])nxt[stk[top--]]=i;
		stk[++top]=i;
	}
	while(q--)
	{
		cin>>opt>>u;
		if(opt=='+')
		{
			cin>>v;
			update(u,v);
		}
		else cout<<a[u]<<endl;
	}
	return 0;
}
原文
存档评论1 条抓取于