HDU 1754 I Hate It 基础线段树

用区间值m表示这段区间的最大值,一直更新这个区间的最大值,很基础的线段树

#include
#include
using namespace std;
#define N 200005
struct node{
    int l,r,m;
}tree[N*4];
int a[N];
void build(int left,int right,int i){
    tree[i].l=left;
    tree[i].r=right;
    if(tree[i].l==tree[i].r){
        tree[i].m=a[tree[i].l];
        return ;
    }
    int mid=(tree[i].l+tree[i].r)>>1;
    build(left,mid,i*2);
    build(mid+1,right,i*2+1);
    tree[i].m=max(tree[i*2].m,tree[i*2+1].m);
}
void updata(int left,int right,int i,int val){
 //   cout<left || tree[i].r>1;
    updata(left,right,i*2,val);
    updata(left,right,i*2+1,val);
    tree[i].m=max(tree[i*2].m,tree[i*2+1].m);
}
int ans=0;
void query(int left,int right,int i){
 //   cout<<"Q 4 5 "<ans)
            ans=tree[i].m;
        return ;
    }
    int mid=(tree[i].l+tree[i].r)>>1;
    if(mid>=right)
        query(left,right,i*2);
    else if(mid


 

你可能感兴趣的:(线段树,ACM题目讲解)