树状数组,一个想法是当往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. 26 BasicUsageEnvironment基本使用环境——Live555源码阅读(三)UsageEnvironment

    26 BasicUsageEnvironment基本使用环境--Live555源码阅读(三)UsageEnvironment 26 BasicUsageEnvironment基本使用环境--Live5 ...

  2. 解压.tar.gz出错gzip: stdin: not in gzip format tar: /Child returned status 1 tar: Error is not recoverable: exiting now

    先查看文件真正的属性是什么? [root@xxxxx ~]# tar -zxvf tcl8.4.16-src.tar.gz  gzip: stdin: not in gzip format tar: ...

  3. BurpSuite使用设置

    一.设置字体 二.设置字符集 三.设置浏览器代理 四.BurpSuite访问步骤 五.在Target中可以查看截获的数据包

  4. IDEA 编译找不到符号,文件却没有错误。

    单独编译提交找不到符号的文件. DIEAA

  5. C# 条件编译备忘

    第一步:配置管理器中新建解决方案配置 第二步:定义条件编译符号: 第三步:在代码中使用自定义的条件编译 #if CustomDebug Console.WriteLine("dsads&qu ...

  6. Context.managedQuery()和context.getContentResolver()获取Cursor关闭注意事项

    在获取图片缩略图时,获取游标并进行相关的操作. Cursor cursor = context.getContentResolver().query(MediaStore.Images.Thumbna ...

  7. docker 初探

    2015-08-14 09:24:42 查看本地已经有的镜像: docker images 启动镜像实例(例centos), 并进入centos命令行提示符 (镜像的一个实例就叫container): ...

  8. win8开发wpf程序遇到的无语问题

    在设置wpf程序全屏后,点击某个listbox列表,发现程序下面出现了任务栏. 查找解决答案未果.仔细一想可能是win8系统的问题. 最后试着把listbox的滚动条去掉了,问题解决. 原因:当程序中 ...

  9. JavaScript高级程序设计学习笔记--面向对象程序设计

    工厂模式 虽然Object构造函数或对象字面量都可以用来创建单个对象,但这些方式有个明显的缺点:使用同一个接口创建很多对象,会产生大量的重复代码.为解决这个问题,人们开始使用 工厂模式的一种变体. f ...

  10. IOS searchBar去掉背景

    修改UISearchBar的背景颜色 UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的 ...