hdu5358 First One(尺取法)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

First One

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

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

被卡得真是惨,必须是O(nlogn)才能过

然后用尺取法搞一搞

 /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author xyiyy @https://github.com/xyiyy
*/ #include <iostream>
#include <fstream> //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype> using namespace std;
#define rep(X, N) for(int X=0;X<N;X++)
#define rep2(X, L, R) for(int X=L;X<=R;X++)
typedef long long ll; //
// Created by xyiyy on 2015/8/7.
// #ifndef JHELPER_EXAMPLE_PROJECT_SCANNER_HPP
#define JHELPER_EXAMPLE_PROJECT_SCANNER_HPP void Out(ll a) {
if (a > )Out(a / );
putchar(a % + '');
} #endif //JHELPER_EXAMPLE_PROJECT_SCANNER_HPP ll a[];
ll l[];
ll r[]; class hdu5358 {
public:
void solve(std::istream &in, std::ostream &out) {
int n;
in >> n;
a[] = ;
rep2(i, , n)in >> a[i];
rep2(i, , n)a[i] += a[i - ];
ll ans = ;
rep(i, n + )l[i] = r[i] = ;
rep2(i, , n) {
int j = ;
while () {
ll L = (1LL << j);
ll R = (L << );
if (!j) L = ;
L += a[i - ];
R += a[i - ];
j++;
if (a[i] >= R)continue;
while ((a[l[j - ]] < L || l[j - ] < i) && l[j - ] <= n)l[j - ]++;
while ((a[r[j - ]] < R || r[j - ] < i) && r[j - ] <= n)r[j - ]++;
if (l[j - ] > n)break;
ans += (ll) j * (i + l[j - ] + i + r[j - ] - ) * (r[j - ] - l[j - ]) / ;
}
}
out << ans << endl;
}
}; int main() {
std::ios::sync_with_stdio(false);
std::cin.tie();
hdu5358 solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
int n;
in >> n;
for (int i = ; i < n; ++i) {
solver.solve(in, out);
} return ;
}
上一篇:HtmlAgilityPack


下一篇:Java EE (11) - 影响性能的因素