fork download
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. using ll = long long;
  7. const int MOD = 1000000007;
  8. const int MOD2 = 998244353;
  9. const ll INF = 1e18;
  10. const int MX = 1000001; //check the limits, dummy
  11.  
  12.  
  13. ll modExp(ll base, ll power) {
  14. if (power == 0) {
  15. return 1;
  16. } else {
  17. ll cur = modExp(base, power / 2); cur = cur * cur; cur = cur % MOD;
  18. if (power % 2 == 1) cur = cur * base;
  19. cur = cur % MOD;
  20. return cur;
  21. }
  22. }
  23.  
  24. ll inv(ll base) {
  25. return modExp(base, MOD-2);
  26. }
  27.  
  28.  
  29. ll mul(ll A, ll B) {
  30. return (A*B)%MOD;
  31. }
  32.  
  33. ll add(ll A, ll B) {
  34. return (A+B)%MOD;
  35. }
  36.  
  37. ll dvd(ll A, ll B) {
  38. return mul(A, inv(B));
  39. }
  40.  
  41. ll sub(ll A, ll B) {
  42. return (A-B+MOD)%MOD;
  43. }
  44.  
  45. ll* facs = new ll[MX];
  46. ll* facInvs = new ll[MX];
  47.  
  48. ll choose(ll a, ll b) {
  49. if (b > a) return 0;
  50. if (a < 0) return 0;
  51. if (b < 0) return 0;
  52. ll cur = facs[a];
  53. cur = mul(cur, facInvs[b]);
  54. cur = mul(cur, facInvs[a-b]);
  55. return cur;
  56. }
  57.  
  58. void initFacs() {
  59. facs[0] = 1;
  60. facInvs[0] = 1;
  61. for (int i = 1 ; i < MX ; i ++ ) {
  62. facs[i] = (facs[i-1] * i) % MOD;
  63. facInvs[i] = inv(facs[i]);
  64. }
  65. }
  66. int main() {
  67. ios_base::sync_with_stdio(0); cin.tie(0);
  68. int t; cin >> t;
  69. while (t --) {
  70. int n ;
  71.  
  72. cin >> n;
  73. ll top = 0 ;
  74. ll bottom = 0;
  75. vector<vector<bool>> exists(n + 1, vector<bool> (2,false));
  76. for (int i = 0 ; i < n; i ++) {
  77. int a, b ; cin >> a >> b;
  78. exists[a][b] = true;
  79. if (b == 1) {
  80. top ++;
  81. } else {
  82. bottom ++;
  83. }
  84. }
  85.  
  86. ll res = 0;
  87. int lefttop = 0;
  88. int leftbottom = 0;
  89. for (int i = 0 ; i <= n ; i ++) {
  90. if (exists[i][0] && exists[i][1]) {
  91. res += lefttop;
  92. res += leftbottom;
  93. res += (top - lefttop - 1);
  94. res += (bottom - leftbottom - 1);
  95. }
  96. if (i > 0 && i < n) {
  97. if (exists[i][0]) {
  98. if (exists[i - 1][1] && exists[i + 1][1]) {
  99. res ++;
  100. }
  101. }
  102. if (exists[i][1]) {
  103. if (exists[i - 1][0] && exists[i + 1][0]) {
  104. res ++;
  105. }
  106. }
  107. }
  108. if (exists[i][0]) {
  109. lefttop ++;
  110. }
  111. if (exists[i][1]) {
  112. leftbottom ++;
  113. }
  114. }
  115.  
  116. cout << res << endl;
  117.  
  118.  
  119. }
  120. return 0;
  121. }
  122.  
Success #stdin #stdout 0.01s 5324KB
stdin
3
5
1 0
1 1
3 0
5 0
2 1
3
0 0
1 0
3 0
9
1 0
2 0
3 0
4 0
5 0
2 1
7 1
8 1
9 1
stdout
4
0
8