Parmidaisaclevergirlandshewantstoparticipateinolympus iadsthisyear. ofcourseshewantshersherertobeclevertoo (althoughhesnot )! ParmidahaspreparedthefollowingtestproblemforPashmak. Thereisasequenceathatcon
Parmida is a clever girl and she wants to particle in Olympus Iads this year. Of course she wants her partner to be clever too (although he's not )! Parmida has prepared the following test problem for Pashmak. There is a sequence a that con
Parmida is a clever girl and she wants to particle in Olympus Iads this year. Of course she wants her partner to be clever too (although he's not )! Parmida has prepared the following test problem for Pashmak.
There is a sequenceAThat consistsNIntegersA1 ,?A2 ,?...,?AN. Let's denoteF(L,?R,?X) The number of indicesKSuch that:L? ≤?K? ≤?RAndAK? =?X. His task is to calculate the number of pairs of indiciesI,?J(1? ≤?I? J? ≤?N) Such thatF(1 ,?I,?AI)?>?F(J,?N,?AJ).
Help Pashmak with the test.
Input
The first line of the input contains an integerN(1? ≤?N? ≤? (106). The second line containsNSpace-separated integersA1 ,?A2 ,?...,?AN(1? ≤?AI? ≤? 109 ).
Output
Print a single integer-the answer to the problem.
Sample test (s)
Input
71 2 1 1 2 2 1
Output
8
Input
31 1 1
Output
1
Input
51 2 3 4 5
Output
0
Because the original number is too large, you can use map ing, so you do not need to discretization ~ Two Arrays: the number of equal numbers before a record and the number of equal numbers after a record ~
The question is converted into a number pair in reverse order, a tree array. Long
CODE:
#include
#include
#include #include
#include
#include
#include
#include
#include
#include
#include
const int inf=0xfffffff;typedef long long ll;using namespace std;const int Max=1000006;int num[Max];int f1[Max],f2[Max];int c[Max];map
m1,m2;int n;int lowbit(int x){ return x&-x;}void add(int x,int d){ while(x<=n){ c[x]+=d; x+=lowbit(x); }}int sum(int x){ int ans=0; while(x>0){ ans+=c[x]; x-=lowbit(x); } return ans;}int main(){ //freopen("in","r",stdin); while(~scanf("%d",&n)){ m1.clear(); m2.clear(); memset(f1,0,sizeof(f1)); memset(f2,0,sizeof(f2)); memset(c,0,sizeof(c)); for(int i=1;i<=n;i++){ scanf("%d",&num[i]); } for(int i=1;i<=n;i++){ f1[i]=(++m1[num[i]]); } for(int i=n;i>=1;i--){ f2[i]=(++m2[num[i]]); } ll ans=0; for(int i=n-1;i>=1;i--){ add(f2[i+1],1); ans+=(ll)sum(f1[i]-1); } printf("%I64d\n",ans); } return 0;}