Case by case Discussion | Line Sweep
IntuitionWe care about when
abs(l1−x)+abs(l2−y)+1<(l2−l1)
So, fix l2 and only consider all l1∈l1<l2.
There are two case, helping us to get rid of abs() symbol:
# Intuition
We care about when
$$
abs(l1 - x) + abs(l2 - y) + 1 < (l2 - l1)
$$
So, fix $l2$ and only consider all $l1 \in l1 < l2$.
There are two case, helping us to get rid of $abs()$ symbol:
$1 <= l1 < x$
yield
$x <= c$
and
$x <= l1$
yield
$l1 <= (x + c) / 2, c=l2 - abs(l2-y)-1$
For partiy, the case $l1 > l2$ is the same, so double all the answers before returning.
Also use line sweep to help with counting in $O(1)$.

# Code
```cpp []
class Solution {
public:
    vector<long long> countOfPairs(int n, int x, int y) {
        if (x > y) swap(x, y);
        vector<long long> result(n + 1);
        auto mark = [&](int x, int y) {
            if (y < x) return; 
            result[x]++;
            if (y + 1 <= n) result[y + 1]--; 
        };
        for (int l2 = 1; l2 <= n; ++l2) {
            int c = l2 - 1 - abs(l2 - y);
            if (c < 0) {
                mark(1, l2 - 1);
                continue;
            }
            // segment1 [1, x)
            int c1 = abs(l2 - y) + 1;
            if (x > 1) {
                if (x <= c) {
                    mark(c1 + 1, (c1 + 1) + (x - 1) - 1);
                } else {
                    mark(max(1, l2 - (x - 1)), l2 - 1);
                }
            }
            // segment2 [x, (x + c) / 2]
            int up = (x + c) / 2;
            if (up >= x) {
                int starting = min(up, l2 - 1);
                mark(c1, c1 + starting - x);
                // segment3 ((x + c) / 2, l2 - 1]
                if ((x + c) / 2 < (l2 - 1)) {
                    int starting = max(x, up);
                    mark(1, l2 - starting - 1);
                }
            } else {
                mark(1, l2 - x);
            }
        }
        for (int i = 1; i <= n; ++i) {
            result[i] += result[i - 1];
        }
        for (int i = 1; i <= n; ++i) {
            result[i] *= 2;
        }
        result.erase(result.begin());
        return result;
    }
};
```
['C++']