cccgift @ 2019-06-19 10:55:31
R19932371 评测详情
以下是代码:
#include<cstdio>
#include<cstring>
#include<cctype>
#include<utility>
#include<algorithm>
#include<cmath>
using namespace std;
#define res register int
//#define cccgift
#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;
namespace wode{
template<typename T>
inline void read(T &x)
{
static char ch;
for(x=0,ch=getchar();!isdigit(ch);ch=getchar());
for(;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=getchar());
}
template<typename T>
inline void print(T x,char ch=0) {
if(!x) {putchar(48);if(ch) putchar(ch);return;}
if(x<0) putchar('-'),x=-x;
static int Stack[sizeof(T)*3],top=-1;
for(;x;Stack[++top]=x%10,x/=10);
for(;~top;putchar(Stack[top--]+48));
if(ch) putchar(ch);
}
template<typename T>
inline T max(T x,T y) {return x<y?y:x;}
template<typename T>
inline T min(T x,T y) {return x<y?x:y;}
template<typename T>
inline void chkmax(T &x,T y) {x=x<y?y:x;}
template<typename T>
inline void chkmin(T &x,T y) {x=x<y?x:y;}
}
using wode::read;using wode::chkmin;using wode::chkmax;using wode::print;
int f[201][201],tot[201][40001],a[40001],last,nn,n,m,x,y,b[40001],L,R,maxx,tong[40001],t,len;
int main()
{
read(n),read(m);
for(res i=1;i<=n;++i) read(a[i]);
memcpy(b,a,sizeof(a)),sort(b+1,b+1+n),nn=unique(b+1,b+1+n)-b-1;
for(res i=1;i<=n;++i) a[i]=lower_bound(b+1,b+1+nn,a[i])-b;
t=sqrt(n),len=n/t;
for(res i=1;i<=t;++i) {
int L=(i-1)*len+1,R=i*len;
memcpy(tot[i],tot[i-1],sizeof(tot[i-1]));
for(res j=1;j<i;++j) f[j][i]=f[j][i-1];
for(res j=L;j<=R;++j) {
++tot[i][a[j]];
for(res k=1;k<=i;++k) {
int tot1=tot[i][a[j]]-tot[k-1][a[j]],tot2=tot[i][f[k][i]]-tot[k-1][f[k][i]];
if(tot1>tot2||(tot1==tot2&&a[j]<f[k][i])) f[k][i]=a[j];
}
}
}
while(m--) {
read(x),read(y),x=(x+last-1)%n+1,y=(y+last-1)%n+1;
if(x>y) x^=y^=x^=y;
int L=(x-1)/len+1,R=(y-1)/len+1;maxx=0;
// printf("%d %d\n",x,y);
if(R-L<=1) {
for(res i=x;i<=y;++i) {
++tong[a[i]];
if(tong[a[i]]>tong[maxx]||(tong[a[i]]==tong[maxx]&&a[i]<maxx)) maxx=a[i];
}
for(res i=x;i<=y;++i) --tong[a[i]];
}
else {
maxx=f[L+1][R-1];
for(res i=x;i<=t*L;++i) {
++tot[R-1][a[i]];
int tot1=tot[R-1][a[i]]-tot[L][a[i]],tot2=tot[R-1][maxx]-tot[L][maxx];
if(tot1>tot2||(tot1==tot2&&a[i]<maxx)) maxx=a[i];
}
for(res i=t*(R-1)+1;i<=y;++i) {
++tot[R-1][a[i]];
int tot1=tot[R-1][a[i]]-tot[L][a[i]],tot2=tot[R-1][maxx]-tot[L][maxx];
if(tot1>tot2||(tot1==tot2&&a[i]<maxx)) maxx=a[i];
}
for(res i=x;i<=t*L;++i) --tot[R-1][a[i]];
for(res i=t*(R-1)+1;i<=y;++i) --tot[R-1][a[i]];
}
print(last=b[maxx],'\n');
}
return 0;
}
by 142857cs @ 2019-06-19 11:31:59
都9102年了还写nsqrt(n)空间的区间众数
by cccgift @ 2019-06-24 10:54:21
@142857cs ……
by cccgift @ 2019-06-24 10:54:45
@142857cs 我初学者一名……
by cccgift @ 2019-06-24 11:46:55
已解决