Cod sursa(job #2644155)

Utilizator popoviciAna16Popovici Ana popoviciAna16 Data 23 august 2020 16:45:38
Problema Distincte Scor 35
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.71 kb
#include <fstream>
#include <algorithm>
using namespace std;

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

struct intr
{
    int st, dr, ind;
    long long rasp;
} q[100001];

bool comp1(intr a, intr b)
{
    return a.dr < b.dr;
}

bool comp2(intr a, intr b)
{
    return a.ind < b.ind;
}

int v[100001], it[100001];

long long a[400001];

int qst, qdr, poz, val;

void update(int nod, int st, int dr)
{
    if (st == dr)
        a[nod] = val;
    else
    {
        int mij = (st+dr)>>1;
        if (poz <= mij)
            update(nod<<1, st, mij);
        else
            update(nod<<1|1, mij+1, dr);
        a[nod] = a[nod<<1] + a[nod<<1|1];
    }
}

long long query(int nod, int st, int dr)
{
    if (qst <= st && dr <= qdr)
        return a[nod];
    int mij = (st+dr)>>1;
    long long eins, zwei;
    eins = zwei = 0;
    if (qst <= mij)
        eins = query(nod<<1, st, mij);
    if (mij < qdr)
        zwei = query(nod<<1|1, mij+1, dr);
    return eins + zwei;
}

int main()
{
    int n, m, i, k, j;
    fin >> n >> k >> m;
    for (i = 1; i<=n; i++)
        fin >> v[i];
    for (i = 1; i<=m; i++)
    {
        fin >> q[i].st >> q[i].dr;
        q[i].ind = i;
    }
    sort(q+1, q+m+1, comp1);
    for (j = 1, i = 1; i<=n; i++)
    {
        if (it[v[i]])
        {
            val = 0;
            poz = it[v[i]];
            update(1, 1, n);
        }
        
        val = v[i];
        poz = i;
        update(1, 1, n);
        
        it[v[i]] = qdr = i;
        while (j <= m && q[j].dr == i)
        {
            qst = q[j].st;
            q[j].rasp = query(1, 1, n);
            j++;
        }
    }
    sort(q+1, q+m+1, comp2);
    for (i = 1; i<=m; i++)
        fout << q[i].rasp << '\n';
    return 0;
}