이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define INF 1000000000000000000
#define MOD 998244353
int fw[2505];
void update(int x, int v) {
for(x++; x < 2505; x += x&-x) fw[x] += v;
}
void update(int x, int y, int v) {
update(x, v); update(y+1, -v);
}
int query(int x) {
int ans = 0;
for(x++; x; x -= x&-x) ans += fw[x];
return ans;
}
long long count_rectangles(vector<vector<int>> a) {
int m = a.size(), n = a[0].size();
vector<int> tot[max(n,m)][max(n,m)];
vector<pair<int, int>> goodh[m][n], goodv[m][n];
for(int i = 1; i < m-1; i++) {
stack<int> s;
int l[n], r[n];
for(int j = 0; j < n; j++) {
while(s.size() && a[i][s.top()] <= a[i][j]) s.pop();
if(s.size()) l[j] = s.top();
else l[j] = -1;
s.push(j);
}
while(s.size()) s.pop();
for(int j = n-1; j >= 0; j--) {
while(s.size() && a[i][s.top()] <= a[i][j]) s.pop();
if(s.size()) r[j] = s.top();
else r[j] = -1;
s.push(j);
}
for(int j = 0; j < n; j++) if(l[j] != -1 && r[j] != -1) tot[l[j]][r[j]].push_back(i);
}
for(int i = 0; i < n; i++) {
for(int j = i+2; j < n; j++) {
if(!tot[i][j].size()) continue;
int prv = -1;
tot[i][j].erase(unique(tot[i][j].begin(), tot[i][j].end()), tot[i][j].end());
for(int k = 1; k < tot[i][j].size(); k++) {
if(tot[i][j][k]-tot[i][j][k-1] > 1) {
while(prv < k-1) {
goodh[tot[i][j][prv+1]][i+1].push_back({tot[i][j][k-1], j-1});
prv++;
}
}
}
while(prv < (int)tot[i][j].size()-1) {
goodh[tot[i][j][prv+1]][i+1].push_back({tot[i][j][tot[i][j].size()-1], j-1});
prv++;
}
tot[i][j].clear();
}
}
for(int i = 1; i < n-1; i++) {
stack<int> s;
int l[m], r[m];
for(int j = 0; j < m; j++) {
while(s.size() && a[s.top()][i] <= a[j][i]) s.pop();
if(s.size()) l[j] = s.top();
else l[j] = -1;
s.push(j);
}
while(s.size()) s.pop();
for(int j = m-1; j >= 0; j--) {
while(s.size() && a[s.top()][i] <= a[j][i]) s.pop();
if(s.size()) r[j] = s.top();
else r[j] = -1;
s.push(j);
}
for(int j = 0; j < m; j++) if(l[j] != -1 && r[j] != -1) tot[l[j]][r[j]].push_back(i);
}
for(int i = 0; i < m; i++) {
for(int j = i+2; j < m; j++) {
if(!tot[i][j].size()) continue;
int prv = -1;
tot[i][j].erase(unique(tot[i][j].begin(), tot[i][j].end()), tot[i][j].end());
for(int k = 1; k < tot[i][j].size(); k++) {
if(tot[i][j][k]-tot[i][j][k-1] > 1) {
while(prv < k-1) {
goodv[i+1][tot[i][j][prv+1]].push_back({j-1, tot[i][j][k-1]});
prv++;
}
}
}
while(prv < (int)tot[i][j].size()-1) {
goodv[i+1][tot[i][j][prv+1]].push_back({j-1, tot[i][j][tot[i][j].size()-1]});
prv++;
}
}
}
long long ans = 0;
for(int i = 1; i < m-1; i++) {
for(int j = 1; j < n-1; j++) {
sort(goodh[i][j].begin(), goodh[i][j].end());
sort(goodv[i][j].begin(), goodv[i][j].end());
int idx = 0;
for(auto k : goodh[i][j]) {
while(idx < goodv[i][j].size() && goodv[i][j][idx].first <= k.first) {
update(j, goodv[i][j][idx++].second, 1);
}
ans += query(k.second);
}
for(int k = 0; k < idx; k++) update(j, goodv[i][j][k].second, -1);
}
}
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:50:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int k = 1; k < tot[i][j].size(); k++) {
| ~~^~~~~~~~~~~~~~~~~~
rect.cpp:88:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for(int k = 1; k < tot[i][j].size(); k++) {
| ~~^~~~~~~~~~~~~~~~~~
rect.cpp:109:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | while(idx < goodv[i][j].size() && goodv[i][j][idx].first <= k.first) {
| ~~~~^~~~~~~~~~~~~~~~~~~~
# | 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... |