AcWing249 蒲公英 分块

蒲公英(区间众数)

题意

区间众数,不带修改,强制在线(否则可以莫队)。如果两个数出现次数一样多,输出较小的数。

解法

分块。首先,一段区间的众数的候选答案一定是大块的众数或者小块的每一个数,先预处理出大块的众数,这样候选答案的范围就缩小到了 n \sqrt n n 个,然后每个再询问区间内出现的次数,维护答案即可。

  • 预处理大块的众数暴力就可以了,左端点是每一个大块的左端点,然后向右扫过去就可以了。由于左端点只有 n \sqrt n n 个,所以这部分的时间复杂度为 O ( n n ) O(n \sqrt n) O(nn )
  • 然后还需要维护区间内出现的次数,首先离散化,把数的范围缩小到 [ 1 , n ] [1,n] [1,n] ,然后记录每一个数出现的位置,那么要查询 [ l , r ] [l,r] [l,r] 区间内这个数的出现次数,只需要 u p p e r b o u n d ( r ) − l o w e r b o u n d ( l ) upperbound_(r)-lowerbound(l) upperbound(r)lowerbound(l) 即可,这样查询的复杂度为 O ( l o g n ) O(logn) O(logn) ,候选答案只有 n \sqrt n n 个,有 q q q 次查询,那么查询的复杂度就是 O ( q n l o g n ) O(q\sqrt n logn) O(qn logn)

但是这样做这题是无法通过所有测试点的,考虑优化查询部分,可以预处理每个数在所有块中出现次数的前缀 O ( n n ) O(n \sqrt n) O(nn ),差分求出现次数,可以去掉log,时间复杂度为 O ( n n ) O(n\sqrt n) O(nn )

这样比较难写,可以尝试玄学优化,比如修改块的大小,把块的大小修改为 n m l o g 2 n \frac{n}{\sqrt{mlog_2n}} mlog2n n,这题也可以卡过去。

代码
#pragma region
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, a, n) for (int i = n; i >= a; --i)
namespace fastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
//fread->R
bool IOerror = 0;
//inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
inline char nc() {
    static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
    if (p1 == pend) {
        p1 = buf;
        pend = buf + fread(buf, 1, BUF_SIZE, stdin);
        if (pend == p1) {
            IOerror = 1;
            return -1;
        }
    }
    return *p1++;
}
inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }
template <class T>
inline bool R(T &x) {
    bool sign = 0;
    char ch = nc();
    x = 0;
    for (; blank(ch); ch = nc())
        ;
    if (IOerror)
        return false;
    if (ch == '-')
        sign = 1, ch = nc();
    for (; ch >= '0' && ch <= '9'; ch = nc())
        x = x * 10 + ch - '0';
    if (sign)
        x = -x;
    return true;
}
inline bool R(double &x) {
    bool sign = 0;
    char ch = nc();
    x = 0;
    for (; blank(ch); ch = nc())
        ;
    if (IOerror)
        return false;
    if (ch == '-')
        sign = 1, ch = nc();
    for (; ch >= '0' && ch <= '9'; ch = nc())
        x = x * 10 + ch - '0';
    if (ch == '.') {
        double tmp = 1;
        ch = nc();
        for (; ch >= '0' && ch <= '9'; ch = nc())
            tmp /= 10.0, x += tmp * (ch - '0');
    }
    if (sign)
        x = -x;
    return true;
}
inline bool R(char *s) {
    char ch = nc();
    for (; blank(ch); ch = nc())
        ;
    if (IOerror)
        return false;
    for (; !blank(ch) && !IOerror; ch = nc())
        *s++ = ch;
    *s = 0;
    return true;
}
inline bool R(char &c) {
    c = nc();
    if (IOerror) {
        c = -1;
        return false;
    }
    return true;
}
template <class T, class... U>
bool R(T &h, U &... tmp) { return R(h) && R(tmp...); }
#undef OUT_SIZE
#undef BUF_SIZE
};  // namespace fastIO
using namespace fastIO;
template <class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) { _W(x.F), putchar(' '), _W(x.S); }
template <class T>
void _W(const vector<T> &x) {
    for (auto i = x.begin(); i != x.end(); _W(*i++))
        if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
#pragma endregion
//https://www.acwing.com/problem/content/251/
//区间众数 强制在线分块做法
const int maxn = 1e5 + 5;
const int maxb = 1005;
int n, q, B, a[maxn], b[maxn];
int cnt[maxn], ans[maxb][maxb];  //候选答案
vector<int> pos[maxn];
inline int getcnt(const int &l, const int &r, const int &x) {
    return upper_bound(pos[x].begin(), pos[x].end(), r) - lower_bound(pos[x].begin(), pos[x].end(), l);
}
int query(int l, int r) {
    int idl = l / B, idr = r / B;
    int num = 0x3f3f3f3f, cnum = 0;
    if (idl == idr) {
        rep(i, l, r) {
            int tmp = getcnt(l, r, a[i]);
            if (tmp > cnum || (tmp == cnum && a[i] < num)) num = a[i], cnum = tmp;
        }
    } else {
        num = ans[idl + 1][idr - 1], cnum = getcnt(l, r, num);
        rep(i, l, (idl + 1) * B - 1) {
            int tmp = getcnt(l, r, a[i]);
            if (tmp > cnum || (tmp == cnum && a[i] < num)) num = a[i], cnum = tmp;
        }
        rep(i, idr * B, r) {
            int tmp = getcnt(l, r, a[i]);
            if (tmp > cnum || (tmp == cnum && a[i] < num)) num = a[i], cnum = tmp;
        }
    }
    return b[num];
}
int main() {
    R(n, q);
    if (n >= 1000)
        B = max(1, (int)(n / sqrt(q * log2(n))));
    else
        B = sqrt(n);
    rep(i, 1, n) R(a[i]);
    memcpy(b, a, sizeof(b));
    sort(b + 1, b + 1 + n);
    int len = unique(b + 1, b + 1 + n) - b - 1;
    rep(i, 1, n) {
        a[i] = lower_bound(b + 1, b + 1 + len, a[i]) - b;  //b[a[i]]
        pos[a[i]].push_back(i);
    }

    for (int l = B; l <= n; l += B) {
        memset(cnt, 0, sizeof(cnt));
        int tmp = 0;
        rep(i, l, n) {
            ++cnt[a[i]];
            if (cnt[a[i]] > cnt[tmp] || (cnt[a[i]] == cnt[tmp] && a[i] < tmp)) tmp = a[i];
            if (i / B * B + B - 1 == i || i == n) ans[l / B][i / B] = tmp;
        }
    }

    int last = 0;
    while (q--) {
        int l, r;
        R(l, r);
        l = (l + last - 1) % n + 1;
        r = (r + last - 1) % n + 1;
        if (l > r) swap(l, r);
        last = query(l, r);
        W(last);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值