Submission #726559

#TimeUsernameProblemLanguageResultExecution timeMemory
726559LittleCubeRectangles (IOI19_rect)C++14
10 / 100
32 ms59732 KiB
#pragma GCC optimize("Ofast,unroll-loops")
#ifndef EVAL
#include "grader.cpp"
#endif
#include "rect.h"
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;

struct BIT
{
	int bit[2501], N;
	vector<int> p;
	int getP(int x)
	{
		return lower_bound(p.begin(), p.end(), x) - p.begin();
	}
	void init()
	{
		p.emplace_back(0);
		sort(p.begin(), p.end());
		p.resize(unique(p.begin(), p.end()) - p.begin());
		N = p.size();
		for (int i = 1; i < N; i++)
			bit[i] = 0;
	}
	void modify(int pos, int val)
	{
		pos = getP(pos);
		for (int i = pos; i < N; i += (i & -i))
			bit[i] += val;
	}
	int query(int pos)
	{
		pos = getP(pos);
		int ans = 0;
		for (int i = pos; i; i -= (i & -i))
			ans += bit[i];
		return ans;
	}

} bit[2500];

int N, M;
pii table[2500][2500];
vector<pii> ver[2500], hor[2500];
vector<tuple<int, int, int, int>> event[2500];
ll ans;
ll cnt;
void add_query(int l, int r, pii range)
{
	if (range == pii(-1, -1))
		return;

	event[l].emplace_back(make_tuple(r, 0, range.F - 1, range.S + 1));
}

void add_modify(pii range, int u, int d)
{
	if (range == pii(-1, -1))
		return;
	for (int i = range.F - 1; i <= range.S + 1; i++)
	{
		cnt++;
		event[i].emplace_back(make_tuple(range.S + 1, 1, u, d));
	}
}

ll count_rectangles(vector<vector<int>> a)
{
	N = a.size(), M = a[0].size();

	if (N <= 2 || M <= 2)
		return 0;

	for (int i = 1; i + 1 < N; i++)
	{
		vector<pii> mono;
		for (int j = 0; j < M; j++)
		{
			while (!mono.empty() && mono.back().F < a[i][j])
			{
				if (j - mono.back().S >= 2)
					ver[i].emplace_back(pii(mono.back().S, j));
				mono.pop_back();
			}
			if (!mono.empty())
				if (j - mono.back().S >= 2)
					ver[i].emplace_back(pii(mono.back().S, j));
			if (!mono.empty() && mono.back().F == a[i][j])
				mono.back().S = j;
			else
				mono.emplace_back(pii(a[i][j], j));
		}
	}

	for (int i = 0; i < M; i++)
		for (int j = 0; j < M; j++)
			table[i][j] = pii(-1, -1);

	for (int i = 1; i + 1 < N; i++)
		for (auto [x, y] : ver[i])
			if (table[x][y].S == i - 1)
				table[x][y].S++;
			else
			{
				add_query(x, y, table[x][y]);
				table[x][y] = pii(i, i);
			}

	for (int i = 0; i < M; i++)
		for (int j = 0; j < M; j++)
			add_query(i, j, table[i][j]);

	for (int i = 1; i + 1 < M; i++)
	{
		vector<pii> mono;
		for (int j = 0; j < N; j++)
		{
			while (!mono.empty() && mono.back().F < a[j][i])
			{
				if (j - mono.back().S >= 2)
					hor[i].emplace_back(pii(mono.back().S, j));
				mono.pop_back();
			}
			if (!mono.empty())
				if (j - mono.back().S >= 2)
					hor[i].emplace_back(pii(mono.back().S, j));
			if (!mono.empty() && mono.back().F == a[j][i])
				mono.back().S = j;
			else
				mono.emplace_back(pii(a[j][i], j));
		}
	}

	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			table[i][j] = pii(-1, -1);

	for (int i = 1; i + 1 < M; i++)
		for (auto [x, y] : hor[i])
			if (table[x][y].S == i - 1)
				table[x][y].S++;
			else
			{
				add_modify(table[x][y], x, y);
				table[x][y] = pii(i, i);
			}

	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			add_modify(table[i][j], i, j);

	for (int i = 0; i < M; i++)
	{
		sort(event[i].begin(), event[i].end(), greater<>());
		for (int j = 0; j < N; j++)
			bit[j].p.clear();
		for (auto [_, t, l, r] : event[i])
			bit[l].p.emplace_back(r);
		for (int j = 0; j < N; j++)
			bit[j].init();
		for (auto [_, t, l, r] : event[i])
		{
			if (t == 1)
				bit[l].modify(r, 1);
			else
				for (int j = l; j < r; j++)
					ans += bit[j].query(r);
		}
	}
	assert(cnt <= 6 * N * M);
	return ans;
}

Compilation message (stderr)

rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:106:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  106 |   for (auto [x, y] : ver[i])
      |             ^
rect.cpp:145:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  145 |   for (auto [x, y] : hor[i])
      |             ^
rect.cpp:163:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  163 |   for (auto [_, t, l, r] : event[i])
      |             ^
rect.cpp:167:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  167 |   for (auto [_, t, l, r] : event[i])
      |             ^
#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...