[Codeforces 459D] Pashmak and Parmida's problem

[题目链接]

         https://codeforces.com/contest/459/problem/D

[算法]

          首先用std :: map预处理 f(1, i, ai)和f(j, n, aj)

          然后用树状数组计算合法二元组对数 , 即可

          时间复杂度 : O(NlogN)

[代码]

          

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;

int n;
int a[MAXN],va[MAXN],vb[MAXN],c[MAXN];
long long ans;
map<int,int> mp;

inline int lowbit(int x)
{
        return x & (-x); 
}
inline void add(int pos)
{
        for (int i = pos; i <= n; i += lowbit(i)) c[i]++;
}        
inline int query(int pos)
{
        int ret = 0;
        for (int i = pos; i >= 1; i -= lowbit(i)) ret += c[i];
        return ret;
}

template <typename T> inline void read(T &x)
{
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}

int main()
{
        
        read(n);
        for (int i = 1; i <= n; i++) read(a[i]);
        for (int i = 1; i <= n; i++) va[i] = ++mp[a[i]];
        mp.clear();
        for (int i = n; i >= 1; i--) vb[i] = ++mp[a[i]];
        for (int i = n; i >= 1; i--)
        {
                ans += query(va[i] - 1);    
                add(vb[i]);
        }
        printf("%I64d\n",ans);
        
        return 0;
    
}

 

posted @ 2018-09-27 21:51  evenbao  阅读(127)  评论(0编辑  收藏  举报