Codeforces 371D 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:

  1. Add xi liters of water to the pi-th vessel;
  2. 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 "pi xi", the query of the second type is represented as "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
题目是给出n个从上往下排的容器,然后往里面倒水,两个操作 一个是倒水多了就会流到下面的容器,另一个是输出编号容器中的水有多少。
之前想用暴力法,但是错了,后来想想就算写对了估计也会超时,做这个题是acm集训挂的专题训练,考虑到该用并查集,把装满的并到一块,这样可以减少时间复杂度。


代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int n,m,instruction,p,x,k;
int sum[200001];
int a[200001];
int f[200001];
int find(int a)
{
    if(f[a]==a)return a;
    return f[a]=find(f[a]);
}
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))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            f[i]=i;
        }
        f[n+1]=n+1;
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d",&instruction);
            if(instruction==1)
            {
                scanf("%d%d",&p,&x);
                while(x>0)
                {
                    int pf=find(p);
                    if(pf==n+1)break;
                    if(a[pf]-sum[pf]>=x)//第pf个能够装住这些水
                    {
                        sum[pf]+=x;
                        x=0;
                    }
                    else                //不能装住,pf装满将他与下一个合并
                    {
                        x-=a[pf]-sum[pf];
                        sum[pf]=a[pf];
                        merge(pf,pf+1);
                    }
                    p=pf;
                }
            }
            if(instruction==2)
            {
                int x;
                scanf("%d",&x);
                printf("%d
",sum[x]);
            }
        }
    }
}
原文地址:https://www.cnblogs.com/8023spz/p/7206271.html