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. oracle报错:ORA-28000: the account is locked

    连接数据库的时候报: ORA-28000: the account is locked 解决方法: cmd-进入命令行 C:\Users\0>sqlplus /nolog SQL*Plus: R ...

  2. Java反射API使用实例

    /**     * 访问Class对应的类所包含的注释:getAnnotation();getDelaredAnnotation();     * 访问Class对应的类所包含的内部类:getDecl ...

  3. Python基本数据类型之int

    一.int的范围 2.7: 32位:-2^31~2^31-1 64位:-2^63~2^63-1 3.5: 在3.5中init长度理论上是无限的 二.python内存机制 在一般情况下当变量被赋值后,内 ...

  4. bootstrap学习总结-04 常用标签2

    1 表格 Bootstrap为表格设计了漂亮的样式. 1)表格基本实例 任意 <table> 标签添加 .table. <table class="table"& ...

  5. UVALive 3989Ladies' Choice(稳定婚姻问题)

    题目链接 题意:n个男生和女生,先是n行n个数,表示每一个女生对男生的好感值排序,然后是n行n列式每一个男生的好感值排序,输出N行,即每个女生在最好情况下的男生的编号 分析:如果是求女生的最好情况下, ...

  6. Python核心编程第三版第二章学习笔记

    第二章 网络编程 1.学习笔记 2.课后习题 答案是按照自己理解和查阅资料来的,不保证正确性.如由错误欢迎指出,谢谢 1. 套接字:A network socket is an endpoint of ...

  7. asp.net mvc @Html.Raw 作用

    转自:http://zhidao.baidu.com/link?url=unayXHAylQiUF0E3Rc9ej4gz_XBC7sbwInupVFuDnp_Cuqdz5NzMyUK5u-HiSfif ...

  8. 什么是linux的ftp

    vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序.特点是小巧轻快,安全易用.并且是一个完全免费开放源码的ftp软件ftp的作用: FTP(File Transfer Protocol, ...

  9. app的描述

    app的描述=需求文档+接口文档+程序架构.   程序架构:类结构图: 需求文档:业务逻辑-->时序图.

  10. LED数码管显示实验

    1.代码: #include <reg52.h>typedef unsigned char  u8;typedef unsigned int   u16;sbit seg_sel = P1 ...