树状数组,一个想法是当往p注水时,认为是其容量变小了,更新时二分枚举,注意一些优化。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = 200008;
const LL INF = (LL)1 << (LL)61;
#define MS(a, num) memset(a, num, sizeof(a))
#define PB(A) push_back(A)
#define FOR(i, n) for(int i = 0; i < n; i++)
LL C[N];
LL cap[N];
LL rem[N];
int n;//注意初始化n
inline int lowbit(int x){
return x&-x;
}
inline void add(int x, LL val){
for(int i=x;i<=n;i+=lowbit(i)){
C[i] += val;
}
}
inline LL sum(int x){//求1到x的和
LL ret = 0;
for(int i=x;i>0;i-=lowbit(i)){
ret+=C[i];
}
return ret;
}
int main(){
cin>>n;
for(int i = 1;i <= n; i++){
scanf("%I64d", &cap[i]);
rem[i] = cap[i];
add(i, cap[i]);
}
cap[n + 1] = INF;
rem[n + 1] = INF;
add(n + 1, INF);
int m;
cin>>m;
int op, x,p , k;
while(m--){
scanf("%d", &op);
if(op == 1){
scanf("%d %d", &p, &x);
int l = p, r = n +1;
int ans = l;
LL s = sum(p - 1);
int start = p;
while(l < r){
int mid = (l + r)>>1;
LL s2= sum(mid) - s;
if(s2 == 0){
start = mid + 1;
}
if(s2 < x){
l = mid + 1;
}else{
r = mid;
}
}
ans = l;
LL s3 = sum(ans -1) - s;
for(int i = start; i < ans ; i++){
if(rem[i]){
add(i, -rem[i]);
rem[i] = 0;
}
}
if(x - s3){
add(ans,-( x - s3));
rem[ans] -= x - s3;
}
}else{
scanf("%d", &k);
printf("%I64d\n", cap[k] - rem[k]);
} }
return 0;
}

  

CodeForces 371D Vessels(树状数组)的更多相关文章

  1. codeforces 597C (树状数组+DP)

    题目链接:http://codeforces.com/contest/597/problem/C 思路:dp[i][j]表示长度为i,以j结尾的上升子序列,则有dp[i][j]= ∑dp[i-1][k ...

  2. Codeforces 597C. Subsequences (树状数组+dp)

    题目链接:http://codeforces.com/contest/597/problem/C 给你n和数(1~n各不同),问你长为k+1的上升自序列有多少. dp[i][j] 表示末尾数字为i 长 ...

  3. Sereja and Brackets CodeForces - 380C (树状数组+离线)

    Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...

  4. Mishka and Interesting sum Codeforces Round #365 (树状数组)

    树状数组,与Turing Tree类似. xr[i]表示从1到i的抑或,树状数组维护从1到i每个数只考虑一次的异或,结果为sum(r) ^ sum(l) ^ xr[r] ^ xr[l] 其中xr[r] ...

  5. Codeforces 1096F(dp + 树状数组)

    题目链接 题意: 对于长度为$n$的排列,在已知一些位的前提下求逆序对的期望 思路: 将答案分为$3$部分 $1.$$-1$与$-1$之间对答案的贡献.由于逆序对考虑的是数字之间的大小关系,故假设$- ...

  6. DNA Evolution CodeForces - 828E(树状数组)

    题中有两种操作,第一种把某个位置的字母修改,第二种操作查询与[L, R]内与给出字符串循环起来以后对应位置的字母相同的个数.给出的字符串最大长度是10. 用一个四维树状数组表示 cnt[ATCG的编号 ...

  7. Petya and Array CodeForces - 1042D (树状数组)

    D. Petya and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. Codeforces 1139F(树状数组+扫描线)

    题目传送 做法 对于每个人,inc为x,pref为y:对于每道菜,p和s为x,b为y 于是根据题意有\[p[i]<=x<=s[i]\]\[p[i]+b[i]<=x+y\]\[p[i] ...

  9. Codeforces 961E - Tufurama 树状数组

    转自:https://blog.csdn.net/my_sunshine26/article/details/79831362 题目大意: i从1开始 基本思路: 完全没思路,所以上来就二分,果不其然 ...

随机推荐

  1. 3 EventTime 事件时间类和TimeNow函数——Live555源码阅读(一)基本组件类

    这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 这里是时间相关类的第三个部分,也是最后一个部分. EventTime 事件时间类 这个类和Dela ...

  2. Python自动化之配置Python模块国内镜像

    Linux环境 在~/.pip/pip.conf文件中添加或修改 [global] index-url = http://mirrors.aliyun.com/pypi/simple/ [instal ...

  3. linux文件远程传输客户端shell脚本与分布式客户机时间同步脚本

    #!/bin/bash # 将代码和脚本传送至worker节点 # 改变当前工作目录 cd ${AMAZONCRAWLER_HOME} #读取worker节点ip列表 i= while read li ...

  4. kill新号专题

    一.在tomcat启动脚本中看到,kill  -3

  5. Spring4 学习笔记

    [9]SpEL语法 [10]Bean的生命周期:(五步)

  6. 在eclipse上开发hadoop2.5.2程序的快捷方法

    本文仍然使用MapReduce的经典例子 WordCount来测试eclipse的开发环境. 与大部分教程不同的是,本文使用的hadoop是2.5.2的版本,相较于之前的0.X版本,hadoop 2. ...

  7. Twisted安装

    Debian sudo apt-get install gcc python-dev && sudo pip install twisted CentOS sudo yum insta ...

  8. android气泡消息提醒布局

    无论是anroid还是ios,气泡消息提醒再正常不过了.然而要定义一个气泡消息提醒确要费一番周折.下面记录下气泡提醒布局. 定义气泡背景shape_unread_message_bg.xml < ...

  9. MySQL 5.6 Threadpool(优先队列)介绍及性能测试【转】

    本文来自:http://www.gpfeng.com/?p=540&utm_source=tuicool&utm_medium=referral 背景介绍 MySQL常用(目前线上使用 ...

  10. 数据结构——B树、B+树

    B树和B+树主要应用于外排序,对于外排序,从硬盘读取的时间要远远大于遍历树的时间,因此要想办法减少从硬盘读取的时间. B树(有时也叫B-树) M阶B树定义如下: 是一种多路搜索树(并不是二叉的):1. ...