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. zookeeper读取事务日志、快照日志

    zookeeper的事务日志的格式如 log.xxx, xxx表示顺序序号 我使用的zookeeper版本:3.5.5 事务日志 执行命令 java -cp .:/tmp/zookeeper-3.5. ...

  2. utraedit不小心把打开文件列表弄得不显示的处理办法

    视图->视图/列表->打开文件标签

  3. 【Linux】CentOS7中yumbackend.py进程的结束方法

    环境: CentOS Linux release 7.3.1611 (Core) 今天启动这个不怎么用的机器,才启动,就发现后台的yum无法进行安装,持续报这个错误 Loaded plugins: f ...

  4. RAC上的DG搭建

    准备工作 修改rman_backup这个文件的所有者和所属组,修改为oracle用户的oinstall组的文件 #chown –R oracle:oinstall /rman_backup/ 主库和备 ...

  5. LeetCode590. N叉树的后序遍历

    题目 1 class Solution { 2 public: 3 vector<int>ans; 4 vector<int> postorder(Node* root) { ...

  6. 容器编排系统K8s之包管理器Helm基础使用

    前文我们了解了k8s上的hpa资源的使用,回顾请参考:https://www.cnblogs.com/qiuhom-1874/p/14293237.html:今天我们来聊一下k8s包管理器helm的相 ...

  7. AQS之ReentrantReadWriteLock写锁

    用法 1.1 定义一个安全的list集合 public class LockDemo { ArrayList<Integer> arrayList = new ArrayList<& ...

  8. Jmeter-插件扩展及性能监控插件的安装

    需要对http服务进行大数据量的传值测试:看看产品中的http服务,能支持传多少字符:目标值是希望能到10w+: 上次测试中,服务器总是内存满导致服务不响应,因此想增加对服务端的性能监控:查阅了smi ...

  9. 转 Fiddler3 使用技巧

    Fiddler3 使用技巧  文章转自:https://www.cnblogs.com/zhengna/category/1466001.html 1.Fiddler抓不到包怎么解决 (1)先确定是H ...

  10. NULL-safe equal null 索引 空字符串

    小结 1. mysql> INSERT INTO my_table (phone) VALUES (NULL); 有手机号但是不知道 mysql> INSERT INTO my_table ...