树状数组,一个想法是当往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. tfw格式图解

    话不多说,直接看图. 上图中的UV坐标,实际上只的是图像的 横向坐标 和 纵向坐标 .即图像的行和列坐标. 对于图上任意一个像素点(col,row)这个坐标,换算其地理坐标就十分简单. GeoX = ...

  2. 基于用户相似性的协同过滤——Python实现

    代码基本来自项亮的<推荐系统实践>,把书上的伪代码具体实现,还参考了https://www.douban.com/note/336280497/ 还可以加入对用户相似性的归一化操作,效果会 ...

  3. ubuntu hash sum mismatch error

    $ sudo rm -rf /var/lib/apt/lists/* $ sudo apt-get update from: askubuntu.com

  4. Arp攻击实战

    hyddd原创,转载请说明出处. 现在网上很多关于ARP攻击的文章,但多数是描述其原理,少数会以某个工具作为例子展开说明,但感觉说的还是不够详细,今天写个文章以实战介绍一个叫"WinArpA ...

  5. [转]Java 征途:行者的地图

    前段时间应因缘梳理了下自己的 Java 知识体系, 成文一篇望能帮到即将走进或正在 Java 世界跋涉的程序员们. 第一张,基础图 大约在 2003 年我开始知道 Java 的(当时还在用 Delph ...

  6. silverlight 获取服务器上图片出现异常 “AG_E_NETWORK_ERROR”

    前言 之前项目一直是发布在IIS上面使用HTTP访问,现在要求改为HTTPS,通过在IIS生成自签名后,打开HTTPS通道,可以将原来的程序已HTTPS的方式发布出来. 可参见 http://blog ...

  7. 【leetcode】Convert Sorted Array to Binary Search Tree

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  8. C#错误之 System.Threading.ThreadAbortException:正在中止线程

    参考:http://www.cnblogs.com/chendaoyin/archive/2013/06/27/3159211.html 1.开启一个子线程 //开启一个子线程,子线程调用方法 Met ...

  9. Java for LeetCode 208 Implement Trie (Prefix Tree)

    Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...

  10. codeforces 493A. Vasya and Football 解题报告

    题目链接:http://codeforces.com/contest/493/problem/A 题目意思:给出两个字符串,分别代表 home 和 away.然后有 t 个player,每个playe ...