I Hate It (线段树)

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <iostream> 
#include <algorithm>  
#define lson idx<<1
#define rson idx<<1|1
#define maxn 200005
using namespace std;
int stu[maxn];
struct set_Tree
{
   int l,r,Max;
}tr[maxn*3];  

void build(int L,int R,int idx)
{
   if(L==R)
   { tr[idx].Max=stu[L]; return ;}
   int mid=(tr[idx].l+tr[idx].r)/2;
   build(L,mid,lson);
   build(mid+1,R,rson);  
   tr[idx].Max=max(tr[lson].Max,tr[rson].Max);
}

void update(int p,int date,int idx)
{
   if(tr[idx].l==tr[idx].r)
   {
     tr[idx].Max=date;
     return ;                      
   }
   int mid=(tr[idx].l+tr[idx].r)/2;
   if(p<=mid)
    update(p,date,lson);
   else
    update(p,date,rson);
   tr[idx].Max=max(tr[lson].Max,tr[rson].Max);
}

int query(int L,int R,int idx)
{
  if(L<=tr[idx].l && tr[idx].r<=R)
    return tr[idx].Max;
  int mid=(tr[idx].l+tr[idx].r)/2;
  int res=0;
  if(L<=mid)
   res=max(res,query(L,R,lson));
  if(R>mid)
   res=max(res,query(L,R,rson));
  return res;  
}

int main()
{
  int i,j,a,b,n,m;
  char str;
  freopen("in.txt","r",stdin);
  freopen("out.txt","w",stdout);
  while(scanf("%d %d",&n,&m)==2)
  {
    for(i=1;i<=n;i++)
     scanf("%d",&stu[i]);
    for(i=1;i<=n;i++)
    printf("%d ",stu[i]);
    build(1,n,1);  
    getchar();
    for(i=1;i<=m;i++)
    {
      scanf("%c %d %d\n",&str,&a,&b);
      printf("%c %d %d\n",str,a,b);
      if(str=='Q')
       printf("%d\n",query(a,b,1));
      else
       update(a,b,1);           
    }                        
  }  
  return 1;
}

你可能感兴趣的:(Algorithm,Date,query,Build,include,iostream)