#include "hexagon.h"
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
long long pw(long long x, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1) {
res = res * x % mod;
}
n /= 2;
x = x * x % mod;
}
return res;
}
const int N = 400400;
const int Q = 200200;
int dx[6] = {-1, -1, 0, 1, 1, 0};
int dy[6] = {-1, 0, 1, 1, 0, -1};
long long X = 0, Y = 0;
vector<int> v[N];
vector<pair<int, int>> g[N];
int G;
int A[N];
int B[N];
int a[N];
int b[N];
int f[N];
long long F(long long x) {
return x * (x + 1) / 2 % mod;
}
void dfs(int x, int p) {
Y += 1ll * f[x] * (B[x] - A[x] + 1) % mod;
Y += F(B[x] - b[x]) + F(a[x] - A[x]);
for (auto yy: g[x]) {
int y = yy.first;
int dir = yy.second;
if (y == p) {
continue;
}
a[y] = a[x] - 1;
b[y] = b[x] + 1;
f[y] = f[x] + 1;
if (dir == 1) {
a[y] += 1;
} else {
b[y] -= 1;
}
if (max(A[y], a[y]) > min(B[y], b[y])) {
if (b[y] < A[y]) {
f[y] += A[y] - b[y];
a[y] = b[y] = A[y];
} else {
f[y] += a[y] - B[y];
a[y] = b[y] = B[y];
}
} else {
a[y] = max(a[y], A[y]);
b[y] = min(b[y], B[y]);
}
dfs(y, x);
}
}
void work() {
for (int i = 0; i < N; i++) {
sort(v[i].begin(), v[i].end());
}
vector<int> shit;
int root = -1;
for (int i = 0; i < N; i++) {
assert (v[i].size() % 2 == 0);
vector<int> cur;
for (int j = 0; j < v[i].size(); j += 2) {
int l = v[i][j];
int r = v[i][j + 1];
G += 1;
A[G] = l;
B[G] = r;
X += r - l + 1;
cur.push_back(G);
if (A[G] <= 0 && 0 <= B[G] && i == Q) {
root = G;
}
}
for (int j = 0, h = 0; j < cur.size(); j++) {
while (h < shit.size() && B[shit[h]] < A[cur[j]]) {
h += 1;
}
if (h < shit.size() && A[shit[h]] <= B[cur[j]]) {
g[shit[h]].push_back({cur[j], 1});
g[cur[j]].push_back({shit[h], 0});
while (h + 1 < shit.size() && A[shit[h + 1]] <= B[cur[j]]) {
h += 1;
g[shit[h]].push_back({cur[j], 1});
g[cur[j]].push_back({shit[h], 0});
}
}
/*
for (int h = 0; h < shit.size(); h++) {
if (max(A[shit[h]], A[cur[j]]) <= min(B[shit[h]], B[cur[j]])) {
g[shit[h]].push_back({cur[j], 1});
g[cur[j]].push_back({shit[h], 0});
}
}
*/
}
swap(cur, shit);
}
dfs(root, root);
}
int draw_territory(int N, int A, int B,
std::vector<int> D, std::vector<int> L) {
if (N == 3) {
assert (L[0] == L[1]);
assert (L[1] == L[2]);
long long x = L[0] + 1;
X = x * (x + 1) / 2;
Y = x * (x + 1) % mod * (2 * x + 1) % mod * pw(6, mod - 2) - X;
} else {
vector<pair<int, int>> p;
set<pair<int, int>> fucked;
int x = 0, y = 0;
for (int i = 0; i < N; i++) {
int dir = D[i] - 1;
for (int j = 0; j < L[i]; j++) {
x += dx[dir];
y += dy[dir];
p.push_back({x, y});
for (int h = 0; h < 6; h++) {
fucked.insert({x + dx[h], y + dy[h]});
}
}
}
for (auto xi: p) {
fucked.erase(xi);
}
set<pair<int, int>> outers;
queue<pair<int, int>> q;
q.push(*fucked.begin());
outers.insert(*fucked.begin());
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i = 0; i < 6; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (fucked.find({nx, ny}) == fucked.end()) {
continue;
} else if (outers.find({nx, ny}) == outers.end()) {
outers.insert({nx, ny});
q.push({nx, ny});
}
}
}
for (auto xi: p) {
int x = xi.first;
int y = xi.second;
if (outers.find({x, y - 1}) != outers.end()) {
v[x + Q].push_back(y);
}
if (outers.find({x, y + 1}) != outers.end()) {
v[x + Q].push_back(y);
}
}
work();
}
X %= mod;
Y %= mod;
long long res = X * A % mod + Y * B % mod;
res = (res % mod + mod) % mod;
return res;
}
Compilation message
hexagon.cpp: In function 'void work()':
hexagon.cpp:87:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int j = 0; j < v[i].size(); j += 2) {
| ~~^~~~~~~~~~~~~
hexagon.cpp:102:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for (int j = 0, h = 0; j < cur.size(); j++) {
| ~~^~~~~~~~~~~~
hexagon.cpp:103:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | while (h < shit.size() && B[shit[h]] < A[cur[j]]) {
| ~~^~~~~~~~~~~~~
hexagon.cpp:106:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | if (h < shit.size() && A[shit[h]] <= B[cur[j]]) {
| ~~^~~~~~~~~~~~~
hexagon.cpp:109:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | while (h + 1 < shit.size() && A[shit[h + 1]] <= B[cur[j]]) {
| ~~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
20572 KB |
Output is correct |
2 |
Correct |
5 ms |
20740 KB |
Output is correct |
3 |
Correct |
5 ms |
20572 KB |
Output is correct |
4 |
Correct |
5 ms |
20756 KB |
Output is correct |
5 |
Correct |
7 ms |
20572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
20572 KB |
Output is correct |
2 |
Correct |
6 ms |
20572 KB |
Output is correct |
3 |
Correct |
6 ms |
20572 KB |
Output is correct |
4 |
Correct |
5 ms |
20572 KB |
Output is correct |
5 |
Correct |
5 ms |
20572 KB |
Output is correct |
6 |
Correct |
6 ms |
20748 KB |
Output is correct |
7 |
Correct |
5 ms |
20572 KB |
Output is correct |
8 |
Correct |
6 ms |
20572 KB |
Output is correct |
9 |
Correct |
6 ms |
20572 KB |
Output is correct |
10 |
Correct |
5 ms |
20740 KB |
Output is correct |
11 |
Correct |
5 ms |
20752 KB |
Output is correct |
12 |
Correct |
5 ms |
20568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
22620 KB |
Output is correct |
2 |
Correct |
9 ms |
22876 KB |
Output is correct |
3 |
Correct |
10 ms |
23128 KB |
Output is correct |
4 |
Correct |
10 ms |
22876 KB |
Output is correct |
5 |
Correct |
11 ms |
22876 KB |
Output is correct |
6 |
Correct |
10 ms |
22876 KB |
Output is correct |
7 |
Correct |
10 ms |
22872 KB |
Output is correct |
8 |
Correct |
10 ms |
22792 KB |
Output is correct |
9 |
Correct |
12 ms |
22876 KB |
Output is correct |
10 |
Correct |
11 ms |
23132 KB |
Output is correct |
11 |
Correct |
11 ms |
23188 KB |
Output is correct |
12 |
Correct |
10 ms |
23128 KB |
Output is correct |
13 |
Correct |
10 ms |
22920 KB |
Output is correct |
14 |
Correct |
10 ms |
23132 KB |
Output is correct |
15 |
Correct |
10 ms |
22876 KB |
Output is correct |
16 |
Correct |
10 ms |
23040 KB |
Output is correct |
17 |
Correct |
12 ms |
22996 KB |
Output is correct |
18 |
Correct |
5 ms |
20568 KB |
Output is correct |
19 |
Correct |
5 ms |
20572 KB |
Output is correct |
20 |
Correct |
6 ms |
20748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
374 ms |
62212 KB |
Output is correct |
2 |
Correct |
228 ms |
42692 KB |
Output is correct |
3 |
Correct |
275 ms |
49088 KB |
Output is correct |
4 |
Correct |
199 ms |
43412 KB |
Output is correct |
5 |
Correct |
289 ms |
51320 KB |
Output is correct |
6 |
Correct |
367 ms |
57276 KB |
Output is correct |
7 |
Correct |
160 ms |
35780 KB |
Output is correct |
8 |
Correct |
291 ms |
45260 KB |
Output is correct |
9 |
Correct |
371 ms |
55232 KB |
Output is correct |
10 |
Correct |
375 ms |
57720 KB |
Output is correct |
11 |
Correct |
285 ms |
52720 KB |
Output is correct |
12 |
Correct |
290 ms |
52416 KB |
Output is correct |
13 |
Correct |
292 ms |
54208 KB |
Output is correct |
14 |
Correct |
231 ms |
43380 KB |
Output is correct |
15 |
Correct |
366 ms |
44184 KB |
Output is correct |
16 |
Correct |
291 ms |
44732 KB |
Output is correct |
17 |
Correct |
5 ms |
20572 KB |
Output is correct |
18 |
Correct |
5 ms |
20572 KB |
Output is correct |
19 |
Correct |
5 ms |
20572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
20572 KB |
Output is correct |
2 |
Correct |
5 ms |
20660 KB |
Output is correct |
3 |
Correct |
363 ms |
62384 KB |
Output is correct |
4 |
Correct |
221 ms |
42664 KB |
Output is correct |
5 |
Correct |
266 ms |
49076 KB |
Output is correct |
6 |
Correct |
197 ms |
43444 KB |
Output is correct |
7 |
Correct |
301 ms |
51440 KB |
Output is correct |
8 |
Correct |
364 ms |
57168 KB |
Output is correct |
9 |
Correct |
146 ms |
35780 KB |
Output is correct |
10 |
Correct |
302 ms |
45204 KB |
Output is correct |
11 |
Correct |
370 ms |
55232 KB |
Output is correct |
12 |
Correct |
372 ms |
57660 KB |
Output is correct |
13 |
Correct |
254 ms |
52672 KB |
Output is correct |
14 |
Correct |
281 ms |
52228 KB |
Output is correct |
15 |
Correct |
331 ms |
54324 KB |
Output is correct |
16 |
Correct |
234 ms |
43460 KB |
Output is correct |
17 |
Correct |
323 ms |
43964 KB |
Output is correct |
18 |
Correct |
291 ms |
44908 KB |
Output is correct |
19 |
Execution timed out |
2041 ms |
374196 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
22616 KB |
Output is correct |
2 |
Correct |
10 ms |
22876 KB |
Output is correct |
3 |
Correct |
11 ms |
23072 KB |
Output is correct |
4 |
Correct |
10 ms |
22872 KB |
Output is correct |
5 |
Correct |
10 ms |
22872 KB |
Output is correct |
6 |
Correct |
10 ms |
22872 KB |
Output is correct |
7 |
Correct |
12 ms |
22876 KB |
Output is correct |
8 |
Correct |
10 ms |
22872 KB |
Output is correct |
9 |
Correct |
12 ms |
22852 KB |
Output is correct |
10 |
Correct |
12 ms |
23132 KB |
Output is correct |
11 |
Correct |
11 ms |
23128 KB |
Output is correct |
12 |
Correct |
10 ms |
23132 KB |
Output is correct |
13 |
Correct |
10 ms |
22876 KB |
Output is correct |
14 |
Correct |
10 ms |
23128 KB |
Output is correct |
15 |
Correct |
10 ms |
22876 KB |
Output is correct |
16 |
Correct |
10 ms |
22872 KB |
Output is correct |
17 |
Correct |
10 ms |
23048 KB |
Output is correct |
18 |
Correct |
362 ms |
62276 KB |
Output is correct |
19 |
Correct |
229 ms |
42964 KB |
Output is correct |
20 |
Correct |
302 ms |
49116 KB |
Output is correct |
21 |
Correct |
211 ms |
43456 KB |
Output is correct |
22 |
Correct |
291 ms |
51400 KB |
Output is correct |
23 |
Correct |
365 ms |
57276 KB |
Output is correct |
24 |
Correct |
144 ms |
35756 KB |
Output is correct |
25 |
Correct |
282 ms |
45168 KB |
Output is correct |
26 |
Correct |
374 ms |
55104 KB |
Output is correct |
27 |
Correct |
371 ms |
57812 KB |
Output is correct |
28 |
Correct |
267 ms |
52716 KB |
Output is correct |
29 |
Correct |
272 ms |
52200 KB |
Output is correct |
30 |
Correct |
288 ms |
54232 KB |
Output is correct |
31 |
Correct |
232 ms |
43352 KB |
Output is correct |
32 |
Correct |
339 ms |
44036 KB |
Output is correct |
33 |
Correct |
318 ms |
44992 KB |
Output is correct |
34 |
Correct |
165 ms |
42696 KB |
Output is correct |
35 |
Correct |
207 ms |
41356 KB |
Output is correct |
36 |
Correct |
192 ms |
40364 KB |
Output is correct |
37 |
Correct |
271 ms |
50072 KB |
Output is correct |
38 |
Correct |
295 ms |
50808 KB |
Output is correct |
39 |
Correct |
341 ms |
52928 KB |
Output is correct |
40 |
Correct |
346 ms |
50052 KB |
Output is correct |
41 |
Correct |
315 ms |
45500 KB |
Output is correct |
42 |
Correct |
376 ms |
54492 KB |
Output is correct |
43 |
Correct |
378 ms |
56164 KB |
Output is correct |
44 |
Correct |
271 ms |
51104 KB |
Output is correct |
45 |
Correct |
290 ms |
52164 KB |
Output is correct |
46 |
Correct |
245 ms |
48404 KB |
Output is correct |
47 |
Correct |
287 ms |
47236 KB |
Output is correct |
48 |
Correct |
334 ms |
45756 KB |
Output is correct |
49 |
Correct |
216 ms |
38240 KB |
Output is correct |
50 |
Correct |
5 ms |
20572 KB |
Output is correct |
51 |
Correct |
5 ms |
20572 KB |
Output is correct |
52 |
Correct |
5 ms |
20572 KB |
Output is correct |
53 |
Correct |
5 ms |
20572 KB |
Output is correct |
54 |
Correct |
5 ms |
20572 KB |
Output is correct |
55 |
Correct |
5 ms |
20740 KB |
Output is correct |
56 |
Correct |
5 ms |
20568 KB |
Output is correct |
57 |
Correct |
5 ms |
20568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
20572 KB |
Output is correct |
2 |
Correct |
6 ms |
20572 KB |
Output is correct |
3 |
Correct |
5 ms |
20568 KB |
Output is correct |
4 |
Correct |
5 ms |
20568 KB |
Output is correct |
5 |
Execution timed out |
2107 ms |
350460 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
22812 KB |
Output is correct |
2 |
Correct |
6 ms |
20568 KB |
Output is correct |
3 |
Correct |
5 ms |
20572 KB |
Output is correct |
4 |
Correct |
5 ms |
20572 KB |
Output is correct |
5 |
Correct |
5 ms |
20572 KB |
Output is correct |
6 |
Correct |
9 ms |
22876 KB |
Output is correct |
7 |
Correct |
10 ms |
23132 KB |
Output is correct |
8 |
Correct |
10 ms |
22876 KB |
Output is correct |
9 |
Correct |
10 ms |
22876 KB |
Output is correct |
10 |
Correct |
10 ms |
23048 KB |
Output is correct |
11 |
Correct |
10 ms |
22876 KB |
Output is correct |
12 |
Correct |
10 ms |
22784 KB |
Output is correct |
13 |
Correct |
10 ms |
22876 KB |
Output is correct |
14 |
Correct |
10 ms |
23128 KB |
Output is correct |
15 |
Correct |
11 ms |
23132 KB |
Output is correct |
16 |
Correct |
10 ms |
22876 KB |
Output is correct |
17 |
Correct |
10 ms |
22876 KB |
Output is correct |
18 |
Correct |
10 ms |
22872 KB |
Output is correct |
19 |
Correct |
10 ms |
22876 KB |
Output is correct |
20 |
Correct |
10 ms |
22876 KB |
Output is correct |
21 |
Correct |
10 ms |
22876 KB |
Output is correct |
22 |
Correct |
366 ms |
62292 KB |
Output is correct |
23 |
Correct |
213 ms |
42664 KB |
Output is correct |
24 |
Correct |
271 ms |
49040 KB |
Output is correct |
25 |
Correct |
204 ms |
43460 KB |
Output is correct |
26 |
Correct |
309 ms |
51484 KB |
Output is correct |
27 |
Correct |
369 ms |
57212 KB |
Output is correct |
28 |
Correct |
145 ms |
35832 KB |
Output is correct |
29 |
Correct |
279 ms |
45248 KB |
Output is correct |
30 |
Correct |
371 ms |
55488 KB |
Output is correct |
31 |
Correct |
398 ms |
57792 KB |
Output is correct |
32 |
Correct |
266 ms |
52760 KB |
Output is correct |
33 |
Correct |
322 ms |
52160 KB |
Output is correct |
34 |
Correct |
298 ms |
54152 KB |
Output is correct |
35 |
Correct |
222 ms |
43456 KB |
Output is correct |
36 |
Correct |
327 ms |
44040 KB |
Output is correct |
37 |
Correct |
294 ms |
44736 KB |
Output is correct |
38 |
Execution timed out |
2040 ms |
375960 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |