Codeforces Round #218 (Div. 2)---D. 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.
Sample test(s)
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

一开始用线段树,怎么都wa,而且效率也不是很好
然后改成并查集就过了,把全部都满的而且连在一起的盘子放到一个集合里,下次要放的话,直接找根,我把根设置为这些都满的盆子的最下面下一个(这个没满)

/*************************************************************************
    > File Name: CF-218-D.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年04月09日 星期四 21时01分20秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

static const int N = 200100;
int par[N];
int arr[N];
int use[N];

int find(int x)
{
    if (par[x] == x)
    {
        return x;
    }
    return par[x] = find(par[x]);
}

int main()
{
    int n, q;
    while (~scanf("%d", &n))
    {
        memset(use, 0, sizeof(use));
        for (int i = 1; i <= n; ++i)
        {
            par[i] = i;
            scanf("%d", &arr[i]);
        }
        int o, X, val;
        scanf("%d", &q);
        while (q--)
        {
            scanf("%d", &o);
            if (o == 2)
            {
                scanf("%d", &X);
                printf("%d\n", use[X]);
            }
            else
            {
                scanf("%d%d", &X, &val);
                int fa = find(X);
                int last;
                if (arr[fa] - use[fa] >= val)
                {
                    use[fa] += val;
                    continue;
                }
                while (fa <= n && val && arr[fa] - use[fa] < val)
                {
                    val -= arr[fa] - use[fa];
                    use[fa] = arr[fa];
                    last = fa;
                    ++fa;
                    if (fa > n)
                    {
                        break;
                    }
                    fa = par[fa];
                    par[last] = fa;
                }
                if (fa <= n && arr[fa] - use[fa] >= val)
                {
                    use[fa] += val;
                }
            }
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值