Codeforces Round #218 (Div. 2) D. Vessels
2 seconds
256 megabytes
standard input
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:
- 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.
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).
For each query, print on a single line the number of liters of water in the corresponding vessel.
2
5 10
6
1 1 4
2 1
1 2 5
1 1 4
2 1
2 2
4
5
8
3
5 10 8
6
1 1 12
2 2
1 1 6
1 3 2
2 2
2 3
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的更多相关文章
- 二分搜索 Codeforces Round #218 (Div. 2) C. Hamburgers
题目传送门 /* 题意:一个汉堡制作由字符串得出,自己有一些原材料,还有钱可以去商店购买原材料,问最多能做几个汉堡 二分:二分汉堡个数,判断此时所花费的钱是否在规定以内 */ #include < ...
- Codeforces Round #218 (Div. 2)
500pt, 题目链接:http://codeforces.com/problemset/problem/371/A 分析:k-periodic说明每一段长度为k,整个数组被分成这样长度为k的片段,要 ...
- Codeforces Round #218 (Div. 2) C. Hamburgers
C. Hamburgers time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 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 ...
- Codeforces Round #218 (Div. 2) C题
C. Hamburgers time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #218 (Div. 2) (线段树区间处理)
A,B大水题,不过B题逗比了题意没理解清楚,讲的太不清楚了感觉= =还是英语弱,白白错了两发. C: 二分答案判断是否可行,也逗比了下...二分的上界开太大导致爆long long了... D: ...
- 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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
随机推荐
- 响应式布局设置--@media only screen and
@media only screen and only(限定某种设备) screen 是媒体类型里的一种 and 被称为关键字,其他关键字还包括 not(排除某种设备) /* 常用类型 */类型 解 ...
- CentOS 7.0系统安装配置图解教程
转自:http://www.osyunwei.com/archives/7829.html 操作系统:CentOS 7.0 64位 IP地址:192.168.21.128 网关:192.168.21. ...
- Python Beautiful Soup模块的安装
以安装Beautifulsoup4为例: 1.到网站上下载:http://www.crummy.com/software/BeautifulSoup/bs4/download/ 2.解压文件到C:\P ...
- Centos7和win7双系统调整默认启动
centos7之后都上grub2了,所以你要更改默认启动项什么的就不能像以前一样去改 /etc/grub.conf 当然你更不能去改/etc/grub2.conf 上了grub2之后,在设计有意规避让 ...
- SqlServer统计最近一周的数据
select * from 表名 where DATEDIFF( day, 日期字段列名,getdate())<7 and DATEPART(w, 日期字段列名) <DATEPART( ...
- ERP_基于Oracle ADF的定制化企业级IT系统解决方案
2014-12-31 Created By BaoXinjian
- jsPlumb
官网:https://jsplumbtoolkit.com GitHub:https://github.com/sporritt/jsplumb/ 初始化 jsPlumb只有等到DOM初始化完成之后才 ...
- 转--C++学习笔记(原创)
http://www.cnblogs.com/maowang1991/p/3290321.html 以下内容为自己一年多的C++学习心得,纯原创,转载请注明源地址. 一年多的C++学习过程中,自己阅读 ...
- Hadoop中两表JOIN的处理方法(转)
1. 概述 在传统数据库(如:MYSQL)中,JOIN操作是非常常见且非常耗时的.而在HADOOP中进行JOIN操作,同样常见且耗时,由于Hadoop的独特设计思想,当进行JOIN操作时,有一些特殊的 ...
- ucos-内存管理:
注意:一个内存分区至少含有2个内存块(块的大小至少能满足一个指针大小) 1先定义一个内存块结构指针OS_MEM *buffMEM,在定义一个而为指针A[m][n] 2创建内存分区:buffMEM=OS ...