BZOJ 3236: [Ahoi2013]作业
2016-12-20 20:10
295 查看
3236: [Ahoi2013]作业
Time Limit: 100 Sec Memory Limit: 512 MBSubmit: 1438 Solved: 577
[Submit][Status][Discuss]
Description
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<cmath> //by NeighThorn using namespace std; const int maxn=100000+5,maxm=1000000+5; int n,m,blo,c[maxn],id[maxn],tr[2][maxn],cnt[maxn]; struct M{ int l,r,a,b,num,ans1,ans2; }q[maxm]; inline int read(void){ char ch=getchar();int x=0,f=1; while(!(ch>='0'&&ch<='9')){ if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f; } inline bool cmp1(const M &x,const M &y){ if(id[x.l]==id[y.l]) return x.r<y.r; return x.l<y.l; } inline bool cmp2(const M &x,const M &y){ return x.num<y.num; } inline void insert(int k,int x,int y){ for(;x<=n;x+=x&(-x)) tr[k][x]+=y; } inline int query(int k,int x){ if(x<=0) return 0; int res=0; for(;x;x-=x&(-x)) res+=tr[k][x]; return res; } inline void change(int pos,int x){ insert(0,c[pos],x);cnt[c[pos]]+=x; if(cnt[c[pos]]==0&&x==-1) insert(1,c[pos],-1); else if(cnt[c[pos]]==1&&x==1) insert(1,c[pos],1); } signed main(void){ memset(cnt,0,sizeof(cnt)); memset(tr,0,sizeof(tr)); n=read();m=read();blo=(int)(sqrt(n)); memset(cnt,0,sizeof(cnt)); for(int i=1;i<=n;i++) c[i]=read(),id[i]=(i-1)/blo+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].num=i; sort(q+1,q+m+1,cmp1); for(int i=1,l=q[1].l,r=q[1].l-1;i<=m;i++){ for(;l<q[i].l;l++) change(l,-1); for(;l>q[i].l;l--) change(l-1,1); for(;r<q[i].r;r++) change(r+1,1); for(;r>q[i].r;r--) change(r,-1); q[i].ans1=query(0,q[i].b)-query(0,q[i].a-1); q[i].ans2=query(1,q[i].b)-query(1,q[i].a-1); } sort(q+1,q+m+1,cmp2); for(int i=1;i<=m;i++) printf("%d %d\n",q[i].ans1,q[i].ans2); return 0; }
View Code
by NeighThorn
相关文章推荐
- 树套树专题——bzoj 3110: [Zjoi2013] K大数查询 & 3236 [Ahoi2013] 作业 题解
- BZOJ 3236: [Ahoi2013]作业
- BZOJ_3809_Gty的二逼妹子序列 && BZOJ_3236_[Ahoi2013]作业 _莫队+分块
- BZOJ 3809: Gty的二逼妹子序列 & 3236: [Ahoi2013]作业 [莫队]
- 【BZOJ 3236】 [Ahoi2013]作业
- 【树套树】【bzoj 3236】: [Ahoi2013]作业
- bzoj 3236: [Ahoi2013]作业 莫队算法+分块
- BZOJ3236 [Ahoi2013]作业 【莫队 + 树状数组】
- 【bzoj3236】[Ahoi2013]作业【树套树 线段树套主席树】
- 【莫队算法】【权值分块】bzoj3236 [Ahoi2013]作业
- 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块
- BZOJ3236 [Ahoi2013]作业
- 【AHOI 2013】【BZOJ 3236】作业
- Bzoj 3236: [Ahoi2013]作业 莫队,分块
- AC日记——[Ahoi2013]作业 bzoj 3236
- BZOJ3236: [Ahoi2013]作业 莫队+分块
- [bzoj3236][AHOI2013]作业
- [BZOJ 3236] [Ahoi2013] 作业 && [BZOJ 3809] 【莫队(+分块)】
- BZOJ 3236: [Ahoi2013]作业( 莫队 + BIT )
- 【BZOJ3236】【AHOI2013】作业 线段树 分治 树状数组