您的位置:首页 > 其它

HDU-1754 I Hate It 线段树

2011-04-06 19:11 302 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1754

#include <stdio.h>

int tree[1000000], left[1000000], right[1000000], ans;

void build(int p, int l, int r){
tree[p] = 0;
left[p] = l;
right[p] = r;
if(l != r){
build((p * 2), l, ((l + r) / 2));
build(((p * 2) + 1), (((l + r) / 2) + 1), r);
}
return;
}

void change(int p, int i, int j){
if(j > tree[p]){
tree[p] = j;
}
if((left[p] == i) && (right[p] == i)){
return;
}
if(i <= right[p * 2]){
change((p * 2), i, j);
}else{
change(((p * 2) + 1), i, j);
}
return;
}

void count(int p, int i, int j){
if((left[p] == i) && (right[p] == j)){
if(tree[p] > ans){
ans = tree[p];
}
return;
}
if(j <= right[p * 2]){
count((p * 2), i, j);
}else if(i >= left[(p * 2) + 1]){
count(((p * 2) + 1), i, j);
}else{
count((p * 2), i, right[p * 2]);
count(((p * 2) + 1), left[(p * 2) + 1], j);
}
return;
}

int main(){
int n, m, i, j, c;
char action[5];
while(scanf("%d %d", &n, &m) != EOF){
build(1, 1, n);
for(i = 1; i <= n; i++){
scanf("%d", &j);
change(1, i, j);
}
while(m){
m--;
scanf("%s", &action);
if(action[0] == 'U'){
scanf("%d %d", &i, &j);
change(1, i, j);
}else{
scanf("%d %d", &i, &j);
ans = 0;
count(1, i, j);
printf("%d\n", ans);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: