标签:== ++ input char 比较 老师 else iostream name
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 106776 Accepted Submission(s): 40096
#include<iostream> #include<algorithm> #include<vector> #include<math.h> #include<stdio.h> #include<string.h> #include<algorithm> const int n = 200050; using namespace std; int tre[n * 4]; void build(int num, int le, int ri)//建立线段树 { if (le == ri)//如果区间左端点等于区间右端点,则输入区间的每一个元素 { scanf("%d", &tre[num]); return; } int mid = (le + ri) / 2; build(num * 2, le, mid); build(num * 2 + 1, mid + 1, ri); tre[num] = max(tre[num * 2], tre[num * 2 + 1]);//通过子节点来初始化父亲节点 } void update(int num, int le, int ri, int x, int y)//单点更新 { if (le == ri)//找到要更新的x节点所在位置,并对x节点值加y { tre[num] = y; return; } int mid = (le + ri) / 2; if (x <= mid) update(num * 2, le, mid, x, y); else update(num * 2 + 1, mid + 1, ri, x, y); tre[num] = max(tre[num * 2], tre[num * 2 + 1]); } int query(int num, int le, int ri, int x, int y)//区间查询 { if (x <= le && y >= ri)//如果我们要查询的[x,y]区间比[le,ri]大,那么[le,ri]区间的值就是结果的一部分,直接返回 { return tre[num]; } int mid = (le + ri) / 2; int ans = 0;//ans是记录查询区间的最大值 if (x <= mid)//查询左子树 ans = max(ans, query(num * 2, le, mid, x, y)); if (y > mid)//查询右子树 ans = max(ans, query(num * 2 + 1, mid + 1, ri, x, y)); return ans; } int main() { int t, n; while (cin >> n >> t) { build(1, 1, n); char operate[10]; for (int i = 0; i < t; i++) { cin >> operate; int x, y; scanf("%d %d", &x, &y); if (operate[0] == ‘U‘) { update(1, 1, n, x, y); } if (operate[0] == ‘Q‘) { cout << query(1, 1, n, x, y) << endl; } } } return 0; }
标签:== ++ input char 比较 老师 else iostream name
原文地址:https://www.cnblogs.com/-citywall123/p/10046438.html