fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define pb push_back
  5. #define all(x) x.begin(), x.end()
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace __gnu_pbds;
  9. template <typename T> using o_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  10. template <typename T, typename R> using o_map = tree<T, R, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  11. typedef long long ll;
  12.  
  13. #define inf 1e9
  14. #define MOD 1000000007
  15. #define vint vector<int>
  16. #define vll vector<ll>
  17. #define no cout << "NO" << endl;
  18.  
  19. void solve() {
  20. ll nby0 = 0 , nby1 = 0;
  21. ll n ; cin >> n ;
  22. vector < bool > fama0(n+1 , false ), fama1(n+1 , false );
  23.  
  24. map < ll , ll > mp ;
  25. vector < ll > x0 , x1 ;
  26. ll ans= 0;
  27. for ( int i = 0 ; i < n; i++ ) {
  28. ll x , y ; cin >> x >> y ;
  29. if (y == 0 ) {
  30. x0.pb(x );
  31. fama0[x] = 1 ;
  32. nby0 ++ ;
  33. continue ;
  34. }
  35. fama1[x] = 1 ;
  36. nby1++ ;
  37. x1.pb(x) ;
  38. }
  39. ll len0 = x0.size(), len1 = x1.size() ;
  40. ll mini0 = x0[0] , maxi0 = x0[len0-1] , mini1 = x1[0] , maxi1 = x1[len1 - 1 ] ;
  41.  
  42. ll sofar0 = 0 , sofar1 = 0 ;
  43. for (int x = 0 ; x<= n ; x++) {
  44. if (fama0[x]) sofar0++;
  45. if ( fama1[x]) sofar1++ ;
  46. if (x >0 && x <n-1 && fama0[x] && fama1[x-1] && fama1[x+1]) ans++;
  47. if (x >0 && x <n-1 && fama1[x] && fama0[x-1] && fama0[x+1]) ans++;
  48. if (fama0[x] && fama1[x] ) ans+=(nby0 - 1 + nby1-1) ;
  49. // cout << ans << endl;
  50. }
  51.  
  52. cout << ans << endl ;
  53. }
  54. int main() {
  55. ios::sync_with_stdio(false);
  56. cin.tie(nullptr);
  57.  
  58. #ifndef ONLINE_JUDGE
  59. freopen("input.txt", "r", stdin);
  60. freopen("output.txt", "w", stdout);
  61. #endif
  62.  
  63. int t = 1;
  64. cin >> t;
  65. while (t--) {
  66. solve();
  67. }
  68.  
  69. return 0;
  70. }
  71.  
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
0