이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "seats.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
ll h, w;
vector<vector<ll>> grid, prefixGrid;
vector<pii> pos;
bool outOfBounds(pii coords)
{
int i = coords.first, j = coords.second;
return i < 0 || i >= h || j < 0 || j >= w;
}
void calcPrefix()
{
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
{
pii above = {i - 1, j};
pii before = {i, j - 1};
pii upLeft = {i - 1, j - 1};
ll res = grid[i][j];
if (!outOfBounds(above))
res += prefixGrid[above.first][above.second];
if (!outOfBounds(before))
res += prefixGrid[before.first][before.second];
if (!outOfBounds(upLeft))
res -= prefixGrid[upLeft.first][upLeft.second];
prefixGrid[i][j] = res;
}
}
ll getRectangleSum(pii topLeftCorner, pii bottomRightCorner)
{
pii before = {bottomRightCorner.first, topLeftCorner.second - 1};
pii above = {topLeftCorner.first - 1, bottomRightCorner.second};
pii upLeft = {topLeftCorner.first - 1, topLeftCorner.second - 1};
ll res = prefixGrid[bottomRightCorner.first][bottomRightCorner.second];
if (!outOfBounds(above))
res -= prefixGrid[above.first][above.second];
if (!outOfBounds(before))
res -= prefixGrid[before.first][before.second];
if (!outOfBounds(upLeft))
res += prefixGrid[upLeft.first][upLeft.second];
return res;
}
ll checkBeauty()
{
ll res = 0;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
for (int k = i; k < h; k++)
for (int l = j; l < w; l++)
{
ll area = (k - i + 1) * (l - j + 1);
res += (getRectangleSum({i, j}, {k, l}) == area * (area - 1) / 2);
}
return res;
}
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C)
{
h = H, w = W;
grid = prefixGrid = vector<vector<ll>>(H, vector<ll>(W));
pos = vector<pair<int, int>>(H * W);
for (int i = 0; i < H * W; i++)
{
grid[R[i]][C[i]] = i;
pos[i] = {R[i], C[i]};
}
calcPrefix();
}
int swap_seats(int a, int b)
{
pair<int, int> aPos = pos[a], bPos = pos[b];
swap(grid[aPos.first][aPos.second], grid[bPos.first][bPos.second]);
swap(pos[a], pos[b]);
calcPrefix();
return checkBeauty();
}
# | 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... |