5 2 3
1
3
4
3
0
0 1
1 3
0 4

1
3
6

# solution:

## code:

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<cstring>
using namespace std;
struct node{
int x,y,z;
}ques[50005];
bool cmp1(node x,node y){
return x.x<y.x||x.x==y.x&&x.y<y.y;
}
bool cmp2(node x,node y){
return x.y<y.y||x.y==y.y&&x.x<y.x;
}
int n,k,q,bit[50005],a[50005],b[50005],cnt,num,belong[50005],l[50005],r[50005],nowx,nowy,Max[500005],Min[500005];
int lowbit(int x){
return x&-x;
}
while(x<=cnt){
bit[x]+=y;
x+=lowbit(x);
}
}
int query(int x){
int ans=0;
while(x){
ans+=bit[x];
x-=lowbit(x);
}
return ans;
}
long long ans,aa[50005];
int main(){
scanf("%d%d%d",&n,&k,&q);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
a[i]++;
b[i]=a[i];
}
sort(b+1,b+1+n);
cnt=unique(b+1,b+1+n)-b-1;
for(int i=1;i<=n;i++){
int tmp=a[i];
a[i]=lower_bound(b+1,b+1+cnt,a[i])-b;
if(!Min[a[i]])
Min[a[i]]=lower_bound(b+1,b+1+cnt,tmp-k)-b;
if(!Max[a[i]])
Max[a[i]]=upper_bound(b+1,b+1+cnt,tmp+k)-b-1;
}
for(int i=1;i<=q;i++){
scanf("%d%d",&ques[i].x,&ques[i].y);
ques[i].x++;
ques[i].y++;
ques[i].z=i;
}
sort(ques+1,ques+1+q,cmp1);
num=sqrt(n);
for(int i=1;i<=q;i++){
int x=(ques[i].x-1)/num+1;
if(x>num)
x=num;
if(!l[x])
l[x]=i;
r[x]=i;
}
nowx=1;
nowy=1;
ans=0;
for(int i=1;i<=num;i++){
if(!l[i])
continue;
sort(ques+l[i],ques+r[i]+1,cmp2);
for(int j=l[i];j<=r[i];j++){
while(nowy<ques[j].y){
ans+=(long long)query(min(cnt,Max[a[nowy+1]]))-query(max(0,Min[a[nowy+1]]-1));
nowy++;
}
while(nowx>ques[j].x){
ans+=(long long)query(min(cnt,Max[a[nowx-1]]))-query(max(0,Min[a[nowx-1]]-1));
nowx--;
}
while(nowy>ques[j].y){
ans-=(long long)query(min(cnt,Max[a[nowy]]))-query(max(0,Min[a[nowy]]-1));
nowy--;
}
while(nowx<ques[j].x){
ans-=(long long)query(min(cnt,Max[a[nowx]]))-query(max(0,Min[a[nowx]]-1));
nowx++;
}
aa[ques[j].z]=ans;
}
}
for(int i=1;i<=q;i++)
printf("%lld\n",aa[i]);
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120