CodeForces 371D Vessels(树状数组)
树状数组,一个想法是当往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(树状数组)的更多相关文章
- codeforces 597C (树状数组+DP)
题目链接:http://codeforces.com/contest/597/problem/C 思路:dp[i][j]表示长度为i,以j结尾的上升子序列,则有dp[i][j]= ∑dp[i-1][k ...
- Codeforces 597C. Subsequences (树状数组+dp)
题目链接:http://codeforces.com/contest/597/problem/C 给你n和数(1~n各不同),问你长为k+1的上升自序列有多少. dp[i][j] 表示末尾数字为i 长 ...
- Sereja and Brackets CodeForces - 380C (树状数组+离线)
Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...
- 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] ...
- Codeforces 1096F(dp + 树状数组)
题目链接 题意: 对于长度为$n$的排列,在已知一些位的前提下求逆序对的期望 思路: 将答案分为$3$部分 $1.$$-1$与$-1$之间对答案的贡献.由于逆序对考虑的是数字之间的大小关系,故假设$- ...
- DNA Evolution CodeForces - 828E(树状数组)
题中有两种操作,第一种把某个位置的字母修改,第二种操作查询与[L, R]内与给出字符串循环起来以后对应位置的字母相同的个数.给出的字符串最大长度是10. 用一个四维树状数组表示 cnt[ATCG的编号 ...
- Petya and Array CodeForces - 1042D (树状数组)
D. Petya and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 1139F(树状数组+扫描线)
题目传送 做法 对于每个人,inc为x,pref为y:对于每道菜,p和s为x,b为y 于是根据题意有\[p[i]<=x<=s[i]\]\[p[i]+b[i]<=x+y\]\[p[i] ...
- Codeforces 961E - Tufurama 树状数组
转自:https://blog.csdn.net/my_sunshine26/article/details/79831362 题目大意: i从1开始 基本思路: 完全没思路,所以上来就二分,果不其然 ...
随机推荐
- c随机数&运行时间
#include<stdlib.h> #include<time.h> srand((unsigned) time(NULL)); //用时间做种,每次产生随机数不一样 pri ...
- 将 JAR 转为 EXE – EXE4J 的使用教程(第一期)(转载)
http://www.iteknical.com/convert-jar-to-exe-phase-i-exe4j-tutorial/
- HIFI播放器--磨机吐槽篇
最近看到淘宝店提供各种随身播放器磨机服务,说的是天花乱坠,给你更换零件, 甚至更改电路,搭载上去,是如何如何的好,整个播放器就上升了几个等次,收费还 不低,至少是好几百,我实在是忍不住吐槽了,你们这些 ...
- iframe操作
http://www.jb51.net/article/15780.htmiframe自适应高度:经典<iframe src="http://www.lanyunwork.com/&q ...
- 【GoLang】golang 面向对象编程 & 面向接口编程
005.面向对象&接口编程 1 面向函数编程 1.1 将数据作为参数传递到函数入参 1.2 对象与函数是分离的 2 面向对象编程 2.1 使用者看起来函数作为对象的属性而非参数 2.2 函数属 ...
- Google Code Jam 2015 R1C B
题意:给出一个键盘,按键都是大写字母.给出一个目标单词和一个长度L.最大值或者最大长度都是100.现在随机按键盘,每个按键的概率相同. 敲击出一个长度为L的序列.求该序列中目标单词最多可能出现几次,期 ...
- NGUI Table页(UIToggle和UIToggledObjects)
1.添加两个按钮Btn1和Btn2.添加2个Spr1和Spr2 2.给每个Btn添加两个脚本UIToggle和UIToggledObjects 3.将每个UIToggle的Group设置个非0的值 4 ...
- stream的seek方法实例
using (FileStream outStream = new FileStream(@"D:\12.txt", FileMode.Open)) { using (FileSt ...
- LightOJ1336 Sigma Function(约数和为偶数的个数)
Sigma Function Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit ...
- jsp通过s:hidden传值给后台,后台数据出现了若干逗号问题
<s:iterator value="rpActionVO.page.result" id="list" status="st"> ...