- #include <bits/stdc++.h>
- #define R 100003
- using namespace std;
- int numbers[R], tree[R*3], I, J;
- void build(int u, int v, int node){
- if(u >= v){
- if(u == v)tree[node] = numbers[u];
- return;
- }
- int mid = (u+v)/2, child1 = node*2, child2 = node*2+1;
- build(u, mid, child1);
- build(mid+1, v, child2);
- tree[node] = min(tree[child1], tree[child2]);
- }
- int query(int u, int v, int node){
- if(u > J || v < I)return INT_MAX;
- if(u >= I && v <= J)return tree[node];
- if(u > v)return INT_MAX;
- int mid = (u+v)/2, child1 = node*2, child2 = node*2+1;
- int x = query(u, mid, child1);
- int y = query(mid+1, v, child2);
- return min(x, y);
- }
- int main(){
- int cs = 0, n, q, t;
- scanf("%d", &t);
- while(t--){
- scanf("%d %d", &n, &q);
- for(int i = 1; i <= n; i++)scanf("%d", &numbers[i]);
- build(1, n, 1);
- printf("Case %d:\n", ++cs);
- for(int i = 1; i <= q; i++){
- scanf("%d %d", &I, &J);
- printf("%d\n", query(1, n, 1));
- }
- }
- return 0;
- }
Saturday, June 6, 2015
LightOJ - 1082 - Array Queries
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment