http://acm.hdu.edu.cn/showproblem.php?pid=1779 線段樹 求區間最大值 結點更新

來源:互聯網
上載者:User

 

#include <iostream><br />#include <cstdio><br />#define LL(x) ((x) << 1)<br />#define RR(x) ((x) << 1 | 1)<br />using namespace std;<br />const int N = 100005;<br />struct Seg_tree {<br />int l, r;<br />int val; //本區間累加值<br />int maxval; //記錄子區間最大值<br />int loction; //子區間最大值位置<br />int mid() {<br />return (l + r) >> 1;<br />}<br />} tree[4 * N];<br />bool readint(int &ret){<br />int sgn;<br />char c;<br />c = getchar();<br />if(c == EOF )<br />return true;<br />while(c != '-' && c < '0' || c > '9')<br />c = getchar();<br />sgn = (c == '-') ? -1 : 1;<br />ret = (c == '-') ? 0 : (c - '0');<br />while((c = getchar()) >= '0' && c <= '9')<br />ret = ret * 10 + (c - '0');<br />ret *= sgn;<br />return false;<br />}<br />void Build(int node, int l, int r) {<br />tree[node].l = l;<br />tree[node].r = r;<br />tree[node].val = 0;<br />tree[node].maxval = 0;<br />tree[node].loction = l;<br />if (l == r) {<br />return;<br />}<br />int mid = (l + r) >> 1;<br />Build(LL(node), l, mid);<br />Build(RR(node), mid + 1, r);<br />}<br />int Update(int node, int l, int r, int val) {<br />if (tree[node].l == l && tree[node].r == r) {<br />tree[node].val += val;<br />return tree[node].maxval + tree[node].val;<br />}<br />int mid = tree[node].mid();<br />if (l <= mid) {<br />if (r <= mid) {<br />int temp = Update(LL(node), l, r, val);<br />if (temp >= tree[RR(node)].maxval + tree[RR(node)].val) {<br />tree[node].maxval = temp;<br />tree[node].loction = tree[LL(node)].loction;<br />} else {<br />tree[node].maxval = tree[RR(node)].maxval + tree[RR(node)].val;<br />tree[node].loction = tree[RR(node)].loction;<br />}<br />} else {<br />tree[node].maxval = Update(LL(node), l, mid, val);<br />tree[node].loction = tree[LL(node)].loction;<br />int temp = Update(RR(node), mid + 1, r, val);<br />if (temp > tree[node].maxval) {<br />tree[node].maxval = temp;<br />tree[node].loction = tree[RR(node)].loction;<br />}<br />}<br />} else {<br />int temp = Update(RR(node), l, r, val);<br />if (temp > tree[LL(node)].val + tree[LL(node)].maxval) {<br />tree[node].maxval = temp;<br />tree[node].loction = tree[RR(node)].loction;<br />} else {<br />tree[node].maxval = tree[LL(node)].val + tree[LL(node)].maxval;<br />tree[node].loction = tree[LL(node)].loction;<br />}<br />}<br />return tree[node].maxval + tree[node].val;<br />}<br />int Query(int node, int l, int r, int &loc) {<br />if (l == tree[node].l && tree[node].r == r) {<br />loc = tree[node].loction;<br />return tree[node].maxval + tree[node].val;<br />}<br />int mid = tree[node].mid();<br />int temp1, temp2;<br />if (l <= mid) {<br />if (r <= mid) {<br />temp1 = Query(LL(node), l, r, loc);<br />} else {<br />int loc2;<br />temp1 = Query(LL(node), l, mid, loc);<br />temp2 = Query(RR(node), mid + 1, r, loc2);<br />if (temp2 > temp1) {<br />temp1 = temp2;<br />loc = loc2;<br />}<br />}<br />} else {<br />temp1 = Query(RR(node), l, r, loc);<br />}<br />return temp1 + tree[node].val;<br />}<br />int main() {<br />int n, m;<br />while (scanf("%d %d", &n, &m) != EOF && m + n) {<br />char str[2];<br />int a, b, c;<br />Build(1, 1, n);<br />while (m--) {<br />scanf("%s", str);<br />if (str[0] == 'I') {<br />//scanf("%d %d %d", &a, &b, &c);<br />readint(a);<br />readint(b);<br />readint(c);<br />Update(1, a, b, c);<br />} else {<br />//scanf("%d %d", &a, &b);<br />readint(a);<br />readint(b);<br />int loc = 0;<br />int ans = Query(1, a, b, loc);<br />printf("%d/n", ans);<br />if (loc != 0) {<br />Update(1, loc, loc, -ans);<br />}<br />}<br />}<br />}<br />}<br /> 題目沒有放出 但的確是好題值得一寫 先給出描述:                              Happy Children’s Day
Children's Day is coming. In this day, children will get a lot of candies. In MAX city, people develop an automatic candy management (ACM) system. ACM can manage N piles of candies. The system can carry out two operations.
1). I a b c (1<=a<=b<=N, 0<c<=100), ACM will add each pile numbered from a to b with c candies.
2). C a b (1<=a<=b<=N), ACM will choose the pile (from a to b) with the most candies, and give all the candies in that pile to one child. If two or more piles have the most candies, then the pile with smaller id will be chosen.
Given a series of instructions, for each operation C a b, please help the people to find out the number of candies a child can get.
Input:
Input may contain several test data sets.
For each data set, the first line contains two integers N, M(0<N,M<=10^5) where N indicates the number of piles, and M indicates the number of operations that follows.
Each of the next M lines describes one operation.
Input will be ended by N=0,M=0,which should not be processed.
NOTE: At the beginning of each data set, all of the N piles have 0 candies.
Output:
For each operation C a b, print the number of candies the child can get in a line.
Sample Input:
5 4
I 1 5 1
C 2 3
I 2 2 4
C 2 3
0 0
Sample Output:
1
4

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.