标签:
Spare Table练习
记得用scanf和printf输入输出,否则会TLE
代码:
1 #include <iostream> 2 #include <cstring> 3 #include <cmath> 4 5 using namespace std; 6 7 #define MAX_SIZE 1000010 8 9 int N, Q; 10 int w[MAX_SIZE]; 11 int q[MAX_SIZE][32]; 12 13 void build() { 14 for (int i = 1; (1 << i) <= N; i++) { 15 for (int j = 0; j + (1 << i) <= N; j++) { 16 q[j][i] = min(q[j][i - 1], q[j + (1 << (i - 1))][i - 1]); 17 } 18 } 19 } 20 21 int query(int l, int r) { 22 int len = r - l + 1; 23 int i = 0; 24 25 while ((1 << (i + 1)) <= len) 26 i++; 27 return min(q[l][i], q[r - (1 << i) + 1][i]); 28 } 29 30 int main() { 31 scanf("%d", &N); 32 for (int i = 0; i < N; i++) { 33 scanf("%d", &(w[i])); 34 q[i][0] = w[i]; 35 } 36 37 build(); 38 39 scanf("%d", &Q); 40 while (Q--) { 41 int l, r; 42 scanf("%d%d", &l, &r); 43 printf("%d\n", query(l - 1, r - 1)); 44 } 45 46 return 0;
标签:
原文地址:http://www.cnblogs.com/boring09/p/4383043.html