Tree array Codeforces Round #261 (Div. 2) D

Source: Internet
Author: User

Tree array Codeforces Round #261 (Div. 2) D

D. Pashmak and Parmida's problemtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

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


This is similar to reverse order. preprocessing is required first.


#include
 
  #include
  
   #include
   
    #include
    #include
     
      #include#include
      
       using namespace std;typedef long long LL;#define maxn (int)1e6+5int A[maxn],c[maxn],b[maxn],s[maxn];int n;void add(int x,int d){ while(x<=n) { s[x]+=d; x+=(x&-x); }}LL Query(int x){ LL sum=0; while(x>0) { sum+=s[x]; x-=(x&-x); } return sum;}typedef long long LL;int main(){ while(~scanf("%d",&n)) { map
       
         q; for(int i=1;i<=n;i++) { scanf("%d",A+i); q[A[i]]++; b[i]=q[A[i]]; } q.clear(); memset(s,0,sizeof(s)); for(int i=n;i>=2;i--) { q[A[i]]++; c[i]=q[A[i]]; add(c[i],1); } LL ans=0; for(int i=1;i
        
         


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.