Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class trapezoid_va {
- public static void main(String[] args) {
- new trapezoid_va().run();
- }
- BufferedReader br;
- StringTokenizer in;
- PrintWriter out;
- public String nextToken() throws IOException {
- while (in == null || !in.hasMoreTokens())
- in = new StringTokenizer(br.readLine());
- return in.nextToken();
- }
- public int nextInt() throws IOException {
- return Integer.parseInt(nextToken());
- }
- public void solve() throws IOException {
- int n = nextInt();
- int[] a = new int[n];
- for (int i = 0; i < n; i++) {
- a[i] = nextInt();
- }
- Arrays.sort(a);
- int[] cnt = new int[n];
- int l = 0;
- cnt[0] = 1;
- for (int i = 1; i < cnt.length; i++) {
- if (a[i] == a[i - 1]) {
- cnt[l]++;
- } else {
- l++;
- a[l] = a[i];
- cnt[l] = 1;
- }
- }
- l++;
- a = Arrays.copyOf(a, l);
- cnt = Arrays.copyOf(cnt, l);
- long ans = 0;
- n = l;
- int[] sum = new int[n];
- for (int i = 0; i < n; i++) {
- sum[i] = (i > 0 ? sum[i - 1] : 0) + cnt[i];
- }
- for (int i = 0; i < n; i++) {
- if (cnt[i] == 1)
- continue;
- long mul = cnt[i] * (cnt[i] - 1) / 2;
- cnt[i] -= 2;
- l = 0;
- int r = 0;
- while (l < n - 1) {
- while (r < n && a[r] < a[l] + 2 * a[i])
- r++;
- int z = sum[r - 1] - sum[l];
- if (l + 1 <= i && i < r) {
- z -= cnt[i] + 2;
- ans += mul * cnt[l] * cnt[i] / 3;
- }
- ans += mul * z * cnt[l] / (l == i ? 3 : 1);
- l++;
- }
- for (int j = i + 1; j < sum.length; j++) {
- ans += mul * cnt[j] * (cnt[j] - 1) / 2;
- }
- ans += mul * cnt[i] * (cnt[i] - 1) / 12;
- cnt[i] += 2;
- }
- out.println(ans);
- }
- public void run() {
- try {
- br = new BufferedReader(new InputStreamReader(System.in));
- out = new PrintWriter(System.out);
- int t = nextInt();
- for (int i = 0; i < t; i++)
- solve();
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement