HDU 1754 I Hate it (線段樹最大值模板)

來源:互聯網
上載者:User

標籤:style   os   io   for   ar   代碼   line   amp   

思路:與我發表的上一遍求和的思想一樣   只是現在變成求最大值而已

AC代碼:

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;inline int Max(int a, int b){return a > b ? a : b;}const int MAXN = 200001; // 區間範圍struct{int l, r, m; // l 左端點,r 右端點,m 為該區間的最大分數} tree[MAXN*4];int a[MAXN];void creat(int t, int l, int r){tree[t].l = l, tree[t].r = r;if(l == r) // 葉子節點{tree[t].m = a[l];return; //遞迴出口}int m = (l+r) / 2;creat(t*2, l, m), creat(t*2+1, m+1, r); // 左孩子tree[t].m = Max(tree[t*2].m, tree[t*2+1].m); // 右孩子}void update(int t, int n, int v) // 把n 點的值更新為v{if(tree[t].l == tree[t].r && tree[t].l == n){tree[t].m = v;return;}if(n <= tree[t*2].r) update(t*2, n, v);else update(t*2+1, n, v);tree[t].m = Max(tree[t*2].m, tree[t*2+1].m);}int query(int t, int l, int r) // 查詢t 節點在【l,r】區間範圍的最大值{if(l == tree[t].l && r == tree[t].r) return tree[t].m;int s;if(r <= tree[t*2].r) s = query(t*2, l, r);else if(l >= tree[t*2+1].l) s= query(t*2+1, l, r);else s = Max(query(t*2, l, tree[t*2].r), query(t*2+1, tree[t*2+1].l, r));return s;}int main(){int n, m, i, x1, x2;char s[2];while(scanf("%d%d", &n, &m) != EOF){for(i = 1; i <= n; i++) scanf("%d", &a[i]);creat(1, 1, n); // 根節點標號為1,區間為【1,n】while(m--){scanf("%s%d%d", s, &x1, &x2);if(s[0] == 'Q') printf("%d\n", query(1, x1, x2)); // 查詢else update(1, x1, x2); // 更新}}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.