First One

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 831    Accepted Submission(s): 253

Problem Description
soda has an integer array a1,a2,…,an. Let S(i,j) be the sum of ai,ai+1,…,aj. Now soda wants to know the value below:

∑i=1n∑j=in(⌊log2S(i,j)⌋+1)×(i+j)

Note: In this problem, you can consider log20 as 0.

 
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains an integer n (1≤n≤105), the number of integers in the array.
The next line contains n integers a1,a2,…,an (0≤ai≤105).

 
Output
For each test case, output the value.
 
Sample Input
1
2
1 1
 
Sample Output
12
 
Source
 
 #include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
const int M = 1e5 + ;
ll n ;
ll a[M] ;
ll ure[M] ;
ll tot ;
void solve () {
for (int k = ; k < ; k ++) {
ll low = 1ll << k , sum = ;
for (int i = , j = ; i <= n ; i ++) {
while (j <= n && sum < low) sum += a[++j] ;
if (sum >= low) tot += i * (n-j+) + ure[j] ;
else break ;
sum -= a[i] ;
}
}
printf ("%I64d\n" , tot) ;
} int main () {
int T ;
scanf ("%d" , &T ) ;
while (T --) {
scanf ("%I64d" , &n) ;
for (int i = ; i <= n ; i ++) scanf ("%I64d" , &a[i]) ; ure[n+] = ;
tot = ;
for (int i = n ; i >= ; i --) {
ure[i] = ure[i+] + i ;
tot += i * (n-i+) + ure[i] ;
} solve () ;
}
return ;
}

比赛的时候思路很明确,log2 + 1那部分最多就1~40,所以枚举一下,每次枚举时用 尺取法 求得所有区间即可。

所以总的复杂度为O(40*n) , 后来又注意到尺取法的界限判断是要映射一下,所以复杂度变成了O(40*n*log40) ,然后oj就给我判TLE了,

这只能说出题人卡的实在是。。。。是在下输了

当然赛后看标成时,还是发现写法漏洞很大。

1.标成上他把log2 和 1 这两部分分开来处理,算1这部分O(n)的复杂度。

2.因为log2那部分是个浮动的区间和,所以直接用 尺取法 不行。(因为我的作法是:比如说枚举到5时,我想利用 尺取法 得到所有映射后为5的区间)

但标成很机智的改成了:枚举到i时,当前多少个区间映射后的值是>=i的,然后加上他们。如果每次枚举都这么做,你会发现区间映射值为5的就加了5次,

为6的被加了6次。

因此把浮动的区间和,变成了一个定值,那么 尺取法 就又能发挥它的作用了。

2015多校1006.First One的更多相关文章

  1. hdu 5288||2015多校联合第一场1001题

    pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...

  2. hdu5379||2015多校联合第7场1011 树形统计

    pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little sun is ...

  3. 2015 多校赛 第五场 1006 (hdu 5348)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题目大意:给出一幅无向图,问是否存在一种方案,使得给每条边赋予方向后,每个点的入度与出度之差小于 ...

  4. 2015 多校赛 第二场 1006 (hdu 5305)

    Problem Description There are n people and m pairs of friends. For every pair of friends, they can c ...

  5. HDU 5358(2015多校联合训练赛第六场1006) First One (区间合并+常数优化)

    pid=5358">HDU 5358 题意: 求∑​i=1​n​​∑​j=i​n​​(⌊log​2​​S(i,j)⌋+1)∗(i+j). 思路: S(i,j) < 10^10 & ...

  6. 2015多校.Zero Escape (dp减枝 && 滚动数组)

    Zero Escape Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  7. HDU 5289 Assignment(2015 多校第一场二分 + RMQ)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  8. hdu5294||2015多校联合第一场1007 最短路+最大流

    http://acm.hdu.edu.cn/showproblem.php? pid=5294 Problem Description Innocent Wu follows Dumb Zhang i ...

  9. 2015 多校联赛 ——HDU5334(构造)

    Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

随机推荐

  1. [IOS 实现TabBar在Push后的隐藏 以及 两级Tabbar的切换]

    翻了好多网页都没找到资料,自己试了下终于成功了,遂分享一下. 1.实现TabBar在Push后的隐藏 假如结构是这样 NavController->A->B,我们想要实现在A里有Tabba ...

  2. web开发 虚拟目录映射

    A 当服务器和 web应用不在一个目录下 $CATALINA_BASE/conf/catalina/localhost/ 文件夹下创建一个xml文件,任意文件名都可以,但是此文件名是web应用发布后的 ...

  3. 演示get、post请求如何算sn,算得sn如何使用

    import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.UnsupportedEncoding ...

  4. Mac xcode 编译产生app的路径

    .../<current User>/Library/Developer/XCode/DerivedData/<Project name>-<other characte ...

  5. ECMAScript严格模式简介

    写在前面 大家都知道使用"use strict"表示使用ECMAScript进行严格模式,使用"use strict"有两种方式 在文件头部写上它(使得整个脚本 ...

  6. Python 培训之正则表达式

    re 模块 re.math 从头匹配 re.search 结构: re.math(r'^c',a)   不符合返回None 原字符: . 任意字符 [ ] 或者 [A-Z,a-z,b] \d 数字 \ ...

  7. linux系统命令

    TOP命令 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.top是一个动态显示过程, 即可以通过用户按键来不断刷新当前状态.如 ...

  8. Maven中的dependencyManagement 意义

    1.在Maven中dependencyManagement的作用其实相当于一个对所依赖jar包进行版本管理的管理器. 2.pom.xml文件中,jar的版本判断的两种途径 1:如果dependenci ...

  9. HIbernate的写法总结

    普通表操作 普通操作莫过于CRUD,建好表了之后对表的数据进行操作.详见代码. package package2; import org.hibernate.Session; import org.h ...

  10. VS2013-解决error C4996: 'fopen'问题

    VS2013中如何解决error C4996: 'fopen'问题 初次使用vs系列编辑器编写控制台应用程序时常出现如下错误: error C4996: 'fopen': This function ...