A
来源:互联网 发布:idm下载器for mac 编辑:程序博客网 时间:2024/04/27 17:10
There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 ton, in the order from the highest to the lowest, the volume of thei-th vessel is ai liters.
Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from thei-th vessel goes to the(i + 1)-th one. The liquid that overflows from then-th vessel spills on the floor.
Your task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries:
- Add xi liters of water to thepi-th vessel;
- Print the number of liters of water in the ki-th vessel.
When you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels.
The first line contains integer n — the number of vessels (1 ≤ n ≤ 2·105). The second line containsn integersa1, a2, ..., an — the vessels' capacities (1 ≤ ai ≤ 109). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integerm — the number of queries (1 ≤ m ≤ 2·105). Each of the nextm lines contains the description of one query. The query of the first type is represented as "1 pi xi", the query of the second type is represented as "2 ki" (1 ≤ pi ≤ n,1 ≤ xi ≤ 109,1 ≤ ki ≤ n).
For each query, print on a single line the number of liters of water in the corresponding vessel.
25 1061 1 42 11 2 51 1 42 12 2
458
35 10 861 1 122 21 1 61 3 22 22 3
7105
思路: 暴力试了多次都过不了 最后选择并查集 好吧 我不会写诗copy网上的
主要思路就是说 当第输入的小船满了 之后直接加到后面没满的小船上这样子时间会比较少
代码:
#include<stdio.h>
#include<string.h>
using namespace std;
int n;
int sum[1500000];
int a[15000000];
int f[15000000];
int find(int a)
{
int r=a;
while(r!=f[r])
r=f[r];
int i=a;
int j;
while(i!=r)
{
j=f[i];
f[i]=r;
i=j;
}
return r;
}
void merge(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx>fy)
f[fy]=fx;
else
f[fx]=fy;
}
int main()
{
while(~scanf("%d",&n))
{
memset(a,0,sizeof(a));
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
f[i]=i;
}
f[n+1]=n+1;
int q;
scanf("%d",&q);
while(q--)
{
int op;
scanf("%d",&op);
if(op==1)
{
int x,have;
scanf("%d%d",&x,&have);
while(have>0)
{
int F=find(x);
if(F==n+1)break;
if(a[F]-sum[F]>=have)
{
sum[F]+=have;
have=0;
}
else
{
have=have-(a[F]-sum[F]);
sum[F]=a[F];
merge(F,F+1);
}
x=F;
}
}
if(op==2)
{
int x;
scanf("%d",&x);
printf("%d\n",sum[x]);
}
}
}
return 0;
}
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- A
- A*
- a
- A
- a
- 要想成为一名高级java工程师,你需要学什么
- Python 函数
- C++ float乘法怎样截取6位小数
- nginx -s stop and -s quit 有什么区别?
- elasticsearch学习总结(三) API的使用范例4
- A
- 表单(五)富文本编辑
- 微商来供应商
- IScroll5+在ios中click事件不兼容解决方法
- 将list数组对象转为json对象
- Laravel 5.4 官方文档摘记:Laravel操作数据库
- 基于VS2010的ITK安装
- 006 栈的顺序存储
- "log4j:configuration" must match "