-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrid.cpp
86 lines (79 loc) · 2.15 KB
/
grid.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
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
bool myfunc(pair<long long, long long > p1, pair<long long,long long> p2){
if ( p1.first <= p2.first )
return true;
return false;
}
// Cases
// --------
// ----
// -------
// ---------
long long computeDistance( vector<pair<long long, long long> > dist){
long long curc1 = -1, curc2 = -1, p = 0, c1, c2;
vector<pair<long long, long long > >::iterator it = dist.begin();
for (; it!= dist.end(); it++){
if (curc1 == -1 && curc2 == -1 ){
curc1 = it->first;
curc2 = it->second;
p = curc2- curc1+1;
}
else {
c1 = it->first;
c2 = it -> second;
if ( c1 > curc2 ){
p += c2-c1+1;
curc1 = c1;
curc2 = c2;
}
if (c1 == curc2 ){
p += c2-c1;
curc2 = c2;
}
else if ( c1 < curc2 ){ // c1< cur2
if ( c2 > curc2 ){
p += c2-curc2;
curc2 = c2;
}
}
}
}
return p;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
long long n, m ,k;
long long row, c1, c2;
cin >> n >> m >> k;
// array of vector of pairs
vector<pair<long long ,long long > > lines [1001];
bool ks[1001];
for (long i = 1;i <= k; i++){
cin >> row >>c1 >> c2;
ks[row] = true;
lines[row].push_back(pair<int,int>(c1,c2));
}
vector<pair<long long , long long> >::iterator it;
for ( long long i = 1; i<= k; i++ ){
it = lines[i].begin();
while (it != lines[i].end()){
it++;
}
}
// sort the vector pairs based on c1
long long p = 0;
for ( long long i = 1; i<= 1000; i++ ){
if ( ks[i] == true ) {
sort( lines[i].begin(), lines[i].end(), myfunc);
p+= computeDistance(lines[i] );
ks[i] = false;
}
}
cout << n*m - p;
return 0;
}