莫队+树状数组 AHOI 作业
3236: [Ahoi2013]作业
Time Limit: 100 Sec Memory Limit: 512 MB
Submit: 1716 Solved: 690
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
3 4
1 2 2
1 2 1 3
1 2 1 1
1 3 1 3
2 3 2 3
Sample Output
2 2
1 1
3 2
2 1
HINT
N=100000,M=1000000
Source
By wangyisong1996加强数据
[Submit][Status][Discuss]
可以用莫队没问题,但裸的莫队超慢。。可以考虑套树状数组。
维护两个树状数组,一个存以当前全职为下标,等于它的个数,一个存当前下标是否有值。按套路维护一下就行了。
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#define N 100005
#define M 1000005
using namespace std;
inline int read()
{
int sum=0,f=1;char x=getchar();
while(x<'0'||x>'9'){if(x=='-')f=-1;x=getchar();}
while(x>='0'&&x<='9'){sum=(sum<<3)+(sum<<1)+x-'0';x=getchar();}
return sum*f;
}
int n,m,a[N],ans1[M],ans2[M],t1[N],t2[N],h,kuai[N],tot,sum[N];
struct Q{int l,r,id,a,b;}q[M];
inline bool cmp(Q a,Q b){return (kuai[a.l]!=kuai[b.l])? kuai[a.l]<kuai[b.l]:a.r<b.r;}
inline int lowbit(int x){return x&(-x);}
inline int get1(int x){int s=0;for(int i=x;i>0;i-=lowbit(i))s+=t1[i];return s;}
inline int get2(int x){int s=0;for(int i=x;i>0;i-=lowbit(i))s+=t2[i];return s;}
inline void add1(int x,int k){for(int i=x;i<=n;i+=lowbit(i))t1[i]+=k;}
inline void add2(int x,int k){for(int i=x;i<=n;i+=lowbit(i))t2[i]+=k;}
int yjn()
{
n=read();m=read();h=int(sqrt(n)+log(2*n))*log(2);int x;
for(int i=1;i<=n;i++)a[i]=read(),kuai[i]=(i-1)/h+1;
sum[a[1]]++;add1(a[1],1);add2(a[1],1);
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;
}
sort(q+1,q+m+1,cmp);
int l=1,r=1;
for(int i=1;i<=m;i++)
{
for(;r<q[i].r;r++){add1(a[r+1],1);if(++sum[a[r+1]]==1)add2(a[r+1],1);}
for(;r>q[i].r;r--){add1(a[r],-1);if(--sum[a[r]]==0)add2(a[r],-1);}
for(;l<q[i].l;l++){add1(a[l],-1);if(--sum[a[l]]==0)add2(a[l],-1);}
for(;l>q[i].l;l--){add1(a[l-1],1);if(++sum[a[l-1]]==1)add2(a[l-1],1);}
ans1[q[i].id]=get1(q[i].b)-get1(q[i].a-1);
ans2[q[i].id]=get2(q[i].b)-get2(q[i].a-1);
}
for(int i=1;i<=m;i++)printf("%d %d\n",ans1[i],ans2[i]);
}
int qty=yjn();
int main(){;}