CodeForces 371D. Vessels
2013-12-11 13:33
946 查看
暴力+胡乱优化就过了。。tags给的东西似乎什么都没用到。。。。。CF的数据是不是有点水啊。。。。。果然是没有营养的题目。。。。。
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.
Sample test(s)
input
output
input
output
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.
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
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int a[320000],v[320000],n,m,s,p,x,next[320000]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",a+i); next[i]=i+1; } next[n+1]=n+1; scanf("%d",&m); while(m--) { scanf("%d",&s); if(s==1) { scanf("%d%d",&p,&x); int i,j; for(i=p;i<=n;i=next[i]) { if(v[i]==a[i]) continue; int temp=min(a[i]-v[i],x); v[i]+=temp; x-=temp; if(!x) break; } if(v[i]==a[i]) i++; int t; for(j=p;j<i;j=t) { t=next[j]; next[j]=i; } } else if(s==2) { scanf("%d",&p); printf("%d\n",v[p]); } } return 0; }
相关文章推荐
- CodeForces - 371D. Vessels(并查集)
- Vessels CodeForces - 371D
- Codeforces 371 A,B,C
- codeforces 371 C-Hamburgers
- codeforces-371 B#
- 二分专题 codeforces 「343 C」 &「371 C」 题解
- codeforces-371-C. Sonya and Queries
- codeforces 371B - Fox Dividing Cheese
- codeforces 371B - Fox Dividing Cheese
- codeforces 371D
- codeforces 371D
- CodeForces 371 E.Subway Innovation(贪心)
- codeforces 257c
- codeforces-550B-Preparing Olympiad
- codeforces_677C. Vanya and Label(快速幂)
- codeforces 285C Building Permutation (简单贪心)
- Codeforces - 817D - Imbalanced Array (单调栈)
- Codeforces 667C Reberland Linguistics 【dp】
- codeforces 449B Jzzhu and Cities (Dij+堆优化)
- codeforces 589 I - Lottery(水)