LSG_Robert @ 2020-10-09 08:54:59
本地跑数据1是对的,为什么交上去就错了
评测记录
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int point;
char ch, stk[100];
const int N = 40010;
const int S = 210;
typedef double db;
typedef long long ll;
typedef long double lb;
typedef unsigned long long ull;
#define il inline
#define ENDL putchar('\n')
#define GET(ch) ch = getchar()
#define PUT(ch) putchar(ch)
#define isd(ch) (ch >= 48 && ch <= 57)
#define mst(a, b) memset(a, b, sizeof(a))
#define rep(a, b, c) for (register int a = b; a <= c; ++ a)
#define drep(a, b, c) for (register int a = b; a >= c; -- a)
template <typename T>
il T Abs(T a) { return a < 0 ? -a : a; }
template <typename T>
il T Min(T a, T b) { return a < b ? a : b; }
template <typename T>
il T Max(T a, T b) { return a > b ? a : b; }
template <typename T>
il void Minn(T &a, T b) { a = a < b ? a : b; }
template <typename T>
il void Maxx(T &a, T b) { a = a > b ? a : b; }
template <typename T>
il void Swap(T &a, T &b) { T c = a; a = b; b = c; }
template <typename T>
il void read(T &x) {
T p = 1; x = 0; GET(ch);
while (!isd(ch)) {
if (ch == '-') p = -1;
GET(ch);
}
while (isd(ch))
x = (x << 1) + (x << 3) + (ch ^ 48), GET(ch);
x *= p;
}
template <typename T>
il void write(T x) {
point = 0;
if (x < 0) PUT('-'), x = -x;
do {
stk[++ point] = (x % 10 + 48);
} while (x /= 10);
while (point) PUT(stk[point --]);
}
int n, m, b[N], s[S][N], p[S][S];
int c[N], pos[N], l[S], r[S], bp[N];
struct flo {
int x, d;
bool operator < (const flo &c) const {
return d < c.d;
}
} a[N];
int maxx, pp, ss, ans;
il int query(int x, int y) {
if (pos[y] - pos[x] <= 1) {
pp = 0;
rep(i, x, y) {
++ c[b[i]];
if (c[b[i]] == c[pp]) Minn(pp, b[i]);
else if (c[b[i]] > c[pp]) pp = b[i];
}
rep(i, x, y) -- c[b[i]];
return pp;
}
ss = p[pos[x] + 1][pos[y] - 1];
maxx = s[pos[y] - 1][ss] - s[pos[x]][ss];
rep(i, x, r[pos[x]]) {
++ c[b[i]];
if (c[b[i]] + s[pos[y] - 1][b[i]] - s[pos[x]][b[i]] == maxx) ss = Min(ss, b[i]);
else if (c[b[i]] + s[pos[y] - 1][b[i]] - s[pos[x]][b[i]] > maxx) ss = b[i],
maxx = c[b[i]] + s[pos[y] - 1][b[i]] - s[pos[x]][b[i]];
}
rep(i, l[pos[y]], y) {
++ c[b[i]];
if (c[b[i]] + s[pos[y] - 1][b[i]] - s[pos[x]][b[i]] == maxx) ss = Min(ss, b[i]);
else if (c[b[i]] + s[pos[y] - 1][b[i]] - s[pos[x]][b[i]] > maxx) ss = b[i],
maxx = c[b[i]] + s[pos[y] - 1][b[i]] - s[pos[x]][b[i]];
}
rep(i, x, r[pos[x]]) -- c[b[i]];
rep(i, l[pos[y]], y) -- c[b[i]];
return ss;
}
int main() {
read(n), read(m);
rep(i, 1, n) read(a[i].d), a[i].x = i;
sort(a + 1, a + n + 1);
int now = 0, x, ql, qr, t = sqrt(n);
rep(i, 1, n) {
if (a[i - 1].d ^ a[i].d) ++ now;
b[a[i].x] = now; bp[now] = a[i].d;
}
rep(i, 1, t) {
l[i] = (i - 1) * t + 1; r[i] = i * t;
rep(j, 1, now) s[i][j] = s[i - 1][j];
rep(j, l[i], r[i])
++ s[i][b[j]], pos[j] = i;
}
if (r[t] < n) {
l[++ t] = r[t - 1] + 1, r[t] = n;
rep(i, 1, now) s[t][i] = s[t - 1][i];
rep(i, l[t], r[t])
++ s[t][b[i]], pos[i] = t;
}
rep(i, 1, t) {
mst(c, 0); pp = 0;
rep(j, i, t) {
rep(k, l[j], r[j]) {
++ c[b[k]];
if (c[pp] < c[b[k]]) pp = b[k];
if (c[pp] == c[b[k]]) Minn(pp, b[k]);
}
p[i][j] = pp;
}
} mst(c, 0);
rep(i, 1, m) {
read(x), ql = (x + ans - 1) % n + 1;
read(x), qr = (x + ans - 1) % n + 1;
if (ql > qr) Swap(ql, qr);
ans = bp[query(ql, qr)];
write(ans); ENDL;
}
return 0;
}
by watermonster @ 2020-10-09 08:57:11
@LSG_Robert %%%喻队%%% 爆切我写不出的题
by LSG_waterlyf @ 2020-10-09 08:58:47
带巨人
by watermonster @ 2020-10-09 08:59:05
Rank8代码给您对照
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define re register
#define il inline
char buf[(1<<21)+100],*p1=buf,*p2=buf;
char get_char(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin))?EOF:*p1++;}
#define isdigit(ch) (ch>=48&&ch<=57)
il void read(int &x)
{
x=0;re char ch=get_char();
while(!isdigit(ch)) ch=get_char();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=get_char();
}
il void write(int x)
{
re char Tmp[100];re int p=0;
do{Tmp[++p]=x%10+48;}while(x/=10);
do{putchar(Tmp[p--]);}while(p);
puts("");
}
const int N=40040;
const int T=220;
int n,m;
struct num{int val,rnk,id;}s[N];
il bool cmp1(num a,num b){return a.val<b.val;}
il bool cmp2(num a,num b){return a.id<b.id;}
int ref[N];
il void decrete()
{
sort(s+1,s+n+1,cmp1);
re int cnt=0;
for(re int i=1;i<=n;++i)
{
if(s[i].val^s[i-1].val) ++cnt;
s[i].rnk=cnt;ref[cnt]=s[i].val;
}
sort(s+1,s+n+1,cmp2);
}
int bel[N];
int L[T],R[T];
int f[T][T],g[T][N];
int tmp[N];
il int query(int l,int r)
{
re int lef=bel[l]+(L[bel[l]]!=l);
re int rig=bel[r]-(R[bel[r]]!=r);
re int ans=f[lef][rig];
if(bel[l]==bel[r])
{
for(re int i=l;i<=r;++i)
{
++tmp[s[i].rnk];
if(tmp[s[i].rnk]>tmp[ans]) ans=s[i].rnk;
if(tmp[s[i].rnk]==tmp[ans]&&s[i].rnk<ans) ans=s[i].rnk;
}
for(re int i=l;i<=r;++i) --tmp[s[i].rnk];
return ref[ans];
}
for(re int i=l;i<=L[lef]-1;++i)
{
++tmp[s[i].rnk];
re int now=s[i].rnk;
re int cntnow=tmp[now]+g[rig][now]-g[lef-1][now];
re int cntans=tmp[ans]+g[rig][ans]-g[lef-1][ans];
if(cntnow>cntans||(cntnow==cntans&&now<ans)) ans=now;
}
for(re int i=r;i>=R[rig]+1;--i)
{
++tmp[s[i].rnk];
re int now=s[i].rnk;
re int cntnow=tmp[now]+g[rig][now]-g[lef-1][now];
re int cntans=tmp[ans]+g[rig][ans]-g[lef-1][ans];
if(cntnow>cntans||(cntnow==cntans&&now<ans)) ans=now;
}
for(re int i=l;i<=L[lef]-1;++i) --tmp[s[i].rnk];
for(re int i=r;i>=R[rig]+1;--i) --tmp[s[i].rnk];
return ref[ans];
}
int lstans;
int main()
{
read(n);read(m);
for(re int i=1;i<=n;++i)
read(s[i].val),s[i].id=i;
decrete();
re int siz=sqrt(n);
for(re int i=1;i<=n;++i)
{
bel[i]=i/siz+1;
if(bel[i]^bel[i-1]) L[bel[i]]=i,R[bel[i-1]]=i-1;
}
R[bel[n]]=n;
for(re int i=1;i<=bel[n];++i)
{
for(re int j=1;j<=n;++j)
g[i][j]=g[i-1][j];
for(re int j=L[i];j<=R[i];++j)
++g[i][s[j].rnk];
}
for(re int i=1;i<=bel[n];++i)
{
for(re int j=i;j<=bel[n];++j)
{
f[i][j]=f[i][j-1];
for(re int k=L[j];k<=R[j];++k)
{
tmp[s[k].rnk]++;
if(tmp[s[k].rnk]>tmp[f[i][j]]) f[i][j]=s[k].rnk;
if(tmp[s[k].rnk]==tmp[f[i][j]]&&s[k].rnk<f[i][j]) f[i][j]=s[k].rnk;
}
}
memset(tmp,0,sizeof(tmp));
}
while(m--)
{
re int l,r;
read(l),read(r);
l=((l+lstans-1)%n)+1;
r=((r+lstans-1)%n)+1;
if(l>r) l^=r^=l^=r;
write(lstans=query(l,r));
}
return 0;
}