fork download
  1. using namespace std;
  2. #include <bits/stdc++.h>
  3.  
  4. long long getPrefSum(int n) {
  5. return 1LL * n * (n + 1) / 2;
  6. }
  7.  
  8. int main() {
  9. ios::sync_with_stdio(false);
  10. cout.tie(nullptr), cin.tie(nullptr);
  11.  
  12. int n, m;
  13. cin >> n >> m;
  14.  
  15. int k = min(m - 2, n - 1);
  16. long long rows = getPrefSum(m - 2) - getPrefSum((m - 2) - k);
  17.  
  18. long long columns = getPrefSum(n - 1);
  19. if (n >= m) columns -= getPrefSum((n - 1) - (k + 1));
  20. else columns -= getPrefSum((n - 1) - k);
  21.  
  22. cout << columns + rows << endl;
  23.  
  24. return 0;
  25. }
Success #stdin #stdout 0s 4316KB
stdin
999999999 999999998
stdout
999999995000000006