Submission #583585

#TimeUsernameProblemLanguageResultExecution timeMemory
583585LucppRectangles (IOI19_rect)C++17
72 / 100
3322 ms1048576 KiB
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int)(x).size())
typedef long long ll;
 
vector<int> fen;
void add(int i, int x){
	for(i++; i < sz(fen); i+=i&-i) fen[i] += x;
}
int qry(int i){
	int ans = 0;
	for(i++; i > 0; i-=i&-i) ans += fen[i];
	return ans;
}
int qry(int i, int j){
	if(i > 0) return qry(j)-qry(i-1);
	else return qry(j);
}
 
long long count_rectangles(vector<vector<int>> a) {
	int n = sz(a), m = sz(a[0]);
	vector<vector<short>> r1(n, vector<short>(m, -1)), r2(n, vector<short>(m, -1));
	for(int i = 1; i < n-1; i++){
		stack<pair<int, int>> st({{a[i][0], 0}});
		for(int j = 1; j < m; j++){
			while(!st.empty() && a[i][j] > st.top().first){
				auto [h, k] = st.top();
				st.pop();
				r2[i][k] = j-1;
			}
			if(!st.empty()) {
				if(st.top().first > a[i][j]) r1[i][j] = st.top().second+1;
				else r1[i][j] = r1[i][st.top().second];
			}
			st.emplace(a[i][j], j);
		}
	}
	vector<vector<short>> c1(n, vector<short>(m, -1)), c2(n, vector<short>(m, -1));
	for(int j = 1; j < m-1; j++){
		stack<pair<int, int>> st({{a[0][j], 0}});
		for(int i = 1; i < n; i++){
			while(!st.empty() && a[i][j] > st.top().first){
				auto [h, k] = st.top(); st.pop();
				c2[k][j] = i-1;
			}
			if(!st.empty()){
				if(st.top().first > a[i][j]) c1[i][j] = st.top().second+1;
				else c1[i][j] = c1[st.top().second][j];
			}
			st.emplace(a[i][j], i);
		}
	}
	a.clear();
	a.shrink_to_fit();
	int mx = max(n, m);
	fen.resize(mx+1);
	vector<bool> valid;
	vector<int> queries;
	vector<vector<int>> byI(mx*mx), byJ(mx*mx);
	unordered_set<ll> noDupl;
	vector<vector<int>> goodI(mx*mx), goodJ(mx*mx);
	int q = 0;
	int ans = 0;
	for(int i = 1; i < n-1; i++){
		for(int j = 1; j < m-1; j++){
			// cerr << "\n" << i << " " << j << " " << r1[i][j] << " " << r2[i][j] << " " << c1[i][j] << " " << c2[i][j] << "\n";
			if(r1[i][j] != -1 && r2[i][j] != -1 && c1[i][j] != -1 && c2[i][j] != -1){
				int x = c1[i][j]*mx + c2[i][j], y = r1[i][j]*mx + r2[i][j];
				goodJ[x].push_back(j);
				goodI[y].push_back(i);
				if(r1[i][j] == r2[i][j] && c1[i][j] == c2[i][j]){
					ans++;
					continue;
				}
				ll t = (ll)x*mx*mx+y;
				if(noDupl.count(t)) continue;
				noDupl.insert(t);
				valid.push_back(true);
				queries.push_back(y);
				byI[x].push_back(q);
				byJ[y].push_back(q);
				q++;
			}
		}
	}
	for(int id = 1; id < mx*mx; id++){
		sort(goodJ[id].begin(), goodJ[id].end());
		int last = -1;
		for(int j : goodJ[id]){
			if(j != last) add(j, 1);
			last = j;
		}
		for(int ind : byI[id]){
			int id2 = queries[ind];
			int L = id2/mx, R = id2%mx;
			if(qry(L, R) != R-L+1) valid[ind] = false;
			queries[ind] = id;
		}
		last = -1;
		for(int j : goodJ[id]){
			if(j != last) add(j, -1);
			last = j;
		}
	}
	for(int id = 1; id < mx*mx; id++){
		sort(goodI[id].begin(), goodI[id].end());
		int last = -1;
		for(int i : goodI[id]){
			if(i != last) add(i, 1);
			last = i;
		}
		for(int ind : byJ[id]){
			int id2 = queries[ind];
			int L = id2/mx, R = id2%mx;
			if(qry(L, R) != R-L+1) valid[ind] = false;
		}
		last = -1;
		for(int i : goodI[id]){
			if(i != last) add(i, -1);
			last = i;
		}
	}
	for(bool b : valid) ans += b;
	return ans;
}
#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...