题目: 题意:给定序列,求[l,r]区间内数字相同的数的最远距离。

链接:https://www.codechef.com/problems/QCHEF

#include<bits/stdc++.h>
#define LL long long
#define ULL unsigned long long
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define dep(i,j,k) for(int i=k;i>=j;i--)
#define INF 0x3f3f3f3f
#define mem(i,j) memset(i,j,sizeof(i))
#define make(i,j) make_pair(i,j)
#define pb push_back
using namespace std;
const int N=1e5+;
int a[N],pos[N],mi[N],ma[N],t[N],ans[N],n,m,k,block,tmp;
struct noq {
int l,r,id;
}q[N];
bool cmp(noq a,noq b) {
return pos[a.l]==pos[b.l]?a.r<b.r:pos[a.l]<pos[b.l];
}
void add(int x) {
mi[a[x]]=min(mi[a[x]],x);
ma[a[x]]=max(ma[a[x]],x);
tmp=max(tmp,ma[a[x]]-mi[a[x]]);
}
int query(int l,int r) {
int mx=;
rep(j,l,r)
t[a[j]]=0x3f3f3f3f;
rep(j,l,r){
t[a[j]]=min(t[a[j]],j);
mx=max(mx,j-t[a[j]]);
}
return mx;
}
int slove(int qnum,int bnum) {
int i=qnum; int L=min(bnum*block,n); int l=L+,r=L;
mem(ma,-); mem(mi,); tmp=;
for(;pos[q[i].l]==bnum;i++) {
if(pos[q[i].l]==pos[q[i].r]) {
ans[q[i].id]=query(q[i].l,q[i].r); continue;
}
while(r<q[i].r) add(++r);
int mx=;
rep(j,q[i].l,l) t[a[j]]=0x3f3f3f3f;
rep(j,q[i].l,l) {
t[a[j]]=min(t[a[j]],j);
mx=max(mx,max(j-t[a[j]],ma[a[j]]-j));
}
ans[q[i].id]=max(mx,tmp);
}
return i;
}
int main() {
scanf("%d %d %d",&n,&m,&k); block=sqrt(n);
rep(i,,n) {
scanf("%d",&a[i]); pos[i]=(i-)/block+;
}
rep(i,,k) {
scanf("%d %d",&q[i].l,&q[i].r); q[i].id=i;
}
sort(q+,q++k,cmp);
int up=pos[k]; int p=;
rep(i,,up) p=slove(p,i);
rep(i,,k) printf("%d\n",ans[i]);
return ;
}
05-26 02:54