标签:
3
1
100
1
1 1
5
1 2 3 4 5
5
1 2
1 3
2 4
3 4
3 5
3
1 999999 1
4
1 1
1 2
2 3
3 3
区间最大值 线段树
#include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; const int oo = 0x3f3f3f3f; const int N = 1005; typedef long long LL; struct da { int left, right, val; }as[N*4]; int n, ac[N]; void build(int left, int right, int i) { as[i].left = left; as[i].right = right; int mid = (left + right)/2; if(left == right) { as[i].val = ac[left]; return ; } build(left, mid, 2*i); build(mid+1, right, 2*i+1); as[i].val = max(as[i*2].val, as[2*i+1].val); } void query(int left, int right, int i, int &ans) { if(as[i].left == left && as[i].right == right) { ans = max(as[i].val, ans); return ; } int mid = (as[i].left + as[i].right)/2; if(right <= mid) query(left, right, 2*i, ans); else if(left > mid) query(left, right, 2*i+1, ans); else { query(left, mid, 2*i, ans); query(mid+1, right, 2*i+1, ans); } } int main() { int T, Q; scanf("%d", &T); while(T--) { scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &ac[i]); build(1, n, 1); scanf("%d", &Q); while(Q--) { int a, b; scanf("%d %d", &a, &b); int ans = 0; query(a, b, 1, ans); printf("%d\n", ans); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/PersistFaith/p/4808128.html