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