码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5352 First One

时间:2015-08-07 00:29:19      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:


First One

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


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
 


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define prt(k)  cout<<#k" = "<<k<<"  "
const int N = 100233;

int a[N];
int n;
int main()
{
    int re; read(re);
    while (re--) {
        read(n);
        ll ans = 0;
        for (ll i=1;i<=n;i++) {
            read(a[i]);
            ans += i * (n-i+1) + (n-i+1)*(n+i)/2;
        }
        for (int k=1;k<40;k++) {
            ll lim = 1LL<<k;
            ll s = 0;
            for (ll i=1, j=1; i<=n; i++) {
                while (j<=n && s<lim) s+=a[j++]; j--;
                if (s >= lim) {
                    ans += i * ll(n - j + 1) + ll(n + j) * ll(n - j + 1)/2;
                    j++;
                }
                else break;
                s -= a[i];
            }
        }
        printf("%I64d\n", ans);
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 5352 First One

标签:

原文地址:http://blog.csdn.net/oilover/article/details/47324029

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!