码迷,mamicode.com
首页 > 其他好文 > 详细

One Oier' s dawn

时间:2019-09-05 12:01:14      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:algorithm   code   cst   不同   bin   false   pen   main   ios   

专题复习-RMQ 问题

与众不同

#include <cstdio>
//#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
#define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))

#define ON_DEBUGG

#ifdef ON_DEBUGG

#define D_e_Line printf("\n----------\n") 
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)

#else

#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;

#endif
using namespace std;
struct ios{
    template<typename ATP>inline ios& operator >> (ATP &x){
        x = 0; int f = 1; char ch;
        for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
        while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
        x *= f;
        return *this;
    }
}io;

template<typename ATP>inline ATP max(ATP &a, ATP &b){
    return a > b ? a : b;
}
template<typename ATP>inline ATP min(ATP &a, ATP &b){
    return a < b ? a : b;
}


const int N = 200007;

int n;
namespace RMQ{

int f[N][21], lg[N], bin[21], last[N * 10], a[N], pos[N];
inline void Prepare(){
    bin[0] = 1;
    R(i,1,20) bin[i] = bin[i - 1] << 1;
    R(i,2,n) lg[i] = lg[i >> 1] + 1;
}
inline void Init(){
    R(i,1,n){
        io >> a[i];
        a[i] += 1000000;
        pos[i] = max(pos[i - 1], last[a[i]] + 1);
        f[i][0] = i - pos[i] + 1;
        last[a[i]] = i;
    }
    
    int t = lg[n];
    R(j,1,t){
        int maxx = n - bin[j] + 1;
        R(i,1,maxx){
            f[i][j] = max(f[i][j - 1], f[i + bin[j - 1]][j - 1]);
        }
    }
}
inline int Query(int l, int r){
    int t = lg[r - l + 1];
    return max(f[l][t], f[r - bin[t] + 1][t]);
}

}

int main(){
//FileOpen();
//freopen("1.txt", "w", stdout);

    int m;
    io >> n >> m;
    RMQ::Prepare();
    RMQ::Init();
    while(m--){
        int L, R;
        io >> L >> R;
        ++L, ++R;
//      int l = L, r = R, P;
//      while(l <= r){
//          int mid = (l + r) >> 1;
//          if(RMQ::pos[mid] <= L){
//              l = mid + 1;
//              P = l;
//          }
//          else
//              r = mid - 1;
//      }
        int P = lower_bound(RMQ::pos + L, RMQ::pos + R + 1, L) - RMQ::pos;
        printf("%d\n", min(R - L + 1, max(P - L, RMQ::Query(P, R)))); // false : P - L + 1
    }
    
    return 0;
}

One Oier' s dawn

标签:algorithm   code   cst   不同   bin   false   pen   main   ios   

原文地址:https://www.cnblogs.com/bingoyes/p/11464986.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!