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