구간합 문제다.

정수의 구간 합 문제이므로 (a + b) * (b - a + 1) / 2 하면 된다. (a < b)

#include <cmath>
#include <vector>

using namespace std;

long long solution(int a, int b) {
    return (long long)(a + b) * (abs(a - b) + 1) / 2;
}

+ Recent posts