C++ | Array manipulation | fastest
First initialize the res array assuming that there is no x-y bond. \n2. partition the array like [0, x-1], [x,y] and [y+1, n] and then do the case work.\n3. The
1. First initialize the ```res``` array assuming that there is no x-y bond. \n2. partition the array like [0, x-1], [x,y] and [y+1, n] and then do the case work.\n3. Then just think of what are the distances that are reduced by x-y bond, remove them \n4. what are the new minimum distances that have been added due to this x-y bond, add them.\n5. if you want to add ```+1``` to all the distances from ```l``` to ```r```, then you make ```change[l] += 1``` and ```change[r+1] -= 1```, because when we do the prefix sum over ```change``` array, then it will automatically add +1 to all the elements from ```l``` to ```r```\n```\nclass Solution {\npublic:\n    \n    #define ll long long\n    vector<long long> countOfPairs(int n, int x, int y) {\n        vector<ll> res(n); // 0-indexed\n        for(int i = 0; i<n; i++){\n            res[i] = n - (i + 1);\n        }\n        \n        if(x == y || abs(x-y) == 1){\n            for(int i = 0; i<n; i++){\n                res[i] = (res[i] << 1);\n            }\n            return res;\n        }\n        \n        vector<ll> change(n+1);  // 1-indexed\n        if(x > y) swap(x, y);\n        int gap = y - x;\n        \n        for(int i = x; i > 0; i--){\n            change[y - i] -= 1; change[n - i + 1] += 1;\n            change[x - i + 1] += 1; change[x - i + 1 + n - y + 1] -= 1;\n        }\n                \n        int z = 0;\n        \n        for(int i = y; i > x; i--){\n            if(y - i + 1 >= i - x){\n                z = i + 1; break;\n            }\n        }\n                \n        for(int i = x-1; i>0; i--){\n            change[z - i] -= 1; change[y - i] += 1;\n            change[x - i + 2] += 1; change[x - i + 1 + y -  z + 1] -= 1;\n        }\n        \n        for(int i = x; z < y; i++, z++){\n            change[z - i] -= 1; change[y - i] += 1;\n            change[i - x + 2] += 1; change[i - x + 1 + y - z + 1] -= 1;\n        }\n        \n        for(int i = x+1; i - x + 1 < y - i; i++){\n            change[y - i] -= 1; change[n - i + 1] += 1;\n            change[i - x + 1] += 1; change[i - x + 1 + n - y + 1] -= 1;\n        }\n        \n        for(int i = 1; i<=n; i++){\n            change[i] += change[i-1];\n            res[i-1] += change[i];\n            res[i-1] *= 2;\n        }\n        \n        return res;\n    }\n};\n```\n![image](https://assets.leetcode.com/users/images/e1164813-35a4-4126-bfc2-e643af31e78b_1705917689.8275642.png)\n
['Array', 'C']