CF1514D Cut and Stick
找一下规律会发现答案就是 \(1\) 或者 \(2x-(r-l+1)\) 。
然后问题转化成为求区间众数个数。
可以直接用 P5048 [Ynoi2019 模拟赛] Yuno loves sqrt technology III 在线做或者用莫队离线做。
然后就没有了。
代码:
#pragma GCC optimize(2)
%:pragma GCC optimize(3)
%: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("-fhoist-adjacent-loads")
%: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("inline-functions-called-once")
%:pragma GCC optimize("-fdelete-null-pointer-checks")
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5,siz=708;
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define lowbit(x) (x&(-x))
inline int inc(int x,int v,int mod){x+=v;return x>=mod?x-mod:x;}
inline int dec(int x,int v,int mod){x-=v;return x<0?x+mod:x;}
#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
inline int read(){int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
template <typename T>
inline void write(T x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10^48);
return ;
}
std::vector<int>ls,v[N];
int n,m,L[710],R[710],bel[N],blocks,mx[710][710],ans,tot[N],wz[N],a[N];
void init(){
blocks=(n-1)/siz+1;
for(int i=1;i<=blocks;++i)L[i]=R[i-1]+1,R[i]=i*siz;
R[blocks]=n;
for(int i=1;i<=blocks;++i){
memset(tot,0,sizeof tot);
for(int j=L[i];j<=R[i];++j)bel[j]=i;
for(int j=i;j<=blocks;++j){
int&F=mx[i][j];
F=mx[i][j-1];
for(int k=L[j];k<=R[j];++k)
F=std::max(F,++tot[a[k]]);
}
}
}
int main(){
ls.push_back(-1);
n=read(),m=read();
for(int i=1;i<=n;ls.push_back(a[i++])) a[i]=read();
std::sort(ls.begin(),ls.end());
ls.erase(std::unique(ls.begin(),ls.end()),ls.end());
for(int i=1;i<=n;++i)v[a[i]=std::lower_bound(ls.begin(),ls.end(),a[i])-ls.begin()].push_back(i),wz[i]=v[a[i]].size()-1;
init();
memset(tot,0,sizeof tot);
while(m--){
int l,r;l=read(),r=read();
ans=0;
if(bel[l]==bel[r]){
for(int i=l;i<=r;++i)ans=std::max(ans,++tot[a[i]]);
for(int i=l;i<=r;++i)tot[a[i]]=0;
}else{
ans=mx[bel[l]+1][bel[r]-1];
for(int i=l;i<=R[bel[l]];++i){
int it=wz[i];
while(it+ans<v[a[i]].size()&&v[a[i]][it+ans]<=r)++ans;
}
for(int i=L[bel[r]];i<=r;++i){
int it=wz[i];
while(it-ans>=0&&v[a[i]][it-ans]>=l)++ans;
}
}
if(ans<=ceil((1.0*r-l+1)/2)) write(1);
else write(2*ans-(r-l+1));
puts("");
}
return 0;
}