Codeforces Round #261 (Div. 2) D. Pashmak and Parmida ' s problem (tree-like array)

Source: Internet
Author: User
Tags time limit

D. Pashmak and Parmida ' s problem
Time limit per test 3 seconds memory limit per test megabytes input standard input output standard output

Parmida is a clever girl and she wants to participate in Olympiads the year. Of course she wants her partner to be clever too (although he's not)! Parmida have prepared the following test problem for Pashmak.

There is a sequence A that consists of n integers a1, a2, ..., an. Let ' s denote f (l, R, X) the number of indices k such that:l≤k≤r and AK = x. His task was to calculate the number of pairs of indicies I, J (1≤i < j≤n) such that f (1, I, AI) > F (J, N, AJ).

Help Pashmak with the test. Input

The first line of the input contains an integer n (1≤n≤106). The second line contains n space-separated integers a1, A2, ..., an (1≤ai≤109). Output

Print a single integer-the answer to the problem. Sample Test (s) Input

7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0



Parse: F (L, r, x) indicates the number of l≤k≤r and AK = x, to be calculated for F (1, I, AI) > F (J, N, AJ), and I, J (1≤i < j≤n) logarithm.

The first preprocessing is F (1, I, AI) and F (j, N, AJ), and then it is particularly like an inverse number. Just use a tree-shaped array to get a little bit done.




AC Code:

#include <bits/stdc++.h> using namespace std;

const int N = 1000005;

int a[n], f[n], g[n], c[n];

Map<int, int> m;

int lowbit (int x) {return x & x;}

void Add (int x, int d) {for (; x < N; x + = Lowbit (x)) c[x] + = D;}
    int sum (int x) {int ret = 0;
    for (; x > 0; X-= Lowbit (x)) ret + = c[x];
return ret;
    } int main () {#ifdef sxk freopen ("In.txt", "R", stdin);
    #endif//SXK int n;
        while (~SCANF ("%d", &n)) {m.clear ();
            for (int i=1; i<=n; i++) {scanf ("%d", &a[i]);
            m[A[i]] + +;        F[i] = m[A[i]];
        F (1, I, AI)} m.clear ();
            for (int i=n; i>0; i--) {m[A[i]] + +;        G[i] = m[A[i]];
        F (j, N, AJ)} memset (c, 0, sizeof (c));
        A long long ans = 0;
            for (int i=n-1; i>0; i--) {Add (g[i+1], 1);
        Ans + = SUM (F[i]-1);

} printf ("%lld\n", ans);    } return 0;
 }


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.