Pagini recente » Cod sursa (job #1021358) | Cod sursa (job #2206634) | Cod sursa (job #1256416) | Cod sursa (job #1786497) | Cod sursa (job #1163623)
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>
using namespace std;
const char infile[] = "rmq.in";
const char outfile[] = "rmq.out";
ofstream fout(outfile);
const int MAXN = 100005;
const int MAXLG = 20;
const int oo = 0x3f3f3f3f;
typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;
const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; }
const inline void Get_min(int &a, const int b) { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b) { if( a < b ) a = b; }
int N, M, RMQ[MAXLG][MAXN];
int Lg[MAXN];
inline void buildRMQ() {
for(int i = 2 ; i <= N ; ++ i)
Lg[i] = Lg[i >> 1] + 1;
for(int i = 1 ; (1 << i) <= N ; ++ i)
for(int j = 1 ; j + (1 << i) - 1 <= N ; ++ j)
RMQ[i][j] = min(RMQ[i - 1][j], RMQ[i - 1][j + (1 << (i - 1))]);
}
inline int Query(int x, int y) {
int lg = Lg[y - x + 1];
int ans = RMQ[lg][x];
if(ans > RMQ[lg][y - (1 << lg) + 1])
ans = RMQ[lg][y - (1 << lg) + 1];
return ans;
}
const int lim = (1 << 20);
char buff[lim];
int pos;
inline int get(int &x) {
while(!('0' <= buff[pos] && buff[pos] <= '9'))
if(++ pos == lim) {
fread(buff, 1, lim, stdin);
pos = 0;
}
x = 0;
while('0' <= buff[pos] && buff[pos] <= '9') {
x = x * 10 + buff[pos] - '0';
if(++ pos == lim) {
fread(buff, 1, lim, stdin);
pos = 0;
}
}
}
int main() {
freopen(infile, "r", stdin);
get(N);
get(M);
for(int i = 1 ; i <= N ; ++ i)
get(RMQ[0][i]);
buildRMQ();
for(int i = 1 ; i <= M ; ++ i) {
int x, y;
get(x);
get(y);
fout << Query(x, y) << '\n';
}
fout.close();
return 0;
}