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 ailiters.

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 integersa1, 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

这个题的做法应该有很多。

这种写法有点像并查集。

其实也就是模拟的思想,把已经满了的节点delete,不断的调整后继节点。每个点大概访问2次左右,跑了140ms。

#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <set>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
typedef long long LL ;
const int Max_N = ;
int Right[Max_N] ;
int now[Max_N] ;
int limit[Max_N] ;
int N ,M ; int find_right(int id,int x){
if(id != -){
int leave = limit[id] - now[id] ;
if(leave >= x){
now[id] += x ;
return id ;
}
else{
now[id] = limit[id] ;
return Right[id] = find_right(Right[id],x - leave) ;
}
}
else
return - ;
} int main(){
int kind ,id ,x ;
while(scanf("%d",&N)!=EOF){
for(int i = ; i <= N ; i++){
scanf("%d",&limit[i]) ;
now[i] = ;
Right[i] = i+ ;
}
Right[N] = - ;
scanf("%d",&M) ;
while(M--){
scanf("%d%d",&kind,&id) ;
if(kind == ){
scanf("%d",&x) ;
find_right(id,x) ;
}
else
printf("%d\n",now[id]) ;
}
}
return ;
}

Codeforces Round #218 (Div. 2) D. Vessels的更多相关文章

  1. 二分搜索 Codeforces Round #218 (Div. 2) C. Hamburgers

    题目传送门 /* 题意:一个汉堡制作由字符串得出,自己有一些原材料,还有钱可以去商店购买原材料,问最多能做几个汉堡 二分:二分汉堡个数,判断此时所花费的钱是否在规定以内 */ #include < ...

  2. Codeforces Round #218 (Div. 2)

    500pt, 题目链接:http://codeforces.com/problemset/problem/371/A 分析:k-periodic说明每一段长度为k,整个数组被分成这样长度为k的片段,要 ...

  3. Codeforces Round #218 (Div. 2) C. Hamburgers

    C. Hamburgers time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. Codeforces Round #218 (Div. 2) B. Fox Dividing Cheese

    B. Fox Dividing Cheese time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. Codeforces Round #218 (Div. 2) C题

    C. Hamburgers time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. Codeforces Round #218 (Div. 2) (线段树区间处理)

    A,B大水题,不过B题逗比了题意没理解清楚,讲的太不清楚了感觉= =还是英语弱,白白错了两发. C: 二分答案判断是否可行,也逗比了下...二分的上界开太大导致爆long long了...   D: ...

  7. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  8. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  9. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

随机推荐

  1. PHPCMS-首页的二级导航、轮播效果

    导航栏:(header.html) <div id="menu"> <a href="{siteurl($siteid)}"><d ...

  2. scala函数定义的四种方式

    最近开始接触scala编程语言,觉得还是比较新的一个东西,虽然说和java比较像,是java的继承者,兼顾面向对象编程和函数式编程的优点,但是,终究是一个新的东西,还是要从最基本的学起.而这当中,函数 ...

  3. mootools里选择器$,$$,$E,$ES等的区别

    区别就是 $和$$都是1个参数, $适用于ID,或者ID代表的对象 $$适用于CSS选择器 $E和$ES,有2个参数,第二个参数是可选参数代表(filter,即某个ID范围里的元素) $E('inpu ...

  4. 转: ExtJS中xtype一览

    转: ExtJS中xtype一览 基本组件: xtype Class 描述 button Ext.Button 按钮 splitbutton Ext.SplitButton 带下拉菜单的按钮 cycl ...

  5. 在程序中使用gettid()的方法

    gettid()这个函数不可以在程序中直接使用,它是linux本身的一个函数,直接使用会出现,尚未声明之类的错误. 我们可以自已定义实现方法,如下: #include <sys/syscall. ...

  6. Microsoft SQL Server Management Studio 导出触发器脚本

  7. C#:基于WMI查询USB设备

    来源:http://blog.csdn.net/jhqin/article/details/6734673 /* ------------------------------------------- ...

  8. VisualSVN Server以及TortoiseSVN客户端的配置和使用方法

    http://www.cnblogs.com/beautifulFuture/archive/2014/07/01/3818211.html 近期学习代码管理工具,首先学习一下svn和Tortoise ...

  9. Python处理Excel文档(xlrd, xlwt, xlutils)

    简介 xlrd,xlwt和xlutils是用Python处理Excel文档(*.xls)的高效率工具.其中,xlrd只能读取xls,xlwt只能新建xls(不可以修改),xlutils能将xlrd.B ...

  10. mysql多表字段名重复的情况

    CREATE TABLE `card` ( `id` ) unsigned NOT NULL AUTO_INCREMENT, `json_str` ) NOT NULL, `f` ,) unsigne ...