# Untitled

user_5379400
plain_text
3 days ago
1.7 kB
1
Indexable
Never
```#include<bits/stdc++.h>
using namespace std;

int n;
long long a[2005];
int st[4 * 2005];
map<long long, vector<pair<int, int>>> mp;

void update(int id, int l, int r, int i, int v){
if (i < l || i > r) return;
if (l == r){
st[id] += v;
if (v == 0) st[id] = 0;
return;
}
int mid = (l + r) >> 1;
update(2 * id, l, mid, i, v);
update(2 * id + 1,mid + 1, r, i, v);
st[id] = st[2 * id] + st[2 * id + 1];
}
int get(int id, int l, int r, int u, int v){
if (u > v || r < u || l > v) return 0;
if (l >= u && r <= v) return st[id];
int mid = (l + r) >> 1;
return get(2 * id, l, mid, u, v) + get(2 * id + 1, mid + 1, r, u, v);
}
//7
//1 1 1 1 3 10 30
void solve(int testcase){
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + 1 + n);
mp.clear();
for (int i = 1; i <= n; i++){
for (int j = i + 1; j <= n; j++){
mp[a[i] * a[j]].push_back({i, j});
}
}
int ans = 0;
for (map<long long, vector<pair<int, int>>>:: iterator it = mp.begin(); it != mp.end(); ++it){
vector<pair<int, int>> u = it -> second;
int sz = u.size();
if (sz == 0) continue;
sort(u.begin(), u.end());
for (int i = 1; i < sz; i++){
if (u[i].first != u[i - 1].first){
int p = i - 1;
while(p >= 0 && u[p].first == u[i - 1].first) update(1, 1, n, u[p].second, 1), p--;
}
ans += get(1, 1, n, u[i].second + 1, n);
}
for (int i = 0; i < sz; i++) update(1, 1, n, u[i].second, 0);
}
cout << ans << '\n';
}

int main(){
//freopen("input.txt", "r", stdin);
int t = 1;
cin >> t;
for (int i = 1; i <= t; i++) solve(i);
}```
Leave a Comment