树状数组,一个想法是当往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. livezilla账号或密码修改方法

    livezilla的账号和密码不在数据库,保存在php文件里面. 今天想修改一下网站livezilla系统管理员账号和密码,去数据库找了半天没找到,推测可能是存在文件中.搜索了一下,果然是在livez ...

  2. Struts2中的OGNL通配符

    <action name="*_*" class="action.{1}Action" method="{2}"> 匹配,第一个 ...

  3. python wraps

    用代码说明问题: def d(f): def _d(*args, **kwargs): print f.__name__, ' is called' f(*args, **kwargs) return ...

  4. excle导入

    public function import_upload(){ set_time_limit(900); if(!empty($_FILES ['xls_path']['name'])){ $tmp ...

  5. struts2框架 初始别

    struts2 是webwork和struts合并而来. 1.下载struts2 说明: Full Distribution: 为完整版下载,建议下载它 Example Applications:st ...

  6. Oracle10g下载地址--多平台下的32位和64位

    前段时间ORACLE把10G的下载从官网拿掉了 ,许多童鞋不知道ORACLE 10g 的下载地址,这里我附上oracle 10g 下载的链接,方便大家下载.      点击链接使用迅雷即可下载.    ...

  7. ACM/ICPC 之 DP-整数划分问题初探 (POJ1221)

    写下这道题的原因很简单= =,因为这一题的状态转移方程不好找,另一方面,我看到很多针对这一题写的解题报告都把累加状态说得模棱两可,甚至直接说成了一个单一状态,弄得本是菜鸟的我硬生生折磨了一上午画了几个 ...

  8. HttpWebRequest.GetResponse 方法

    GetResponse 方法返回包含来自 Internet 资源的响应的 WebResponse 对象. 实际返回的实例是 HttpWebResponse,并且能够转换为访问 HTTP 特定的属性的类 ...

  9. MVP设计模式的实现

    MVP:界面与业务逻辑分离在Winform中的应用 MVP,Model-View-Presenter的缩写. 在MSDN上,下载了一个示例,http://www.microsoft.com/china ...

  10. 【编程题目】题目:定义 Fibonacci 数列 输入 n,用最快的方法求该数列的第 n 项。

    第 19 题(数组.递归):题目:定义 Fibonacci 数列如下:/ 0 n=0f(n)= 1 n=1/ f(n-1)+f(n-2) n=2输入 n,用最快的方法求该数列的第 n 项. 思路:递归 ...