tocek_shiki @ 2018-10-02 23:30:17
经检测,preper挂了???
#include <bits/stdc++.h>
#define newline printf ("\n")
#define space printf (" ")
#define cinfals ios::sync_with_stdio(fals)
#define fread(a) freopen (a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rint register int
#define For(i, a, b) for (rint i = a; i <= b; i ++)
#define Low(i, a, b) for (rint i = a; i >= b; i --)
#define FFr(i, a, b, c) for (rint i = a; i <= b; i += c)
#define FLw(i, a, b, c) for (rint i = a; i >= b; i -= c)
#define min(a, b) (a)>(b)?(b):(a)
#define max(a, b) (a)>(b)?(a):(b)
#define MAXN 40050
#define MAXM 250
using namespace std;
int read()
{
int x = 0, f = 1;
char c = getchar();
while(c < '0' || c > '9')
{
if(c == '-')
f = -1;
c = getchar();
}
while(c >= '0' && c <= '9')
x = x*10 + c-'0', c = getchar();
return f*x;
}
int n, m, L, len, sum[MAXM][MAXN], used[MAXN];
int tnum[MAXN], block[MAXN], lst, pre[MAXN];
struct INPUT
{
int id, d, s;
};
INPUT a[MAXN];
struct node
{
int n, s;
node (){n = s = 0;}
};
node p[MAXM][MAXM];
bool cmp1 (INPUT a, INPUT b){return a.d<b.d;}
bool cmp2 (INPUT a, INPUT b){return a.id<b.id;};
int get_block (int x)
{
rint res = x/L;
if (x%L) res++;
return res;
}
void preper ()
{
For (i, 1, len)
{
memset (block, 0, sizeof (block));
node tmp;
For (j, i, len)
{
For(k, (j-1)*L+1, min (n, L*j))
{
block[a[k].s]++;
if (block[a[k].s]>tmp.s) tmp.n = a[k].s, tmp.s = block[a[k].s];
else if (block[a[k].s] == tmp.s) tmp.n = min (tmp.n, a[k].s), tmp.s = block[a[k].s];
}
p[i][j] = tmp;
}
}
For (i, 1, len)
{
For (j, 1, n) sum[i][a[j].s] = sum[i-1][a[j].s];
For (j, (i-1)*L+1, min(n, i*L)) sum[i][a[j].s] ++;
}
}
void modify(int ans, int l, int r, int pos1, int pos2)
{
tnum[ans] = 0, used[ans] = 0;
for(int j = l; j <= min(n, pos1 * L); j++) tnum[a[j].s] = 0, used[a[j].s] = 0;
for(int j = (pos2 - 1) * L + 1; j <= r; j++) tnum[a[j].s] = 0, used[a[j].s] = 0;
for(int j = l; j <= min(n, pos1 * L); j++) tnum[a[j].s]++;
for(int j = (pos2 - 1) * L + 1; j <= r; j++) tnum[a[j].s]++;
int MXnum, MX = 0;
for(int j = l; j <= min(n, pos1 * L); j++)
if(!used[a[j].s])
{
used[a[j].s] = 1;
int val = tnum[a[j].s] + sum[pos2 - 1][a[j].s] - sum[pos1][a[j].s];
if(MX < val) MX = val, MXnum = a[j].s;
else if(MX == val) MXnum = min(MXnum, a[j].s);
}
for(int j = (pos2 - 1) * L + 1; j <= r; j++)
if(!used[a[j].s])
{
used[a[j].s] = 1;
int val = tnum[a[j].s] + sum[pos2 - 1][a[j].s] - sum[pos1][a[j].s];
if(MX < val) MX = val, MXnum = a[j].s;
else if(MX == val) MXnum = min(MXnum, a[j].s);
}
if(MX > tnum[ans] + p[pos1 + 1][pos2 - 1].s) ans = MXnum;
else if(MX == tnum[ans] + p[pos1 + 1][pos2 - 1].s) ans = min(ans, MXnum);
printf("%d\n", lst = pre[ans]);
}
int main()
{
scanf ("%d%d", &n, &m), L = sqrt (n);
len = (n+L-1)/L;
For (i, 1, n) scanf ("%d", &a[i].d), a[i].id = i;
sort (a+1, a+n+1, cmp1), a[0].d = -1;
For (i, 1, n)
{
a[i].s = a[i-1].s;
if (a[i].d != a[i-1].d) a[i].s++;
pre[a[i].s] = a[i].d;
}
cout << "finish";
sort (a+1, a+n+1, cmp2), preper();
For (i, 1, m)
{
rint l = read(), r = read();
l = (l+lst-1)%n+1, r = (r+lst-1)%n+1;
if (l>r) swap(l, r);
int pos1 = get_block(l), pos2 = get_block(r);
if (pos2-pos1 <= 2)
{
rint ans = 0;
For (j, l, r) tnum[a[j].s] = 1;
For (j, l, r)
if (tnum[a[j].s] > tnum[ans]) ans = a[j].s;
else if (tnum[a[j].s] == tnum[ans]) ans = min (ans, a[j].s);
printf("%d\n", lst = pre[ans]);
}
else
{
rint ans = p[pos1+1][pos2-1].n;
modify (ans, l, r, pos1, pos2);
}
}
return 0;
}
by sleepyNick @ 2018-10-02 23:35:13
拼错了是prepare
by tocek_shiki @ 2018-10-02 23:38:14
@mega_aspirin 咳咳
by 2x6_81 @ 2018-10-02 23:38:54
不会,下一个
by tocek_shiki @ 2018-10-02 23:39:41
@fff团666
手动@自己向自己求助
by arfa @ 2018-10-03 07:22:02
@fff团666 怎么感觉你的思路特别复杂
by tocek_shiki @ 2018-10-03 07:30:10
@arfa 没有啊,我就是学第2篇有图的题解的
by arfa @ 2018-10-03 07:38:00
@fff团666 都好麻烦,我们直接求出几个数组就好啦
搞定
by tocek_shiki @ 2018-10-03 07:50:08
@arfa orz