比赛 20120709 评测结果 AAAAAWWTTA
题目名称 数列 最终得分 60
用户昵称 CC 运行时间 2.919 s
代码语言 C++ 内存使用 1.45 MiB
提交时间 2012-07-09 11:46:05
显示代码纯文本
#include <cstdio>
#include <algorithm>
#include <cstring>
struct node {
	int l,r,v;
	node *s[2];
};
int n,ans;
int a[50050],pre[50050],suc[50050],f[50050],g[50050],w[50050];
node *build(int l,int r) {
	node *t = new node;
	if (l == r) {
		t->l = t->r = l;
		t->v = a[l];
		return t;
	}
	int mid = (l + r) >> 1;
	t->l = l,t->r = r;
	t->s[0] = build(l,mid);t->s[1] = build(mid + 1,r);
	t->v = std::max(t->s[0]->v,t->s[1]->v);
	return t;
}
int find(node *u,int l,int r,int v) {
	if (u->l == u->r) return u->v < v;
	if (u->l == l && u->r == r && u->v < v) return (r - l + 1);
	int mid = (u->l + u->r) >> 1,tmp = 0;
	if (l <= mid) tmp += find(u->s[0],l,std::min(r,mid),v);
	if (r > mid) tmp += find(u->s[1],std::max(l,mid + 1),r,v);
	return tmp;
}

int main() {
      freopen("queueb.in","r",stdin);
	freopen("queueb.out","w",stdout);
	scanf("%d", &n);
	for (int i = 1;i <= n;i++) scanf("%d", &a[i]);
	node *root = build(1,n);
	for (int i = 1;i <= n;i++) {
		int pre = find(root,1,i - 1,a[i]),suc = find(root,i + 1,n,a[i]);
		ans += pre * suc;
	}
	printf("%d\n", ans);
	return 0;
}