Cod sursa(job #3319214)

Utilizator dansiminaSimina Dan Marius dansimina Data 31 octombrie 2025 11:36:20
Problema P-sir Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.83 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("psir.in");
ofstream fout ("psir.out");

struct elem
{
    int val, id;
};

struct cmp
{
    bool operator()(const elem& x, const elem& y) const
    {
        if (x.val != y.val)
            return x.val < y.val;
        return x.id < y.id;
    }
};

int sir[2009], normalizare[2009];
map<pair<int, int>, int> currAns;
map<int, int> freq;
set<elem, cmp> st;
multiset<int> dr;

int main()
{
    int ans = 0;
    int n;
    int nn = 0;
    fin >> n;
    for (int i = 1; i <= n; i++)
    {
        fin >> sir[i];
        if (!freq.count(sir[i]))
        {
            nn++;
            normalizare[nn] = sir[i];
        }
        dr.insert(sir[i]);
        freq[sir[i]]++;
    }

    sort(normalizare + 1, normalizare + nn + 1);

    dr.extract(sir[1]);
    st.insert({sir[1], 1});

    for (int i = 2; i <= n; i++)
    {
        dr.extract(sir[i]);
        int ansj[2009];
        memset(ansj, 0, sizeof(ansj));

        int poz = lower_bound(normalizare + 1, normalizare + nn + 1, sir[i]) - normalizare;

        int goodDr = 0;
        for (int j = poz; j >= 1; j--)
        {
            int curr = normalizare[j];
            int nrSt = 0;

            auto start = st.lower_bound({curr, 0});
            auto end = st.upper_bound({curr, int(1e9)});

            int otherSt = 0;
            for (auto it = start; it != end; it++)
            {
                nrSt++;
                otherSt += currAns[{sir[i], it -> id}];
            }

            int nrDr = dr.count(curr);

            ans += nrSt;
            ans += (nrSt + otherSt) * goodDr;

            ansj[j + 1] = nrSt;
            goodDr += nrDr;
        }

        ansj[poz] = 0;
        for (int j = poz - 1; j >= 1; j--)
        {
            ansj[j] += ansj[j + 1];
            currAns[{normalizare[j], i}] += ansj[j];
        }

        st.insert({sir[i], i});











        memset(ansj, 0, sizeof(ansj));

        poz = upper_bound(normalizare + 1, normalizare + nn + 1, sir[i]) - normalizare;

        goodDr = 0;
        for (int j = poz; j <= nn; j++)
        {
            int curr = normalizare[j];
            int nrSt = 0;

            auto start = st.lower_bound({curr, 0});
            auto end = st.upper_bound({curr, int(1e9)});

            int otherSt = 0;
            for (auto it = start; it != end; it++)
            {
                nrSt++;
                otherSt += currAns[{sir[i], it -> id}];
            }

            int nrDr = dr.count(curr);

            ans += nrSt;
            ans += (nrSt + otherSt) * goodDr;

            ansj[j - 1] = nrSt;
            goodDr += nrDr;
        }

        ansj[poz] = 0;
        for (int j = poz + 1; j <= nn; j++)
        {
            ansj[j] += ansj[j - 1];
            currAns[{normalizare[j], i}] += ansj[j];
        }

        st.insert({sir[i], i});
    }
    fout << ans;
    return 0;
}