「一本通 4.2 例 3」与众不同 (loj10121)

题目描述

A 是某公司的 CEO,每个月都会有员工把公司的盈利数据送给 A,A 是个与众不同的怪人,A 不注重盈利还是亏本,而是喜欢研究「完美序列」:一段连续的序列满足序列中的数互不相同。

A 想知道区间 [L,R][ 之间最长的完美序列长度。

输入格式

第一行两个整数 N,MN 表示连续 N 个月,编号为 0N−1M 表示询问的次数;

第二行 N个整数,第 i 个数表示该公司第 i 个月的盈利值 aii​​;

接下来 M 行每行两个整数 L,R,表示 A 询问的区间。

输出格式

输出 M 行,每行一个整数对应询问区间内的完美序列的最长长度。

样例
样例输入
9 2
2 5 4 1 2 3 6 2 4
0 8
2 6
样例输出
6
5
数据范围与提示

对于全部数据,1N,M2×10^5,0LRN1,|ai| <=10^6

 

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn = 200010;
inline void qread(int &x){
    x = 0;
    register int ch = getchar(), flag = 0;
    while(ch < '0' || ch > '9')    {
        if(ch == '-')    flag = 1;
        ch =getchar();
    }
    while(ch >='0' && ch <='9'){
        x = 10 * x + ch - 48;
        ch = getchar();
    }
    if(flag)    x = -x;
}
int n, m;
int data[maxn];
int llog[maxn];
int st[maxn];
int last[maxn];
int f[maxn][25];
int flast[maxn*10];
int main(void)
{
    qread(n);
    qread(m);
    for(int i=1; i<=n; ++i)    {
        qread(data[i]);
        last[i] = flast[data[i] + 1000010];
        flast[data[i] + 1000010] = i;
    }
    for(int i=2; i<=n; ++i)
        llog[i] = llog[i/2] + 1;
    for(int i=1; i<=n; ++i)
        st[i] = max(st[i-1], last[i] + 1);
    for(int i=1; i<=n; ++i)
        f[i][0] = i - st[i] + 1;
    for(int j=1; j<=llog[n]; ++j)
        for(int i=1; i + (1 << (j-1)) <= n; ++i)
            f[i][j] = max(f[i][j-1], f[i + (1 << (j-1))][j-1]);
    while(m--){
        int a, b;
        qread(a), qread(b);
        a++, b++;
        int l = a, r = b;
        while(l < r){
            int mid = (l + r) >> 1;
            if(st[mid] < a)        l = mid + 1;    
            else                r = mid;
        }
        if(st[l] >= a)    printf("%d\n", max(l - a, max(f[l][llog[b - l + 1]], f[b - (1 << llog[b - l + 1]) + 1][llog[b - l + 1]])));
        else            printf("%d\n", b - a + 1);
    }                
}

 

思路:

  ST表加二分。此题预处理极其复杂,依次处理出log[n],完美数列的起始位置,以及ST表。需要注意数据范围中ai的绝对值!!!否则极易RE

i​​106​​。5​​,0LRN1,ai​​106​​。50LRN1,ai​​106​​。​​,0LRN1,ai​​106​​。

转载于:https://www.cnblogs.com/junk-yao-blog/p/9474314.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值