笛卡尔树

笛卡尔树:

序号满足二分查找树的性质,值满足堆的性质的树。

构建:

方法:

单调栈构建。

代码:

const int N = 1e7 + 10;

inline ll Read()
{
	ll x = 0, f = 1;
	char c = getchar();
	while (c != '-' && (c < '0' || c > '9')) c = getchar();
	if (c == '-') f = -f, c = getchar();
	while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
	return x * f;
}

int n;
int a[N], ch[N][2];
int st[N], top;
ll ansl, ansr;
int main()
{
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	n = Read();
	for (int i = 1; i <= n; i++) a[i] = Read();
	st[++top] = 0;
	for (int i = 1; i <= n; i++)
	{
		for (; top && a[st[top]] > a[i]; ch[i][0] = st[top--]);
		if (st[top]) ch[st[top]][1] = i;
		st[++top] = i;
	}
	for (ll i = 1; i <= n; i++) 
		ansl ^= i * (ch[i][0] + 1), ansr ^= i * (ch[i][1] + 1);
	printf ("%lld %lld\n", ansl, ansr);
	return 0;
}

posted @ 2021-10-21 11:08  Jayun  阅读(47)  评论(1编辑  收藏  举报