2724: [Violet 6]蒲公英
Time Limit: 40 Sec Memory Limit: 512 MBSubmit: 2464 Solved: 848
[ Submit][ Status][ Discuss]
Description
Input
修正一下
l = (l_0 + x - 1) mod n + 1, r = (r_0 + x - 1) mod n + 1
Output
Sample Input
6 3
1 2 3 2 1 2
1 5
3 6
1 5
Sample Output
1
2
1
这篇文章讲的非常好,我就不写题解了
看了秒懂,懂了秒过
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[50005], p[50005], bel[50005], B, sum[235][50005], zh[235][235], flag[50005];
int main(void)
{
int n, m, i, j, cnt, x, y, bet, l, r, temp, ans;
scanf("%d%d", &n, &m);
cnt = 0;
for(i=1;i<=n;i++)
{
scanf("%d", &a[i]);
p[++cnt] = a[i];
}
sort(p+1, p+cnt+1);
cnt = unique(p+1, p+cnt+1)-(p+1);
for(i=1;i<=n;i++)
a[i] = lower_bound(p+1, p+cnt+1, a[i])-p;
B = sqrt(n)+1;
x = 1, y = 0;
for(i=1;i<=n;i++)
{
y += 1;
sum[x][a[i]]++;
bel[i] = x;
if(y>B)
y = 0, x++;
}
if(y==0)
x--;
B = x;
for(i=1;i<=B;i++)
{
for(j=1;j<=cnt;j++)
sum[i][j] += sum[i-1][j];
}
for(i=1;i<=B;i++)
{
bet = 0;
for(j=1;j<=n;j++)
{
if(bel[j]<i)
continue;
flag[a[j]]++;
if(flag[a[j]]>bet || flag[a[j]]==bet && a[j]<temp)
bet = flag[a[j]], temp = a[j];
if(bel[j+1]!=bel[j])
zh[i][bel[j]] = temp;
}
memset(flag, 0, sizeof(flag));
}
ans = 0;
while(m--)
{
scanf("%d%d", &l, &r);
l = (l+ans-1)%n+1;
r = (r+ans-1)%n+1;
if(l>r)
swap(l, r);
bet = 0;
if(bel[l]==bel[r] || bel[l]+1==bel[r])
{
for(i=l;i<=r;i++)
{
flag[a[i]]++;
if(flag[a[i]]>bet || flag[a[i]]==bet && a[i]<temp)
bet = flag[a[i]], temp = a[i];
}
for(i=r;i>=l;i--)
flag[a[i]] = 0;
}
else
{
for(i=l;bel[i]==bel[l];i++)
{
flag[a[i]]++;
if(flag[a[i]]==1)
flag[a[i]] += sum[bel[r]-1][a[i]]-sum[bel[l]][a[i]];
if(flag[a[i]]>bet || flag[a[i]]==bet && a[i]<temp)
bet = flag[a[i]], temp = a[i];
}
for(i=r;bel[i]==bel[r];i--)
{
flag[a[i]]++;
if(flag[a[i]]==1)
flag[a[i]] += sum[bel[r]-1][a[i]]-sum[bel[l]][a[i]];
if(flag[a[i]]>bet || flag[a[i]]==bet && a[i]<temp)
bet = flag[a[i]], temp = a[i];
}
x = zh[bel[l]+1][bel[r]-1];
if(sum[bel[r]-1][x]-sum[bel[l]][x]>bet || sum[bel[r]-1][x]-sum[bel[l]][x]==bet && x<temp)
temp = x;
for(i=l;bel[i]==bel[l];i++)
flag[a[i]] = 0;
for(i=r;bel[i]==bel[r];i--)
flag[a[i]] = 0;
}
ans = p[temp];
printf("%d\n", ans);
}
return 0;
}
/*
17 550
1 1 1 1 2 2 4 4 3 3 4 4 5 5 5 5 4
10 10
*/