#include "stdafx.h"
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=110;
int a[maxn];
struct node{
int data,left,right;
}no[4*maxn];
void build(int i,int left,int right){
no[i].left=left;
no[i].right=right;
if(left==right)no[i].data=a[left];
else{
int mid=(left+right)/2;
build(i<<1,left,mid);
build(i<<1|1,mid+1,right);
no[i].data=max(no[i<<1].data,no[i<<1|1].data);
}
}
void update(int pos,int x,int i){
if(no[i].left==pos&&no[i].right==pos)no[i].data=x;
else{
int mid=(no[i].left+no[i].right)/2;
if(pos<mid){
update(pos,x,i<<1);
}
else update(pos,x,i<<1|1);
no[i].data=max(no[i<<1].data,no[i<<1|1].data);
}
}
int query_max(int left,int right,int i){
if(no[i].left==left&&no[i].right==right)return no[i].data;
else{
int mid=(left+right)/2;
if(right<=mid)return query_max(left,right,i<<1);
else if(left>mid)return query_max(left,right,i<<1|1);
else return max(query_max(left,mid,i<<1),query_max(mid+1,right,i<<1|1));
}
}
线段树模板
最新推荐文章于 2020-02-10 13:59:53 发布