【bzoj2724】[Violet 6]蒲公英

*题目描述:
2724
*输入:
2724
修正一下
l = (l_0 + x - 1) mod n + 1, r = (r_0 + x - 1) mod n + 1
*输出:
2724
*样例输入:
6 3
1 2 3 2 1 2
1 5
3 6
1 5
*样例输出:
1
2
1
*提示:
2724
修正下:
n <= 40000, m <= 50000
*题解:
强制在线求区间众数。我们考虑将序列分块,对于每一个块,我们先维护0~当前块的每个数字的出现次数(这样的话我们就可以以O(1)的时间得到两个块之间某个数字的出现个数,类似于前缀和),这样做的空间和时间复杂度都是(sqrt(n) * n),然后我们再暴力预处理出块与块之间的答案,时间复杂度O(n * sqrt(n)),空间复杂度O(sqrt(n) * sqrt(n)) = O(n)。然后每个询问就和其他的分块题一样做就好了。
*代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

#ifdef WIN32
    #define LL "%I64d"
#else
    #define LL "%lld"
#endif

#ifdef CT
    #define debug(...) printf(__VA_ARGS__)
    #define setfile() 
#else
    #define debug(...)
    #define filename ""
    #define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
#endif

#define R register
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
#define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
char B[1 << 15], *S = B, *T = B;
inline int FastIn()
{
    R char ch; R int cnt = 0; R bool minus = 0;
    while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
    ch == '-' ? minus = 1 : cnt = ch - '0';
    while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
    return minus ? -cnt : cnt;
}
#define maxn 40010
int n, m, block, num, ans, anscnt;
int hash[maxn], a[maxn], id[maxn], cnt[maxn], Ans[210][210], Cnt[210][maxn];
int main()
{
//  setfile();
    n = FastIn(), m = FastIn();
    block = sqrt(n); num = n / block;
    R int tot = n;
    for (R int i = 1; i <= n; ++i) a[i] = hash[i] = FastIn(), id[i] = i / block;
    std::sort(hash + 1, hash + tot + 1);
    tot = std::unique(hash + 1, hash + tot + 1) - hash - 1;
    for (R int i = 1; i <= n; ++i)
        a[i] = std::lower_bound(hash + 1, hash + tot + 1, a[i]) - hash;
    // lisanhua
    for (R int i = 0; i <= num; ++i)
        for (R int j = 1; j < dmin((i + 1) * block, n + 1); ++j)
            ++Cnt[i][a[j]];
    for (R int i = 0; i < num; ++i)
    {
        anscnt = 0;
        for (R int j = i; j <= num; ++j)
        {
            for (R int k = j * block; k < dmin((j + 1) * block, n + 1); ++k)
                ++cnt[a[k]] > anscnt || (cnt[a[k]] == anscnt && a[k] < ans)
                    ? anscnt = cnt[a[k]], ans = a[k]
                    : 0;
            Ans[i][j] = ans;
        }
        memset(cnt, 0, sizeof(cnt));
    }
    ans = 0;
    for (; m; --m)
    {
        R int ql = (FastIn() + ans - 1) % n + 1, qr = (FastIn() + ans - 1) % n + 1;
        anscnt = 0;
        if (ql > qr) std::swap(ql, qr);
        if (id[ql] == id[qr])
        {
            for (R int i = ql; i <= qr; ++i)
                ++cnt[a[i]] > anscnt || (cnt[a[i]] == anscnt && a[i] < ans)
                    ? anscnt = cnt[a[i]], ans = a[i]
                    : 0;
            for (R int i = ql; i <= qr; ++i) --cnt[a[i]];
        }
        else
        {
            R int ls = id[ql] + 1, rs = id[qr] - 1;
            ans = Ans[ls][rs]; anscnt = Cnt[rs][ans] - Cnt[ls - 1][ans];
            for (R int i = ql; i < ls * block; ++i)
            {
                R int tmp = ++cnt[a[i]] + Cnt[rs][a[i]] - Cnt[ls - 1][a[i]];
                tmp > anscnt || (tmp == anscnt && a[i] < ans)
                    ? anscnt = tmp, ans = a[i]
                    : 0;
            }
            for (R int i = (rs + 1) * block; i <= qr; ++i)
            {
                R int tmp = ++cnt[a[i]] + Cnt[rs][a[i]] - Cnt[ls - 1][a[i]];
                tmp > anscnt || (tmp == anscnt && a[i] < ans)
                    ? anscnt = tmp, ans = a[i]
                    : 0;
            }
            for (R int i = ql; i < ls * block; ++i) --cnt[a[i]];
            for (R int i = (rs + 1) * block; i <= qr; ++i) --cnt[a[i]];
        }
        printf("%d
", ans = hash[ans]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/cocottt/p/5550953.html