Guest User

USACO 2020 Silver Problem 2

a guest
Dec 22nd, 2020
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5.     ios::sync_with_stdio(false); cin.tie(0);
  6.     int n; cin >> n;
  7.     vector<pair<int, int>> a(n);
  8.     for (auto& it : a) cin >> it.first >> it.second;
  9.     sort(a.begin(), a.end());
  10.    
  11.     typedef long long ll;
  12.     ll nl[n][n][2], ng[n][n][2]; //precalculate the number of y-coordinates less than one of the endpoints, and the number of y-coordinates greater than one of the endpoints
  13.     for (int i = 0; i < n; i++){
  14.         int cl=0, cg=0;
  15.         for (int j = i; j < n; j++){
  16.             if (a[j].second < a[i].second) cl++;
  17.             if (a[j].second > a[i].second) cg++;
  18.             nl[i][j][0] = cl;
  19.             ng[i][j][0] = cg;
  20.         }
  21.     }
  22.     for (int j = 0; j < n; j++){
  23.         int cl=0, cg=0;
  24.         for (int i = j; i >= 0; i--){
  25.             if (a[i].second < a[j].second) cl++;
  26.             if (a[i].second > a[j].second) cg++;
  27.             nl[i][j][1] = cl;
  28.             ng[i][j][1] = cg;
  29.         }
  30.     }
  31.     ll ans=n+1; //empty subset counts -> initially start with n+1 for all rectangles that have single points
  32.     for (int i = 0; i < n; i++){
  33.         for (int j = i+1; j < n; j++){
  34.             if (a[i].second <= a[j].second){
  35.                 ans += (1+nl[i][j][0])*(1+ng[i][j][1]);
  36.             } else {
  37.                 ans += (1+nl[i][j][1])*(1+ng[i][j][0]);
  38.             }
  39.         }
  40.     }
  41.     //number outside the bounds are possible, the rest are fixed
  42.     cout << ans << '\n';
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment