1. 程式人生 > 其它 >CF round 789 C - Tokitsukaze and Strange Inequality

CF round 789 C - Tokitsukaze and Strange Inequality

Tokitsukaze and Strange Inequality

維護二維字首和 \(s[i][j]\) 為前 i 個數中比 j 小的有幾個

列舉 b,c,分別判斷 a,d 有多少種,每對 b,c 的貢獻為 \(cnt_a*cnt_d\)

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;
typedef long long ll;
const int N = 5e3 + 10;
int n;
int s[N][N];//s[i][j]為前i個數中小於j的有幾個
int p[N];
int main()
{
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while(T--)
	{
		cin >> n;
		for (int i = 1; i <= n; i++)
		{
			cin >> p[i];
			for (int j = 1; j <= n; j++)
				s[i][j] = s[i-1][j];
			for (int j = p[i]; j <= n; j++)
				s[i][j]++;
		}
		ll ans = 0;
		for (int b = 2; b <= n - 2; b++)
		{
			for (int c = b + 1; c <= n - 1; c++)
			{
				ll t1 = s[b-1][p[c] - 1];
				ll t2 = s[n][p[b]] - s[c][p[b]];
				ans += t1 * t2;
			}
		}
		cout << ans << endl;
	}
	return 0;
}