my dumb [Java] code: 3 segments + 4 cases + prefix
First, my code is very stupid, there must be better ways!\n\nExplanation:\nThe extra line brings possible circle to the graph\ndivide the array into left + circ
First, my code is very stupid, there must be better ways!\n\nExplanation:\nThe extra line brings possible circle to the graph\ndivide the array into left + circle + right\n\nthen we have 4 different cases:\nleft internal, right internal\ncircle internal,\nleft to circle, right to circle\nleft to right\n\ninternal can be directly calculated\nconnection of two parts need to use the concept of prefix\n\n\n\n```\nclass Solution {\n    public long[] countOfPairs(int n, int x, int y) {\n        \n        long[] result = new long[n];\n\n        int leftCount = Math.min(x, y) - 1;\n        int rightCount = n - Math.max(x, y);\n        int circleCount = n - leftCount - rightCount;\n        \n        //in circle count\n        circleInternal(circleCount, result);\n        \n        //left/right to in circle\n        lineToCircle(leftCount, circleCount, result);\n        lineToCircle(rightCount, circleCount, result);\n        \n        //line to line\n        lineToLine(leftCount, rightCount, x == y ? 1 : 2, result);\n        \n        //line internal\n        lineInternal(leftCount, result);\n        lineInternal(rightCount, result);\n        \n        return result;\n        \n    }\n    \n    \n    private void lineToCircle(int lineCount, int circleCount, long[] curRes){\n        int circleLen = circleCount / 2 + 1;\n        int curModifier = 0;\n        \n        for(int i = 1; i < circleLen + lineCount; ++i){\n            if(i <= Math.min(lineCount, circleLen)){\n                curModifier += 4;\n            } else if (i > Math.max(lineCount, circleLen)){\n                curModifier -= 4;\n            }\n            \n            curRes[i - 1] += curModifier;\n            \n            //global modify\n            if(i <= lineCount){\n                curRes[i - 1] -= 2;\n            }\n            if(i >= circleLen && circleCount % 2 == 0){\n                curRes[i - 1] -= 2;\n            }\n        }\n        \n    }\n    \n    \n    private void lineToLine(int lineCount1, int lineCount2, int initialDis, long[] curRes){\n        int curModifier = 0;\n        \n        for(int i = 1; i < lineCount1 + lineCount2; ++i){\n            if(i <= Math.min(lineCount1, lineCount2)){\n                curModifier += 2;\n            } else if (i > Math.max(lineCount1, lineCount2)){\n                curModifier -= 2;\n            }\n            \n            curRes[i - 1 + initialDis] += curModifier;\n        }\n    }\n    \n    \n    private void lineInternal(int lineCount, long[] curRes){\n        for(int i = 1; i < lineCount; ++i){\n            curRes[i - 1] += (lineCount - i) * 2;\n        }\n    }\n    \n    \n    private void circleInternal(int circleCount, long[] curRes){\n        for(int i = 0; i < circleCount / 2; ++i){\n            if(circleCount % 2 == 0 && i + 1 == circleCount / 2){\n                curRes[i] += circleCount;\n            } else {\n                curRes[i] += circleCount * 2;\n            }\n        }\n    }\n}\n```
['Java']