Skip to content

Commit fc88fcb

Browse files
committed
Added 1080B - Margarite and the best present.cpp
1 parent 1a75467 commit fc88fcb

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// ॐ नमः शिवाय
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
#define ll long long
6+
#define pb push_back
7+
#define ff first
8+
#define ss second
9+
#define mp make_pair
10+
11+
// Code Written By: Vikash Patel
12+
// Codeforces Profile: https://codeforces.com/profile/vikashpatel
13+
14+
int sumOdd(int n)
15+
{
16+
int terms = (n + 1) / 2;
17+
int sum = terms * terms;
18+
return sum;
19+
}
20+
21+
// Function to return the sum
22+
// of all odd numbers in range L and R
23+
int suminRange(int l, int r)
24+
{
25+
return sumOdd(r) - sumOdd(l - 1);
26+
}
27+
28+
int sumNatural(int n)
29+
{
30+
int sum = (n * (n + 1));
31+
return sum;
32+
}
33+
34+
// Function to return sum
35+
// of even numbers in range L and R
36+
int sumEven(int l, int r)
37+
{
38+
return sumNatural(r/2) - sumNatural((l-1) / 2);
39+
}
40+
41+
// https://www.geeksforgeeks.org/sum-of-all-even-numbers-in-range-l-and-r/
42+
// https://www.geeksforgeeks.org/sum-of-all-odd-natural-numbers-in-range-l-and-r/
43+
44+
int main()
45+
{
46+
ios_base::sync_with_stdio(0);
47+
cin.tie(0);
48+
cout.tie(0);
49+
50+
// #ifndef ONLINE_JUDGE
51+
// freopen("input.txt", "r", stdin);
52+
// freopen("/Users/vikash/Desktop/output.txt", "w", stdout);
53+
// #endif
54+
55+
int q;
56+
cin>>q;
57+
while(q--)
58+
{
59+
int l, r;
60+
cin>>l>>r;
61+
cout<<sumEven(l, r) - suminRange(l, r)<<endl;
62+
}
63+
return 0;
64+
}

0 commit comments

Comments
 (0)