forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10031.cpp
109 lines (100 loc) · 1.97 KB
/
10031.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <bits/stdc++.h>
using namespace std;
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define ALL(C) (C).begin(), (C).end()
#define pb push_back
#define mp make_pair
typedef complex<int> P;
typedef long long LL;
const int N = 201;
struct Line
{
int minx, maxx;
double A, B;
};
vector<Line> cons(vector<P> &in)
{
vector<Line> ret;
for (int i = 0; i < in.size() - 1; i++)
{
if (in[i].real() == in[i + 1].real())
{
continue;
}
double A = (double)(in[i + 1].imag() - in[i].imag()) / (in[i + 1].real() - in[i].real());
double B = -A * in[i + 1].real() + in[i + 1].imag();
ret.pb((Line){
min(in[i].real(), in[i + 1].real()),
max(in[i].real(), in[i + 1].real()), A, B});
}
return ret;
}
LL get_sect(vector<Line> &in, int x)
{
LL ret = 0;
static pair<double, double> data[N];
int p = 0;
for (int i = 0; i < in.size(); i++)
{
if (in[i].minx <= x && x < in[i].maxx)
{
data[p++] = mp(in[i].A * x + in[i].B, in[i].A * (x + 1) + in[i].B);
if (data[p - 1].first > data[p - 1].second)
{
swap(data[p - 1].first, data[p - 1].second);
}
}
}
sort(data, data + p);
// assert(p % 2 == 0 && p < N);
for (int i = 0; i < p; i += 2)
{
int tmp = (int)(floor(data[i + 1].first)) - (int)(ceil(data[i].second));
if (tmp < 0)
{
continue;
}
ret += (int)(floor(data[i + 1].first)) - (int)(ceil(data[i].second));
}
return ret;
}
// y-y1 = (y2-y1)/(x2-x1)*x(x-x1);
// y = (y2-y1)/(x2-x1)*x(x-x1) + y1;
// y = ax - ax1 + y1;
LL solve(vector<P> &in)
{
vector<Line> a = cons(in);
LL ret = 0LL;
for (int i = 0; i < 100000; i++)
{
ret += get_sect(a, i);
}
return ret;
}
int main()
{
int T;
scanf("%d", &T);
string _;
getline(cin, _);
getline(cin, _);
while (T--)
{
vector<P> in;
while (getline(cin, _) && _.size() > 0)
{
stringstream sin(_);
int re, im;
sin >> re >> im;
in.pb(P(re, im));
}
in.pb(in[0]);
printf("%lld\n", solve(in));
if (T)
{
printf("\n");
}
}
return 0;
}