树状数组,一个想法是当往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. 深入理解学习Git工作流

    http://blog.csdn.net/hongchangfirst/article/list/3 //可以看看 http://blog.csdn.net/hongchangfirst/articl ...

  2. Nunit-Writing Tests

    Nunit 测试可以被任意支持attributes的.net语言使用 Attributes被用于去标识测试类和测试方法,然后通过不同的方式修改他们的行为 Assertions针对一个或多个约束,测试一 ...

  3. Increasing Triplet Subsequence

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  4. Python QRCODE

  5. Unity3d《Shader篇》自定义光照模型

    一.理论 公式1:被光照的物体最终所呈现的颜色公式 最终颜色=材质颜色*发光颜色 公式2:材质颜色 tex2D(_MainTex,uv) 公式3:光照颜色 光照颜色=自发光+环境光+漫反射+镜面反射 ...

  6. sql语句全集

    --删除外键alter table AdItem drop constraint AdOrder_AdItem_FK1(外键名称) --增加外键alter table AdItem add const ...

  7. js计算在线时长

    后台传来毫秒,前台转换成对应的时分秒 //在线时长 function longTime(seconds){ //总秒数 //seconds=seconds/1000; //得到小时 var hour ...

  8. 【linux】nohup运行守护进程

    来源:http://www.cnblogs.com/allenblogs/archive/2011/05/19/2051136.html nohup 命令 用途:不挂断地运行命令. 语法:nohup ...

  9. IOS - 首次启动程序出现的画面介绍

    1.在appdelegate.m中找到 “application:didFinishLaunchingWithOptions:” 通过NSUserDefaults 来进行判断 if (![[NSUse ...

  10. osgearth 配置mapNode TerrainOptions

    设置瓦片PagedLOD节点最小可视距离因子,默认是6.0 minTileRangeFactor() Map *map = new Map();WWOptions wwImgGlobe;map-> ...