求
转换为二维数点
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;
const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;
#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))
#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
using ll=long long;
int tt,n=1e5,q,fk,a[(int)1e5+9],belong[(int)1e5+9],ans[(int)1e5+9],cnt[(int)1e5+9],sum,wz[(int)1e5+9];
vector<int> sq;
struct BIT
{
vector<ll> a;
int n;
BIT(int x){
n=x;
a.resize(n+1);
fill(a.begin(),a.end(),0ll);
}
void add(int x,int k){
for(int i=x;i<=n;i+=lowbit(i))a[i]+=k;
}
ll query(int x){
ll res=0ll;
for(int i=x;i>0;i-=lowbit(i))res+=a[i];
return res;
}
};
void solve(){
sum=0ll;
cin>>n;
BIT b(n);
for(int i=1;i<=n;++i){
cin>>a[i];
cnt[i]=0,wz[a[i]]=i;
}
cin>>q;
vector<array<int,2>> Q[(int)1e5+9];
for(int i=1,x,y;i<=q;++i){
ans[i]=0ll;
cin>>x>>y;
Q[y].push_back({y,i});
Q[x-1].push_back({x-1,i});
Q[x-1].push_back({y,-i});
Q[y].push_back({x-1,-i});
}
//cerr<<"??\n";
for(int i=0;i<=n;++i){
if(i){
for(auto it:sq){
int x=it-a[i];
if(x<=0)continue;
if(wz[x]>=wz[a[i]])continue;
if(x>n)break;
b.add(wz[x],1);
}
}
// cerr<<"??\n";
for(auto [bb,cc]:Q[i]){
if(cc>0){
ans[cc]+=b.query(bb);
}else ans[-cc]-=b.query(bb);
}
// cerr<<"!!!\n";
}
for(int i=1;i<=q;++i)cout<<ans[i]<<"\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
for(int i=1;i*i<=2*n-1;++i){
sq.push_back(i*i);
}
//cerr<<sq.size();
cin>>tt;
while(tt--){
solve();
}
//cout<<clock()*1.0/CLOCKS_PER_SEC<<endl;
return 0;
}