标签:
比如维护一个最大值,注意初始化的log[0] = -1 Σ( ° △ °|||)︴逢写必错有木有!
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 #define REP(i, s, n) for(int i = s; i <= n; i ++) 7 using namespace std; 8 const int maxn = 100000 + 10; 9 int Log[maxn], A[maxn], d[maxn][20], n, Q; 10 void RMQ(){ 11 Log[0] = -1; 12 REP(i, 1, n) d[i][0] = A[i], Log[i] = Log[i >> 1] + 1; 13 for(int j = 1; (1 << j) <= n; j ++) 14 for(int i = 1; i + (1 << j) - 1 <= n; i ++) 15 d[i][j] = max(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]); 16 return ; 17 } 18 int query(int L, int R){ 19 int k = Log[R - L + 1]; return max(d[L][k], d[R - (1 << k) + 1][k]); 20 } 21 void read(int &x){ 22 x = 0; int sig = 1; char ch = getchar(); 23 while(!isdigit(ch)) { if(ch == ‘-‘) sig = -1; ch = getchar(); } 24 while(isdigit(ch)) x = 10 * x + ch - ‘0‘, ch = getchar(); 25 x *= sig; return ; 26 } 27 void init(){ 28 read(n); read(Q); 29 REP(i, 1, n) read(A[i]); 30 RMQ(); 31 return ; 32 } 33 void work(){ 34 int L, R; 35 while(Q --){ 36 read(L); read(R); 37 printf("%d\n", query(L, R)); 38 } 39 return ; 40 } 41 void print(){ 42 43 return ; 44 } 45 int main(){ 46 init(); 47 work(); 48 print(); 49 return 0; 50 }
标签:
原文地址:http://www.cnblogs.com/chxer/p/4419463.html