Vessels

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 to n, in the order from the highest to the lowest, the volume of the i-th vessel is ai liters.
这里写图片描述
Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the i-th vessel goes to the (i + 1)-th one. The liquid that overflows from the n-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 the pi-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.
Input
The first line contains integer n — the number of vessels (1 ≤ n ≤ 2·105). The second line contains n integers a1, 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 integer m — the number of queries (1 ≤ m ≤ 2·105). Each of the next m 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).
Output
For each query, print on a single line the number of liters of water in the corresponding vessel.
Example
Input
2
5 10
6
1 1 4
2 1
1 2 5
1 1 4
2 1
2 2
Output
4
5
8
Input
3
5 10 8
6
1 1 12
2 2
1 1 6
1 3 2
2 2
2 3
Output
7
10
5

一开始暴力更新水量,发现超时,这个题一开始猜测是不是用栈,保存下一些值,发现根本不存在的,栈是用来找相邻最小或者是最大,就像一直往里面钻,栈中一直保持一个单调的序列,是用来做这样的题目。而这个题是保存下,船下第一个不满的船的序号,这就有点像pre[]数组了。就是合并已经装满的船,这就很像合并一个家族了。但是当时困扰在一个当当前的船满了之后,怎样找到下一个没满的,然后再怎么更新从当前船到现在的祖宗船之间的船的祖宗船,当时这个逻辑关系,直接就把我搞蒙了。然而事实就是根本不用更新,因为我们只是对一艘船进行操作,只需要更新当前船的祖宗船。因为下面的祖宗船在满了当前船的操作上是不会变的。他下面的首先应该temp=当前船,然后pre[当前船]=find_ancestor(当前船+1),就是合并这两个船。然后temp=find_ancestor(当前船);

#include<stdio.h>
const int cn=2e5+10;
typedef long long ll;
ll max[cn],now[cn]={0};
ll pre[cn];
int find_root(int x)
{
    return pre[x]==x ? x:pre[x]=find_root(pre[x]);
}
int mix(int x,int y)
{
    int fy = find_root(y);
    pre[x]=fy;
}
int main()
{
    ll n,i,m,choice,vessel,x,root;
    long long volumn;
    scanf("%lld",&n);
    for(i=1;i<=n;i++)
    {
       scanf("%lld",&max[i]);
       pre[i]=i;
    }
    pre[i]=i;
    scanf("%lld",&m);

    for(i=1;i<=m;i++)
    {
        scanf("%lld",&choice);
        if(choice==1)
        {
            scanf("%lld%lld",&vessel,&volumn);

            while(volumn!=0)
            {
                root=find_root(vessel);
                if(root==n+1)
                    break;
                if(volumn<=max[root]-now[root])
                {
                    now[root] +=volumn;
                    volumn=0;
                }
                else
                {
                    volumn -= max[root]-now[root];
                    now[root] = max[root];
            //如果溢出当前的杯子的话,让它合并下面的杯子,更新pre[temp]
                   mix(root,root+1);//这里已经改变了root的祖先
                }
                vessel=root;//不停的变换这个祖先
            }
        }
        else
        {
            scanf("%lld",&vessel);
            if(pre[vessel]==vessel)
                printf("%lld\n",now[vessel]);
            else
                printf("%lld\n",max[vessel]);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值