hdu 1754 I Hate It 线段树 初步

本文介绍了一种基于线段树的数据结构实现,并针对出现的运行时错误(RE)进行逐步排查与修复。文章详细展示了线段树的构建、更新及查询操作的代码实现过程,同时指出了常见的错误及其解决办法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有错误,一直RE 还没检查出来

代码:

#include<iostream>
#include<cstdio>
#include<memory.h>
using namespace std;

const int MAX=1000000;
struct node
{
	int left,right;
	int mx;
}tree[MAX];
int a[MAX];
inline int max(int x,int y)
{
	if(x<y)
		return y;
	else
		return x;
}
void build(int T,int L,int R)
{
	tree[T].left=L;
	tree[T].right=R;//少了这两个
	if(L==R)
	{
		tree[T].mx=a[L];
	}
	else
	{
		int mid=(L+R)/2;
		build(T*2,L,mid);
		build(T*2+1,mid+1,R);
		tree[T].mx=max(tree[T*2].mx,tree[T*2+1].mx);
	}
}
void UpDate(int T,int LL,int value)
{
	tree[T].mx=max(tree[T].mx,value);

	if(tree[T].left==LL&&tree[T].right==LL)
	  {
		  tree[T].mx=value;
		  return ;
	  }
	  //int mid=(L+R)/2;
	  if(LL<=tree[T*2].right)
	  {
		  UpDate(T*2,LL,value);
	  }
	  else
	  {
		  UpDate(T*2+1,LL,value);
	  }
	  //tree[T].mx=max(tree[T*2].mx,tree[T*2+1].mx);
}
int query(int T,int LL,int RR)
{
	if(tree[T].left==LL&&tree[T].right==RR)
	{
		return tree[T].mx;
	}
	else
	{
		
		if(RR<=tree[T*2].right)
		    return query(T*2,LL,RR);
		else if(tree[T*2+1].left<=LL)
			return query(T*2+1,LL,RR);
		else
		{
			int mid=(tree[T].left+tree[T].right)/2;
			return max(query(T*2,LL,mid),query(T*2+1,mid+1,RR));
			//return max(query(T*2,L,R,LL,mid),query(T*2+1,L,R,mid+1,RR));//错了
		}
	}
}
int main()
{
	int n,m;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		//memset(tree,0,sizeof(tree));
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
		}
		build(1,1,n);
		getchar();
		char s[10];
		for(int i=1;i<=m;i++)
		{
            gets(s);
			//scanf("%s",s);
			int x=s[2]-'0',y=s[4]-'0';
			if(s[0]=='Q')
			{
				int ans=query(1,x,y);
				printf("%d\n",ans);
			}
			else
			{
		         UpDate(1,x,y);
			}
		}
	}
	system("pause");
	return 0;
}





/*
***************TLE**************
#include<iostream>
#include<cstdio>
#include<memory.h>
using namespace std;

const int MAX=1000000;
struct node
{
	int left,right;
	int value;
	int mx;
}tree[MAX];
inline int max(int x,int y)
{
	if(x<y)
		return y;
	else
		return x;
}
void build(int T,int L,int R)
{
     tree[T].left=L;
	 tree[T].right=R;
	if(L==R)
	{
		tree[T].left=L;
		tree[T].right=R;
	}
	else
	{
		int mid=(L+R)/2;
		build(T*2,L,mid);
		build(T*2+1,mid+1,R);
	}
}
void UpDate(int T,int L,int R,int LL,int value)
{
      if(L==LL&&R==LL)
	  {
		  tree[T].value=value;
		  tree[T].mx=value;
		  return ;
	  }
	  int mid=(L+R)/2;
	  if(LL<=mid)
	  {
		  UpDate(T*2,L,mid,LL,value);
	  }
	  else
	  {
		  UpDate(T*2+1,mid+1,R,LL,value);
	  }
	  tree[T].mx=max(tree[T*2].mx,tree[T*2+1].mx);
}
int query(int T,int L,int R,int LL,int RR)
{
	if(L==LL&&R==RR)
	{
		return tree[T].mx;
	}
	else
	{
		int mid=(L+R)/2;
		if(RR<=mid)
		    return query(T*2,L,mid,LL,RR);
		else if(mid<LL)
			return query(T*2+1,mid+1,R,LL,RR);
		else
		{
			return max(query(T*2,L,mid,LL,mid),query(T*2+1,mid+1,R,mid+1,RR));
			//return max(query(T*2,L,R,LL,mid),query(T*2+1,L,R,mid+1,RR));//错了
		}
	}
}
int main()
{
	int n,m;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		int temp;
		build(1,1,n);
		memset(tree,0,sizeof(tree));
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&temp);
			UpDate(1,1,n,i,temp);
		}
		getchar();
		char s[10];
		for(int i=1;i<=m;i++)
		{
	
            gets(s);
			//scanf("%s",s);
			int x=s[2]-'0',y=s[4]-'0';
			if(s[0]=='Q')
			{
				int ans=query(1,1,n,x,y);
				printf("%d\n",ans);
			}
			else
			{
		         UpDate(1,1,n,x,y);
			}
		}
	}
	system("pause");
	return 0;
}*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值