Topic description
Erwin has recently become interested in something called a "thair". . .
In the sequence a1,a2...an containing n integers,
Three numbers are called "thair" if and only if i<j<k and ai<aj<ak
Find the number of "thair" in a sequence.
Input and output format
Input format:start with a positive integer n,
Next n numbers a1~an.
Output format:number of "thair"
Input and output example
5
1 2 2 3 4
7
illustrate
Explanation for Example 2:
The seven "thair" are
1 2 3 1 2 4 1 2 3 1 2 4 1 3 4 2 3 4 2 3 4 30% of the data n<=100
60% of the data n<=2000
100% of the data n<=30000
Big data is randomly generated
0<=a[i]<=maxlongint
Solution:
This topic is actually quite watery. . .
First discretize, use the tree array to find the number $a_i$ of the number in front of each number that is smaller than it, and then find the number $b_i$ behind each number that is larger than it, then $ans=\sum{ a_i*b_i},i\in[1,n]$.
The output of $ans$ is $ok$ (note that the answer may explode $long\;long$).
Code:
1 // luogu-judger-enable-o2 2 #include<bits/stdc++.h> 3 #define ll long long 4 #define il inline 5 #define lson l,m,rt<<1 6 #define rson m+1,r,rt<<1|1 7 using namespace std; 8 const int N=100005; 9 ll n,m,a[N],tr[N],tot[N]; 10 ll ans; 11 struct node{ 12 int v,id; 13 bool operator < (const node a)const{return v<a.v;} 14 }q[N]; 15 il int gi() 16 { 17 int a=0;char x=getchar();bool f=0; 18 while((x<'0'||x>'9')&&x!='-')x=getchar(); 19 if(x=='-')x=getchar(),f=1; 20 while(x>='0'&&x<='9')a=a*10+x-48,x=getchar(); 21 return f?-a:a; 22 } 23 il void update(int k){while(k<=n){tr[k]++;k+=k&-k;}} 24 il ll query(int k){ll sum=0;while(k){sum+=tr[k];k-=k&-k;}return sum;} 25 int main(){ 26 n=gi(); 27 for(int i=1;i<=n;i++)q[i].v=gi(),q[i].id=i; 28 sort(q+1,q+n+1);q[0].v=-10000000; 29 for(int i=1;i<=n;i++)if(q[i].v!=q[i-1].v)a[q[i].id]=++m;else a[q[i].id]=m; 30 for(int i=1;i<=n;i++)update(a[i]),tot[i]=query(a[i]-1); 31 memset(tr,0,sizeof(tr)); 32 for(int i=n;i>=1;i--){update(a[i]);if(tot[i])ans+=tot[i]*(query(n)-query(a[i]));} 33 cout<<ans; 34 return 0; 35 }