# BZOJ 3236: [Ahoi2013]作业(莫队+树状数组)

## 解题思路

莫队+树状数组。把求$$[a,b]$$搞成前缀和形式，剩下的比较裸吧，用$$cnt$$记一下数字出现次数。时间复杂度$$O(msqrt(n)log(n)$$，莫名其妙过了。

## 代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

using namespace std;
const int MAXM = 1000005;
const int MAXN = 100005;

inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) f=ch=='-'?0:1,ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return f?x:-x;
}

int n,m,ans[MAXM][2],a[MAXN],f[MAXM*3],g[MAXM*3],siz,cnt[MAXM*3];
int cpy[MAXM*3],num;

struct Data{
int l,r,a,b,id;
friend bool operator<(Data A,Data B){
if(A.l/siz!=B.l/siz) return A.l<B.l;
if((A.l/siz)&1) return A.r>B.r;
return A.r<B.r;
}
}data[MAXM];

for(;x<=n;x+=x&-x) f[x]+=k;
}
for(;x<=n;x+=x&-x) g[x]+=k;
}
inline int query1(int x){
if(x<1) return 0;int ret=0;
for(;x;x-=x&-x) ret+=f[x];
return ret;
}
inline int query2(int x){
if(x<1) return 0;int ret=0;
for(;x;x-=x&-x) ret+=g[x];
return ret;
}

int main(){
n=rd(),m=rd();siz=sqrt(n)+1;
for(int i=1;i<=n;i++) a[i]=rd(),cpy[++num]=a[i];
for(int i=1;i<=m;i++){
data[i].l=rd(),data[i].r=rd(),data[i].a=rd();
data[i].b=rd(),data[i].id=i;cpy[++num]=data[i].a;
cpy[++num]=data[i].b;
}
sort(cpy+1,cpy+1+num);int u=unique(cpy+1,cpy+1+num)-cpy-1;
for(int i=1;i<=n;i++) a[i]=lower_bound(cpy+1,cpy+1+u,a[i])-cpy;
for(int i=1;i<=m;i++){
data[i].a=lower_bound(cpy+1,cpy+1+u,data[i].a)-cpy;
data[i].b=lower_bound(cpy+1,cpy+1+u,data[i].b)-cpy;
}
sort(data+1,data+1+m);
int L=1,R=0,l,r;
for(int i=1;i<=m;i++){
l=data[i].l,r=data[i].r;