今天把这题一做,才知道以前很多地方都用错了,杭电的数据也太水了,今天算是大彻大悟了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#define N 1000005
using namespace std;
int n,m;
int a[N];
struct node{
int l,r,mid,minn;
}tree[N<<2];
void build(int m,int l,int r){
tree[m].l = l;
tree[m].r = r;
tree[m].mid = (l+r) >> 1;
if(l == r){
tree[m].minn = a[l];
return ;
}
build(m<<1,l,tree[m].mid);
build((m<<1)+1,tree[m].mid+1,r);
tree[m].minn = min(tree[m<<1].minn,tree[(m<<1)+1].minn);
}
void update(int m,int a,int val){
if(tree[m].l == tree[m].r){
tree[m].minn = val;
return ;
}
if(a <= tree[m].mid)
update(m<<1,a,val);
else
update((m<<1)+1,a,val);
tree[m].minn = min(tree[m<<1].minn,tree[(m<<1)+1].minn);
}
int query(int m,int l,int r){
if(l == tree[m].l && r == tree[m].r){
return tree[m].minn;
}
if(r <= tree[m].mid)
return query(m<<1,l,r);
if(l > tree[m].mid)
return query((m<<1)+1,l,r);
return min(query(m<<1,l,tree[m].mid), query((m<<1)+1,tree[m].mid+1,r));
}
int main(){
while(scanf("%d",&n) != EOF){
for(int i = 1; i <= n; i++)
scanf("%d",&a[i]);
build(1,1,n);
scanf("%d",&m);
for(int i = 1; i <= m; i++){
int op,x,y;
scanf("%d%d%d",&op,&x,&y);
if(op == 1)
update(1,x,y);
else
printf("%d\n",query(1,x,y));
}
}
return 0;
}
另贴一错误思想代码,但是hdu1754居然能过。。。
#include <iostream>
#include <cstdio>
#include <algorithm>
#define N 200005
using namespace std;
int n,m;
int a[N];
struct node{
int l,r,mid,maxn;
}tree[N<<2];
void build(int m,int l,int r){
tree[m].l = l;
tree[m].r = r;
tree[m].mid = (l+r) >> 1;
if(l == r){
tree[m].maxn = a[l];
return ;
}
build(m<<1,l,tree[m].mid);
build((m<<1)+1,tree[m].mid+1,r);
tree[m].maxn = max(tree[m<<1].maxn,tree[(m<<1)+1].maxn);
}
void update(int m,int a,int val){
tree[m].maxn = max(tree[m].maxn,val);
if(tree[m].l == tree[m].r)
return ;
if(a <= tree[m].mid)
update(m<<1,a,val);
else
update((m<<1)+1,a,val);
}
int query(int m,int l,int r){
if(l == tree[m].l && r == tree[m].r){
return tree[m].maxn;
}
if(r <= tree[m].mid)
return query(m<<1,l,r);
if(l > tree[m].mid)
return query((m<<1)+1,l,r);
return max(query(m<<1,l,tree[m].mid), query((m<<1)+1,tree[m].mid+1,r));
}
int main(){
while(scanf("%d%d",&n,&m) != EOF){
for(int i = 1; i <= n; i++)
scanf("%d",&a[i]);
build(1,1,n);
for(int i = 1; i <= m; i++){
int op,x,y;
getchar();
scanf("%c%d%d",&op,&x,&y);
if(op == 'U')
update(1,x,y);
else
printf("%d\n",query(1,x,y));
}
}
return 0;
}