题目描述
输入
输出
Solution
普通莫队,对每个区间用树状数组维护权值的前缀和
没有权值的范围,不离散化也能水过
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e6+10;
int blg[maxn],w[maxn],N,tr[maxn],tr1[maxn];
int sum[maxn];
inline int read(){
int ret=0,ff=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-') ff=-ff;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
ret=ret*10+ch-'0';
ch=getchar();
}
return ret*ff;
}
struct X{
int a,b,l,r,id,ans1,ans2;
}q[maxn];
struct cmp{
bool operator()(const X& t1,const X& t2){
return blg[t1.l]^blg[t2.l]?blg[t1.l]<blg[t2.l]:blg[t1.l]&1?t1.r<t2.r:t1.r>t2.r;
}
};
struct cmp2{
bool operator()(const X& t1,const X& t2){
return t1.id<t2.id;
}
};
void update(int x,int f){
int t=w[x];
for(int i=t;i<=N;i+=i&-i) tr[i]+=f;
sum[t]+=f;
if((f==1&&sum[t]==1)||(f==-1&&sum[t]==0)) for(int i=t;i<=N;i+=i&-i) tr1[i]+=f;
return ;
}
int query(int x){
int ret=0;
while(x){
ret+=tr[x];
x-=x&-x;
}
return ret;
}
int Query(int x){
int ret=0;
while(x){
ret+=tr1[x];
x-=x&-x;
}
return ret;
}
int main(){
freopen("bzoj3236.in","r",stdin);
int n=read();
int m=read();
for(int i=1;i<=n;++i) w[i]=read();
for(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,N=max(N,q[i].b);
int block=n/sqrt(m*2/3);
for(int i=1;i<=n;++i) blg[i]=(i-1)/block+1;
sort(q+1,q+1+m,cmp());
int L=q[1].l,R=q[1].l-1;
for(int i=1;i<=m;++i){
while(L<q[i].l) update(L++,-1);
while(L>q[i].l) update(--L,1);
while(R>q[i].r) update(R--,-1);
while(R<q[i].r) update(++R,1);
q[i].ans1=query(q[i].b)-query(q[i].a-1);
q[i].ans2=Query(q[i].b)-Query(q[i].a-1);
}
sort(q+1,q+1+m,cmp2());
for(int i=1;i<=m;++i){
printf("%d %d\n",q[i].ans1,q[i].ans2);
}
return 0;
}