询问多了10倍,但还能跑过(smog
#include <bits/stdc++.h>
#define N 100005
#define M 1000005
//#define getchar nc
using namespace std;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
register int x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*f;
}
inline void write(register int x)
{
if(!x)putchar('0');if(x<0)x=-x,putchar('-');
static int sta[20];register int tot=0;
while(x)sta[tot++]=x%10,x/=10;
while(tot)putchar(sta[--tot]+48);
}
int n,m,blocksize=0,ans[M],v[N],p[N];
struct BinaryIndexTree{
int tr[N];
inline void update(register int pos,register int x)
{
for(register int i=pos;i<=n;i+=i&(-i))
tr[i]+=x;
}
inline int query(register int pos)
{
int res=0;
for(register int i=pos;i;i-=i&(-i))
res+=tr[i];
return res;
}
}tr;
struct query{
int l,r,a,b,id,bl;
}q[M];
inline bool cmp(register query a,register query b)
{
return a.bl!=b.bl?a.l<b.l:((a.bl&1)?a.r<b.r:a.r>b.r);
}
inline void add(register int x)
{
if(++p[v[x]]==1)
tr.update(v[x],1);
}
inline void del(register int x)
{
if(--p[v[x]]==0)
tr.update(v[x],-1);
}
int main()
{
n=read(),m=read();
blocksize=sqrt(n);
for(register int i=1;i<=n;++i)
v[i]=read();
for(register int i=1;i<=m;++i)
q[i].l=read(),q[i].r=read(),q[i].a=read(),q[i].b=read(),q[i].id=i,q[i].bl=(q[i].l-1)/blocksize+1;
sort(q+1,q+1+m,cmp);
int l=1,r=0;
for(register int i=1;i<=m;++i)
{
int ll=q[i].l,rr=q[i].r;
while(l>ll)
add(--l);
while(l<ll)
del(l++);
while(r<rr)
add(++r);
while(r>rr)
del(r--);
ans[q[i].id]=tr.query(q[i].b)-tr.query(q[i].a-1);
}
for(register int i=1;i<=m;++i)
write(ans[i]),puts("");
return 0;
}