Chef was invited to the party of N people numbered from 1 to N. Chef knows the growth of all the people, i.e. he knows the growth of the ith person is denoted by an integer Ainot exceeding M.
Chef decided to have some fun. At first, he forms K groups of people. The ith group consists of all the people numbered from Li to Ri. Groups may overlap too.
For each group, Chef wants to know the following information: the maximum difference between the numberings of two people having same growth. Formally, Chef wants to know the following:
Please help Chef to have fun.
Input
There is only one test case in one test file.
The first line of input contains three space-separated integers N, M and K, denoting the number of people, the maximum growth and number of groups respectively. The second line contains N space-separated integers A1, A2, ..., AN denoting the growth of people. Then the ith line of the next K lines contains two space-separated integers Li, Ri, denoting the ith group.
Output
For each group, output the integer denoting the maximum difference between numbering of two people having same growth in a single line.
Constraints and Subtasks
- 1 ≤ Ai ≤ M
- 1 ≤ Li ≤ Ri ≤ N
Subtask 1: 20 points
- 1 ≤ N, M, K ≤ 1000 = 103
Subtask 2: 80 points
- 1 ≤ N, M, K ≤ 100000 = 105
Example
Input: 7 7 5 4 5 6 6 5 7 4 6 6 5 6 3 5 3 7 1 7 Output: 0 0 1 1 6
Explanation
Group 1. There is only one person in the group. Thus the maximum difference of numbers should be 0.
Group 2. There are two persons in the group. Their growth are A5 = 5 and A6 = 7. Thus there is no pair of persons who have the same growth. Thus the answer for this group will also be 0.
Group 3. There are three persons in the group. Their growth are A3 = 6, A4 = 6 and A5= 5. Here person 3 and person 4 has the same growth. Thus the answer is |4 − 3| = 1.
Group 4. There are more persons than the group 3. But they has different growth, other than person 3 and person 4.Thus the answer is also |4 − 3| = 1.
Group 5. This group contains all the people, and person 1 and person 7 has the same growth A1 = A7 = 4. So the answer is |7 − 1| = 6.
题意:有N个数,每个数范围在[1,m],K个询问,每个询问给出两个数L,R,要求找出区间[L,R]内同一个数字的最远距离。
思路:分块处理,1e5分的块长度100左右可以保证效率,计算L,R时,取L,R中间预处理好的块的答案,再枚举左右两边的数即可。
# include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+3;
int a[maxn], b[1200][1200], vis[maxn];
vector<int>v[maxn];
int main()
{
int n, m, k, x, y;
scanf("%d%d%d",&n,&m,&k);
for(int i=0; i<n; ++i) scanf("%d",&a[i]);
int len = 92;
int num = (n+len-1)/len;
for(int i=0; i<n; i+=len)
{
int imax = 0;
memset(vis, -1, sizeof(vis));
for(int j=i; j<n; ++j)
{
if(j%len==0) b[i/len][j/len] = imax;
if(vis[a[j]] == -1) vis[a[j]] = j;
else imax = max(imax, j-vis[a[j]]);
}
b[i/len][num] = imax;
}
for(int i=0; i<n; ++i)
v[a[i]].push_back(i);
while(k--)
{
scanf("%d%d",&x,&y);
--x;--y;
int l=(x+len)/len*len;
int r = y/len*len;
int ans = b[l/len][r/len];
for(int i=x; i<l; ++i)
{
int pos = lower_bound(v[a[i]].begin(), v[a[i]].end(), y)-v[a[i]].begin()-1;
if(pos < 0) continue;
ans = max(ans, v[a[i]][pos]-i);
}
for(int i=y; i>=r; --i)
{
int pos = lower_bound(v[a[i]].begin(), v[a[i]].end(), x)-v[a[i]].begin();
if(pos == v[a[i]].size()) continue;
ans = max(ans, i-v[a[i]][pos]);
}
printf("%d\n",ans);
}
return 0;
}