zoukankan      html  css  js  c++  java
  • 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=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

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

    然后用尺取法搞一搞

      1 /**
      2  * code generated by JHelper
      3  * More info: https://github.com/AlexeyDmitriev/JHelper
      4  * @author xyiyy @https://github.com/xyiyy
      5  */
      6 
      7 #include <iostream>
      8 #include <fstream>
      9 
     10 //#####################
     11 //Author:fraud
     12 //Blog: http://www.cnblogs.com/fraud/
     13 //#####################
     14 //#pragma comment(linker, "/STACK:102400000,102400000")
     15 #include <iostream>
     16 #include <sstream>
     17 #include <ios>
     18 #include <iomanip>
     19 #include <functional>
     20 #include <algorithm>
     21 #include <vector>
     22 #include <string>
     23 #include <list>
     24 #include <queue>
     25 #include <deque>
     26 #include <stack>
     27 #include <set>
     28 #include <map>
     29 #include <cstdio>
     30 #include <cstdlib>
     31 #include <cmath>
     32 #include <cstring>
     33 #include <climits>
     34 #include <cctype>
     35 
     36 using namespace std;
     37 #define rep(X, N) for(int X=0;X<N;X++)
     38 #define rep2(X, L, R) for(int X=L;X<=R;X++)
     39 typedef long long ll;
     40 
     41 //
     42 // Created by xyiyy on 2015/8/7.
     43 //
     44 
     45 #ifndef JHELPER_EXAMPLE_PROJECT_SCANNER_HPP
     46 #define JHELPER_EXAMPLE_PROJECT_SCANNER_HPP
     47 
     48 void Out(ll a) {
     49     if (a > 9)Out(a / 10);
     50     putchar(a % 10 + '0');
     51 }
     52 
     53 #endif //JHELPER_EXAMPLE_PROJECT_SCANNER_HPP
     54 
     55 ll a[100010];
     56 ll l[100010];
     57 ll r[100010];
     58 
     59 class hdu5358 {
     60 public:
     61     void solve(std::istream &in, std::ostream &out) {
     62         int n;
     63         in >> n;
     64         a[0] = 0;
     65         rep2(i, 1, n)in >> a[i];
     66         rep2(i, 1, n)a[i] += a[i - 1];
     67         ll ans = 0;
     68         rep(i, n + 1)l[i] = r[i] = 0;
     69         rep2(i, 1, n) {
     70             int j = 0;
     71             while (1) {
     72                 ll L = (1LL << j);
     73                 ll R = (L << 1);
     74                 if (!j) L = 0;
     75                 L += a[i - 1];
     76                 R += a[i - 1];
     77                 j++;
     78                 if (a[i] >= R)continue;
     79                 while ((a[l[j - 1]] < L || l[j - 1] < i) && l[j - 1] <= n)l[j - 1]++;
     80                 while ((a[r[j - 1]] < R || r[j - 1] < i) && r[j - 1] <= n)r[j - 1]++;
     81                 if (l[j - 1] > n)break;
     82                 ans += (ll) j * (i + l[j - 1] + i + r[j - 1] - 1) * (r[j - 1] - l[j - 1]) / 2;
     83             }
     84         }
     85         out << ans << endl;
     86     }
     87 };
     88 
     89 int main() {
     90     std::ios::sync_with_stdio(false);
     91     std::cin.tie(0);
     92     hdu5358 solver;
     93     std::istream &in(std::cin);
     94     std::ostream &out(std::cout);
     95     int n;
     96     in >> n;
     97     for (int i = 0; i < n; ++i) {
     98         solver.solve(in, out);
     99     }
    100 
    101     return 0;
    102 }
  • 相关阅读:
    etcd:从应用场景到实现原理的全方位解读
    open-falcon编写的整个脑洞历程
    开源还是商用?十大云运维监控工具横评
    我的后端开发书架2015 2.0版
    【MDCC 2015】友盟数据平台负责人吴磊:移动大数据平台的架构与实践
    Effective Go
    Airbnb JavaScript Style Guide
    Google HTML/CSS Style Guide
    nservicebus教程-目录
    测试
  • 原文地址:https://www.cnblogs.com/fraud/p/4712146.html
Copyright © 2011-2022 走看看