题意:满足题目中的式子,a < b && c < d && Va < Vb && Vc > Vd

  思路:先求不讨论位置重合的情况,把对应的2种关系相乘,然后得到的答案减去重合的地方。不想解释,我特么改着改着就对了。都不知道哪里错了,叫对了数据还是找不到。因为只有一组数据出错。

#include<bits/stdc++.h>
using namespace std; const int maxn = 5e4 + ;
int tr[maxn], in[maxn], sar[maxn], ls[maxn], lb[maxn], rs[maxn], rb[maxn];
int n, m; int lowbit(int x){
return x & -x;
} void add(int x, int d){
while(x <= n){
tr[x] += d;
x += lowbit(x);
}
} int query(int x){
int ret = ;
while(x){
ret += tr[x];
x -= lowbit(x);
}
return ret;
} int main(){
while(~scanf("%d", &n)){
long long ans = , sum1 = , sum2 = ;
for(int i = ; i <= n; i ++) scanf("%d", &in[i]), sar[i] = in[i];
sort(sar + , sar + n + );
m = unique(sar + , sar + n + ) - sar;
for(int i = ; i <= n; i ++)
in[i] = lower_bound(sar + , sar + n + , in[i]) - sar;
memset(tr, , sizeof(tr)); for(int i = ; i <= n; i ++){
ls[i] = query(in[i] - );
lb[i] = query(n) - query(in[i]);
sum1 += ls[i];
sum2 += lb[i];
add(in[i], );
}
for(int i = ; i <= n; i ++){
rs[i] = query(in[i] - );
rb[i] = query(n) - query(in[i]);
add(in[i], -);
}
ans = sum1 * sum2;
for(int i = ; i <= n; i ++){
ans -= 1LL * ls[i] * lb[i];
ans -= 1LL * ls[i] * rs[i];
ans -= 1LL * rb[i] * lb[i];
ans -= 1LL * rs[i] * rb[i];
}
printf("%lld\n",ans); }
return ;
}
05-11 19:46