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. bzoj3297[USACO2011 Open]forgot(dp + string)

    3297: [USACO2011 Open]forgot Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 137  Solved: 94[Submit] ...

  2. selenium3 + python - gird分布式(转载)

    本篇转自博客:上海-小T 转载链接:https://blog.csdn.net/real_tino/article/details/53467406 Selenium grid是用来分布式执行测试用例 ...

  3. 浅谈自学Python之路(day1)

    2018-02-19  17:15:14 Python语言相对于其他语言较为简洁,也相对好入门比如后面不加分号,基本见不着大括号等优点 第一个程序,也是学每门语言都需要掌握的第一个代码 print(& ...

  4. Angular 显示英雄列表

    在本页面,你将扩展<英雄指南>应用,让它显示一个英雄列表, 并允许用户选择一个英雄,查看该英雄的详细信息. 创建模拟(mock)英雄数据 你需要一些英雄数据以供显示. 最终,你会从远端的数 ...

  5. Springboot 版本+ jdk 版本 + Maven 版本的对应关系

    Spring boot 版本 Spring Framework jdk 版本 maven 版本 1.2.0 版本之前   6 3.0 1.2.0 4.1.3+ 6 3.2+ 1.2.1 4.1.3+ ...

  6. A - Voting(queue)

    Problem description There are n employees in Alternative Cake Manufacturing (ACM). They are now voti ...

  7. Vue 区别

    computed和methods区别 效果是一样的,但是 computed 是基于它的依赖缓存,只有相关依赖发生改变时才会重新取值. 而methods,在重新渲染的时候,函数总会重新调用执行.

  8. SQL Server中char与varchar数据类型区别

    在SQL Server中char类型的长度是不可变的,而varchar的长度是可变的 . 存入数据时: 如果数据类型为char时,当定义一个字段固定长度时,如果存进去数据长度小于char的长度,那么存 ...

  9. Laravel5.1学习笔记9 系统架构1 请求生命周期 (待修)

    Request Lifecycle Introduction Lifecycle Overview Focus On Service Providers Introduction When using ...

  10. MainActivity 多个Fragment 内存被回收

    0. 前言 应用首页采用Activity +Tab 模式,多个Fragment 替换显示隐藏 FragmentTransaction transaction = getSupportFragmentM ...