Pair Sum and Perfect Square

区间有多少

转换为二维数点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#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;
}