社区讨论

最后一个点过不了,不知道哪里出了问题,有大佬能帮忙看一下吗

P5076【深基16.例7】普通二叉树(简化版)参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@luz4wd1x
此快照首次捕获于
2024/04/14 14:17
2 年前
此快照最后确认于
2024/04/14 15:50
2 年前
查看原帖
C
#include <bits/stdc++.h>
using namespace std;
int cnt=1;
struct Node
{
    int left;
	int right;
	int value;
	int num;
	int size;	
};
Node t[10010];
int fun1(int x,int root)
{
	if(root==0) return 1;
	if(x<t[root].value)
	  return fun1(x,t[root].left);
	else  if(x==t[root].value)
	  return t[t[root].left].size;
	else  if(x>t[root].value)
	  return t[t[root].left].size+t[root].num+fun1(x,t[root].right);
}
int fun2(int x,int root)
{
	if(x<=t[t[root].left].size)
	  return fun2(x,t[root].left);
	else  if(x<=t[t[root].left].size+t[root].num)
	  return t[root].value;
	else
	  return fun2(x-t[t[root].left].size-t[root].num,t[root].right);
}
void insert(int x,int root)
{
	if(cnt==1)
	{
		Node temp={0,0,x,1,1};
		t[cnt]=temp;
		cnt++;
	}
	else
	{
		if(x<t[root].value)
		{
			if(t[root].left==0)
			{
				Node temp={0,0,x,1,1};
				t[cnt]=temp;
				t[root].left=cnt;
				cnt++;
			}
			else
			  insert(x,t[root].left);
		}
		if(x==t[root].value) 
		  t[root].num++;
		if(x>t[root].value)
		{
			if(t[root].right==0)
			{
				Node temp={0,0,x,1,1};
				t[cnt]=temp;
				t[root].right=cnt;
				cnt++;
			}
			else
			  insert(x,t[root].right);
		}
	}
	t[root].size=t[t[root].left].size+t[t[root].right].size+t[root].num;
}
int main()
{
	int n,op,x;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
	    cin>>op>>x;
	    if(op==1)  cout<<fun1(x,1)<<endl;
	    if(op==2)  cout<<fun2(x,1)<<endl;
	    if(op==3)  
	    {
		    int temp=fun1(x,1);
			if(temp==1) cout<<-2147483647<<endl;
			else cout<<fun2(temp-1,1)<<endl;
		}
	    if(op==4)
	    {
			int temp=fun1(x+1,1);
			if(temp==cnt) cout<<2147483647<<endl;
			else cout<<fun2(temp,1)<<endl;
		}
	    if(op==5)  insert(x,1);
	}
	return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...