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

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<string>
  6. #include<algorithm>
  7. #include<map>
  8. #include<queue>
  9. #include<vector>
  10. #include<cmath>
  11. #include<utility>
  12. using namespace std;
  13. typedef long long LL;
  14. const int N = 200008;
  15. const LL INF = (LL)1 << (LL)61;
  16. #define MS(a, num) memset(a, num, sizeof(a))
  17. #define PB(A) push_back(A)
  18. #define FOR(i, n) for(int i = 0; i < n; i++)
  19. LL C[N];
  20. LL cap[N];
  21. LL rem[N];
  22. int n;//注意初始化n
  23. inline int lowbit(int x){
  24. return x&-x;
  25. }
  26. inline void add(int x, LL val){
  27. for(int i=x;i<=n;i+=lowbit(i)){
  28. C[i] += val;
  29. }
  30. }
  31. inline LL sum(int x){//求1到x的和
  32. LL ret = 0;
  33. for(int i=x;i>0;i-=lowbit(i)){
  34. ret+=C[i];
  35. }
  36. return ret;
  37. }
  38. int main(){
  39. cin>>n;
  40. for(int i = 1;i <= n; i++){
  41. scanf("%I64d", &cap[i]);
  42. rem[i] = cap[i];
  43. add(i, cap[i]);
  44. }
  45. cap[n + 1] = INF;
  46. rem[n + 1] = INF;
  47. add(n + 1, INF);
  48. int m;
  49. cin>>m;
  50. int op, x,p , k;
  51. while(m--){
  52. scanf("%d", &op);
  53. if(op == 1){
  54. scanf("%d %d", &p, &x);
  55. int l = p, r = n +1;
  56. int ans = l;
  57. LL s = sum(p - 1);
  58. int start = p;
  59. while(l < r){
  60. int mid = (l + r)>>1;
  61. LL s2= sum(mid) - s;
  62. if(s2 == 0){
  63. start = mid + 1;
  64. }
  65. if(s2 < x){
  66. l = mid + 1;
  67. }else{
  68. r = mid;
  69. }
  70. }
  71. ans = l;
  72. LL s3 = sum(ans -1) - s;
  73. for(int i = start; i < ans ; i++){
  74. if(rem[i]){
  75. add(i, -rem[i]);
  76. rem[i] = 0;
  77. }
  78. }
  79. if(x - s3){
  80. add(ans,-( x - s3));
  81. rem[ans] -= x - s3;
  82. }
  83. }else{
  84. scanf("%d", &k);
  85. printf("%I64d\n", cap[k] - rem[k]);
  86. }
  87.  
  88. }
  89. return 0;
  90. }

  

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. c随机数&运行时间

    #include<stdlib.h> #include<time.h> srand((unsigned) time(NULL)); //用时间做种,每次产生随机数不一样 pri ...

  2. 将 JAR 转为 EXE – EXE4J 的使用教程(第一期)(转载)

    http://www.iteknical.com/convert-jar-to-exe-phase-i-exe4j-tutorial/

  3. HIFI播放器--磨机吐槽篇

    最近看到淘宝店提供各种随身播放器磨机服务,说的是天花乱坠,给你更换零件, 甚至更改电路,搭载上去,是如何如何的好,整个播放器就上升了几个等次,收费还 不低,至少是好几百,我实在是忍不住吐槽了,你们这些 ...

  4. iframe操作

    http://www.jb51.net/article/15780.htmiframe自适应高度:经典<iframe src="http://www.lanyunwork.com/&q ...

  5. 【GoLang】golang 面向对象编程 & 面向接口编程

    005.面向对象&接口编程 1 面向函数编程 1.1 将数据作为参数传递到函数入参 1.2 对象与函数是分离的 2 面向对象编程 2.1 使用者看起来函数作为对象的属性而非参数 2.2 函数属 ...

  6. Google Code Jam 2015 R1C B

    题意:给出一个键盘,按键都是大写字母.给出一个目标单词和一个长度L.最大值或者最大长度都是100.现在随机按键盘,每个按键的概率相同. 敲击出一个长度为L的序列.求该序列中目标单词最多可能出现几次,期 ...

  7. NGUI Table页(UIToggle和UIToggledObjects)

    1.添加两个按钮Btn1和Btn2.添加2个Spr1和Spr2 2.给每个Btn添加两个脚本UIToggle和UIToggledObjects 3.将每个UIToggle的Group设置个非0的值 4 ...

  8. stream的seek方法实例

    using (FileStream outStream = new FileStream(@"D:\12.txt", FileMode.Open)) { using (FileSt ...

  9. LightOJ1336 Sigma Function(约数和为偶数的个数)

    Sigma Function Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit ...

  10. jsp通过s:hidden传值给后台,后台数据出现了若干逗号问题

    <s:iterator value="rpActionVO.page.result" id="list" status="st"> ...