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;
}