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, b, c, d;
  31.  
  32. //* Hashing + 2 Ptr => O(n^3)
  33. int consistency1(int n){
  34.  
  35. sort(begin(c), end(c));
  36. sort(begin(d), end(d));
  37.  
  38. int ans = 0;
  39. for(int i=0; i<n; i++){
  40. for(int j=0; j<n; j++){
  41. int sum = a[i]+b[j];
  42.  
  43. int ct = 0;
  44. int s = 0, e = n-1;
  45.  
  46. while(s<n && e>=0){
  47. if(c[s] + d[e] == -sum ){
  48. int x = c[s];
  49. int f1 = 0;
  50. while(s<n && c[s] == x){
  51. f1++;
  52. s++;
  53. }
  54.  
  55. int y = d[e];
  56. int f2 = 0;
  57. while(e>=0 && d[e] == y){
  58. f2++;
  59. e--;
  60. }
  61.  
  62. ct += f1*f2;
  63. }
  64. else if(c[s] + d[e] > -sum){
  65. e--;
  66. }
  67. else{
  68. s++;
  69. }
  70. }
  71. ans += ct;
  72. }
  73. }
  74.  
  75. return ans;
  76. }
  77.  
  78.  
  79.  
  80. //* Optimization :
  81. //* Fetch all possible sum in map : SC = O(n^2)
  82. //* max distinct sum in map == O(n^2) , TC == O(n^2 * n) == O(n^3)
  83.  
  84. int consistency2(int n){
  85.  
  86. sort(begin(c), end(c));
  87. sort(begin(d), end(d));
  88.  
  89. unordered_map<int,int> mp;
  90. for(int i=0; i<n; i++){
  91. for(int j=0; j<n; j++){
  92. int sum = a[i]+b[j];
  93. mp[sum]++;
  94. }
  95. }
  96.  
  97. int ans = 0;
  98. for(auto& t : mp){
  99. int sum = t.first;
  100. int f = t.second;
  101. int ct = 0;
  102. int s = 0, e = n-1;
  103. while(s<n && e>=0){
  104. if(c[s] + d[e] == -sum ){
  105.  
  106. int x = c[s];
  107. int f1 = 0;
  108. while(s<n && c[s] == x){
  109. f1++;
  110. s++;
  111. }
  112.  
  113. int y = d[e];
  114. int f2 = 0;
  115. while(e>=0 && d[e] == y){
  116. f2++;
  117. e--;
  118. }
  119.  
  120. ct += f1*f2;
  121. }
  122. else if(c[s] + d[e] > -sum){
  123. e--;
  124. }
  125. else{
  126. s++;
  127. }
  128. }
  129. ans += f*ct;
  130. }
  131. return ans;
  132. }
  133.  
  134.  
  135.  
  136. //* Hashmap
  137.  
  138. int consistency3(int n){
  139.  
  140. unordered_map<int,int> mp;
  141. for(int i=0; i<n; i++){
  142. for(int j=0; j<n; j++){
  143. int sum = a[i]+b[j];
  144. mp[sum]++;
  145. }
  146. }
  147.  
  148. int ans = 0;
  149. for(int i=0; i<n; i++){
  150. for(int j=0; j<n; j++){
  151. int sum = c[i]+d[j];
  152.  
  153. if(mp.count(-sum)) ans += mp[-sum];
  154. }
  155. }
  156. return ans;
  157.  
  158. }
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. int practice(int n){
  172.  
  173.  
  174. return 0;
  175. }
  176.  
  177.  
  178.  
  179.  
  180.  
  181. void solve() {
  182.  
  183. int n;
  184. cin>> n;
  185.  
  186. a.resize(n);
  187. b.resize(n);
  188. c.resize(n);
  189. d.resize(n);
  190. for(int i=0; i<n; i++) cin >> a[i];
  191. for(int i=0; i<n; i++) cin >> b[i];
  192. for(int i=0; i<n; i++) cin >> c[i];
  193. for(int i=0; i<n; i++) cin >> d[i];
  194.  
  195. cout << consistency1(n) << " " << consistency2(n) << " " << consistency3(n) << endl;
  196.  
  197.  
  198. }
  199.  
  200.  
  201.  
  202.  
  203.  
  204. int32_t main() {
  205. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  206.  
  207. int t = 1;
  208. // cin >> t;
  209. while (t--) {
  210. solve();
  211. }
  212.  
  213. return 0;
  214. }
Success #stdin #stdout 0s 5308KB
stdin
3
0 1 -1
-1 1 0
0 0 1
-1 1 1
stdout
17 17 17