# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1104947 |
2024-10-24T21:11:28 Z |
belgianbot |
Mosaic (IOI24_mosaic) |
C++17 |
|
136 ms |
44452 KB |
#include "mosaic.h"
#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<signed> X, Y, T, B, L, R;
vector<long long> C;
int N, Q;
void sub() {
vector<vector<bool>> grid(N, vector<bool>(N));
for (int i = 0; i < N; i++) {
grid[0][i] = X[i];
grid[i][0] = Y[i];
}
for (int i = 1; i < N; i++) {
for (int j = 1; j < N; j++) {
grid[i][j] = !(grid[i - 1][j] || grid[i][j - 1]);
}
}
vector<vector<int>> dp(N , vector<int> (N, 0));
for (int i = 0; i < N; i++) {
int l = 0;
for (int j = 0; j < N; j++) {
l += grid[i][j];
if (i) dp[i][j] = dp[i - 1][j];
dp[i][j] += l;
}
}
for (int i = 0; i < Q; i++) {
int ans = 0;
if (!L[i] && ! T[i]) ans = dp[B[i]][R[i]];
else {
ans = dp[B[i]][R[i]];
if (L[i]) ans -= dp[B[i]][L[i] - 1];
if (T[i]) ans -= dp[T[i] - 1][R[i]];
if (L[i] && T[i]) ans += dp[T[i] - 1][L[i] - 1];
}
C[i] = ans;
}
}
std::vector<long long> mosaic(std::vector<signed> X_, std::vector<signed> Y_,std::vector<signed> T_, std::vector<signed> B_, std::vector<signed> L_, std::vector<signed> R_) {
ios::sync_with_stdio(false);
cin.tie(0);
X = X_; Y = Y_; T = T_; B = B_; L = L_; R = R_;
Q = (int)T.size();
N = X.size();
C.resize(Q, 0);
if (N <= 3) {
sub();
return C;
}
vector<vector<bool>> line(3, vector<bool>(N)), column(3, vector<bool>(N));
for (int i = 0; i < N; i++) {
line[0][i] = X[i];
column[0][i] = Y[i];
}
line[1][0] = Y[1], line[2][0] = Y[2];
column[1][0] = X[1], column[2][0] = X[2];
for (int i = 1; i < 3; i++) {
for (int j = 1; j < N; j++) {
line[i][j] = !(line[i - 1][j] || line[i][j - 1]);
column[i][j] = !(column[i - 1][j] || column[i][j - 1]);
}
}
vector<vector<int>> dpLine(3 , vector<int> (N, 0)), dpColumn(3, vector<int>(N, 0));
for (int i = 0; i < 3; i++) {
int lLine = 0, lColumn = 0;
for (int j = 0; j < N; j++) {
lLine += line[i][j]; lColumn += column[i][j];
if (i) {
dpLine[i][j] = dpLine[i - 1][j];
dpColumn[i][j] = dpColumn[i - 1][j];
}
dpLine[i][j] += lLine, dpColumn[i][j] += lColumn;
}
}
vector<bool> diag(2 * N-1, false);
for (int i = 0; i <= N - 2; i++) {
diag[N + 1 - i] = column[2][i];
diag[N - 3 + i] = line[2][i];
}
vector<int> prefL(2 * N - 1, 0), prefR(2 * N-1, 0), pref(2 * N-1);
int cnt = 1;
for (int i = 0; i < 2 * N - 1; i++) {
if (i) {
prefL[i] = prefL[i - 1];
pref[i] = pref[i - 1];
prefR[2 * N - i - 2] = prefR[2 * N - i - 1];
}
prefL[i] += diag[i] * cnt;
prefR[2 * N - i - 2] += diag[2 * N - i - 2] * cnt;
pref[i] += diag[i];
cnt++;
}
/*for (int i = 0; i < 3; i++) {
prefL[i] = 0;
pref[i] = 0;
prefR[i] = 0;
prefL[2*N - 2 - i] = 0;
prefR[2*N - 2 - i] = 0;
pref[2*N - 2 - i] = 0;
}*/
for (int i = 0; i < Q; i++) {
int t = T[i], b = B[i], l = L[i], r = R[i];
if (t < 3) {
if (b < 3) {
C[i] += dpLine[b][r];
if (t) C[i] -= dpLine[t - 1][r];
if (l) C[i] -= dpLine[b][l - 1];
if (t && l) C[i] += dpLine[t - 1][l - 1];
continue;
}
else {
C[i] += dpLine[2][r];
if (t) C[i] -= dpLine[t - 1][r];
if (l) C[i] -= dpLine[2][l - 1];
if (t && l) C[i] += dpLine[t - 1][l - 1];
}
t = 3;
}
if (l < 3) {
if (r < 3) {
C[i] += dpColumn[r][b];
if (t) C[i] -= dpColumn[r][t - 1];
if (l) C[i] -= dpColumn[l - 1][b];
if (t && l) C[i] += dpColumn[l - 1][t - 1];
continue;
}
else {
C[i] += dpColumn[2][b];
if (t) C[i] -= dpColumn[2][t - 1];
if (l) C[i] -= dpColumn[l - 1][b];
if (t && l) C[i] += dpColumn[l - 1][t - 1];
}
l = 3;
}
int diag1 = N - 1 - b + l;
int diag2 = N - 1 - t + r;
int mid = (diag1 + diag2 + 1) / 2;
C[i] += prefL[mid] - prefL[diag1 - 1];
C[i] -= (pref[mid] - pref[diag1 - 1]) * diag1;
C[i] += prefR[mid + 1] - prefR[diag2 + 1];
C[i] -= (pref[diag2] - pref[mid]) * (2 * N - 2 - diag2);
int height = b - t + 1, large = r - l + 1, limit = min(height, large);
/*if (mid >= diag1 + limit - 1)*/C[i] -= prefL[mid] - prefL[diag1 + limit - 1] - (pref[mid] - pref[diag1 + limit - 1]) * (diag1 + limit);
/*if (mid + 1 - diag2 + limit - 1 <= 0)*/ C[i] -= prefR[mid + 1] - prefR[diag2 - limit + 1] - (pref[diag2 - limit]- pref[mid]) * (2 * N - 2 - diag2 + limit);
}
return C;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
444 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
42436 KB |
Output is correct |
2 |
Correct |
105 ms |
41896 KB |
Output is correct |
3 |
Correct |
110 ms |
41516 KB |
Output is correct |
4 |
Correct |
105 ms |
41920 KB |
Output is correct |
5 |
Correct |
89 ms |
29032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
444 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
70 ms |
17224 KB |
Output is correct |
19 |
Correct |
61 ms |
17040 KB |
Output is correct |
20 |
Correct |
61 ms |
16976 KB |
Output is correct |
21 |
Correct |
61 ms |
16712 KB |
Output is correct |
22 |
Correct |
64 ms |
16980 KB |
Output is correct |
23 |
Correct |
63 ms |
16712 KB |
Output is correct |
24 |
Correct |
63 ms |
16624 KB |
Output is correct |
25 |
Correct |
61 ms |
16640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
14408 KB |
Output is correct |
2 |
Correct |
119 ms |
43944 KB |
Output is correct |
3 |
Correct |
107 ms |
43244 KB |
Output is correct |
4 |
Correct |
112 ms |
43248 KB |
Output is correct |
5 |
Correct |
105 ms |
43632 KB |
Output is correct |
6 |
Correct |
114 ms |
43884 KB |
Output is correct |
7 |
Correct |
115 ms |
44008 KB |
Output is correct |
8 |
Correct |
115 ms |
44076 KB |
Output is correct |
9 |
Correct |
89 ms |
30184 KB |
Output is correct |
10 |
Correct |
93 ms |
30276 KB |
Output is correct |
11 |
Correct |
90 ms |
30312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
124 ms |
44088 KB |
Output is correct |
2 |
Correct |
136 ms |
44068 KB |
Output is correct |
3 |
Correct |
117 ms |
42404 KB |
Output is correct |
4 |
Correct |
111 ms |
42540 KB |
Output is correct |
5 |
Correct |
118 ms |
44452 KB |
Output is correct |
6 |
Correct |
91 ms |
30520 KB |
Output is correct |
7 |
Correct |
89 ms |
24872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
42436 KB |
Output is correct |
2 |
Correct |
105 ms |
41896 KB |
Output is correct |
3 |
Correct |
110 ms |
41516 KB |
Output is correct |
4 |
Correct |
105 ms |
41920 KB |
Output is correct |
5 |
Correct |
89 ms |
29032 KB |
Output is correct |
6 |
Correct |
124 ms |
44088 KB |
Output is correct |
7 |
Correct |
136 ms |
44068 KB |
Output is correct |
8 |
Correct |
117 ms |
42404 KB |
Output is correct |
9 |
Correct |
111 ms |
42540 KB |
Output is correct |
10 |
Correct |
118 ms |
44452 KB |
Output is correct |
11 |
Correct |
91 ms |
30520 KB |
Output is correct |
12 |
Correct |
89 ms |
24872 KB |
Output is correct |
13 |
Correct |
130 ms |
44076 KB |
Output is correct |
14 |
Correct |
133 ms |
43900 KB |
Output is correct |
15 |
Correct |
113 ms |
44204 KB |
Output is correct |
16 |
Correct |
109 ms |
42408 KB |
Output is correct |
17 |
Correct |
113 ms |
43564 KB |
Output is correct |
18 |
Correct |
131 ms |
44076 KB |
Output is correct |
19 |
Correct |
89 ms |
27624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
444 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
112 ms |
42436 KB |
Output is correct |
20 |
Correct |
105 ms |
41896 KB |
Output is correct |
21 |
Correct |
110 ms |
41516 KB |
Output is correct |
22 |
Correct |
105 ms |
41920 KB |
Output is correct |
23 |
Correct |
89 ms |
29032 KB |
Output is correct |
24 |
Correct |
70 ms |
17224 KB |
Output is correct |
25 |
Correct |
61 ms |
17040 KB |
Output is correct |
26 |
Correct |
61 ms |
16976 KB |
Output is correct |
27 |
Correct |
61 ms |
16712 KB |
Output is correct |
28 |
Correct |
64 ms |
16980 KB |
Output is correct |
29 |
Correct |
63 ms |
16712 KB |
Output is correct |
30 |
Correct |
63 ms |
16624 KB |
Output is correct |
31 |
Correct |
61 ms |
16640 KB |
Output is correct |
32 |
Correct |
61 ms |
14408 KB |
Output is correct |
33 |
Correct |
119 ms |
43944 KB |
Output is correct |
34 |
Correct |
107 ms |
43244 KB |
Output is correct |
35 |
Correct |
112 ms |
43248 KB |
Output is correct |
36 |
Correct |
105 ms |
43632 KB |
Output is correct |
37 |
Correct |
114 ms |
43884 KB |
Output is correct |
38 |
Correct |
115 ms |
44008 KB |
Output is correct |
39 |
Correct |
115 ms |
44076 KB |
Output is correct |
40 |
Correct |
89 ms |
30184 KB |
Output is correct |
41 |
Correct |
93 ms |
30276 KB |
Output is correct |
42 |
Correct |
90 ms |
30312 KB |
Output is correct |
43 |
Correct |
124 ms |
44088 KB |
Output is correct |
44 |
Correct |
136 ms |
44068 KB |
Output is correct |
45 |
Correct |
117 ms |
42404 KB |
Output is correct |
46 |
Correct |
111 ms |
42540 KB |
Output is correct |
47 |
Correct |
118 ms |
44452 KB |
Output is correct |
48 |
Correct |
91 ms |
30520 KB |
Output is correct |
49 |
Correct |
89 ms |
24872 KB |
Output is correct |
50 |
Correct |
130 ms |
44076 KB |
Output is correct |
51 |
Correct |
133 ms |
43900 KB |
Output is correct |
52 |
Correct |
113 ms |
44204 KB |
Output is correct |
53 |
Correct |
109 ms |
42408 KB |
Output is correct |
54 |
Correct |
113 ms |
43564 KB |
Output is correct |
55 |
Correct |
131 ms |
44076 KB |
Output is correct |
56 |
Correct |
89 ms |
27624 KB |
Output is correct |
57 |
Correct |
131 ms |
43880 KB |
Output is correct |
58 |
Correct |
124 ms |
43564 KB |
Output is correct |
59 |
Correct |
117 ms |
43880 KB |
Output is correct |
60 |
Correct |
123 ms |
43308 KB |
Output is correct |
61 |
Correct |
120 ms |
43320 KB |
Output is correct |
62 |
Correct |
123 ms |
43564 KB |
Output is correct |
63 |
Correct |
98 ms |
30520 KB |
Output is correct |
64 |
Correct |
88 ms |
24044 KB |
Output is correct |