【瞎搞】 HDU 5358 First One

點擊打開鏈接

根據| log2 a +1 | =k    枚舉k得到 區間 [2^(k-1), 2^k-1] ;  k =[0,34]

再枚舉左端 i: 對於一個i 找到一個區間 [l, r]滿足[ 2^(k-1),  2^k-1]

然後就加起來

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <bitset>
#include <set>
#include <map>
typedef long long LL;
const int MAXN = 100009;//點數的最大值
const int MAXM = 604000;//邊數的最大值
const LL INF = 1152921504;
const LL mod= 1000000007;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int read()
{
    char ch=' ';
    int ans=0;
    while(ch<'0' || ch>'9')
        ch=getchar();
    while(ch<='9' && ch>='0')
    {
        ans=ans*10+ch-'0';
        ch=getchar();
    }
    return ans;
}
int a[MAXN],n;
LL b[36],sum[MAXN],ans;
int gao(LL low,LL top,int k)//[l,r]
{
    int l=1,r=0;
    for(int i=1; i<=n; i++)//star in i
    {
        if(l<i) l=i;
        if(r<i-1) r=i-1;
        while(l<=n&&sum[l]-sum[i-1]<low)
            l++;
        while(r+1<=n&&sum[r+1]-sum[i-1]<=top)
            r++;
        if(l>r) continue;
        if(sum[r]-sum[i-1]<low || sum[r]-sum[i-1]>top) continue;
        if(sum[l]-sum[i-1]<low || sum[l]-sum[i-1]>top) continue;
        ans+=((LL)(r-l+1)*i+(LL)(l+r)*(r-l+1)/2)*k;
    }
}
int main()
{
    b[0]=1;
    b[1]=2;
    for(int i=2; i<=34; i++)
        b[i]=b[i-1]*(LL)2;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        n=read();
        sum[0]=0;
        for(int i=1; i<=n; i++)
        {
            a[i]=read();
            sum[i]=sum[i-1]+a[i];
        }
        ans=0;
        gao(0,0,1);
        for(int i=1; i<=34; i++)
        {
            gao(b[i-1],b[i]-1,i);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
/*
1
4
1 1 1 1
*/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章