# 题目大意

$$n\leq 50000,m\leq 200000,0\leq a_i \leq 1000000$$

# 分析

• $$l \leq i \leq r$$
• $$a_i$$$$[l,r]$$中最后一次出现（等价于$$a_i$$$$[1,r]$$中最后一次出现）

# Code

#include <cstdio>
#include <cstring>

const int N = 50007, W = 1000007;

int n, q, l, r, tot = 0;

int las[W], root[N], a[N];

struct Tree
{
int lson[N << 5], rson[N << 5], sum[N << 5];

void insert(int &rt, int fa, int l, int r, int po, int val)
{
if (!rt)
rt = ++tot;
sum[rt] = sum[fa] + val;
if (l == r)
return;
int mid = (l + r) >> 1;
if (po <= mid)
rson[rt] = rson[fa], insert(lson[rt], lson[fa], l, mid, po, val);
else
lson[rt] = lson[fa], insert(rson[rt], rson[fa], mid + 1, r, po, val);
}

int qrysum(int rt, int l, int r, int ql, int qr)
{
if (ql <= l && qr >= r)
return sum[rt];
int mid = (l + r) >> 1, ret = 0;
if (ql <= mid)
ret += qrysum(lson[rt], l, mid, ql, qr);
if (mid + 1 <= qr)
ret += qrysum(rson[rt], mid + 1, r, ql, qr);
return ret;
}
} tree;

{
int x = 0, f = 0;
char c = getchar();
for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = 1;
for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + c - '0';
return f ? -x : x;
}

void init()
{
for (int i = 1; i <= n; i++)
{
if (!las[a[i]])
tree.insert(root[i], root[i - 1], 1, n, i, 1);
else
{
tree.insert(root[i], root[i - 1], 1, n, las[a[i]], -1);
tree.insert(root[i], root[i], 1, n, i, 1);
}
las[a[i]] = i;
}
}

void solve()
{
while (q--)
{