线段树入门
http://ac.jobdu.com/problem.php?pid=1544
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> const int Maxint = 0x7fffffff; const int Max = 200010; int node[Max]; int data[Max]; void createSegTree(int L, int R, int root) { if (L == R) { node[root] = data[L]; return ; } int mid = (L + R) / 2; createSegTree(L, mid, root * 2 ); createSegTree(mid + 1, R, root * 2 + 1); if (node[root] > node[root * 2]) node[root] = node[root * 2]; if (node[root] > node[root * 2 + 1]) node[root] = node[root * 2 + 1]; } int query(int qL, int qR, int L, int R, int root) { if (L > qR || R < qL) return Maxint; if (qL <= L && R <= qR) return node[root]; int mid = (L + R) / 2; int Lmin = query(qL, qR, L, mid, root * 2); int Rmin = query(qL, qR, mid + 1, R, root * 2 + 1); return Lmin < Rmin ? Lmin : Rmin; } void setNode(int n) { for (int i = 1; i <= n; ++i) node[i] = Maxint; } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); int n, t, qL, qR; while(scanf("%d", &n) != EOF) { setNode(2 * n + 1); for (int i = 1; i <= n; ++i) scanf("%d", &data[i]); createSegTree(1, n, 1); //for (int i = 1; i <= 2 * n + 1; ++i) //printf("%d ", node[i]); printf("\n"); scanf("%d", &t); while(t) { --t; scanf("%d %d", &qL, &qR); if (qL == qR) printf("%d\n", data[qL]); else printf("%d\n", query(qL, qR, 1, n, 1)); } } return 0; }