codeforces日记371d

来源:互联网 发布:应收账款软件标志 编辑:程序博客网 时间:2024/06/05 07:31

题目:

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:

  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
25 1061 1 42 11 2 51 1 42 12 2
output
458
input
35 10 861 1 122 21 1 61 3 22 22 3
output
7105

解题思路:

依照题意最直接的解法会超时。改进的方案是使用一个nex数组记录水会流向的下一个未满的碟子。如2满3未满,则向1倒水时,倒满1之后直接跳到3.

ac代码:

#include <iostream>using namespace std;int a[200000];int b[200000];int nex[200000];int n;int  add(int p,int x){        if(n==p) return n;    b[p]+=x;    if(b[p]>a[p]){        int remain=b[p]-a[p];        b[p]=a[p];        return nex[p]=add(nex[p],remain);    }    else{        return p;    }    }    int main(){            int m;    int flag;    int p,x,k;    cin>>n;    for(int i=0;i<n;i++){        cin>>a[i];        b[i]=0;        nex[i]=i+1;    }    cin>>m;    for(int i=0;i<m;i++){        cin>>flag;        if(flag==1){            cin>>p>>x;            p--;            add(p,x);        }        else{            cin>>k;            cout<<b[k-1]<<endl;        }    }}

0 0