fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  5.  
  6. #define int long long
  7. #define pb push_back
  8. #define ff first
  9. #define ss second
  10. #define all(x) (x).begin(), (x).end()
  11. #define rall(x) (x).rbegin(), (x).rend()
  12. #define sz(x) ((int)(x).size())
  13. #define endl '\n'
  14. #define yes cout << "yes\n"
  15. #define no cout << "no\n"
  16.  
  17. #define rep(i,a,b) for(int i=a;i<b;++i)
  18. #define per(i,a,b) for(int i=b-1;i>=a;--i)
  19. #define each(x, a) for (auto& x : a)
  20.  
  21. const int INF = 1e18;
  22. const int MOD = 1e9+7;
  23. const int N = 2e5 + 5;
  24.  
  25. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  26. int lcm(int a, int b) { return (a / gcd(a, b)) * b; }
  27. int power(int a, int b, int m = MOD) {
  28. int res = 1;
  29. while (b > 0) {
  30. if (b & 1) res = res * a % m;
  31. a = a * a % m;
  32. b >>= 1;
  33. }
  34. return res;
  35. }
  36. int modinv(int a, int m = MOD) {
  37. return power(a, m - 2, m);
  38. }
  39.  
  40. void solve() {
  41. int n;
  42. cin >> n;
  43. vector<int> a(n), b(n), c(n);
  44. each(x, a) cin >> x;
  45. each(x, b) cin >> x;
  46. each(x, c) cin >> x;
  47.  
  48. int cnt_ab = 0;
  49. rep(shift, 0, n) {
  50. bool ok = true;
  51. rep(i, 0, n) {
  52. if (a[i] >= b[(i + shift) % n]) {
  53. ok = false;
  54. break;
  55. }
  56. }
  57. if (ok) cnt_ab++;
  58. }
  59.  
  60. int cnt_bc = 0;
  61. rep(shift, 0, n) {
  62. bool ok = true;
  63. rep(i, 0, n) {
  64. if (b[i] >= c[(i + shift) % n]) {
  65. ok = false;
  66. break;
  67. }
  68. }
  69. if (ok) cnt_bc++;
  70. }
  71.  
  72. cout << n * cnt_ab * cnt_bc << endl;
  73. }
  74.  
  75. int32_t main() {
  76. fast_io;
  77. int t;
  78. cin >> t;
  79. while (t--) {
  80. solve();
  81. }
  82. return 0;
  83. }
Success #stdin #stdout 0.01s 5316KB
stdin
4
2
1 2
3 4
5 4
3
1 1 1
2 2 2
3 3 3
4
1 2 1 2
3 3 2 2
5 5 5 5
5
1 4 2 3 5
6 4 5 7 6
7 5 8 10 10
stdout
4
27
0
10