﻿ HDU 5358 多校第6场 First One

First One

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 672    Accepted Submission(s): 193

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=1nj=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 (1n105), the number of integers in the array.
The next line contains n integers a1,a2,,an (0ai105).

Output
For each test case, output the value.

Sample Input
1
2
1 1

Sample Output
12

Source

/****************************************
** 2015 Multi-University Training Contest 6
** 1006 First One
** HDU 5358
** by calamity_coming
**************************************/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MAX_LONG = 1E5;
ll   a[MAX_LONG + 10];
ll sum[MAX_LONG + 10];
ll cf2[40];

void init()
{
ll k = 1;
for(int i=0; i<=34; ++i)
{
cf2[i] = (k<<(i));
}
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
sum[0] = 0;
for(int i=1; i<=n; ++i)
{
scanf("%I64d",&a[i]);
sum[i] = sum[i-1] + a[i];//前缀和处理
}
sum[n+1] = 1E17;
ll ans = 0;

//0
for(int i=1; i<=n; ++i)//0比較特殊。要特殊的干
{
int p = i;
while(sum[p]==sum[i-1] && p<=n+1)
{
++p;
}
--p;
if(p>=i)
{
ans += (ll)(i*3+p)*(ll)(p-i+1)/2;
}
}

for(int j=0; j<=33; ++j)//这是每一个情况
{
int p1 = 0,p2 = 0;//用两个指针扫一遍
for(int i=1; i<=n; ++i)//当i递增时，新的p1,p2一定在后面
{
ll adc = sum[i-1] + cf2[j];
{
++p1;
}
{
++p2;
}
--p2;
if(p2>=p1 && p2)
{
ans += (j+1)*(ll)(i*2+p1+p2)*(ll)(p2-p1+1)/2;
}
}
}
printf("%I64d
",ans);
}
return 0;
}