Problem Description
Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies:
a≠b≠c≠d,1≤a<b≤n,1≤c<d≤n,Aa<Ab,Ac>Ad
.
Input
The input consists of multiple test cases.
Each test case begin with an integer n in a single line.
The next line contains n integers A1,A2⋯An .
1≤n≤50000
0≤Ai≤1e9
Each test case begin with an integer n in a single line.
The next line contains n integers A1,A2⋯An .
1≤n≤50000
0≤Ai≤1e9
Output
For each test case,output a line contains an integer.
Sample Input
4 2 4 1 3 4 1 2 3 4
Sample Output
1 0
思路:算出总的,减去不合法的
#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
int n;
struct BIT
{
double c[N+20];
BIT()
{
memset(c , 0 , sizeof(c));
}
void init()
{
for(int i = 0 ; i <= n ; ++i) c[i] = 0;
}
int lowbit(int x)
{
return x & (-x);
}
void update(int pos , int x)
{
while(pos <= n)
{
c[pos] += x;
pos += lowbit(pos);
}
}
int sum(int pos)
{
int ans = 0 ;
while(pos)
{
ans += c[pos];
pos -= lowbit(pos);
}
return ans;
}
} t;
int a[N];
int pos[N];
int big[N];
int small[N];
int x[N];
int b[N],c[N];
int main()
{
while(~scanf("%d",&n))
{
t.init();
memset(pos, 0, sizeof(pos));
for(int i = 1; i <= n; ++i)
{
scanf("%d",&a[i]);
// pos[a[i]] = i;
x[i] = a[i];
}
sort(x + 1 , x + 1 + n);
for(int i = 1 ; i <= n ; ++i)
{
pos[i] = lower_bound(x + 1 , x + n + 1 , a[i]) - (x + 1) + 1;
//cerr << pos[i] << endl;
a[i] = pos[i];
b[a[i]] = c[a[i]] = 0;
}
int now;
long long sum1 = 0, sum2 = 0;
for(int i = 1; i <= n; ++i)
{
now = t.sum(a[i] - 1);
t.update(a[i], 1);
big[i] = now;
small[i] = i - now - 1 - b[a[i]];
sum1 += now;
b[a[i]]++;
}
t.init();
for(int i = n; i >= 1; --i)
{
now = t.sum(a[i] - 1);
t.update(a[i], 1);
small[i] += now;
big[i] += (n -i +1) - now - 1 -c[a[i]];
sum2 += now;
c[a[i]]++;
}
long long ans = sum1 * sum2;
for(int i = 1; i <= n; ++i)
{
//cout << "num: " << i << endl;
//cout << big[i] << ' ' << small[i] << endl;
ans -= (big[i] * small[i]);
}
printf("%I64d\n",ans);
//cout << sum1 <<' ' <<sum2 << endl;
//for()
}
return 0;
}
Problem Description
Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies:
a≠b≠c≠d,1≤a<b≤n,1≤c<d≤n,Aa<Ab,Ac>Ad
.
Input
The input consists of multiple test cases.
Each test case begin with an integer n in a single line.
The next line contains n integers A1,A2⋯An .
1≤n≤50000
0≤Ai≤1e9
Each test case begin with an integer n in a single line.
The next line contains n integers A1,A2⋯An .
1≤n≤50000
0≤Ai≤1e9
Output
For each test case,output a line contains an integer.
Sample Input
4 2 4 1 3 4 1 2 3 4
Sample Output
1 0