pid=5358">HDU 5358

题意:

求∑​i=1​n​​∑​j=i​n​​(⌊log​2​​S(i,j)⌋+1)∗(i+j)。

思路:

S(i,j) < 10^10 < 2^34。所以log2(S)+1的值仅仅可能在1~35之间。因为log变化缓慢的函数特性,我们能够S(i,n)分作多个同样log值的区间来计算,用pos[i][j]预处理记录每一个以i为起点,log(s)值为j的区间的右边界。就可以优化至nlogn的复杂度。

主要是写起来比較难一些,一些细节比較纠结,一定思路理清后再写。

ps.此题卡常数毫无人性,一定记得预处理好区间映射,否则n(logn)^2也得跪。。

code:

/*
* @author Novicer
* language : C++/C
*/
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
#define INF 2147483647
#define cls(x) memset(x,0,sizeof(x))
#define rise(i,a,b) for(int i = a ; i <= b ; i++)
using namespace std;
const double eps(1e-8);
typedef long long lint; const int maxn = 100000 + 5;
lint a[maxn];
lint pos[maxn][34];
lint two[34] = {2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,
268435456,536870912,1073741824,2147483648,4294967296,8589934592,17179869184}; int main(){
int t ; cin >> t ;
while(t--) {
int n; cin >> n;
for(int i = 1 ; i <= n ; i++){
scanf("%I64d",&a[i]);
}
for(int i = 0 ; i < 34 ; i++){
lint sum = a[1];
int right = 1;
for(int j = 1 ; j <= n ; j++){
if(j != 1) sum -= a[j-1];
while(sum < two[i] && right <= n)
sum += a[++right];
pos[j][i] = right;//以j为起点。log2(sum+1)值为i的区间右边界为right
}
}
lint ans = 0;
for(int i = 1 ; i <= n ; i++){
lint l = i;
lint r;
for(int j = 0 ; j < 34 ; j++){
r = pos[i][j];
ans += (1+j) * ((i) * (r-l) + (r+l-1)*(r-l)/2);//将同样log值区间的(i+j)累加起来
l = r;
}
}
cout << ans << endl; }
return 0;
}

HDU 5358(2015多校联合训练赛第六场1006) First One (区间合并+常数优化)的更多相关文章

  1. hdu 5361 2015多校联合训练赛#6 最短路

    In Touch Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total ...

  2. HDU 5371 (2015多校联合训练赛第七场1003)Hotaru&#39;s problem(manacher+二分/枚举)

    pid=5371">HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分同样,第一部分与第二部分对称. 如今给你一个长为n(n<10^5)的序 ...

  3. hdu 5358 First One 2015多校联合训练赛#6 枚举

    First One Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...

  4. hdu 5381 The sum of gcd 2015多校联合训练赛#8莫队算法

    The sum of gcd Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) T ...

  5. 2015多校联合训练赛 hdu 5308 I Wanna Become A 24-Point Master 2015 Multi-University Training Contest 2 构造题

    I Wanna Become A 24-Point Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 ...

  6. 2015多校联合训练赛hdu 5301 Buildings 2015 Multi-University Training Contest 2 简单题

    Buildings Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...

  7. 2015多校联合训练赛 Training Contest 4 1008

    构造题: 比赛的时候只想到:前面一样的数,后面 是类似1,2,3,4,5,6....t这 既是:t+1,t+1...,1,2,3,...t t+1的数目 可能 很多, 题解时YY出一个N 然后对N   ...

  8. hdu 5288||2015多校联合第一场1001题

    pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...

  9. HDU OJ 5317 RGCDQ( 2015多校联合训练第3场) 暴力打表+小技巧

    题目连接:Click here 题意:在一个[L,R]内找到最大的gcd(f[i],f[j])其中L<=i<j<=R,f[x]表示i分解质因数后因子的种类数.eg:f[10]=2(1 ...

随机推荐

  1. [Apple开发者帐户帮助]二、管理你的团队(5)转移帐户持有人角色

    组织团队的帐户持有人可以将帐户持有人角色转移给团队中的其他人.如果您是个人注册并需要将您的会员资格转移给其他人,请与我们联系. 所需角色:帐户持有人. 转移Apple Developer Progra ...

  2. Python 33(2)进程理论

    一:什么是进程         进程指的是一个正在进行 / 运行的程序,进程是用来描述程序执行过程的虚拟概念 进程vs程序 程序:一堆代码 进程:程序的执行的过程 进程的概念起源于操作系统,进程是操作 ...

  3. [SGU 199] Beautiful People

    [SGU 199] Beautiful People The most prestigious sports club in one city has exactly N members. Each ...

  4. (转)用JS实现表格中隔行显示不同颜色

    用JS实现表格中隔行显示不同颜色 第一种: <style> tr{bgColor:expression(     this.bgColor=((this.rowIndex)%2==0 )? ...

  5. div 背景放图和直接放图区别

    <html> <head> <meta charset="UTF-8"> <title></title> <sty ...

  6. Scala——面向对象和函数式编程语言

    Scala Scala是一门运行时基于JVM的编程语言,具备函数式编程和面向对象特点. 基本知识 basics 任意词均可作为符号名,对于关键词,以反引号包裹,避免使用下划线,避免带美元符的名字. 声 ...

  7. Deutsch lernen (14)

    1.    das Abseits, -  越位 Der Linienrichter winkte Abseits.  winken - winkte - gewunken  示意 2.    abs ...

  8. Deutsch lernen (03)

    1. das Gewerbe, - 行业 Was ist Ihr Gewerbe? Welches Gewerbe treibt er? treiben - trieb - getrieben  从事 ...

  9. java中的位运算及移位运算

    为了方便对二进制位进行操作,Java给我们提供了以下四个二进制位操作符: &    按位与 |     按位或 ^    按位异或 ~    按位取反 Java中有三个移位运算符: 左移:&l ...

  10. 【sqli-labs】 less20 POST - Cookie injections - Uagent field - Error based (POST型基于错误的cookie头部注入)

    以admin admin成功登陆之后,保存并显示了cookies信息 如果不点击Delete Your Cookie!按钮,那么访问 http://localhost/sqli-labs-master ...