题意:给定 n 个数,让你数出 a < b && c < d && a != b != c != d && Aa < Ab && Ac > Ad。
析:首先,给的数太大了,先要进行离散化处理,然后先算出Aa < Ab 和 Ac > Ad。这可以用树状数组解决,一个正向的,一个反向,同时再求出四种数,然后减去,就好了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include <bitset> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a, b, sizeof a) //#define sz size() #define pu push_up #define pd push_down #define cl clear() #define all 1,n,1 #define FOR(x,n) for(int i = (x); i < (n); ++i) #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e15; const double inf = 1e20; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 5e4 + 100; const int mod = 7; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c) { return r >= 0 && r < n && c >= 0 && c < m; } vector<int> v; int a[maxn]; int sum[2][maxn<<1]; int lowbit(int x){ return -x&x; } void add(int pos, int x){ while(x <= n){ ++sum[pos][x]; x += lowbit(x); } } int query(int pos, int x){ int ans = 0; while(x){ ans += sum[pos][x]; x -= lowbit(x); } return ans; } int getPos(int x){ return lower_bound(v.begin(), v.end(), x) - v.begin(); } int b[maxn], c[maxn], d[maxn], e[maxn]; int main(){ while(scanf("%d", &n) == 1){ v.cl; ms(sum, 0); v.pb(-1); for(int i = 0; i < n; ++i){ scanf("%d", a+i); v.pb(a[i]); } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); LL ans = 0; LL tmp1 = 0, tmp2 = 0; LL tmp3 = 0, tmp4 = 0, tmp5 = 0; for(int i = 0; i < n; ++i){ int pos = getPos(a[i]); b[i] = query(0, pos-1); tmp1 += b[i]; // min c[i] = i - query(0, pos); tmp3 += (LL)b[i] * c[i]; add(0, pos); } for(int i = n-1; i >= 0; --i){ int pos = getPos(a[i]); d[i] = query(1, pos-1); tmp2 += d[i]; //right min e[i] = (n-i-1) - query(1, pos); tmp4 += (LL)d[i] * e[i]; //tmp4 += (n-i-1) - query(1, pos); // right max add(1, pos); } LL tmp6 = 0; for(int i = 0; i < n; ++i){ tmp5 += (LL)c[i] * e[i]; tmp6 += (LL)b[i] * d[i]; } ans = tmp1 * tmp2 - tmp3 - tmp4 - tmp5 - tmp6; printf("%I64d ", ans); } return 0; }