【模板】笛卡尔树

【模板】笛卡尔树

\(\text{Code}\)

#include <cstdio> 
#include <iostream>
#define re register
#define LL long long
using namespace std;

const int N = 1e7 + 5;
int n, a[N], stk[N], top, ls[N], rs[N];

inline void read(int &x)
{
	x = 0; char ch = getchar();
	while (!isdigit(ch)) ch = getchar();
	while (isdigit(ch)) x = (x<<3) + (x<<1) + (ch^48), ch = getchar();
}

int main()
{
	read(n);
	for(re int i = 1, cur; i <= n; i++)
	{
		read(a[i]), cur = top;
		while (a[stk[cur]] > a[i]) --cur;
		if (cur) rs[stk[cur]] = i;
		if (cur < top) ls[i] = stk[cur + 1];
		stk[top = cur + 1] = i;
	}
	LL s1 = 0, s2 = 0;
	for(re int i = 1; i <= n; i++) s1 ^= 1LL * i * (ls[i] + 1), s2 ^= 1LL * i * (rs[i] + 1);
	printf("%lld %lld\n", s1, s2);
}
posted @ 2021-09-10 13:13  leiyuanze  阅读(56)  评论(0)    收藏  举报