public int countRangeSum(int[] nums, int lower, int upper) {
        if (nums == null || nums.length == 0) return 0;

        long[] sums = new long[nums.length + 1];
        for (int i = 0; i < nums.length; ++i) sums[i + 1] = sums[i] + nums[i];

        TreeNode root = new TreeNode(sums[sums.length - 1]);
        int cnt = 0;
        for (int i = sums.length - 2; i >= 0; --i) {
            cnt += countRange(root, sums[i] + lower, sums[i] + upper);
            addNode(root, sums[i]);
        }
        return cnt;
    }