转载请注明出处: 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 ;
}

hdu5358 First One(尺取法)的更多相关文章

  1. hdu-5358 First One(尺取法)

    题目链接: First One Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Other ...

  2. 5806 NanoApe Loves Sequence Ⅱ(尺取法)

    传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K ...

  3. POJ3061 尺取法

    题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...

  4. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  5. CF 701C They Are Everywhere(尺取法)

    题目链接: 传送门 They Are Everywhere time limit per test:2 second     memory limit per test:256 megabytes D ...

  6. nyoj133_子序列_离散化_尺取法

    子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...

  7. Codeforces 676C Vasya and String(尺取法)

    题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长. 用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变 ...

  8. POJ 3061 (二分+前缀和or尺取法)

    题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...

  9. POJ 3320 尺取法,Hash,map标记

    1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...

随机推荐

  1. web标准(复习)--1

    XHTML CSS基础知识 一.xhtml css基础知识首先说一下我们这节课的知识点 1.文档类型 2.语言编码 3.html标签 4.css样式 5.css优先级 6.css盒模型组成 1)文档类 ...

  2. 面向对象重写(override)与重载(overload)区别---转载“竹木人”

    一.重写(override) override是重写(覆盖)了一个方法,以实现不同的功能.一般是用于子类在继承父类时,重写(重新实现)父类中的方法. 重写(覆盖)的规则: 1.重写方法的参数列表必须完 ...

  3. JQuery实现隔行变色和突出显示当前行 效果

    运行效果如下图: jquery关键代码: <script type="text/javascript"> //该文件为:js.js // 当鼠标移到表格上是,当前一行背 ...

  4. Java虚拟机--字节码指令集

    1. 字节码指令集简介: Java虚拟机的指令由一个字节长度的,代表着某种特定操作含义的操作码(Opcode)以及跟随其后的零至多个代表此操作所需参数的操作数(Operands)所构成.虚拟机中许多指 ...

  5. vi使用入门指南

    一.Unix编辑器概述 编辑器是使用计算机的重要工具之一,在各种操作系统中,编辑器都是必不可少的部件.Unix及其相似的ix操作系统系列中,为方便各种用户在各个不同的环境中使用,提供了一系列的ex编辑 ...

  6. web.xml 3.0头部模板

    <?xml version=”1.0″ encoding=”UTF-8″?><web-appversion=”3.0″xmlns=”http://java.sun.com/xml/n ...

  7. 这样就算会了PHP么?-6

    关于PHP与WEB表单交互的CASE,十多年前没拾起来啊. 下一步进入JS与PHP的交互... <form name="form1" method="post&qu ...

  8. Unit Test相关问题汇总

    1.测试私有方法(1)使用反射 public class Calcutate { public int test() { return add(2, 3); } private int add(int ...

  9. Linux 硬连接和软连接的原理 (in使用)

    引子 目前,UNIX的文件系统有很多种实现,例如UFS(基于BSD的UNIX文件系统).ext3.ext4.ZFS和Reiserfs等等. 不论哪一种文件系统,总是需要存储数据.硬盘的最小存储单位是扇 ...

  10. java实现二维码

    说起二维码,微信好像最先启用,随后各类二维码就开始流行起来了.那什么是二维码呢. 1.什么是二维码?百度一下即可 http://baike.baidu.com/view/132241.htm?fr=a ...