http://acm.hdu.edu.cn/showproblem.php?pid=6534
题意:给n个数的数组,m个询问L-R,k;问区间内差值不超过k的数有多少对;
思路:离散化,莫队+树状数组维护;
反思:树状数组维护每次指针移动贡献的变化。离散化没学好,离散化就是把数的相对位置处理出来,再操作。把一个离散化了的数减一个没离散的=-=。因为维护时查询R和L-1;所以还要离散化每个元素+k和-k-1的数,还要先处理它们的上下界,每次查询都lower_bound上下界会tle; 有maxn个数,但是每个元素又多两个+-k的,所以树状数组最大值要大三倍,不然会wa;
#include<algorithm>
#include<set>
#include<queue>
#include<cmath>
#include<cstring>
#include<iostream>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<cstdio>
#include<map>
#include<stack>
#include<string>
#include<bits/stdc++.h>
using namespace std;
#define sfi(i) scanf("%d",&i)
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-16
#define PI acos(-1)
#define lowbit(x) ((x)&(-x))
#define zero(x) (((x)>0?(x):-(x))<eps)
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define endl '\n'
#define pb push_back`
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int maxn=3e4+9;
const int mod=1e9+7;
inline ll read()
{
ll f=1,x=0;
char ss=getchar();
while(ss<'0'||ss>'9')
{
if(ss=='-')f=-1;ss=getchar();
}
while(ss>='0'&&ss<='9')
{
x=x*10+ss-'0';ss=getchar();
} return f*x;
}
struct Mo
{
int l,r;
int id;
}q[maxn];
int Be[maxn];
int n,m,k;
int ans[maxn];
int tree[maxn*4];
int upk[maxn];
int lowk[maxn];
int mp[maxn*4];
int b[maxn];
int a[maxn];
int cnt=0;
int num;
int res;
bool cmp(Mo x,Mo y)
{
if(Be[x.l]==Be[y.l])
{
return x.r<y.r;
}
return x.l<y.l;
}
void add(int x,int v)
{
while(x<=maxn*3)//不乘会wa!!!
{
tree[x]+=v;
x+=lowbit(x);
}
}
int get(int x)
{
int ans=0;
while(x)
{
ans+=tree[x];
x-=lowbit(x);
}
return ans;
}
int main()
{
FAST_IO;
//freopen("input.txt","r",stdin);
cin>>n>>m>>k;
//n=read();
//m=read();
//k=read();
int u=sqrt(n);
for(int i=1;i<=n;i++)
{
//int x=read();
int x;
cin>>x;
a[i]=x;
mp[++cnt]=x;
mp[++cnt]=x-k-1;
mp[++cnt]=x+k;
Be[i]=i/u+1;
}
sort(mp+1,mp+1+cnt);
num=unique(mp+1,mp+1+cnt)-mp-1;
int pos;
for(int i=1;i<=n;i++)
{
pos=lower_bound(mp+1,mp+1+num,a[i]+k)-mp;
upk[i]=pos;
pos=lower_bound(mp+1,mp+1+num,a[i]-k-1)-mp;
lowk[i]=pos;
pos=lower_bound(mp+1,mp+1+num,a[i])-mp;
b[i]=pos;
}
for(int i=1;i<=m;i++)
{
//q[i].l=read();
//q[i].r=read();
cin>>q[i].l;
cin>>q[i].r;
q[i].id=i;
}
sort(q+1,q+m+1,cmp);
int l=1;
int r=0;
for(int i=1;i<=m;i++)
{
//cout<<q[i].l<<" "<<q[i].r<<endl;
while(r<q[i].r)
{
//int ri=lower_bound(mp+1,mp+1+num,mp[b[r+1]]+k)-mp;
//int le=lower_bound(mp+1,mp+1+num,mp[b[r+1]]-k-1)-mp;
//res+=get(b[r+1]+k)-get(b[r+1]-k-1);
res+=get(upk[r+1])-get(lowk[r+1]);
add(b[r+1],1);
r++;
}
while(r>q[i].r)
{
add(b[r],-1);
//int ri=lower_bound(mp+1,mp+1+num,mp[b[r]]+k)-mp;
//int le=lower_bound(mp+1,mp+1+num,mp[b[r]]-k-1)-mp;
res-=get(upk[r])-get(lowk[r]);
//res-=get(b[r]+k)-get(b[r]-k-1);
r--;
}
while(l<q[i].l)
{
//res-=get(b[l]+k)-get(b[l]-k-1);
add(b[l],-1);
//int ri=lower_bound(mp+1,mp+1+num,mp[b[l]]+k)-mp;
//int le=lower_bound(mp+1,mp+1+num,mp[b[l]]-k-1)-mp;
//res-=get(b[l]+k)-get(b[l]-k-1);
res-=get(upk[l])-get(lowk[l]);
l++;
}
while(l>q[i].l)
{
//int ri=lower_bound(mp+1,mp+1+num,mp[b[l-1]]+k)-mp;
//int le=lower_bound(mp+1,mp+1+num,mp[b[l-1]]-k-1)-mp;
//res+=get(b[l-1]+k)-get(b[l-1]-k-1);
res+=get(upk[l-1])-get(lowk[l-1]);
add(b[l-1],1);
l--;
}
ans[q[i].id]=res;
//cout<<q[i].l<<" "<<q[i].r<<" "<<ans[q[i].id]<<endl;
}
for(int i=1;i<=m;i++)
{
//cout<<ans[i]<<endl;
printf("%d\n",ans[i]);
}
return 0;
}