UVA11235 Frequent values(分块+RMQ)

原题链接

将相同的数分到一块中
loc[i]表示当前ai属于那一块
l[i]表示第i块的左边界
r[i]表示第i块的右边界
sum[i]表示第i块的元素个数

ans=max(r[loc[x]] - x + 1,y - l[loc[y]] + 1,RMQ(loc[r[loc[x]] + 1], loc[l[loc[y]] - 1]))

即搜索目标区间左、右的残缺块元素个数+中间完整块的区间最值

注意特判:x,y属于同一块:cout << y - x + 1 << ‘\n’;

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <map>
#include <queue>
#define ll long long
#include<set>
#include<string.h>
#include<istream>
using namespace std;
inline ll read() {
    ll k = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { k = k * 10 + ch - '0'; ch = getchar(); }return k * f;
}


ll bin[30];
ll Log[100005];
ll dp1[30][100005];
ll n;
ll a[100005];
ll l[100005];
ll r[100005];
ll loc[100005];
ll sum[100005];

void ST1(ll p)
{
    for (int i = 1; i <= p; i++)
        dp1[0][i] = sum[i];
    for (int i = 1; i <= Log[p]; i++)
        for (int j = 1; j <= p; j++)
            if (j + bin[i] - 1 <= p)
                dp1[i][j] = max(dp1[i - 1][j], dp1[i - 1][j + bin[i - 1]]);
}

ll query(ll x, ll y)
{
    if (x > y)return 0;
    ll t = Log[y - x + 1];
    return max(dp1[t][x], dp1[t][y - bin[t] + 1]);
}
int main()
{
    ll q;
    bin[0] = 1;
    for (int i = 1; i < 20; i++)
        bin[i] = bin[i - 1] * 2;
    Log[0] = -1;
    for (int i = 1; i <= 100003; i++)
        Log[i] = Log[i / 2] + 1;
    while (1)
    {
        memset(loc, 0, sizeof(loc));
        memset(l, 0, sizeof(l));
        memset(r, 0, sizeof(r));
        memset(sum, 0, sizeof(sum));
        n = read();
        if (n == 0)break;
        q = read();
        a[0] = -1000005;
        for (int i = 1; i <= n; i++)a[i] = read();
        ll p = 0;
        for (int i = 1; i <= n;)
        {
            p++;
            l[p] = i;
            loc[i] = p;
            i++;
            sum[p] = 1;
            while (a[i] == a[i - 1])
            {
                loc[i] = p;
                sum[p]++;
                i++;
            }
            r[p] = i - 1;
        }
        memset(dp1, 0, sizeof(dp1));
        ST1(p);
        while (q--)
        {
            ll x = read(), y = read();
            if (loc[x] == loc[y])
            {
                cout << y - x + 1 << '\n';
            }
            else
            {

                ll ans = 0;
                ans = max(ans, r[loc[x]] - x + 1);
                ans = max(ans, y - l[loc[y]] + 1);
                ans = max(ans, query(loc[r[loc[x]] + 1], loc[l[loc[y]] - 1]));
                cout << ans << '\n';
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shihao Weng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值