luogu P4688 [Ynoi2016]掉进兔子洞
题面传送门
第二道\(Ynoi\)题目。感觉还是很良心的。
区间相同个数,很容易想到\(bitset\)。
然后可以用莫队维护出来,最后与一下就好了。
但是相同的数怎么办呢?
离散的时候把位置空出来,之后维护一下有几个就好了。
注意加一些常数优化之类的。
代码实现:
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<bitset>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("-fdelete-null-pointer-checks")
using namespace std;
int n,m,k,a[100039],now[100039],f[100039],x,y,nows[100039],head,mid;
bitset<100001> fs,ans[41503];
inline void read(int &x){
char s=getchar();x=0;
while(s<'0'||s>'9') s=getchar();
while(s>='0'&&s<='9') x=(x<<3)+(x<<1)+(s^48),s=getchar();
}
struct yyy{
int x,y,num;
}s[41539],tmp;
inline bool cmp(yyy x,yyy y){return x.x/k==y.x/k?(((x.x/k)&1)?x.y<y.y:x.y>y.y):x.x<y.x;}
inline bool cmp1(yyy x,yyy y){return x.num<y.num;}
int main(){
register int i,j,l,r;
scanf("%d%d",&n,&m);
k=sqrt(n);
for(i=1;i<=n;i++) read(a[i]),now[i]=a[i];
sort(now+1,now+n+1);
for(i=1;i<=n;i++){
nows[i]=nows[i-1];
if(now[i]!=now[i-1]) nows[i]=i;
}
for(i=1;i<=n;i++){
l=0;r=n+1;
while(l+1<r){
mid=(l+r)>>1;
if(a[i]>now[mid])l=mid;
else r=mid;
}
a[i]=nows[r];
}
for(i=1;i<=m;i++){
read(x);read(y);
head++;
s[head]=(yyy){x,y,head};
read(x);read(y);
head++;
s[head]=(yyy){x,y,head};
read(x);read(y);
head++;
s[head]=(yyy){x,y,head};
if(head>=41500||i==m){
sort(s+1,s+head+1,cmp);
l=1;r=0;
memset(f,0,sizeof(f));
fs.reset();
for(j=1;j<=head;j++){
tmp=s[j];
while(l>tmp.x){
l--;
fs[a[l]+(f[a[l]]++)]=1;
}
while(r<tmp.y){
r++;
fs[a[r]+(f[a[r]]++)]=1;
}
while(l<tmp.x){
fs[a[l]+(--f[a[l]])]=0;
l++;
}
while(r>tmp.y){
fs[a[r]+(--f[a[r]])]=0;
r--;
}
ans[tmp.num]=fs;
}
sort(s+1,s+head+1,cmp1);
for(j=1;j<=head/3;j++)printf("%d\n",3+s[j*3-2].y-s[j*3-2].x+s[j*3-1].y-s[j*3-1].x+s[j*3].y-s[j*3].x-3*((ans[j*3-2]&ans[j*3-1]&ans[j*3]).count()));
head=0;
}
}
}