hdu5358 First One 尺取法 多校联合第六场
来源:互联网 发布:软件脱壳 编辑:程序博客网 时间:2024/04/30 08:59
First One
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1511 Accepted Submission(s): 457
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 considerlog20 as 0.
Note: In this problem, you can consider
Input
There are multiple test cases. The first line of input contains an integerT , indicating the number of test cases. For each test case:
The first line contains an integern (1≤n≤105) , the number of integers in the array.
The next line containsn integers a1,a2,…,an (0≤ai≤105) .
The first line contains an integer
The next line contains
Output
For each test case, output the value.
Sample Input
121 1
Sample Output
12
计算上面给的那个公式。
比赛的时候想了个方法,以每个位置作为终点,二分log每种取值对应的起点,这样的复杂度是O(NlogN*35)左右,但是超时。这道题时间卡的很紧,复杂度O(N*35)的都要运行1000多ms。
枚举log不同取值的区间范围,在O(N)的复杂度下算出这个范围对应的所有i,j和。采用尺取法,假设以i为起始位置,对应[p1,p2]这个区间为终止位置,这些区间都是满足的,那么当i+1时,一定有终止位置p1'>=p1,p2'>=p2才可能满足。
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;typedef long long LL;const LL MAXN=100010;const LL INF=0x3f3f3f3f;const LL SIGMA_SIZE=28;int T,N;int a[MAXN];LL sum[MAXN];//[l,r) i+j的和LL solve(LL l,LL r){ LL ret=0; int p1=1,p2=1; for(int i=1;i<=N;i++){ if(p1<i) p1=i; if(p2<i) p2=i; while(p1<=N&&sum[p1]-sum[i-1]<l) p1++; while(p2<=N&&sum[p2]-sum[i-1]<r) p2++; p2--; if(p1<=p2&&sum[p1]-sum[i-1]>=l&&sum[p2]-sum[i-1]<r){ LL n=p2-p1+1; ret+=i*n+p1*n+n*(n-1)/2; } } return ret;}int main(){ freopen("in.txt","r",stdin); scanf("%d",&T); while(T--){ scanf("%d",&N); sum[0]=0; for(int i=1;i<=N;i++){ scanf("%d",&a[i]); sum[i]=sum[i-1]+a[i]; } LL ans=0; ans+=solve(0,1); for(int i=0;i<35;i++){ ans+=solve(1LL<<i,1LL<<(i+1))*(i+1); } printf("%I64d\n",ans); } return 0;}
0 0
- hdu5358 First One 尺取法 多校联合第六场
- [区间合并,优化] hdu5358多校联合 第六场 First one
- hdu 5358 First One (2015多校第六场第6题)尺取法枚举区间和
- HDU 5358(2015多校联合训练赛第六场1006) First One (区间合并+常数优化)
- hdu5358 First One
- HDU5358-First One
- 多校第六场 1006 hdu 5358 First One(枚举)
- hdu5358(尺取法)
- 2014多校联合-第六场
- HDOJ多校联合第六场
- 2015多校联合第六场 hdu5360hiking
- hdu5358--First One(双指针)
- HDU 5358 · First One【尺取法】【读题】【多校联赛】
- hdu 5358 First One (尺取法)
- HDU 5358 First One(尺取法)
- HDU 5338. First One(尺取法)
- HDU 5358 First One 数学+尺取法
- hdu 5358 First One [枚举+尺取法]
- 【Java多线程】之十:Timer and TimerTask
- Swift调用C代码的方法
- 华为上机 循环报数 java
- 零基础学python-1.4 hello world
- openssl 提取秘钥
- hdu5358 First One 尺取法 多校联合第六场
- conn.setAutoCommit()用法及作用
- 音量图标不显示了怎么恢复
- 架构师要以自己的编程能为依托
- HDU 1198 Farm Irrigation(DFS)
- 二叉搜索树的基本操作(查找、插入、删除)【数据结构】
- java中classpath的详解
- HDU 5366-The mook jong(组合数学)
- 如何使用qq截图