Test instructions
Gives the length of the sequence of N, followed by the sequence A. Find out that ABCD satisfies ABCD non-equal 1<=a<b<c<d<=n while a[a]<a[b],a[c]>a[d], asking such ABCD there are several.
Idea: First ignore four number 22 unequal condition, that is (, reverse order number) multiply (number of sequential pairs). For example {2,4,1,3}, Reverse is {(2,1), (4,1), (4,3)}, the order is {(2,4), (2,3), (1,3)}, so 3*3=9, a total of nine a<b && c>d four tuples. But there are a lot of illegal, for the number of T, the number of illegal cases is (about T in reverse order number x about the number of pairs of T), minus is the result. (illegal rules can be found by writing a few more groups)
Input:
4
2 4 1 3
4
1 2 3 4
Output:
1
0
Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <algorithm>
using namespace Std;
typedef long Long LL;
typedef unsigned long long ull;
#define MM (A, B) memset (A,b,sizeof (a));
#define INF 0x7f7f7f7f
#define for (i,n) for (int i=1;i<=n;i++)
#define CT continue;
#define PF printf
#define SC scanf
const int mod=1000000007;
const int N=1E6+10;
int tmp[n],a[n],n,m,c[n],pos[n];
ll Lmin[n],rmin[n],lmax[n],rmax[n];
int lowbit (int i)
{
Return i& (-i);
}
void Add (int x)
{
while (X<=M)
{
C[x]+=1;
X+=lowbit (x);
}
}
int query (int x)
{
int res=0;
while (x>0)
{
RES+=C[X];
X-=lowbit (x);
}
return res;
}
int main ()
{
while (~SCANF ("%d", &n))
{
for (int i=1;i<=n;i++)
{
scanf ("%d", &tmp[i]);
A[i]=tmp[i];
}
Sort (tmp+1,tmp+n+1);//tmp is sorted from small to large, used for tree array to find shun/reverse pairs
M=unique (tmp+1,tmp+n+1)-tmp-1;//de-weight
for (int i=1;i<=n;i++)
Pos[i]=lower_bound (Tmp+1,tmp+m+1,a[i])-tmp;//Find the original number of I in the position after sorting
MM (c,0);
for (int i=1;i<=n;i++)//query 0-i-1 than A[i] small number in lmin[i], between i-m than A[i] put in Lmax
{
Lmin[i]=query (pos[i]-1);//
Lmax[i]=query (M)-query (Pos[i]);
Add (Pos[i]);
}
MM (c,0);
for (int i=n;i>=1;i--)//Reverse One More time
{
Rmin[i]=query (pos[i]-1);
Rmax[i]=query (M)-query (Pos[i]);
Add (Pos[i]);
}
ll ans=0,l=0,r=0;
for (int i=1;i<=n;i++) {r+=rmax[i],l+=lmax[i];};
Ans=l*r;
for (int i=1;i<=n;i++)//Remove illegal
{
Ans-=lmin[i]*rmin[i];
Ans-=lmax[i]*rmax[i];
Ans-=lmax[i]*lmin[i];
Ans-=rmax[i]*rmin[i];
}
printf ("%lld\n", ans);
}
return 0;
}
HDU 5792 World is exploding