Pagini recente » Cod sursa (job #190156) | Cod sursa (job #1550115) | Cod sursa (job #2906914) | Cod sursa (job #1486618) | Cod sursa (job #2715546)
#include <bits/stdc++.h>
using namespace std;
ifstream f("distincte.in");
ofstream g("distincte.out");
int n, m, vmax, v[100001], cnt[100001];
struct query
{
int st, dr, p;
} q[100001];
void add(int);
void remove(int);
long long rez, af[100001];
void change(int, int, int, int);
int main()
{
f >> n >> vmax >> m;
int dim = sqrt(n);
for (int i = 1; i <= n; i++)
f >> v[i];
for (int i = 1; i <= m; i++)
f >> q[i].st >> q[i].dr, q[i].p = i;
sort(q + 1, q + m + 1, [dim](query a, query b) {
int ax = a.st / dim, ay = b.st / dim;
if (ax != ay)
return ax < ay;
if (ax & 1)
return a.dr < b.dr;
return a.dr > b.dr;
});
int st = 1, dr = 1;
rez = v[1];
cnt[v[1]] = 1;
for (int i = 1; i <= m; i++)
{
int nst = q[i].st, ndr = q[i].dr;
change(st, dr, nst, ndr);
st = nst;
dr = ndr;
af[q[i].p] = rez;
}
for (int i = 1; i <= m; i++)
g << af[i] << '\n';
return 0;
}
void add(int x)
{
cnt[x]++;
if (cnt[x] == 1)
rez += x;
}
void remove(int x)
{
cnt[x]--;
if (cnt[x] == 0)
rez -= x;
}
void change(int st, int dr, int nst, int ndr)
{
if (dr <= nst || st >= ndr)
{
for (int i = st; i <= dr; i++)
remove(v[i]);
for (int i = nst; i <= ndr; i++)
add(v[i]);
return;
}
if (st < nst)
while (st < nst)
remove(v[st]), st++;
else
while (st - 1 >= nst)
add(v[st - 1]), st--;
if (dr < ndr)
while (dr + 1 <= ndr)
add(v[dr + 1]), dr++;
else
while (dr > ndr)
remove(v[dr]), dr--;
}