[BZOJ 1660] Bad Hair Day

[题目链接]

         https://www.lydsy.com/JudgeOnline/problem.php?id=1660

[算法]

         Sprease Table + 二分

         时间复杂度 : O(NlogN)

[代码]

        

#include<bits/stdc++.h>
using namespace std;
#define MAXN 80010
#define MAXLOG 20
typedef long long ll;

ll a[MAXN];
ll f[MAXN][MAXLOG];

template <typename T> inline void read(T &x)
{
        ll f = 1; x = 0;
        char c = getchar();
        for (; !isdigit(c); c = getchar())
        {
                if (c == '-') f = -f;
        }
        for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
        x *= f; 
}
inline ll rmq(int x,int y)
{
        int k = (int)(log(y - x + 1) / log(2.0));
        return max(f[x][k],f[y - (1 << k) + 1][k]);        
}

int main() 
{
        
        int n;
        read(n);
        for (int i = 1; i <= n; i++) read(a[i]);
        reverse(a + 1,a + n + 1);
        for (int i = 1; i <= n; i++) f[i][0] = a[i];
        for (int i = 1; i < MAXLOG; i++)
        {
                for (int j = 1; j + (1 << i) - 1 <= n; j++)
                {
                        f[j][i] = max(f[j][i - 1],f[j + (1 << (i - 1))][i - 1]);        
                }        
        }        
        ll ans = 0;
        for (int i = 1; i <= n; i++)
        {
                int l = 1,r = i - 1,t = -1;
                while (l <= r)
                {
                        int mid = (l + r) >> 1;
                        if (rmq(mid,i - 1) < a[i])
                        {
                                t = mid;
                                r = mid - 1;
                        } else l = mid + 1;
                }
                if (t != -1) ans += (ll)(i - t);
        }
        printf("%lld\n",ans);
        
        return 0;
    
}

 

posted @ 2018-08-18 21:51  evenbao  阅读(145)  评论(0编辑  收藏  举报