标签:
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 54534 Accepted Submission(s): 21353
中规中矩,单点更新,区间求最值。
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #include <time.h> #include <algorithm> using namespace std; const int N = 200005; int num[N << 2]; void Update(int i){ num[i] = max(num[i * 2], num[i * 2 + 1]); //更新父亲节点 } void Build(int left, int right, int i){ //建树 if(left == right){ scanf("%d%", &num[i]); return; } int mid = (left + right) / 2; Build(left, mid, i * 2); Build(mid + 1, right, i * 2 + 1); Update(i); } void Change(int loc, int number, int left, int right, int i){ //单点更新 if(left == right){ num[i] = number; return; } int mid = (left + right) / 2; if(loc <= mid) Change(loc, number, left, mid, i * 2); else Change(loc, number, mid + 1, right, i * 2 + 1); Update(i); } int Query(int ll, int rr, int left, int right, int i){ //区间求最值 if(ll == left && rr == right) return num[i]; int mid = (left + right) / 2; if(rr <= mid) return Query(ll, rr, left, mid, i * 2); else if(ll > mid) return Query(ll, rr, mid + 1, right, i * 2 + 1); else return max(Query(ll, mid, left, mid, i * 2), Query(mid + 1, rr, mid + 1, right, i * 2 + 1)); } int main(){ int N, M; while(~scanf("%d%d", &N, &M)){ Build(1, N, 1); while(M--){ char order[2]; int a, b; scanf("%s%d%d", order, &a, &b); if(order[0] == ‘Q‘) printf("%d\n", Query(a, b, 1, N, 1)); else if(order[0] == ‘U‘) Change(a, b, 1, N, 1); } } }
标签:
原文地址:http://www.cnblogs.com/Cyy666/p/4969184.html