QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 512 MB Total points: 100

#6269. 模棱两可

统计

给定一个序列 $a_1,a_2,\dots,a_n$,每次询问一个区间 $[l,r]$ 内出现最多的数的出现次数。

但今天我们不太关心问题的精确答案,只关心问题的数量级。因此,如果精确答案是 $ans$,你的回答只要在 $[0.5ans, 2ans]$ 内就算你对。

输入格式

第一行两个正整数 $n,q$ 表示序列长度和询问次数。

接下来一行输入 $n$ 个正整数,分别表示 $a_1,a_2,\dots,a_n$。

接下来 $q$ 行每行两个正整数 $l,r$ 表示一个询问区间。

输出格式

输出 $q$ 行,每行一个数表示答案。

样例数据

样例输入

10 3
1 1 4 5 1 4 1 9 1 9
1 10
1 5
3 10

样例输出

5
3
3

样例 解释

注意样例输出是严格正确的答案,但分别在 $[3,10], [2,6], [2, 6]$ 区间内的输出都是正确的。

子任务

对于 $100\%$ 的数据,保证 $1\le n,q\le 10^6$,$1\leq a_i\leq n$。

对于测试点 $1\sim 3$,保证 $n, q\leq 10^3$。

对于测试点 $4\sim 5$,保证 $n\leq 10^3$。

对于测试点 $6\sim 7$,保证 $n,q\leq 10^5$。

对于测试点 $8\sim 10$,无特殊限制。

提示

本题输入输出范围较大,可能需要使用 IO 优化,提供如下模板,并注意注释处是文件 IO 的开启位置:

using u32 = unsigned;
struct IO_Tp
{
    const static int _I_Buffer_Size = 30 << 20;
    char _I_Buffer[_I_Buffer_Size], *_I_pos = _I_Buffer;

    const static int _O_Buffer_Size = 8 << 20;
    char _O_Buffer[_O_Buffer_Size], *_O_pos = _O_Buffer;

    u32 m[10000];

    IO_Tp()
    {
//        freopen("ambiguous.in", "r", stdin);
//        freopen("ambiguous.out", "w", stdout);
        constexpr u32 e0 = '\0\0\0\1', e1 = '\0\0\1\0', e2 = '\0\1\0\0', e3 = '\1\0\0\0';
        int x = 0;
        for (u32 i = 0, c0 = '0000'; i != 10; ++i, c0 += e0)
            for (u32 j = 0, c1 = c0; j != 10; ++j, c1 += e1)
                for (u32 k = 0, c2 = c1; k != 10; ++k, c2 += e2)
                    for (u32 l = 0, c3 = c2; l != 10; ++l, c3 += e3)
                        m[x++] = c3;

        fread(_I_Buffer, 1, _I_Buffer_Size, stdin);
    }
    ~IO_Tp() { fwrite(_O_Buffer, 1, _O_pos - _O_Buffer, stdout); }

    IO_Tp &operator>>(int &res)
    {
        while (!isdigit(*_I_pos))
            ++_I_pos;
        res = *_I_pos++ - '0';
        while (isdigit(*_I_pos))
            res = res * 10 + (*_I_pos++ - '0');
        return *this;
    }

    IO_Tp &operator<<(int x)
    {
        if (x == 0)
        {
            *_O_pos++ = '0';
            return *this; 
        }
        static char _buf[35];
        char *_pos = _buf + 35;
        while (x >= 10000)
            *--reinterpret_cast<u32 *&>(_pos) = m[x % 10000], x /= 10000;
        *--reinterpret_cast<u32 *&>(_pos) = m[x];
        _pos += (x < 1000) + (x < 100) + (x < 10);
        _O_pos = std::copy(_pos, _buf + 35, _O_pos);
        return *this;
    }

    IO_Tp &operator<<(char ch)
    {
        *_O_pos++ = ch;
        return *this;
    }
} IO;