3809: Gty的二逼妹子序列 莫队算法+分块
来源:互联网 发布:流畅的python 编辑:程序博客网 时间:2024/05/21 17:15
这题还卡树状数组!@#¥%……
#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<queue>#include<vector>#include<set>#include<map>#define lowbit(x) (x&(-x))using namespace std;int sc(){ int i=0;char c=getchar(); while(c>'9'||c<'0')c=getchar(); while(c>='0'&&c<='9')i=i*10+c-'0',c=getchar(); return i;}struct W{int l,r,a,b,pos;}q[1000001];int a[100010],bl[100010],cnt[100010];int block_ans[100100];int ans[1000010],n,m,block;bool cmp(W a,W b){ return bl[a.l]==bl[b.l]?a.r<b.r:bl[a.l]<bl[b.l];}int query(int l,int r){ int x=bl[l],y=bl[r],ans=0; if(x==y) { for(int i=l;i<=r;i++)ans+=(bool)cnt[i]; return ans; } for(int i=x+1;i<y;i++)ans+=block_ans[i]; for(int i=l;bl[i]==x;i++)ans+=(bool)cnt[i]; for(int i=r;bl[i]==y;i--)ans+=(bool)cnt[i]; return ans;}void cal(int x,int f){ cnt[x]+=f; if(f==1&&cnt[x]==1) block_ans[bl[x]]++; else if(f==-1&&cnt[x]==0) block_ans[bl[x]]--;}void print(int x){ if(x==0){puts("0");return;} char c[20]; int i=0; while(x)c[++i]=x%10+'0',x/=10; while(i)putchar(c[i--]); puts("");}int main(){ n=sc(),m=sc();block=sqrt(n); for(int i=1;i<=n;i++)a[i]=sc(); for(int i=1;i<=m;i++)q[i]=(W){sc(),sc(),sc(),sc(),i}; for(int i=1;i<=n;i++)bl[i]=(i-1)/block+1; sort(q+1,q+m+1,cmp); int l=q[1].l,r=q[1].l; cal(a[q[1].l],1); for(int i=1;i<=m;i++) { while(l<q[i].l)cal(a[l++],-1); while(r>q[i].r)cal(a[r--],-1); while(l>q[i].l)cal(a[--l],1); while(r<q[i].r)cal(a[++r],1); ans[q[i].pos]=query(q[i].a,q[i].b); } for(int i=1;i<=m;i++)print(ans[i]); return 0;}
0 0
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
- BZOJ 3809: Gty的二逼妹子序列|莫队算法|分块
- 3809: Gty的二逼妹子序列 莫队算法+分块
- bzoj 3809: Gty的二逼妹子序列(莫队算法+分块)
- BZOJ-3809 Gty的二逼妹子序列 莫队算法 分块
- 【BZOJ3809】Gty的二逼妹子序列【莫队算法】【分块】
- [bzoj 3809] Gty的二逼妹子序列 莫队+分块
- 【BZOJ 3809】Gty的二逼妹子序列 莫队+分块
- BZOJ 3809: Gty的二逼妹子序列 莫队套分块
- [BZOJ3809]Gty的二逼妹子序列(莫队+分块)
- 【bzoj3809】【GTY的二逼妹子序列】【莫队+分块】
- 【bzoj3809】Gty的二逼妹子序列 莫队+分块
- 【BZOJ3809】Gty的二逼妹子序列 (莫队+分块)
- bzoj3809 Gty的二逼妹子序列(莫队+分块)
- 【BZOJ3809】Gty的二逼妹子序列 莫队 分块
- 3809: Gty的二逼妹子序列
- [BZOJ]3809 3809: Gty的二逼妹子序列 莫队+分块
- 推荐!国外程序员整理的机器学习资源大全
- 2194: 快速傅立叶之二 FFT
- 3527: [Zjoi2014]力 FFT
- git分支原理命令图文解析
- 3720: Gty的妹子树 块状树
- 3809: Gty的二逼妹子序列 莫队算法+分块
- Windows不重装系统,使用分区助手分区
- oc数组的四种遍历方法
- BroadcastReceiver最全实例——静态、动态、系统(监听短信、网络连接状态、SD卡、应用安装……)
- SQL中Group By的使用
- 1951: [Sdoi2010]古代猪文 费马小定理+中国剩余定理+卢卡斯定理+快速幂
- android studio修改module名称
- Jquery即点既改
- 算法_Rome to Integer