Problem link
A solution in c++
#include<bits/stdc++.h>
using namespace std;
/// Typedef
typedef long long ll;
#define sc1(a) scanf("%lld",&a)
#define sc2(a,b) scanf("%lld %lld",&a,&b)
#define pf1(a) printf("%lld\n",a)
#define pf2(a,b) printf("%lld %lld\n",a,b)
#define mx 1000010
#define mod 10000007
#define PI acos(-1.0)
ll ans(ll num)
{
if(num < 0) return -1*((- num * (-num +1))/2);
else return (num * (num + 1)) / 2;
}
int main() {
ll tc, k, t = 1;
//freopen("C:\\Users\\morol\\Desktop\\Clion\\input.txt", "r", stdin);
ll first, sec;
while (cin >> first >> sec){
if(first > sec) swap(first, sec);
if(sec < 0)
printf("%lld\n", ans(first) - ans(sec+1));
else if(first <= 0)
printf("%lld\n", ans(sec) + ans(first));
else
printf("%lld\n", ans(sec) - ans(first - 1));
}
return 0;
}
0 Comments
If you have any doubts, Please let me know