BZOJ3236: [Ahoi2013]作业(权值分块+莫队)
来源:互联网 发布:经济学书单 知乎 编辑:程序博客网 时间:2024/06/06 03:34
传送门
听说一堆莫队+树状数组过的。。正解不应该是
莫队配合权值分块有奇效。。
#include<bits/stdc++.h>using namespace std;inline int read(){ char ch=getchar();int i=0,f=1; while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch)){i=(i<<1)+(i<<3)+ch-'0';ch=getchar();} return i*f;}inline void W(int x){ static int buf[50]; if(!x){putchar('0');return;} if(x<0){putchar('-');x=-x;} while(x){buf[++buf[0]]=x%10;x/=10;} while(buf[0])putchar(buf[buf[0]--]+'0');}const int Maxn=1e5+50,Maxm=1e6+50,Smax=550;struct Q{ int l,r,a,b,bl,id; friend inline bool operator <(const Q &a,const Q &b){ return a.bl<b.bl||(a.bl==b.bl&&a.r<b.r); }}qry[Maxm];int n,m,a[Maxn],lsh[(Maxm<<1)+Maxn],tot,S_qry,S_val,ans1[Maxm],ans2[Maxm],bg[Maxn],ed[Maxn];int bl_cnt[Smax],bl_ans[Smax],cnt[(Maxm<<1)+Maxn],bl[(Maxm<<1)+Maxn];inline int query(int id,int l,int r){ int ct=0,tt=0; if(r-l+1>=2*S_val){ int L=bl[l]+(bl[l-1]==bl[l]); int R=bl[r]-(bl[r+1]==bl[r]); for(int i=l;i<bg[L];i++)ct+=(cnt[i]>0),tt+=cnt[i]; for(int i=ed[R]+1;i<=r;++i)ct+=(cnt[i]>0),tt+=cnt[i]; for(int i=L;i<=R;i++)ct+=bl_ans[i],tt+=bl_cnt[i]; }else{ for(int i=l;i<=r;i++)ct+=(cnt[i]>0),tt+=cnt[i]; } ans1[id]=tt;ans2[id]=ct;}int main(){ n=read(),m=read();S_qry=sqrt(n)+1; for(int i=1;i<=n;i++)a[i]=read(),lsh[++tot]=a[i]; for(int i=1;i<=m;i++){ qry[i].l=read();qry[i].r=read();qry[i].bl=(qry[i].l-1)/S_qry+1; lsh[++tot]=(qry[i].a=read());lsh[++tot]=(qry[i].b=read()); qry[i].id=i; } sort(qry+1,qry+m+1);sort(lsh+1,lsh+tot+1); tot=unique(lsh+1,lsh+tot+1)-lsh-1;S_val=sqrt(tot)+1; for(int i=1;i<=n;i++)a[i]=lower_bound(lsh+1,lsh+tot+1,a[i])-lsh-1; for(int i=1;i<=m;i++){ qry[i].a=lower_bound(lsh+1,lsh+tot+1,qry[i].a)-lsh-1; qry[i].b=lower_bound(lsh+1,lsh+tot+1,qry[i].b)-lsh-1; } for(int i=1;i<=tot;i++)bl[i]=(i-1)/S_val+1; for(int i=1,h=1;i<=tot;i+=S_val,++h){ bg[h]=i;ed[h]=min(tot,i+S_val-1); } int L=1,R=0; for(int i=1;i<=m;i++){ int l=qry[i].l,r=qry[i].r; while(L>l){ --L; if(!cnt[a[L]]++)++bl_ans[bl[a[L]]]; ++bl_cnt[bl[a[L]]]; } while(L<l){ if(!--cnt[a[L]])--bl_ans[bl[a[L]]]; --bl_cnt[bl[a[L]]]; ++L; } while(R<r){ ++R; if(!cnt[a[R]]++)++bl_ans[bl[a[R]]]; ++bl_cnt[bl[a[R]]]; } while(R>r){ if(!--cnt[a[R]])--bl_ans[bl[a[R]]]; --bl_cnt[bl[a[R]]]; --R; } query(qry[i].id,qry[i].a,qry[i].b); } for(int i=1;i<=m;i++)W(ans1[i]),putchar(' '),W(ans2[i]),putchar('\n');}
阅读全文
0 0
- BZOJ3236: [Ahoi2013]作业(权值分块+莫队)
- [BZOJ3236][Ahoi2013]作业(莫队+分块)
- BZOJ3236: [Ahoi2013]作业 莫队+分块
- 莫队+分块+树状数组 【Ahoi2013】 作业 bzoj3236
- [bzoj3236][Ahoi2013]作业(莫队+树状数组)
- [bzoj3236][AHOI2013]作业
- bzoj 3236: [Ahoi2013]作业 (莫队+分块)
- [ 莫队 树状数组 ] [ AHOI2013 ] BZOJ3236
- Bzoj3236:[Ahoi2013]作业:莫队算法+树状数组
- 【BZOJ3236】【AHOI2013】作业 线段树 分治 树状数组
- bzoj3236 作业 莫队+树状数组
- bzoj 3236: [Ahoi2013]作业 莫队算法+分块
- [AHOI2013]作业
- 【BZOJ】【P3236】【Ahoi2013】【作业】【题解】【莫队】
- bzoj 3236: [Ahoi2013]作业
- 【BZOJ 3236】 [Ahoi2013]作业
- 3236: [Ahoi2013]作业
- [Ahoi2013]作业 解题报告
- 居家生活|装修避免的坑
- 【11.08】一套不太难的题
- hashmap,hashtable区别
- Python几种创建list的方法的效率对比
- 缺陷管理重点知识
- BZOJ3236: [Ahoi2013]作业(权值分块+莫队)
- svn更新,覆盖本地代码,提交
- 传值调用 VS 传址调用 VS 引用调用
- 基于QFtp类的上传、下载客户端
- 20171107-每日一练
- hihocoder 1043(完全背包)
- 当银行不同期限存款的年利率不同时,求从银行得到的利息与本金的合计。
- BIO与NIO、AIO的区别(这个容易理解)
- Kubernetes和Spring Cloud哪个部署微服务更好?