题目链接:http://codeforces.com/problemset/problem/371/D点击打开链接

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

Examples
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

每个位置满了之后跳转到下一个不满的位置 用pre储存 这样就不用一次遍历而t

#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <stack>
#include <algorithm>
#include <queue>
#include <string.h>
#include <set>
using namespace std;
int n=0;
struct xjy
{int t;int v;
}a[222222];
int pre[222222];
int findx(int x)
{int r=x;while(pre[r]!=r){r=pre[r];}int i=x;int j;while(pre[i]!=r){j=pre[i];pre[i]=r;i=j;}return r;
}
void join (int x,int y)
{int p1=findx(x);int p2=findx(y);if(p1!=p2){if(p1>p2)pre[p2]=p1;elsepre[p1]=p2;}
}
void add(int num,int addv)
{int r=findx(num);while(addv>0&&r<=n){if((a[r].v-a[r].t)>=addv){a[r].t+=addv;addv=0;}else{addv-=(a[r].v-a[r].t);a[r].t=a[r].v;join(r,r+1);}r=findx(r);}
}
int main()
{for(int i=0;i<=200001;i++)pre[i]=i;scanf("%d",&n);xjy mid;for(int i=1;i<=n;i++){scanf("%d",&mid.v);mid.t=0;a[i]=mid;}int nn=0;scanf("%d",&nn);for(int i=0;i<nn;i++){int midmid;scanf("%d",&midmid);if(midmid==1){int num;int addv;scanf("%d%d",&num,&addv);add(num,addv);}else{int num;scanf("%d",&num);printf("%d\n",a[num].t);}}}

CodeForces - 371D. Vessels(并查集)相关推荐

  1. CodeForces - 371D Vessels 【并查集】

    Vessels 题意: 自上而下的n个碗,向某个碗中倒水,如果溢出,则会流向它之下的下一个未满的碗.有两种操作:1 p x表示往第p个碗中导入x的水,2 p表示询问此时第p个碗中的水量. 题解: 未经 ...

  2. Codeforces 371D. Vessels【并查集】

    题目大意: 给出一堆从上到下叠起来的容器,有两种操作:第一种是在编号为i的容器中加入x的水(保证水溢出之后会流到下一个最近的容器(如果存在)中):第二种是查询编号为i的容器中有多少水. 做法: 首先, ...

  3. Secret Passwords CodeForces - 1263D(并查集)

    One unknown hacker wants to get the admin's password of AtForces testing system, to get problems fro ...

  4. Codeforces 103B - Cthulhu(并查集 找环和块)

    传送门:https://codeforces.com/problemset/problem/103/B 题意: 给一个n个结点,m条边的无向图 判断给定图是否满足以下条件: 能被表示为有3个及以上的有 ...

  5. D. Vessels(并查集+模拟)

    https://codeforces.com/problemset/problem/371/D 题意:给一个从上到下容积依次增大的容器,多次操作,给某个容器倒水,如果这个容器满了就会溢出,直到最后一个 ...

  6. Rumor CodeForces - 893C(并查集)

    Vova promised himself that he would never play computer games- But recently Firestorm - a well-known ...

  7. Mahmoud and a Dictionary CodeForces - 766D 种类并查集

    题意 给出多个字符串 然后再输入多个字符串之间的关系 让我们判断这其中是否存在矛盾的关系 有矛盾输出NO 没矛盾输出 YES 然后再输入多个询问 每个询问 两个字符串 让我们判断其中的关系 同义词输出 ...

  8. Mr. Kitayuta‘s Technology CodeForces - 505D(并查集+拓扑排序或dfs找环) 题解

    题目  Shuseki Kingdom is the world's leading nation for innovation and technology. There are n cities ...

  9. Codeforces 437D 贪心+并查集

    这个题目让我想起了上次在湘潭赛的那道跪死了的题.也是最值问题,这个也是,有n个动物园 每个都有权值 然后被m条路径相连接,保证图是连通的,然后求所有的p[i][j]之和.i,j为任意两个zoo,pij ...

  10. Mobile Phone Network CodeForces - 1023F(并查集lca+修改环)

    题意: 就是有几个点,你掌控了几条路,你的商业对手也掌控了几条路,然后你想让游客都把你的所有路都走完,那么你就有钱了,但你又想挣的钱最多,真是的过分..哈哈 游客肯定要对比一下你的对手的路 看看那个便 ...

最新文章

  1. SpringMVC和Dubbo的整合
  2. Charles是Mac的Fiddler抓包工具
  3. ubuntun 16.04 protobuf安装过程
  4. 两台服务器之间mysql数据库怎么做同步_MySQL 数据库同步结构总结
  5. 中国人工智能论文首超美国,背后的秘密竟然是……
  6. PLSQL_海量数据处理系列7_DBSM_STATS
  7. 使用openssl库实现des,3des加密
  8. 教你安装ps,pr,ae,ai等Adobe软件,办公必备
  9. AGX平台MCU升级过程分析2014.10.3
  10. 【光学设计】- 第一节
  11. 大数据挖掘步骤都包括哪些?
  12. 如何写一篇学术研讨会级的论文
  13. 实训(一)学生管理系统
  14. 深度解读:阿里云视频直播功能升级
  15. android 获取设备的mac地址,Android编程获取设备MAC地址的实现方法
  16. 红米note9 android10,红米note9尺寸大小_红米note9手机尺寸
  17. Java、统计正数和负数的个数然后计算这些数的平均值
  18. 最近进行的一次技术选型(工作流引擎)及相关知识介绍
  19. java 事务 回滚
  20. 有苦有乐的算法 --- 一个数组中,有两种数出现了奇数次,其余数都出现了偶数次,找到这两种数

热门文章

  1. 计算机启动F1正在准备自动修复,win10系统卡在正在准备自动修复如何解决
  2. 花店管理java_JAVA基于B/S模式下的在线花店
  3. 多级缓存架构体系介绍
  4. 东西湖秋季手选的学校出游线路导游小妹是这样计划的
  5. 什么是摩尔定律,摩尔定律是否还能继续生效?
  6. 中国省市县JSON对象(Oject)数据、2020年的数据、2021年12月整理
  7. maven--TestEngine with ID ‘junit-vintage‘ failed to discover tests
  8. Monterey安装教程
  9. 【SVN】SVN之管窥蠡测
  10. 电影《一生一世》观后感