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. C#实现 Eval

    主要是使用 CSharpCodeProvider,动态调用来进行返回. 首先会拼接成一个类的一个方法,然后返回这个方法的值.不过,因为还是使用反射,所以效率不高. public static clas ...

  2. Beta版本——第六次冲刺博客

    我说的都队 031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬 ...

  3. js中substring与substr的学习。

    今天在工作的过程中,看到js中两个双胞胎函数.分别是substring与substr.顿时被两个可恶的家伙给迷惑住了,不知道具体有什么作用.. 先来看看substring手册是怎么介绍的. 手册解释的 ...

  4. HTML5学习总结-10 Android 控件WebView显示网页

    WebView可以使得网页轻松的内嵌到app里,还可以直接跟js相互调用. webview有两个方法:setWebChromeClient 和 setWebClient 1)setWebClient: ...

  5. rhino(犀牛) --- color control

    create color materials, if "材料赋予方式" is "图层", the color of "材质" is show ...

  6. Lock的用法,为什么要用?

    当多个进程分享数据的时候,对某段程序代码要lock(当对分享数据进行改写的时候). 我们先看些这段代码: namespace ThreadTest { class Program { static b ...

  7. paramiko模块使用

    paramiko是一个用于做远程控制的模块,使用该模块可以对远程服务器进行命令或文件操作,fabric和ansible内部远程管理就是使用paramiko来实现. #!/usr/bin/env pyt ...

  8. Android项目结构 以及体系结构

    学习Android平台的人一般对Android的平台的应该有点认识 其它的就不多讲了 Android项目一般由以下几个部分构成 以上是一个简单的Android项目结构目录图 1. src  主要是 源 ...

  9. Linux开放1521端口允许网络连接Oracle Listene

    症状:1. TCP/IP连接是通的.可以用ping 命令测试. 2. 服务器上Oracle Listener已经启动.  lsnrctl status  查看listener状态  lsnrctl s ...

  10. 非阻塞socket学习,select基本用法

    server #include <stdio.h> #include <winsock2.h> #include <iostream> #pragma commen ...