I Hate It
题意:
中文题,自己看
题解:
基础的线段树,很适合练模板
个人问题:
目前情况,我的线段树还离不开模板。这个题必须用scanf和printf,因为我是用VS编译器,不支持,所以吃亏了。关于如何设置编译器,以后可能会出博客。
代码:
///I Hate It
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
using namespace std;
struct xiao {
int l,r,w;
}tr[800001];
int num[200005],now,ad;
void build(int root, int l, int r) {
tr[root].l = l;
tr[root].r = r;
if (l == r) {
tr[root].w = num[l];
return;
}
int m = (l + r) / 2;
build(root <<1, l, m);
build(root <<1|1, m + 1, r);
tr[root].w = max(tr[root * 2].w, tr[root * 2 + 1].w);
}
void add(int root) {
if (tr[root].l == tr[root].r) {
tr[root].w = now;
return;
}
int m = (tr[root].l + tr[root].r) / 2;
if (ad <= m) add(root * 2);//*
else add(root * 2 + 1);
tr[root].w = max(tr[root * 2].w ,tr[root * 2 + 1].w);
}
int findx(int root, int l, int r) {
if (tr[root].l> r || tr[root].r < l) return 0;
if (l <= tr[root].l && tr[root].r <= r) return tr[root].w;
int a = findx(2 * root, l, r);
int b = findx(2 * root + 1, l, r);
return a>b?a:b;
}
int main() {
int stu, cmp;
char C;
while (scanf("%d%d", &stu, &cmp) != EOF) {
for (int i = 1; i <= stu; i++)
scanf("%d", &num[i]);
build(1, 1, stu);
for (int j = 0; j < cmp; j++) {
cin >> C >> ad >> now;
if (C == 'Q') printf("%d\n", findx(1, ad, now));
else add(1);
}
}
return 0;
}
写在最后:
推荐两篇博客个人关于线段树的总结,关于这个题一位同学的题解。