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

这题题意容易懂,就是求和,其中(⌊log2S(i,j)⌋+1)的意思就是S(i,j)化成二进制后的比特位个数,因为S(i,j)不超过10^10,所以比特位不会超过35个。我们可以先初始化b[],

记录比特位为i的所有数中的最后一个数2^i-1,用sum[i]把从1到i的总和记录下来,然后用35个指针pt[i]记录以i为起点的最大下标k满足sum[k]-sum[i-1]<=b[j]。

最后注意要用G++交,C++会超时。。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define ll long long
#define maxn 100060
ll b[50],sum[maxn];//b[1]=2^i-1
ll a[maxn];
int pt[44];//指针
void init()
{
int i,j;
b[0]=-1;
b[1]=1;
for(i=2;i<=35;i++){
b[i]=(1LL<<i)-1; //也可以是b[i]=((ll)1<<i)-1;,但不加的话会爆int
}
}
int main()
{
int n,m,i,j,T,len;
ll ans;
init();
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
sum[0]=0;ans=0;
for(i=1;i<=n;i++){
scanf("%lld",&a[i]);
sum[i]=sum[i-1]+a[i];
}
for(i=1;i<=35;i++)pt[i]=0;
for(i=1;i<=n;i++){
pt[0]=i-1;
for(j=1;j<=34;j++){
while(sum[pt[j]+1]-sum[i-1]<=b[j] && pt[j]<n){//如果a>b,那么pt[a]一定大于等于pt[b]
pt[j]++;
}
//if(sum[pt[j]]-sum[i-1]>b[j-1] && sum[pt[j]]-sum[i-1]<=b[j] && pt[j]>=i ){ 这一句可以不用写
len=(pt[j]-pt[j-1]);
ans+=(ll)j*len*i;
ans+=(ll)j*len*(pt[j-1]+1+pt[j])/2;
//}
}
}
printf("%lld\n",ans);
}
return 0;
}

hdu5358 First One的更多相关文章

  1. hdu5358 First One(尺取法)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud First One Time Limit: 4000/2000 MS (Java/ ...

  2. hdu5358 推公式+在一个区间内的尺取+枚举法

    尺取+枚举,推出公式以后就是一个枚举加尺取 但是这题的尺取不是对一个值尺取,而是在一个区间内,所以固定左边界,尺取右边界即可 #include<bits/stdc++.h> #define ...

  3. hdu-5358 First One(尺取法)

    题目链接: First One Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Other ...

  4. [hdu5358]分类统计,利用单调性优化

    题意:直接来链接吧http://acm.hdu.edu.cn/showproblem.php?pid=5358 思路:注意S(i,j)具有区间连续性且单调,而⌊log2x⌋具有区间不变性,于是考虑枚举 ...

随机推荐

  1. oracle动态采样导致数据库出现大量cursor pin s wait on x等待

    生产库中,突然出现了大量的cursor pin s wait on x等待,第一反应是数据库出现了硬解析,查看最近的DDL语句,没有发现DDL.那么有可能这个sql是第一次进入 在OLTP高并发下产生 ...

  2. DDIC_TYPELENG_INCONSISTENT错误的解决办法

    当执行某个TCODE,例如SM66,出现类似如下的dump界面 大概意思就是说是ddic种的某个数据类型有问题,可能是数据结构,可能是数据元素或者是表等等 通过查阅资料了解到,对于note122290 ...

  3. django 组件 自定义过滤器 自定义标签 静态文件配置

    组件 将一些功能标签写在一个html文件里,这个文件作为一个组件,如果那个文件需要就直接拿过来使用即可: 这是title.html文件,写了一个导航栏,作为一个公用的组件 <div style= ...

  4. 2、fork函数与进程ID

    1. fork函数 fork函数用于克隆一份当前的进程资源,调用fork函数之后,进程一分为二,并且两个进程的资源是一样的(只是资源内容完全一样,并不是同一份资源).fork函数的函数原型为:pid_ ...

  5. 解决Linux下mysql区分大小写的问题

    1.查看lower_case_table_names的值,0代表区分大小写,1代表不区分大小写. 通过命令:SHOW VARIABLES LIKE 'lower%'; 1. 解决方法 以root用户登 ...

  6. 将连续增长 N 次字符串所需的内存重分配次数从必定 N 次降低为最多 N 次 二进制安全

    SDS 与 C 字符串的区别 - Redis 设计与实现 http://redisbook.com/preview/sds/different_between_sds_and_c_string.htm ...

  7. mysqldump 内存消耗

    MySQL :: MySQL 8.0 Reference Manual :: 4.5.4 mysqldump - A Database Backup Program https://dev.mysql ...

  8. list里放map list 放list

    Map<String,Integer> hashMap = new HashMap<String, Integer>(); Map<String,Integer> ...

  9. 慕课网金职位 Python工程师 百度网盘下载

    百度网盘链接:https://pan.baidu.com/s/1xshLRO3ru0LAsQQ0pE67Qg 提取码:bh9f 如果失效加我微信:610060008[视频不加密,资料代码齐全,超清一手 ...

  10. 从一片森林(JavaScript)到另一片森林(C++)

    从JavaScript到C Plus Plus 作为一个忠诚的Web开发者,JavaScript几乎是我这一年多以来的首选,不管是开发网站后端服务,还是开发跨端应用,我都会首选一个使用JavaScri ...