fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. vector<int> a;
  31.  
  32. int consistency1(int n){
  33.  
  34. int ans = 0;
  35.  
  36. for(int i=0; i<=n-4; i++){
  37. for(int j=i+1; j<=n-3; j++){
  38.  
  39. unordered_map<int,int> sufixMp;
  40. for(int k=n-1; k>=j+2; k--){
  41. sufixMp[a[k]]++;
  42. }
  43.  
  44. for(int k=j+1; k<=n-2; k++){
  45. int sum = a[i] + a[j] + a[k];
  46.  
  47. if(sufixMp.count(-sum)){
  48. ans += sufixMp[-sum];
  49. }
  50. sufixMp[a[k+1]]--;
  51. if(sufixMp[a[k+1]] == 0) sufixMp.erase(a[k+1]);
  52. }
  53. }
  54. }
  55.  
  56. return ans;
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63. int consistency2(int n){
  64.  
  65. int ans = 0;
  66.  
  67. sort(begin(a), end(a));
  68.  
  69. for(int i=0; i<=n-4; i++){
  70. for(int j=i+1; j<=n-3; j++){
  71.  
  72. int sum = a[i]+a[j];
  73.  
  74. int t = -sum;
  75.  
  76. int s = j+1, e = n-1;
  77. while(s<e){
  78. if(a[s] + a[e] == t){
  79.  
  80. //* t = 8 [4 4 4 4 4 ] => 5C2
  81. if(a[s] == a[e]){
  82. int ct = e-s+1;
  83. ans += (ct*(ct-1))/2;
  84. break;
  85. }
  86.  
  87. int ct1 = 0;
  88. int x1 = a[s];
  89. while(s<=e && a[s] == x1){
  90. ct1++;
  91. s++;
  92. }
  93.  
  94. int ct2 = 0;
  95. int x2 = a[e];
  96. while(s<=e && a[e] == x2){
  97. ct2++;
  98. e--;
  99. }
  100.  
  101. ans += ct1 * ct2;
  102. }
  103. else if(a[s] + a[e] > t) e--;
  104. else s++;
  105. }
  106.  
  107. }
  108. }
  109.  
  110. return ans;
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117. int consistency3(int n){
  118.  
  119. int ans = 0;
  120.  
  121. unordered_map<int,int> mp;
  122.  
  123. for(int k=2; k<=n-2; k++){
  124. for(int l=k+1; l<=n-1; l++){
  125. int sum = a[k]+a[l];
  126. mp[sum]++;
  127. }
  128. }
  129.  
  130. for(int j=1; j<=n-3; j++){
  131.  
  132. for(int i=j-1; i>=0; i--){
  133. int sum = a[i]+a[j];
  134. ans += mp[-sum];
  135. }
  136.  
  137. for(int k=j+2; k<=n-1; k++){
  138. int sum = a[j+1] + a[k];
  139. mp[sum]--;
  140. if(mp[sum] == 0) mp.erase(sum);
  141. }
  142.  
  143. }
  144.  
  145.  
  146.  
  147. return ans;
  148. }
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. int practice(int n){
  174.  
  175.  
  176. return 0;
  177. }
  178.  
  179.  
  180.  
  181.  
  182.  
  183. void solve() {
  184.  
  185. int n;
  186. cin>> n;
  187.  
  188. a.resize(n);
  189. for(int i=0; i<n; i++) cin >> a[i];
  190.  
  191. cout << consistency1(n) << " " << consistency2(n) << " " << consistency3(n) << endl;
  192.  
  193.  
  194. }
  195.  
  196.  
  197.  
  198.  
  199.  
  200. int32_t main() {
  201. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  202.  
  203. int t = 1;
  204. cin >> t;
  205. while (t--) {
  206. solve();
  207. }
  208.  
  209. return 0;
  210. }
Success #stdin #stdout 0s 5324KB
stdin
2
7
1 2 3 4 -1 -2 -2
6
5 10 -5 -5 -10 -10
stdout
3 3 3
4 4 4