求助,分块样例能过但是全WA,悬关

P4168 [Violet] 蒲公英

Yellow_and_Strong @ 2023-07-15 19:49:36

rt

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 4e4 + 40;
const int SQRT = 220;

inline int read()
{
    int x = 0; char ch = getchar();
    while (!isdigit(ch)) ch = getchar();
    while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch xor 48), ch = getchar();
    return x;
}
inline void write (int x)
{
    if (x > 9) write(x / 10);
    putchar (x % 10 + 48);
}

int n, Q, a[MAXN], b[MAXN];
inline void input()
{
    n = read(), Q = read();
    for (register int i = 1; i <= n; ++ i)
        a[i] = read(), b[i] = a[i];
}

int m, id[MAXN], L[SQRT], R[SQRT];
int rnk[MAXN], _rnk[MAXN];
int f[SQRT][SQRT], g[SQRT][MAXN];
inline int SUM (int l, int r, int x) { return g[r][x] - g[l - 1][x]; }
inline void init()
{
    sort (b + 1, b + n + 1);
    int k = unique(b + 1, b + n + 1) - b - 1;
    for (register int i = 1; i <= n; ++ i)
        rnk[i] = lower_bound(b + 1, b + k + 1, a[i]) - b, _rnk[rnk[i]] = a[i];

    m = sqrt(n);
    for (register int i = 1; i <= m; ++ i)
        L[i] = n / m * (i - 1) + 1, R[i] = n / m * i;
    R[m] = n;
    for (register int i = 1; i <= m; ++ i)
        for (register int j = L[i]; j <= R[i]; ++ j)
            id[j] = i;

    for (register int i = 1; i <= m; ++ i)
    {
        for (register int j = 1; j <= n; ++ j)
            g[i][j] = g[i - 1][j];
        for (register int j = L[i]; j <= R[i]; ++ j)
            ++ g[i][rnk[j]];
    }
    for (register int l = 1; l <= m; ++ l)
        for (register int r = l; r <= m; ++ r)
        {
            if (l == r)
            {
                f[l][r] = rnk[L[r]];
                for (register int i = L[r] + 1; i <= R[r]; ++ i)
                    if (SUM(l, r, rnk[i]) > SUM(l, r, f[l][r]) or SUM(l, r, rnk[i]) == SUM(l, r, f[l][r]) and rnk[i] < f[l][r]) f[l][r] = rnk[i];
            }
            else
            {
                f[l][r] = f[l][r - 1];
                for (register int i = L[r]; i <= R[r]; ++ i)
                    if (SUM(l, r, rnk[i]) > SUM(l, r, f[l][r]) or SUM(l, r, rnk[i]) == SUM(l, r, f[l][r]) and rnk[i] < f[l][r]) f[l][r] = rnk[i];
            }
        }
}

int last, T[MAXN];
inline int query (int l, int r)
{
    int ans = 0, ans_num = 0;
    if (id[l] == id[r])
    {
        for (register int i = l; i <= r; ++ i)
        {
            ++ T[rnk[i]];
            if (T[rnk[i]] > ans_num or T[rnk[i]] == ans_num and rnk[i] < ans) ans = rnk[i], ans_num = T[rnk[i]];
        }
        for (register int i = l; i <= r; ++ i) T[rnk[i]] = 0;
        return ans;
    }

    ans = f[id[l] + 1][id[r] - 1], ans_num = SUM(id[l] + 1, id[r] - 1, ans);
    for (register int i = l; i <= R[id[l]]; ++ i)
        if (rnk[i] == ans) ++ ans_num;
    for (register int i = L[id[r]]; i <= r; ++ i)
        if (rnk[i] == ans) ++ ans_num;
    for (register int i = l; i <= R[id[l]]; ++ i)
        T[rnk[i]] += SUM(id[l] + 1, id[r] - 1, rnk[i]);
    for (register int i = L[id[r]]; i <= r; ++ i)
        T[rnk[i]] += SUM(id[l] + 1, id[r] - 1, rnk[i]);
    for (register int i = l; i <= R[id[l]]; ++ i) ++ T[rnk[i]];
    for (register int i = L[id[r]]; i <= r; ++ i) ++ T[rnk[i]];
    for (register int i = l; i <= R[id[l]]; ++ i)
        if (T[rnk[i]] > ans_num or T[rnk[i]] == ans_num and rnk[i] < ans) ans = rnk[i], ans_num = T[rnk[i]];
    for (register int i = L[id[r]]; i <= r; ++ i)
        if (T[rnk[i]] > ans_num or T[rnk[i]] == ans_num and rnk[i] < ans) ans = rnk[i], ans_num = T[rnk[i]];
    for (register int i = l; i <= R[id[l]]; ++ i) T[rnk[i]] = 0;
    for (register int i = L[id[r]]; i <= r; ++ i) T[rnk[i]] = 0;
    return ans;
}
inline void work()
{
    while (Q --)
    {
        int l0 = read(), r0 = read();
        int l = ((l0 + last - 1) % n) + 1, r = ((r0 + last - 1) % n) + 1;
        if (l > r) swap(l, r);
        write(last = _rnk[query(l, r)]), putchar('\n');
    }
}

int main()
{
    input();
    init();
    work();
    return 0;
}

by Yellow_and_Strong @ 2023-07-16 07:55:58

已解决,此贴结(还挺押韵

PS:帮我的人是我已经关注的人


by Joy_Dream_Glory @ 2024-01-10 15:19:17

考古 @Yellow_and_Strong


|