Submission #823094

#TimeUsernameProblemLanguageResultExecution timeMemory
823094vjudge1Rectangles (IOI19_rect)C++17
72 / 100
5072 ms584856 KiB
#include<bits/stdc++.h>
#include "rect.h"

using namespace std;
using ll = long long;

const int M = (1 << 13);
int t[2 * M];

void update(int pos, int val) {
	for(t[pos += M] += val; pos > 1; pos >>= 1) 
		t[pos >> 1] = t[pos] + t[(pos ^ 1)];
}
int get(int ql, int qr) {
	int sum = 0;
	for(ql += M, qr += M + 1; ql < qr; ql >>= 1, qr >>= 1) {
		if(ql & 1) sum += t[ql++];
		if(qr & 1) sum += t[--qr];
	}
	return sum;
}

void compress(vector<int> &a) {
	map<int, int> mp;
	int n = (int)a.size();
	for(int i = 0; i < n; i++) 	
		mp[a[i]] = 1;
	int k = 0;
	for(auto &c : mp) {
		c.second = k++;
	}
	for(int i = 0; i < n; i++) 
		a[i] = mp[a[i]];
}

// O(nlogn)
vector<pair<int, int>> get_segments(vector<int> a) {
	compress(a);
	int n = (int)a.size();

	vector<int> b[n];
	vector<pair<int, int>> res;
	
	for(int i = 0; i < n; i++) {
		b[a[i]].push_back(i);
	}

	set<int> st;
	for(int x = n - 1; x >= 0; x--) {
		for(int pos	: b[x]) 
			st.insert(pos);
		for(int i = 0; i < (int)b[x].size(); i++) {
			int pos = b[x][i];
			auto it = st.lower_bound(pos);
			if(it != st.begin()) {
				auto lb = it; lb--;
				if(*it - *lb > 1) 
					res.push_back({*lb + 1, *it - 1});
			}
			if(it != (--st.end())) {
				auto rb = it; rb++;
				if(*rb - *it > 1 && (i == (int)b[x].size() - 1 || b[x][i + 1] != *rb)) 
					res.push_back({*it + 1, *rb - 1});
			}
		}
	}
	return res;
}

const int N = 2505;

struct segment {
	int l, d;
};

vector<segment> h[N][N], g[N][N];

ll count_rectangles(vector<vector<int>> a) {
	int n = (int)a.size(), m = (int)a[0].size();
	if(n < 3 || m < 3) return 0;
	for(int i = 0; i < n; i++) {
		vector<pair<int, int>> s = get_segments(a[i]);
		for(auto [l, r] : s) 
			g[i][r].push_back({l, 0});
	}
	for(int i = 0; i < m; i++) {
		vector<int> b;
		for(int j = 0; j < n; j++) 
			b.push_back(a[j][i]);
		vector<pair<int, int>> s = get_segments(b);
		for(auto [l, r] : s) 
			h[r][i].push_back({l, 0});
	}
	for(int i = 0; i < n; i++) {
		int cnt[n] = {};
		bool us[n] = {};
		for(int j = 0; j < m; j++) {
			if(!j) {
				for(auto c : h[i][j]) 
					cnt[c.l]++;
			} else {
				for(auto c : h[i][j])
					us[c.l] = 1;
				for(auto c : h[i][j - 1])
					if(!us[c.l]) cnt[c.l] = 0;
				
				for(auto c : h[i][j]) {
					cnt[c.l]++;
					us[c.l] = 0;
				}
			}
			for(auto &c : h[i][j]) {
				c.d = cnt[c.l];
			}
		}
	}
	for(int i = 0; i < m; i++) {
		int cnt[m] = {};
		bool us[m] = {};
		for(int j = 0; j < n; j++) {
			if(!j) {
				for(auto c : g[j][i]) 
					cnt[c.l]++;
			} else {
				for(auto c : g[j][i]) 
					us[c.l] = 1;
				for(auto c : g[j - 1][i]) 
					if(!us[c.l]) 
						cnt[c.l] = 0;
				for(auto c : g[j][i]) {
					cnt[c.l]++;
					us[c.l] = 0;
				}
			}
			for(auto &c : g[j][i])
				c.d = cnt[c.l];
		}
	}
	ll ans = 0;
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			sort(g[i][j].begin(), g[i][j].end(), [](segment x, segment y) {return x.l < y.l;});
			sort(h[i][j].begin(), h[i][j].end(), [](segment x, segment y) {return x.d > y.d;});
			int h_l = 0;
			for(segment cur : g[i][j]) {
				while(h_l < (int)h[i][j].size() && j - h[i][j][h_l].d < cur.l) {
					update(h[i][j][h_l++].l, +1);
				} 
				ans += get(i - cur.d + 1, i);
			}
			while(h_l > 0) 
				update(h[i][j][--h_l].l, -1);
		}
	}
	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...