Difference Is Beautiful
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit Status Practice POJ 3419
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 followingM 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
//模拟的方法: #include<cstdio> #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int up=1000000; int p,q,i; bool vis[2*up+2];//记录有没有访问过 int dis[2*up+2];//到该点i的最长连续长度 int f[2*up+2]; //记录前一个(最长连续串的终点位置+1),即f[i]-1 int a[2*up+2];// 读入使用的数组 int main() { scanf("%d%d",&p,&q); for(i=1;i<=p;i++) scanf("%d",&a[i]); memset(vis,0,sizeof(vis)); f[0]=1; dis[0]=0; for(i=1;i<=p;i++) { if (!vis[a[i]+up])//如果没有被访问过 { dis[i]=dis[i-1]+1;//那么长度+1 vis[a[i]+up]=1;//标记访问过 f[i]=f[i-1];//前一个最长串的终点+1 } else { int start=i-dis[i-1]; while(a[start]!=a[i])//将重复的点前的点全部还原成未访问 { vis[a[start]+up]=0; start++; } dis[i]=i-start;//从重复点后面的都可以利用 f[i]=i;//前一个连续串的终点是i-1 } } for(;q>0;q--) { int l,r,ans; scanf("%d%d",&l,&r); l++; r++; ans=0; while(r-l+1>ans)//根据f数组来确定答案 { ans=max(ans,min(dis[r],r-l+1)); r=f[r]-1; } printf("%d\n",ans); } return 0; }