只有44分???越卡常越慢了就离谱
#include<bits/stdc++.h>
using namespace std;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<23],*p1=buf,*p2=buf;
inline int read1(int &res)
{
res=0;
char ch=getchar();
while(ch<'0'||ch>'9')
{
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
res=(res<<1)+(res<<3)+(ch^48),ch=getchar();
}
return res;
}
inline void write(int X)
{
if(X>9)
{
write(X/10);
}
putchar(X%10+'0');
}
const int maxn=1e6+11;
int n,m;
int a[maxn],belong[maxn],_size=2035,ans[maxn];
struct node
{
int l,r,id;
};
node nodes[maxn];
bool cmp(node a,node b)
{
return a.l==b.l?(a.l&1)?a.r<b.r:a.r>b.r:a.l<b.l;
}
int nowans=0;
int flag[maxn];
inline void add(int x)
{
nowans+=++flag[a[x]]==1;
}
inline void del(int x)
{
nowans-=--flag[a[x]]==0;
}
signed main()
{
read1(n);
for(register int i=1; i<=n; i++)
{
read1(a[i]);
belong[i]=i/_size;
}
read1(m);
for(register int i=1; i<=m; i++)
{
read1(nodes[i].l);
read1(nodes[i].r);
nodes[i].id=i;
}
sort(nodes+1,nodes+m+1,cmp);
register int nowl=1,nowr=0;
for(register int i=1; i<=m; i++)
{
while(nodes[i].l<nowl)
{
nowl--;
add(nowl);
}
while(nodes[i].l>nowl)
{
del(nowl);
nowl++;
}
while(nodes[i].r>nowr)
{
nowr++;
add(nowr);
}
while(nodes[i].r<nowr)
{
del(nowr);
nowr--;
}
ans[nodes[i].id]=nowans;
}
for(register int i=1; i<=m; i++)
{
write(ans[i]);
putchar(10);
}
}