Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 2247 | Accepted: 703 |
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
Hint
Source
单调性+二分+RMQ;
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define M 1000000
using namespace std;
const int N = 200000+10;
int la[M*2+100],dp[N],a[N],mm[N][25],mi[N][25];
int n,m;
void RMQ() {
for(int i=0; i<n; i++) {
mm[i][0]=mi[i][0]=la[i];
}
for(int j=1; j<20; j++) {
for(int i=0; i<n; i++) {
if(i+(1<<j)-1<n) {
mm[i][j]=max(mm[i][j-1],mm[i+(1<<(j-1))][j-1]);
mi[i][j]=min(mi[i][j-1],mi[i+(1<<(j-1))][j-1]);
}
}
}
}
int max_rmq(int x,int y) {
int cnt=(int)(log((double)(y-x+1))/log(2.0));
return max(mm[x][cnt],mm[y-(1<<cnt)+1][cnt]);
}
int main() {
while(scanf("%d%d",&n,&m)!=EOF) {
for(int i=0; i<n; i++) {
scanf("%d",&a[i]);
}
memset(la,-1,sizeof(la));
la[a[0]+M]=0;
dp[0]=0;
for(int i=1; i<n; i++) {
if(dp[i-1]>la[a[i]+M])
dp[i]=dp[i-1];
else
dp[i]=la[a[i]+M]+1;
la[a[i]+M]=i;
}
for(int i=0; i<n; i++) {
la[i]=i-dp[i]+1;
}
RMQ();
int x,y;
while(m--) {
scanf("%d%d",&x,&y);
int temp=lower_bound(dp+x,dp+y+1,x)-dp;
temp--;
int ans=temp-x+1;
if(temp+1<=y) {
ans=max(ans,max_rmq(temp+1,y));
}
printf("%d\n",ans);
}
}
return 0;
}
题目地址:http://poj.org/problem?id=3419