为什么我的笛卡尔树占的空间大,而且还很慢?
  • 板块学术版
  • 楼主czy0323
  • 当前回复8
  • 已保存回复8
  • 发布时间2022/8/24 11:57
  • 上次更新2023/10/27 13:54:09
查看原帖
为什么我的笛卡尔树占的空间大,而且还很慢?
538427
czy0323楼主2022/8/24 11:57

AC记录

#include <iostream>
using namespace std;
typedef long long ll;
const ll MAXN=1e7+5;
ll n,cnt,top,xorl,xorr;
ll stack[MAXN];

struct node{
	ll data,lson,rson;
	node(){
		data=lson=rson=0;
	}
} a[MAXN];

inline int read(){
	ll x=0,f=1;
	char ch=getchar();
	while( ch<'0' || ch>'9' ){
		if( ch=='-' )
			f=-1;
		ch=getchar();
	}
	while( ch>='0' && ch<='9' ){
		x=x*10+ch-48;
		ch=getchar();
	}
	return x*f;
}

inline void build(){
	ll temp=top;
	while( top && a[stack[top]].data>=a[cnt].data )
		top--;
	if( top )
		a[stack[top]].rson=cnt;
	if( top<temp )
		a[cnt].lson=stack[top+1];
	stack[++top]=cnt;
}

int main() {
    n=read();
    for(ll i=1;i<=n;i++){
    	++cnt;
    	a[cnt].data=read();
    	build();
    }
    for(ll i=1;i<=n;i++){
    	xorl^=i*(a[i].lson+1);
    	xorr^=i*(a[i].rson+1);
    }
    printf("%lld %lld",xorl,xorr);
    return 0;
}
2022/8/24 11:57
加载中...