제출 #724393

#제출 시각아이디문제언어결과실행 시간메모리
724393QwertyPiRectangles (IOI19_rect)C++14
37 / 100
5091 ms384428 KiB
#include "rect.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
using namespace std;
const int N = 2500 + 11;
vector<pii> x[N], y[N];
 
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 >= 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{
	int 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)};
	}
};

vector<int> xs[N * N], ys[N * N];
vector<Rect> xr, yr;
long long count_rectangles(std::vector<std::vector<int>> a) {
	int n = a.size(), m = a[0].size();
	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});
	}
	// cout << "X" << endl;
	// for(auto i : xr) i.pr();
	// cout << "Y" << endl;
	// for(auto i : yr) i.pr();
	long long ans = 0;
	for(auto r1 : xr){
		for(auto r2 : yr){
			if(r2.x1 <= r1.x1 && r1.x2 <= r2.x2
			&& r1.y1 <= r2.y1 && r2.y2 <= r1.y2)
				ans++;
		}
	}
	return ans;
}

컴파일 시 표준 에러 (stderr) 메시지

rect.cpp: In function 'int at(const std::vector<int>&, int)':
rect.cpp:29:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |  if(idx < 0 || idx >= v.size()) return -(1 << 30);
      |                ~~~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...