BZOJ3236: [Ahoi2013]作业 莫队+分块
2016-08-22 09:10
330 查看
3236: [Ahoi2013]作业
Time Limit: 100 Sec Memory Limit: 512 MBSubmit: 1272 Solved: 490
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
3 41 2 2
1 2 1 3
1 2 1 1
1 3 1 3
2 3 2 3
Sample Output
2 21 1
3 2
2 1
HINT
N=100000,M=1000000题解:
莫队+分块 时间复杂度O(n√n)
#include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define N 100010 #define M 1000010 using namespace std; int a ,p; struct pp {int l,r,a,b,num;}b[M]; int bl ,L[1001],R[1001]; int tot,ans ,ansv[M],ansn[M]; int tn[1001],tv[1001]; bool cmp(pp x,pp y) { if(bl[x.l]!=bl[y.l]) return bl[x.l]<bl[y.l]; return x.r<y.r; } void add(int x) { ans[x]++; tv[bl[x]]++; if(ans[x]==1) tn[bl[x]]++; } void del(int x) { ans[x]--; tv[bl[x]]--; if(ans[x]==0) tn[bl[x]]--; } int Query_value(int a,int b) { int ka=bl[a],kb=bl[b]; int ret=0; for(int i=ka+1;i<=kb-1;i++) ret+=tv[i]; if(ka==kb) { for(int i=a;i<=b;i++) ret+=ans[i]; } else { for(int i=a;i<=R[ka];i++) ret+=ans[i]; for(int i=L[kb];i<=b;i++) ret+=ans[i]; } return ret; } int Query_num(int a,int b) { int ka=bl[a],kb=bl[b]; int ret=0; for(int i=ka+1;i<=kb-1;i++) ret+=tn[i]; if(ka==kb) { for(int i=a;i<=b;i++) if(ans[i]) ret++; } else { for(int i=a;i<=R[ka];i++) if(ans[i]) ret++; for(int i=L[kb];i<=b;i++) if(ans[i]) ret++; } return ret; } int n,m,k; int main() { scanf("%d%d",&n,&m); int block=sqrt(n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) bl[i]=(i-1)/block+1; for(int i=1;i<=n;i++) { R[bl[i]]=i; if(!L[bl[i]]) L[bl[i]]=i; } for(int i=1;i<=m;i++) { scanf("%d%d%d%d",&b[i].l,&b[i].r,&b[i].a,&b[i].b); b[i].num=i; } sort(b+1,b+m+1,cmp); int l=1,r=0; for(int i=1;i<=m;i++) { for(;r<b[i].r;r++) add(a[r+1]); for(;r>b[i].r;r--) del(a[r]); for(;l<b[i].l;l++) del(a[l]); for(;l>b[i].l;l--) add(a[l-1]); ansv[b[i].num]=Query_value(b[i].a,b[i].b); ansn[b[i].num]=Query_num(b[i].a,b[i].b); } for(int i=1;i<=m;i++) printf("%d %d\n",ansv[i],ansn[i]); }
相关文章推荐
- [BZOJ3236][Ahoi2013]作业(莫队+分块)
- bzoj 3236: [Ahoi2013]作业 (莫队+分块)
- [BZOJ 3236] [Ahoi2013] 作业 && [BZOJ 3809] 【莫队(+分块)】
- Bzoj 3236: [Ahoi2013]作业 莫队,分块
- 【莫队算法】【权值分块】bzoj3236 [Ahoi2013]作业
- 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块
- bzoj 3236: [Ahoi2013]作业 莫队算法+分块
- BZOJ_3809_Gty的二逼妹子序列 && BZOJ_3236_[Ahoi2013]作业 _莫队+分块
- BZOJ3236 [Ahoi2013]作业 【莫队 + 树状数组】
- BZOJ 3236: [Ahoi2013]作业|莫队算法|树状数组
- [bzoj3236][Ahoi2013]作业(莫队+树状数组)
- [BZOJ3236][Ahoi2013]作业(莫队+树状数组)
- BZOJ 3809: Gty的二逼妹子序列 & 3236: [Ahoi2013]作业 [莫队]
- BZOJ 3236: [Ahoi2013]作业( 莫队 + BIT )
- [ 莫队 树状数组 ] [ AHOI2013 ] BZOJ3236
- 莫队+分块+树状数组 【Ahoi2013】 作业 bzoj3236
- BZOJ 3236 AHOI 2013 作业 莫队算法
- bzoj 3236: [Ahoi2013]作业
- COGS.1822.[AHOI2013]作业(莫队 树状数组/分块)
- 【树套树】【bzoj 3236】: [Ahoi2013]作业