2015長春網路賽 1007 - The Water Problem(裸線段樹)

來源:互聯網
上載者:User
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,...,anrepresenting 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(T≤10) indicating the number of test cases. For each test case, there is a number  n(0≤n≤1000) 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(0≤q≤1000) representing the number of queries. After that, there will be  q lines with two integers  l and  r(1≤l≤r≤n) 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, rtypedef 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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.