Codeforces Round #789 (Div. 2) C- Tokitsukaze and Strange Inequality(二维前缀和)
思路:见代码注释
#pragma GCC optimize(2) #pragma GCC optimize(1) #include<bits/stdc++.h> typedef long long ll; typedef unsigned long long ull; const ull base=131; #define MAX 5009 #define PI 3.141592653589793 using namespace std; inline void solve(){ int len;cin>>len; vector<int> a(len+1); for(int i=1;i<=len;i++) cin>>a[i]; vector<vector<int>> dp(len+1,vector<int>(len+1,0)); for (int i = 1; i <= len; ++i) { for (int j = 1; j <= len; ++j) if (a[j] < i) dp[i][j] = 1;//如果小于,设这点为一个逆序对 for (int j = 1; j <= len; ++j) dp[i][j] += dp[i][j - 1];//前缀和求出在1-j内逆序对个数 } ll ans=0; for(int b=1;b<=len;b++) for(int c=b+1;c<=len;c++){ ans+=(dp[a[c]][b-1]*(dp[a[b]][len]-dp[a[b]][c]));//枚举c下标的数字在1-b(即a的范围数字)的逆序对*(枚举b下标状态下的数字在总长范围内-枚举b状态下的数字在1-c内(即所有不是b范围的) } cout<<ans<<"\n"; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0);std::cout.tie(0); int sum;cin>>sum; while(sum--){ solve(); } }