Difference Is Beautiful

Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
 

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 [LR] of the turnover sequence, can you help him?

Input

The first line of the input contains two integers N and MN is the number of companies. M is the number of queries. (1 ≤ NM ≤ 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: LR (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 [LR]  

Sample Input

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

Sample Output

6
5
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int N=200005;
const int M=1000000;
int n,m;
int a[N];
bool vis[2000005];
int cnt[N],r[N];
void solve()
{
    int tt,pp;
   memset(vis,0,sizeof(vis));
   cnt[0]=1;
   r[0]=0;
   vis[a[0]+M]=1;
   for(int i=1;i<n;i++)
   {
       tt=a[i]+M;
       if(!vis[tt])
       {
           vis[tt]=1;
           cnt[i]=cnt[i-1]+1;
           r[i]=r[i-1];
       }
       else
       {
           pp=i-cnt[i-1];
           while(a[pp]!=a[i])
           {
               vis[a[pp]+M]=0;
               pp++;
           }
            cnt[i]=i-pp;
            r[i]=i;

       }

   }

}
int main()
{

    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    solve();
    while(m--)
    {
        int f,e;
        int ans=0;
        scanf("%d%d",&f,&e);
        while(1)
        {
             if(e-f+1<=ans) break;
             ans=max(ans,min(cnt[e],e-f+1));
             e=r[e]-1;
        }
       printf("%d
",ans);

    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/iwantstrong/p/5733180.html