이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "rect.h"
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#define fi first
#define se second
#define pii pair<int, int>
using namespace std;
const int N = 2500 + 11;
struct DSU{
int dsu[N], l[N], r[N];
void init(int n){
for(int i = 0; i <= n + 2; i++) dsu[i] = l[i] = r[i] = i;
}
int root(int x){
if(x == dsu[x]) return x;
return dsu[x] = root(dsu[x]);
}
void join(int x, int y){
x = root(x), y = root(y);
if(x == y) return;
dsu[y] = x;
l[x] = min(l[x], l[y]);
r[x] = max(r[x], r[y]);
}
} dsu;
int at(const vector<int>& v, int idx){
if(idx < 0 || idx >= (int) v.size()) return -(1 << 30);
return v[idx];
}
vector<pii> build(const vector<int>& v){
vector<pii> res;
vector<pii> vp; int n = v.size();
for(int i = 0; i < n; i++){
vp.push_back({v[i], i});
}
sort(vp.begin(), vp.end());
dsu.init(n);
for(auto i : vp){
dsu.join(i.se, i.se + 1);
int rt = dsu.root(i.se);
int b_l = dsu.l[rt], b_r = dsu.r[rt] - 1;
if(at(v, b_l - 1) > i.fi && at(v, b_r + 1) > i.fi){
res.push_back({b_l, b_r});
}
}
return res;
}
struct Rect{
int16_t x1, x2, y1, y2;
void pr() { printf("%d %d %d %d\n", x1, x2, y1, y2); }
friend Rect operator+ (const Rect& a, const Rect& b){
return {max(a.x1, b.x1), min(a.x2, b.x2), max(a.y1, b.y1), min(a.y2, b.y2)};
}
};
void split(const vector<Rect>& a, vector<Rect>& al, vector<Rect>& ar, int xm){
for(auto r : a) {
if(r.x1 <= xm) al.push_back(r);
else ar.push_back(r);
}
}
long long ans = 0;
struct BIT2D{
int bit[N][N];
void add(int x, int y, int v){
x++; y++;
for(int i = x; i < N; i += i & -i){
for(int j = y; j < N; j += j & -j){
bit[i][j] += v;
}
}
}
void qry(int x, int y){
x++; y++;
for(int i = x; i; i -= i & -i){
for(int j = y; j; j -= j & -j){
ans += bit[i][j];
}
}
}
} bit2;
void calc(const vector<Rect>& ad, const vector<Rect>& qr){
if(ad.size() * qr.size() <= 200){
for(auto& r2 : qr){
for(auto& r1 : ad){
if(r1.x2 > r2.x2) break;
if(r1.y1 <= r2.y1 && r1.y2 <= r2.y2) ans++;
}
}
return;
}else{
int l = 0, r = 0;
while(l + r < (int) ad.size() + (int) qr.size()){
if(r == (int) qr.size() || (l != (int) ad.size() && ad[l].x2 <= qr[r].x2)){
bit2.add(ad[l].y1, ad[l].y2, 1); l++;
}else{
bit2.qry(qr[r].y1, qr[r].y2); r++;
}
}
for(int l = 0; l < (int) ad.size(); l++){
bit2.add(ad[l].y1, ad[l].y2, -1);
}
}
}
void solve(int xl, int xr, vector<Rect>& A, vector<Rect>& B) {
if(xl == xr){
calc(A, B);
}else{
int xm = (xl + xr) / 2;
vector<Rect> AL, AR, BL, BR; split(A, AL, AR, xm), split(B, BL, BR, xm);
solve(xl, xm, AL, BL), solve(xm + 1, xr, AR, BR), calc(AL, BR);
}
}
vector<int> xs[N * N], ys[N * N];
vector<Rect> xr, yr;
vector<pii> x[N], y[N];
long long count_rectangles(std::vector<std::vector<int>> a) {
int n = a.size(), m = a[0].size(), mx = 0;
for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) mx = max(mx, a[i][j]);
for(int i = 0; i < n; i++){
vector<int> v; for(int j = 0; j < m; j++) v.push_back(a[i][j]);
x[i] = build(v);
for(pii p : x[i]){
xs[p.fi * m + p.se].push_back(i);
}
}
for(int i = 0; i < m * m; i++){
int x1 = i / m, x2 = i % m; if(x1 > x2) continue;
int y1 = -2, y2 = -2;
for(auto j : xs[i]){
if(j == y2 + 1){
y2++;
}else{
if(y1 != -2) xr.push_back({x1, x2, y1, y2});
y1 = y2 = j;
}
}
if(y1 != -2) xr.push_back({x1, x2, y1, y2});
}
for(int j = 0; j < m; j++){
vector<int> v; for(int i = 0; i < n; i++) v.push_back(a[i][j]);
y[j] = build(v);
for(pii p : y[j]){
ys[p.fi * n + p.se].push_back(j);
}
}
for(int i = 0; i < n * n; i++){
int y1 = i / n, y2 = i % n; if(y1 > y2) continue;
int x1 = -2, x2 = -2;
for(auto j : ys[i]){
if(j == x2 + 1){
x2++;
}else{
if(x1 != -2) yr.push_back({x1, x2, y1, y2});
x1 = x2 = j;
}
}
if(x1 != -2) yr.push_back({x1, x2, y1, y2});
}
for(auto& r1 : xr) r1.x1 = m - 1 - r1.x1, r1.y2 = n - 1 - r1.y2;
for(auto& r2 : yr) r2.x1 = m - 1 - r2.x1, r2.y2 = n - 1 - r2.y2;
sort(xr.begin(), xr.end(), [](const Rect& a, const Rect& b){ return a.x2 < b.x2; });
sort(yr.begin(), yr.end(), [](const Rect& a, const Rect& b){ return a.x2 < b.x2; });
solve(0, m - 1, xr, yr);
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:146:32: warning: narrowing conversion of 'x1' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
146 | if(y1 != -2) xr.push_back({x1, x2, y1, y2});
| ^~
rect.cpp:146:36: warning: narrowing conversion of 'x2' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
146 | if(y1 != -2) xr.push_back({x1, x2, y1, y2});
| ^~
rect.cpp:146:40: warning: narrowing conversion of 'y1' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
146 | if(y1 != -2) xr.push_back({x1, x2, y1, y2});
| ^~
rect.cpp:146:44: warning: narrowing conversion of 'y2' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
146 | if(y1 != -2) xr.push_back({x1, x2, y1, y2});
| ^~
rect.cpp:150:30: warning: narrowing conversion of 'x1' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
150 | if(y1 != -2) xr.push_back({x1, x2, y1, y2});
| ^~
rect.cpp:150:34: warning: narrowing conversion of 'x2' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
150 | if(y1 != -2) xr.push_back({x1, x2, y1, y2});
| ^~
rect.cpp:150:38: warning: narrowing conversion of 'y1' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
150 | if(y1 != -2) xr.push_back({x1, x2, y1, y2});
| ^~
rect.cpp:150:42: warning: narrowing conversion of 'y2' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
150 | if(y1 != -2) xr.push_back({x1, x2, y1, y2});
| ^~
rect.cpp:166:32: warning: narrowing conversion of 'x1' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
166 | if(x1 != -2) yr.push_back({x1, x2, y1, y2});
| ^~
rect.cpp:166:36: warning: narrowing conversion of 'x2' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
166 | if(x1 != -2) yr.push_back({x1, x2, y1, y2});
| ^~
rect.cpp:166:40: warning: narrowing conversion of 'y1' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
166 | if(x1 != -2) yr.push_back({x1, x2, y1, y2});
| ^~
rect.cpp:166:44: warning: narrowing conversion of 'y2' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
166 | if(x1 != -2) yr.push_back({x1, x2, y1, y2});
| ^~
rect.cpp:170:30: warning: narrowing conversion of 'x1' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
170 | if(x1 != -2) yr.push_back({x1, x2, y1, y2});
| ^~
rect.cpp:170:34: warning: narrowing conversion of 'x2' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
170 | if(x1 != -2) yr.push_back({x1, x2, y1, y2});
| ^~
rect.cpp:170:38: warning: narrowing conversion of 'y1' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
170 | if(x1 != -2) yr.push_back({x1, x2, y1, y2});
| ^~
rect.cpp:170:42: warning: narrowing conversion of 'y2' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
170 | if(x1 != -2) yr.push_back({x1, x2, y1, y2});
| ^~
# | 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... |