【莫队算法】【权值分块】bzoj3585 mex
2015-03-10 17:31
302 查看
orz PoPoQQQ。
本来蒟蒻以为这种离散化以后就对应不起来的题不能权值分块搞的说。
……结果,实际上>n的权值不会对答案作出贡献。
本来蒟蒻以为这种离散化以后就对应不起来的题不能权值分块搞的说。
……结果,实际上>n的权值不会对答案作出贡献。
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define N 200002 #define BN 452 int n,m,num ,a ,l[BN],size[BN],anss ,b ,sumv[BN]; struct ASK{int l,r,p;}Q ; bool operator < (const ASK &a,const ASK &b) {return num[a.l]!=num[b.l]?num[a.l]<num[b.l]:a.r<b.r;} void Insert(const int &x){if(x<=n){if(!b[x])++sumv[num[x]];++b[x];}} void Delete(const int &x){if(x<=n){--b[x];if(!b[x])--sumv[num[x]];}} int Query(){for(int i=1;;++i) if(sumv[i]<size[i]) for(int j=l[i];;++j) if(!b[j]) return j;} int main() { scanf("%d%d",&n,&m); int sz=sqrt(n),blo=0; if(!sz) sz=1; for(int i=1;i<=n;++i) { scanf("%d",&a[i]); if(i%sz==1||sz==1) l[++blo]=i; num[i]=blo; ++size[blo]; } l[1]=0; num[0]=1; ++size[1]; for(int i=1;i<=m;++i) {scanf("%d%d",&Q[i].l,&Q[i].r); Q[i].p=i;} sort(Q+1,Q+m+1); for(int i=Q[1].l;i<=Q[1].r;++i) Insert(a[i]); anss[Q[1].p]=Query(); for(int i=2;i<=m;++i) { if(Q[i].l<Q[i-1].l) for(int j=Q[i-1].l-1;j>=Q[i].l;--j) Insert(a[j]); else for(int j=Q[i-1].l;j<Q[i].l;++j) Delete(a[j]); if(Q[i].r<Q[i-1].r) for(int j=Q[i-1].r;j>Q[i].r;--j) Delete(a[j]); else for(int j=Q[i-1].r+1;j<=Q[i].r;++j) Insert(a[j]); anss[Q[i].p]=Query(); } for(int i=1;i<=m;++i) printf("%d\n",anss[i]); return 0; }
相关文章推荐
- BZOJ 3585 mex 莫队算法+分块
- BZOJ 3585 mex 莫队算法+分块
- bzoj 3585 mex - 线段树 - 分块 - 莫队算法
- 【DFS序】【莫队算法】【权值分块】bzoj2809 [Apio2012]dispatching
- [BZOJ 3585] mex 【莫队+分块】
- BZOJ 3585: mex|莫队算法
- 【BZOJ4129】Haruna’s Breakfast,树上带修莫队+权值分块求mex
- BZOJ_3585_mex && BZOJ_3339_Rmq Problem_莫队+分块
- BZOJ 3339/3585 Rmq Problem/mex 莫队算法
- 【莫队算法】【权值分块】bzoj3809 Gty的二逼妹子序列
- 【莫队算法】【权值分块】bzoj3920 Yuuna的礼物
- 【莫队算法】【权值分块】bzoj2223 [Coci 2009]PATULJCI
- 【莫队算法】【权值分块】bzoj3236 [Ahoi2013]作业
- [莫队] [分块] [BZOJ3585] mex
- 【DFS序】【莫队算法】【权值分块】bzoj1803 Spoj1487 Query on a tree III
- 【树上莫队】【带修莫队】【权值分块】bzoj1146 [CTSC2008]网络管理Network
- 【树上莫队】【带修莫队】【权值分块】bzoj4129 Haruna’s Breakfast
- 【莫队算法】【权值分块】poj2104 K-th Number / poj2761 Feed the dogs
- 【BZOJ3339&&3585】mex [莫队][分块]
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块