zoukankan      html  css  js  c++  java
  • [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
    ",ans);
            
            return 0;
        
    }
  • 相关阅读:
    EasyUi中使用自定义图标
    EasyUi初始配置
    struts2文件上传
    转换器
    Oracle 视图
    存储过程的优缺点
    Oracle 序列详解
    plsql使用技巧
    MYSQL和ORACLE的一些区别
    JAVA操作Excle之Poi(二)批量导出Excle数据
  • 原文地址:https://www.cnblogs.com/evenbao/p/9498811.html
Copyright © 2011-2022 走看看