您的位置:首页 > 其它

CodeForces - 371D. Vessels(并查集)

2017-07-21 20:08 465 查看
题目链接:http://codeforces.com/problemset/problem/371/D点击打开链接

D. Vessels

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

Examples

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储存 这样就不用一次遍历而t

#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <stack>
#include <algorithm>
#include <queue>
#include <string.h>
#include <set>
using namespace std;
int n=0;
struct xjy
{
int t;
int v;
}a[222222];
int pre[222222];
int findx(int x)
{
int r=x;
while(pre[r]!=r)
{
r=pre[r];
}

int i=x;int j;
while(pre[i]!=r)
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
void join (int x,int y)
{
int p1=findx(x);
int p2=findx(y);
if(p1!=p2)
{
if(p1>p2)
pre[p2]=p1;
else
pre[p1]=p2;
}
}
void add(int num,int addv)
{
int r=findx(num);
while(addv>0&&r<=n)
{
if((a[r].v-a[r].t)>=addv)
{
a[r].t+=addv;
addv=0;
}
else
{
addv-=(a[r].v-a[r].t);
a[r].t=a[r].v;
join(r,r+1);
}
r=findx(r);
}
}
int main()
{
for(int i=0;i<=200001;i++)
pre[i]=i;
scanf("%d",&n);
xjy mid;
for(int i=1;i<=n;i++)
{
scanf("%d",&mid.v);
mid.t=0;
a[i]=mid;
}
int nn=0;
scanf("%d",&nn);
for(int i=0;i<nn;i++)
{
int midmid;
scanf("%d",&midmid);
if(midmid==1)
{
int num;int addv;
scanf("%d%d",&num,&addv);
add(num,addv);
}
else
{
int num;
scanf("%d",&num);
printf("%d\n",a[num].t);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: