Code
#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
using namespace std;
namespace inout
{
const int S = 1 << 20;
char frd[S], *ihed = frd + S;
const char *ital = ihed;
inline char inChar()
{
if (ihed == ital)
fread(frd, 1, S, stdin), ihed = frd;
return *ihed++;
}
inline int get()
{
char ch; int res = 0; bool flag = false;
while (!isdigit(ch = inChar()) && ch != '-');
(ch == '-' ? flag = true : res = ch ^ 48);
while (isdigit(ch = inChar()))
res = res * 10 + ch - 48;
return flag ? -res : res;
}
char fwt[S], *ohed = fwt;
const char *otal = ohed + S;
inline char outChar(char ch)
{
if (ohed == otal)
fwrite(fwt, 1, S, stdout), ohed = fwt;
return *ohed++ = ch;
}
inline void put(int x)
{
if (x > 9) put(x / 10);
outChar(x % 10 + 48);
}
};
using namespace inout;
const int N = 2e6 + 5;
int n, C, m;
int suf[N], low[N], a[N], b[N], c[N], Ans[N];
struct node
{
int l, r, z;
bool operator < (const node &x) const
{
return l < x.l;
}
}p[N];
inline void Modify(int x, int y)
{
for (; x <= n; x += low[x]) b[x] += y;
}
inline int Query(int x)
{
int res = 0;
for (; x; x ^= low[x]) res += b[x];
return res;
}
int main()
{
n = get(); C = get(); m = get();
for (int i = 1; i <= n; ++i)
a[i] = get(), low[i] = i & -i;
for (int i = n; i >= 1; --i)
suf[i] = c[a[i]], c[a[i]] = i;
for (int i = 1; i <= m; ++i)
p[i].l = get(), p[i].r = get(), p[i].z = i;
sort(p + 1, p + m + 1);
for (int i = 1; i <= C; ++i)
if (c[i] && suf[c[i]]) Modify(suf[c[i]], 1);
int j = 1;
for (int i = 1; i <= m; ++i)
{
for (; j < p[i].l; ++j)
{
if (suf[j]) Modify(suf[j], -1);
if (suf[j] && suf[suf[j]]) Modify(suf[suf[j]], 1);
}
Ans[p[i].z] = Query(p[i].r) - Query(p[i].l - 1);
}
for (int i = 1; i <= m; ++i)
put(Ans[i]), outChar('\n');
fwrite(fwt, 1, ohed - fwt, stdout);
return 0;
}