D. Vessels

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.

D. Vessels_第1张图片

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 ≤ n1 ≤ xi ≤ 1091 ≤ 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


开始一直re  检查不到数组越界。。后来才发现原来是没看到这句 The liquid that overflows from the n-th vessel spills on the floor.

因此要限定p<n   

此外 还会超时;

///////////////////////////

本以为就这么简单,没想到数据太变态。。一直超时。。。

......最后的最后发现,数据没问题,算法也没问题,居然是实现的过程漏了一个“巨大”的小细节。代码中带*行。

代码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;


int n;
int   vol[200050];
int water[200050];
int pour_to[200050];
int main()
{ 
	int i,p,x,m;
	scanf("%d",&n);
	for (i=1;i<=n;i++)
	{
		scanf("%d",&vol[i]);
		pour_to[i]=i+1;
 
	}
 
	
	
	scanf("%d",&m);
	int c;
	for (i=1;i<=m;i++)
	{
		scanf("%d",&c);
		if (c==1)
		{
			
			scanf("%d%d",&p,&x);
			
			 
			
			if (p>n||p<=0) continue;
			
			while  (x-(vol[p]-water[p])>0)
			{
				
		
		
				if (p>n||p<=0) break; 
				x-=vol[p]-water[p];
				water[p]=vol[p];
                   //////////////////////////////////////////////////////
				int ok=0;
				int tmp=p;
					while (water[pour_to[p]]==vol[pour_to[p]]&&pour_to[p]<=n) 
					{	
						p=pour_to[p]; 
						ok=1;
					} 
				if (ok) 	pour_to[tmp]=p;       //*****************************一直忘记写这句,没这句就相当于没有跳跃,和不存在pour_to数组一样,难怪超时
///////////////////////////////////////////////////////////跳跃---防超时

 if (!ok) p++;
				 
			
			}
	    	if (p>n||p<=0) continue;
			
			if (x-(vol[p]-water[p])<=0)
				water[p]+=x;
		
			
		}
		if (c==2)
		{
			int y;
			scanf("%d",&y);
			
			printf("%d\n",water[y]);
			continue;
		}
		
	}
	
	
	
	
	
	
	return 0;
	
} 





你可能感兴趣的:(D. Vessels)