其他分享
首页 > 其他分享> > One Oier' s dawn

One Oier' s dawn

作者:互联网

专题复习-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;
}

标签:bin,ch,int,Oier,pos,ATP,dawn,define
来源: https://www.cnblogs.com/bingoyes/p/11464986.html