이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "rect.h"
#include <stdlib.h>
using namespace std;
typedef vector<int> vi;
const int N = 2500, M = 2500;
int *ej[N][N], eo[N][N];
void append(int i1, int i2, int j) {
int o = eo[i1][i2]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i1][i2] = (int *) realloc(ej[i1][i2], o * 2 * sizeof *ej[i1][i2]);
ej[i1][i2][o] = j;
}
int qu[N], ii1[M][M], ii2[M][M], jj1[N][M], jj2[N][M], kk[N], ft[M][M];
void update(int *ft, int i, int n, int x) {
while (i < n) {
ft[i] += x;
i |= i + 1;
}
}
int query(int *ft, int i) {
int x = 0;
while (i >= 0) {
x += ft[i];
i &= i + 1, i--;
}
return x;
}
long long count_rectangles(vector<vi> aa) {
int n = aa.size(), m = aa[0].size();
long long ans;
int h, i, i1, i2, j, j1, j2, o, cnt;
for (i1 = 0; i1 < n; i1++)
for (i2 = i1; i2 < n; i2++)
ej[i1][i2] = (int *) malloc(2 * sizeof *ej[i1][i2]);
for (j = 0; j < m; j++) {
cnt = 0;
for (i = 0; i < n; i++) {
while (cnt && aa[qu[cnt - 1]][j] < aa[i][j])
cnt--;
if (cnt && qu[cnt - 1] + 1 < i)
append(qu[cnt - 1] + 1, i - 1, j);
qu[cnt++] = i;
}
cnt = 0;
for (i = n - 1; i >= 0; i--) {
while (cnt && aa[qu[cnt - 1]][j] < aa[i][j])
cnt--;
if (cnt && aa[qu[cnt - 1]][j] > aa[i][j] && i + 1 < qu[cnt - 1])
append(i + 1, qu[cnt - 1] - 1, j);
qu[cnt++] = i;
}
}
for (j1 = 0; j1 < m; j1++)
for (j2 = j1; j2 < m; j2++)
ii1[j1][j2] = ii2[j1][j2] = n + 1;
ans = 0;
for (i1 = n - 1; i1 >= 0; i1--) {
cnt = 0;
for (j = 0; j < m; j++) {
while (cnt && aa[i1][qu[cnt - 1]] < aa[i1][j])
cnt--;
if (cnt && qu[cnt - 1] + 1 < j) {
j1 = qu[cnt - 1] + 1, j2 = j - 1;
if (i1 + 1 < ii1[j1][j2])
ii2[j1][j2] = i1;
ii1[j1][j2] = i1;
i2 = ii2[j1][j2];
jj1[i2][kk[i2]] = j1, jj2[i2][kk[i2]] = j2, kk[i2]++;
update(ft[j1], j2, m, 1);
}
qu[cnt++] = j;
}
cnt = 0;
for (j = m - 1; j >= 0; j--) {
while (cnt && aa[i1][qu[cnt - 1]] < aa[i1][j])
cnt--;
if (cnt && aa[i1][qu[cnt - 1]] > aa[i1][j] && j + 1 < qu[cnt - 1]) {
j1 = j + 1, j2 = qu[cnt - 1] - 1;
if (i1 + 1 < ii1[j1][j2])
ii2[j1][j2] = i1;
ii1[j1][j2] = i1;
i2 = ii2[j1][j2];
jj1[i2][kk[i2]] = j1, jj2[i2][kk[i2]] = j2, kk[i2]++;
update(ft[j1], j2, m, 1);
}
qu[cnt++] = j;
}
for (i2 = i1; i2 < n; i2++) {
for (o = eo[i1][i2], j2 = m + 1; o--; ) {
j1 = ej[i1][i2][o];
if (o + 1 == eo[i1][i2] || j1 + 1 < ej[i1][i2][o + 1])
j2 = j1;
ans += query(ft[j1], j2);
}
for (h = 0; h < kk[i2]; h++) {
j1 = jj1[i2][h], j2 = jj2[i2][h];
update(ft[j1], j2, m, -1);
}
kk[i2] = 0;
}
}
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
rect.cpp: In function 'void append(int, int, int)':
rect.cpp:15:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
15 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |