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. jpa注解

    http://www.oracle.com/technetwork/cn/middleware/ias/toplink-jpa-annotations-100895-zhs.html#ManyToOn ...

  2. CentOS关机

    1. 关机前准备 1.1 查看有谁在线 who 1.2 查看网络联机状态 netstat -a 1.3 查看后台正在执行的程序 ps -aux 1.4 向所有在线用户发出警告信息 shutdown - ...

  3. 在Ubuntu上如何往fcitx里添加输入法

    Ubuntu 16.04引入了一个新的包管理工具apt, 用法与apt-get类似. 在终端用apt搜索fcitx支持的输入法 apt search fcitx All Fcitx related p ...

  4. Linux rsync网站目录同步功能的实现

    实现目标: 172.16.1.64服务器上的/var/www/sw_service目录,与172.16.1.60服务器上的/var/www/sw_service目录实现同步, 即1.60主动向1.64 ...

  5. mybatis 传递参数的方法总结

    有三种mybatis传递参数的方式: 第一种 mybatis传入参数是有序号的,可以直接用序号取得参数 User selectUser(String name,String area); 可以在xml ...

  6. SQL Server编程(05)游标【转载】

    在关系数据库中,我们对于查询的思考是面向集合的.而游标打破了这一规则,游标使得我们思考方式变为逐行进行.对于类C的开发人员来着,这样的思考方式会更加舒服. 正常面向集合的思维方式是: 而对于游标来说: ...

  7. js002-在HTML中使用JavaScript

    js002-在HTML中使用JavaScript 2.1            <script>元素   定义了以下6个属性   async: 可选.表示应该立即下载脚本,但不妨碍页面中的 ...

  8. Oralce配置正确,报监听错误或无法识别描述中的服务

    出差客户现场,修改过网络配置,回来后本地虚拟机的Oracle数据库就不能登陆了 报监听错误,在服务器中使用Net Configration Assistant删除以前的,重新配置新的,还是不行,重启系 ...

  9. can not import Flask错误

    在学习flask官方文档——应用构建代码时,遇到了一个小问题照例子敲好代码以后出现了can not import  Flask错误,思考很久也没有解决,最后发现是因为应用模块和flask重名了.下面是 ...

  10. 重启nginx

    在env/nginx/sbin目录下输入:nginx,即可重启