POJ_3419 Difference Is Beautiful(ST表)

Difference Is Beautiful

Time Limit: 5000MS
Memory Limit: 65536K
Problem Description

Mr. Flower’s business is growing much faster than originally planned. He has now become the CEO of a world-famous beef corporation. However, the boss never lives a casual life because he should take charge of the subsidiary scattered all over the world. Every year, Mr. Flower needs to analyze the performance reports of these subsidiary companies.

Mr. Flower has N companies, and he numbered them with 0 to N – 1. All of the companies will give Mr. Flower a report about the development each year. Among all of the tedious data, only one thing draws Mr. Flower’s attention – the turnover. Turnover of a company can be represented as an integer Pi: positive one represents the amount of profit-making while negative for loss-making.

In fact, Mr. Flower will not be angry with the companies running under deficit. He thinks these companies have a large room for future development. What dissatisfy him are those companies who created the same turnover. Because in his eyes, keeping more than one companies of the same turnover is not necessary.

Now we know the annual turnover of all companies (an integer sequence Pi, the ith represents the turnover of the ith company this year.). We say a number sequence is perfect if all of its numbers are different from each other. Mr. Flower wants to know the length of the longest consecutive perfect sequence in a certain interval [L, R] of the turnover sequence, can you help him?

Input

The first line of the input contains two integers N and M. N is the number of companies. M is the number of queries. (1 ≤ N, M ≤ 200000). The second line contains N integer numbers not exceeding 106 by their absolute values. The ith of them represents the turnover of the ith company this year. The following M lines contain query descriptions, each description consists of two numbers: L, R (0 ≤ L ≤ R ≤ N – 1) and represents the interval that Mr. Flower concerned.

Output

The output contains M lines. For each query, output the length of the longest consecutive perfect sequence between [L, R]

Sample Input

9 2
2 5 4 1 2 3 6 2 4
0 8
2 6

Sample Output

6
5

题意

有一个长度为n的序列,有m个询问,每个询问包含两个整数L,R,求区间[L,R]内最长的子串,子串不能包含重复的元素。

题解:

区间元素长度最长的子串,可以预处理出每个左端点i,满足区间元素互异,最多能到达的位置r[i]。设 l e n [ i ] = r [ i ] − i + 1 len[i] = r[i]-i+1 len[i]=r[i]i+1注意:数组r是非递减的。
对于询问区间[L,R], 其结果分两种情况,一种是完整的 [ i , r [ i ] ] [ i,r[i] ] [i,r[i]]的区间,另一种是 [ i , R ] [i,R] [i,R]的区间。
结果可能是一个完整的最长子串,也可能是最长子串的一部分。所以考虑[L,R]区间内,分别以每个点为起点。设pos为最靠左的r[i]>R的位置,即 r [ p o s ] > R r[pos]>R r[pos]>R && r [ p o s − 1 ] < = R r[pos-1]<=R r[pos1]<=R.

  • L < = i < p o s L<=i<pos L<=i<pos,即 r [ i ] < R r[i]<R r[i]<R,可以利用ST表来找区间的长度( l e n [ i ] len[i] len[i])最大值;
  • p o s < = i < = R pos<=i<=R pos<=i<=R,则所有的只能取部分,每个i对应长度为 R − i + 1 R-i+1 Ri+1,显然当 i = p o s i=pos i=pos时,长度最大。

对于每个询问按上述处理即可,注意pos可能不在[L,R]区间内的情况。(线段树由于常数大可能会T)

#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<ctype.h>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define eps 1e-6
 
using namespace std;
typedef long long LL;   
typedef pair<int, int> P;
const int maxn = 200100;
const int mod = 1000000007;
int a[maxn], len[maxn], r[maxn], st[maxn][20], b[maxn*10];
void st_init(int n);
int get(int l, int r);

int main()
{
    int n, m, i, j, k, rmax, mid = 1000000;
    scanf("%d %d", &n, &m);
    for(i=0;i<n;i++)
        scanf("%d", &a[i]);
    rmax = n-1;
    for(i=n-1;i>=0;i--)
    {
        //也可以用map,时间会慢几百ms,但空间复杂度会小很多
        if(b[mid+a[i]] != 0)rmax = min(rmax, b[mid+a[i]]-1);
        r[i] = rmax;
        b[mid+a[i]] = i;
        st[i][0] = rmax-i+1;
    }
    st_init(n);
    while(m--)
    {
        int L, R, ans = 0;
        scanf("%d %d", &L, &R);
        int pos = upper_bound(r, r+n, R)-r;
        if(pos-1>=L)ans = get(L, pos-1);
        ans = max(ans, R-max(pos,L)+1);
        printf("%d\n", ans);
    }
    return 0;
}

int get(int l, int r)
{
    int k = log(1.0*r-l+1)/log(2.0);
    return max(st[l][k], st[r-(1<<k)+1][k]);
}

void st_init(int n)
{
    int i, j;
    for(j=1;j<19;j++)
        for(i=0;i<n;i++)
            if(i+(1<<j)-1<n)
                st[i][j] = max(st[i][j-1], st[i+(1<<(j-1))][j-1]);
            else break;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值