HDU 5352 First One


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
2015 Multi-University Training Contest 6
 


#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)