hdu5338 (二进制,双指针)
2015-08-07 10:21
302 查看
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #include <iostream> #include <queue> #include <stack> #include <vector> #include <map> #include <set> #include <string> #include <math.h> using namespace std; #pragma warning(disable:4996) #pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; const int INF = 1<<30; /* */ const int N = 100000 + 10; int a ; LL sum ; int main() { int t, n; scanf("%d", &t); LL ans; while (t--) { ans = 0; scanf("%d", &n); sum[n + 1] = 0; for (int i = 1; i <= n; ++i)scanf("%d", &a[i]); for (int i = n; i >= 1; --i) { sum[i] = sum[i + 1] + i; //计算所有的1*(i+j), 因为log取整之后有+1 ans += (LL)(n - i + 1)*i + sum[i]; } for (int k = 1; k < 40; ++k) { LL lim = 1LL << k; LL s = 0; for (int i = 1, j = 1; i <= n; ++i) { while (j <= n &&s < lim) s += a[j++]; if (s >= lim)//[i,j-1->n]的区间肯定是大于等于lim的 ans += (LL)(n- j + 2) * i + sum[j - 1]; else break; s -= a[i]; } } printf("%lld\n", ans); } return 0; }
View Code
相关文章推荐
- 【转】Java I/O流概念分析整理
- Linux软件安装
- iOS 9应用开发教程之编辑界面与编写代码
- editui刷新数据与列表循环
- 8月4日14家中国域名商(国际域名)解析量报告
- oracle PL/SQL基础学习
- Eclipse-property文件中文显示设置
- easyui阻止事件冒泡
- Codeforces 567A Lineland Mail
- erlang分布式编程模型
- 检查本功能是否在Excel中运行
- POJ 3694 Network(无向图求桥的个数,有重边)
- Unity Singleton 单例类(Unity3D开发之二十)
- 项目启动c3p0 APPARENT DEADLOCK!!! 错误
- MFC中onctlcolor函数返回的画刷导致的内存资源泄露问题
- [贪心]hdu5355 多校联合第六场 Cake
- Unity Singleton 单例类(Unity3D开发之二十)
- 南邮 OJ 1957 计算机基础知识大赛 4
- 南邮 OJ 1956 计算机基础知识大赛 3
- linux下/etc/inittab详解