P1637 三元上升子序列

链接

https://www.luogu.com.cn/problem/P1637

题目

思路

事实上和求逆序对的题目有点像,但是求的是同序对(?。 先回顾下树状数组求逆序对的题目。 https://www.cnblogs.com/zzzsacmblog/p/18314521 这个总的思路其实就是前缀和,只不过拿树状数组优化了。先给每个节点对应的值对应树上的节点+1,然后求比它值小1的前缀和就是数量。

代码

#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
#define IOS ios::sync_with_stdio(false), cin.tie(0) ,cout.tie(0)
using namespace std;
typedef long long ll;
const int N = 3e4 + 10;
#define int long long
int a[N], b[N];
int tree[N << 2];
int lowbit(int x)
{
	return x & (-x);
}
void add(int x)
{
	int xnow = x;
	while (xnow < N)
	{
		tree[xnow]++;
		xnow += lowbit(xnow);
	}
}
int sum(int x)
{
	int ans = 0;
	int xnow = x;
	while (xnow > 0)
	{
		ans += tree[xnow];
		xnow -= lowbit(xnow);
	}

	return ans;
}
stack<int>st; queue<int>q;
int suma[N];
signed main()
{
	IOS;
	int n; cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		b[i] = a[i];
	}
	sort(b + 1, b + 1 + n);
	//stac
	int cnt = 1;
	map<int, int>small;
	for (int i = 1; i <= n; i++)
	{
		if (!small[b[i]])
			small[b[i]] = cnt++;
	}
	for (int i = 1; i <= n; i++)
		a[i] = small[a[i]];
	for (int i = n; i >= 1; i--)
	{
		add(a[i]);
		st.push(n - i - sum(a[i]) + 1);
	}
	st.pop();
	memset(tree, 0, sizeof(tree));
	for (int i = 1; i <= n; i++)
	{
		add(a[i]);
		q.push(sum(a[i]-1));
	}
	q.pop();
	ll ans = 0;
	for (int i = 2; i < n; i++)
	{
		ans += q.front() * st.top();
		q.pop(), st.pop();
	}
	cout << ans;

	return 0;
}
其中st是求后面的,q是求前面的。遍历的范围是(1,n),两边是开区间。

posted on 2024-07-21 15:29  WHUStar  阅读(1)  评论(0编辑  收藏  举报