protagonist

First One

12269: First One

时间限制: 2 Sec  内存限制: 128 MB
提交: 4  解决: 2
[提交] [状态] [命题人:外部导入]

题目描述

 

输入

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 (1≤n≤105), the number of integers in the array.
The next line contains n integers a1,a2,…,an (0≤ai≤105).

 

输出

For each test case, output the value.

 

样例输入

1
2
1 1

样例输出

12
#include<bits/stdc++.h>

using namespace std;


const int maxn = 300060;
typedef long long ll;

int T, n;
ll c[maxn];
ll sum[maxn];

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
#endif
    scanf("%d", &T);
    while (T--) {
        scanf("%d", &n);
        for (register int i = 1; i <= n; ++i) {
            scanf("%lld", &c[i]);
            sum[i] = sum[i - 1] + c[i];
        }
        ll res = 0;
        for (register int i = 1; i <= 36; ++i) {
            ll l = 1, r = 0;
            ll lmax = (1ll << (i - 1)), rmax = (1ll << i) - 1;
            if (i == 1)lmax = 0;
            for (register ll pos = 1; pos <= n; ++pos) {
                l = max(l * 1ll, pos);
                while (l <= n && sum[l] - sum[pos - 1] < lmax) {
                    ++l;
                }
                r = max(l - 1, r);
                while (r + 1 <= n && sum[r + 1] - sum[pos - 1] >= lmax && sum[r + 1] - sum[pos - 1] <= rmax) {
                    ++r;
                }
                if (l > r)continue;
                res += (i * (pos * (r - l + 1) + (r - l + 1) * (l + r) / 2));
            }
        }
        printf("%lld\n", res);
    }
    return 0;
}

 

 
posted @ 2019-09-09 08:18  czy-power  阅读(389)  评论(0编辑  收藏  举报