标签:tom 正整数 else 结束 hint inpu 成绩 ota 处理
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1754
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 102825 Accepted Submission(s): 38669
线段树裸题, 模板
AC code:
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstring> 5 #define MAXNODE 1<<19 6 using namespace std; 7 const int MAXN = 2e5+10; 8 struct data 9 { 10 int l, r, value; 11 }node[MAXNODE]; 12 int score[MAXN]; 13 int N, M; 14 15 void BuildTree(int left, int right, int num) //建树 16 { 17 node[num].l = left; 18 node[num].r = right; 19 if(left == right) 20 { 21 node[num].value = score[left]; 22 } 23 else 24 { 25 BuildTree(left, (left+right)>>1, num<<1); //左子树 26 BuildTree(((left+right)>>1)+1, right, (num<<1)+1); //右子树 27 node[num].value = max(node[num<<1].value, node[(num<<1)+1].value); 28 } 29 } 30 31 void Update(int stu, int val, int num) //更新 32 { 33 node[num].value = max(val, node[num].value); 34 if(node[num].l == node[num].r) return; //更新到叶子结点 35 if(stu <= node[num<<1].r) Update(stu, val, num<<1); //更新左子树 36 else Update(stu, val, (num<<1)+1); //更新右子树 37 } 38 39 int Query(int left, int right, int num) 40 { 41 if(node[num].l == left && node[num].r == right) //区间恰好重合 42 return node[num].value; 43 if(right <= node[num<<1].r) //在左子树区间内 44 return Query(left, right, num<<1); 45 if(left >= node[(num<<1)+1].l) //在右子树区间内 46 return Query(left, right, (num<<1)+1); 47 int mid = (node[num].l + node[num].r)>>1; 48 return max(Query(left, mid, num<<1), Query(mid+1, right, (num<<1)+1)); 49 } 50 51 int main() 52 { 53 54 while(~scanf("%d%d", &N, &M)) 55 { 56 for(int i = 1; i <= N; i++) 57 scanf("%d", &score[i]); 58 BuildTree(1, N, 1); 59 char com[3]; 60 int a, b; 61 while(M--) 62 { 63 scanf("%s", com); 64 scanf("%d%d", &a, &b); 65 if(com[0] == ‘U‘) 66 { 67 Update(a, b, 1); 68 } 69 else 70 { 71 printf("%d\n", Query(a, b, 1)); 72 } 73 } 74 } 75 return 0; 76 }
HDU 1754 I Hate It 【线段树单点修改 区间查询】
标签:tom 正整数 else 结束 hint inpu 成绩 ota 处理
原文地址:https://www.cnblogs.com/ymzjj/p/9517879.html