-
Notifications
You must be signed in to change notification settings - Fork 0
/
Belajar berhitung.cpp
73 lines (50 loc) · 1.12 KB
/
Belajar berhitung.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<bits/stdc++.h>
using namespace std;
struct Interval
{
int s, e;
};
unsigned long long perhitungan(unsigned long long a,unsigned long long b,unsigned long long n){
unsigned long long count = n*(a+b);
unsigned long long hasil = count/2;
return hasil;
}
bool mycomp(Interval a, Interval b)
{ return a.s < b.s; }
void mergeIntervals(Interval arr[], int n)
{
sort(arr, arr+n, mycomp);
int index = 0;
for (int i=1; i<n; i++)
{
if (arr[index].e >= arr[i].s)
{
arr[index].e = max(arr[index].e, arr[i].e);
arr[index].s = min(arr[index].s, arr[i].s);
}
else {
index++;
arr[index] = arr[i];
}
}
unsigned long long jum = 0;
for(int i = 0; i <= index; i++){
jum = jum + perhitungan(arr[i].s, arr[i].e, arr[i].e - arr[i].s + 1);
}
cout << jum << endl;
// for (int i = 0; i <= index; i++)
// cout << "[" << arr[i].s << ", " << arr[i].e << "] ";
//
}
// Driver program zz
int main()
{
int n;
cin >> n;
Interval arr[n] ;
for(int i = 0; i<n; i++){
cin >> arr[i].s >> arr[i].e;
}
mergeIntervals(arr, n);
return 0;
}