2015长春网络赛 1007 - The Water Problem(裸线段树)

The Water Problem

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
In Land waterless, water is a very limited resource. People always fight for the biggest source of water. Given a sequence of water sources with  a1,a2,a3,...,an representing the size of the water source. Given a set of queries each containing  2  integers  l  and  r , please find out the biggest water source between  al  and  ar .
 

Input
First you are given an integer  T(T10)  indicating the number of test cases. For each test case, there is a number  n(0n1000)  on a line representing the number of water sources.  n  integers follow, respectively  a1,a2,a3,...,an , and each integer is in  {1,...,106} . On the next line, there is a number  q(0q1000)  representing the number of queries. After that, there will be  q  lines with two integers  l  and  r(1lrn)  indicating the range of which you should find out the biggest water source.
 

Output
For each query, output an integer representing the size of the biggest water source.
 

Sample Input
   
   
   
   
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
 

Sample Output
   
   
   
   
100 2 3 4 4 5 1 999999 999999 1
由于题目一样,水题
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;


#define pb push_back
#define mp make_pair
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r


typedef long long LL;
typedef pair<int, int > PII;
typedef unsigned long long uLL;
template<typename T>
void print(T* p, T* q, string Gap = " ") {
    int d = p < q ? 1 : -1;
    while(p != q) {
        cout << *p;
        p += d;
        if(p != q) cout << Gap;
    }
    cout << endl;
}
template<typename T>
void print(const T &a, string bes = "") {
    int len = bes.length();
    if(len >= 2)cout << bes[0] << a << bes[1] << endl;
    else cout << a << endl;
}

const int INF = 0x3f3f3f3f;
const int MAXM = 2e5;
const int MAXN = 1e3 + 5;

int T, n , Sum[MAXN << 2];

void pushup(int rt) {
    Sum[rt] = max(Sum[rt << 1], Sum[rt << 1|1]);
}

void build(int rt, int l, int r) {
    if(l == r){
        cin >> Sum[rt];
        return;
    }
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
    pushup(rt);
}

int query(int L, int R, int rt, int l, int r) {
    if(L <= l && r <= R) {
        return Sum[rt];
    }
    int mid = (l + r) >> 1;
    int ret = 0;
    if(L <= mid) ret = max(ret, query(L, R, lson));
    if(R > mid) ret = max(ret, query(L, R, rson));
    return ret;
}

int main(){
    //freopen("D://imput.txt", "r", stdin);
    int l, r, q;
    cin >> T;
    while(T --){
        cin >> n;
        build(1, 1, n);
        cin >> q;
        while(q --){
            cin >> l >> r;
            cout << query(l, r, 1, 1, n) << endl;
        }
    }
    return 0;
}


你可能感兴趣的:(2015长春网络赛 1007 - The Water Problem(裸线段树))