Submission #171999

# Submission time Handle Problem Language Result Execution time Memory
171999 2019-12-30T18:43:41 Z Nightmar UFO (IZhO14_ufo) C++17
25 / 100
2000 ms 188316 KB
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <iomanip>

#define SWS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define ppb pop_back
#define ft first
#define sd second
#define read freopen("input.txt", "r", stdin)
#define write freopen("output.txt", "w", stdout)
#define files read; write

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int Z = (int)1e3 + 228;
const int N = (int)1e5 + 228;
const int INF = (int)1e9 + 228;
const int MOD = (int)1e9 + 7;

vector<vector<int> > a;
vector<vector<ll> > pref;
vector<vector<int> > t_row, t_col;

void build_row(int ind, int v, int tl, int tr)
{
	if (tl == tr)
	{
		t_row[ind][v] = a[ind][tl];
		return; 
	}
	int mid = (tl + tr) / 2;
	build_row(ind, 2 * v, tl, mid);
	build_row(ind, 2 * v + 1, mid + 1, tr);
	t_row[ind][v] = max(t_row[ind][2 * v], t_row[ind][2 * v + 1]);
}

void build_col(int ind, int v, int tl, int tr)
{
	if (tl == tr)
	{
		t_col[ind][v] = a[tl][ind];
		return;
	}
	int mid = (tl + tr) / 2;
	build_col(ind, 2 * v, tl, mid);
	build_col(ind, 2 * v + 1, mid + 1, tr);
	t_col[ind][v] = max(t_col[ind][2 * v], t_col[ind][2 * v + 1]);
}

void update_row(int ind, int v, int tl, int tr, int pos)
{
	if (tl == tr)
	{
		t_row[ind][v]--;
		return;
	}
	int mid = (tl + tr) / 2;
	if (pos <= mid) update_row(ind, 2 * v, tl, mid, pos);
	else update_row(ind, 2 * v + 1, mid + 1, tr, pos);
	t_row[ind][v] = max(t_row[ind][2 * v], t_row[ind][2 * v + 1]);
}

void update_col(int ind, int v, int tl, int tr, int pos)
{
	if (tl == tr)
	{
		t_col[ind][v]--;
		return;
	}
	int mid = (tl + tr) / 2;
	if (pos <= mid) update_col(ind, 2 * v, tl, mid, pos);
	else update_col(ind, 2 * v + 1, mid + 1, tr, pos);
	t_col[ind][v] = max(t_col[ind][2 * v], t_col[ind][2 * v + 1]); 
}

int max_row(int ind, int v, int tl, int tr, int l, int r)
{
	if (tl >= l && tr <= r)
		return t_row[ind][v];
	if (tl > r || tr < l || l > r)
		return -INF;
	int mid = (tl + tr) / 2;
	return max(max_row(ind, 2 * v, tl, mid, l, r), max_row(ind, 2 * v + 1, mid + 1, tr, l, r));
}

int max_col(int ind, int v, int tl, int tr, int l, int r)
{
	if (tl >= l && tr <= r)
		return t_col[ind][v];
	if (tl > r || tr < l || l > r)
		return -INF;
	int mid = (tl + tr) / 2;
	return max(max_col(ind, 2 * v, tl, mid, l, r), max_col(ind, 2 * v + 1, mid + 1, tr, l, r));
}

int get_up(int ind, int v, int tl, int tr, int l, int r, int h)
{
	if (t_col[ind][v] < h) 
		return -1;
	if (tl > r || tr < l)
		return -1;
	if (tl == tr)
		return tl;
	int mid = (tl + tr) / 2;
	int ql = get_up(ind, 2 * v, tl, mid, l, r, h), qr = get_up(ind, 2 * v + 1, mid + 1, tr, l, r, h);
	if (ql != -1) return ql;
	return qr;
}

int get_down(int ind, int v, int tl, int tr, int l, int r, int h)
{
	if (tl > r || tr < l)
		return -1;
	if (t_col[ind][v] < h) 
		return -1;
	if (tl == tr)
		return tl;
	int mid = (tl + tr) / 2;
	int ql = get_down(ind, 2 * v, tl, mid, l, r, h), qr = get_down(ind, 2 * v + 1, mid + 1, tr, l, r, h);
	if (qr != -1) return qr;
	return ql;
}

int get_left(int ind, int v, int tl, int tr, int l, int r, int h)
{
	if (tl > r || tr < l)
		return -1;
	if (t_row[ind][v] < h) 
		return -1;
	if (tl == tr)
		return tl;
	int mid = (tl + tr) / 2;
	int ql = get_left(ind, 2 * v, tl, mid, l, r, h), qr = get_left(ind, 2 * v + 1, mid + 1, tr, l, r, h);
	if (ql != -1) return ql;
	return qr;
}

int get_right(int ind, int v, int tl, int tr, int l, int r, int h)
{
	if (tl > r || tr < l)
		return -1;
	if (t_row[ind][v] < h)
		return -1;
	if (tl == tr)
		return tl;
	int mid = (tl + tr) / 2;
	int ql = get_right(ind, 2 * v, tl, mid, l, r, h), qr = get_right(ind, 2 * v + 1, mid + 1, tr, l, r, h);
	if (qr != -1) return qr;
	return ql;
}

ll get_pref(int x1, int y1, int x2, int y2)
{
	return pref[x2][y2] - pref[x1 - 1][y2] - pref[x2][y1 - 1] + pref[x1 - 1][y1 - 1];
}

int main()
{
	SWS;
	//files;
	int n, m, r, k, p;
	cin >> n >> m >> r >> k >> p;
	a.resize(n + 1, vector<int> (m + 1, 0));
	pref.resize(n + 1, vector<ll> (m + 1, 0));
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			cin >> a[i][j];
	t_row.resize(n + 1, vector<int> (4 * (m + 5), 0));
	t_col.resize(m + 1, vector<int> (4 * (n + 5), 0));
	for (int i = 1; i <= n; i++)
		build_row(i, 1, 1, m);
	for (int i = 1; i <= m; i++)
		build_col(i, 1, 1, n);
	while (k--)
	{
		char c;
		int num, h;
		cin >> c >> num >> h;
		if (c == 'N')
		{
			int l = 1;
			//cout << "POS : ";
			for (int i = 1; i <= r; i++)
			{
				//if (max_col(num, 1, 1, n, l, n) < h) break;
				int pos = get_up(num, 1, 1, n, l, n, h);
				if (pos == -1) break;
				a[pos][num]--;
				update_col(num, 1, 1, n, pos);
				l = pos + 1;
				//cout << pos << ' ';
			}
			//cout << "\n";
		}
		else if (c == 'S')
		{
			int R = n;
			//cout << "POS : ";
			for (int i = 1; i <= r; i++)
			{
				//if (max_col(num, 1, 1, n, 1, r) < h) break;
				int pos = get_down(num, 1, 1, n, 1, R, h);
				if (pos == -1) break;
				a[pos][num]--;
				update_col(num, 1, 1, n, pos);
				R = pos - 1;
				//cout << pos << ' ';
			}
			//cout << "\n";
		}
		else if (c == 'W')
		{
			int l = 1;
			//cout << "POS : ";
			for (int i = 1; i <= r; i++)
			{
				//if (max_row(num, 1, 1, m, l, m) < h) break;
				int pos = get_left(num, 1, 1, m, l, m, h);
				if (pos == -1) break;
				a[num][pos]--;
				update_row(num, 1, 1, m, pos);
				l = pos + 1;
				//cout << pos << ' ';
			}
			//cout << "\n";
		}
		else
		{
			int R = m;
			//cout << "POS : ";
			for (int i = 1; i <= r; i++)
			{
				//if (max_row(num, 1, 1, m, 1, r) < h) break;
				int pos = get_right(num, 1, 1, m, 1, R, h);
				if (pos == -1) break;
				a[num][pos]--;
				update_row(num, 1, 1, m, pos);
				R = pos - 1;
				//cout << pos << ' ';
			}
			//cout << "\n";
		}
	}
	/*for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
			cout << a[i][j] << ' ';
		cout << endl;
	}*/
	ll ans = 0;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			pref[i][j] = pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1] + a[i][j];
	for (int i = p; i <= n; i++)
		for (int j = p; j <= m; j++)
			ans = max(ans, get_pref(i - p + 1, j - p + 1, i, j));
	cout << ans;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 4 ms 504 KB Output is correct
4 Correct 95 ms 760 KB Output is correct
5 Execution timed out 2049 ms 3256 KB Time limit exceeded
6 Execution timed out 2068 ms 21980 KB Time limit exceeded
7 Execution timed out 2067 ms 57920 KB Time limit exceeded
8 Execution timed out 2064 ms 58768 KB Time limit exceeded
9 Execution timed out 2066 ms 50844 KB Time limit exceeded
10 Execution timed out 2066 ms 57988 KB Time limit exceeded
11 Execution timed out 2047 ms 59168 KB Time limit exceeded
12 Execution timed out 2076 ms 57992 KB Time limit exceeded
13 Execution timed out 2066 ms 66936 KB Time limit exceeded
14 Execution timed out 2071 ms 59164 KB Time limit exceeded
15 Execution timed out 2068 ms 58292 KB Time limit exceeded
16 Execution timed out 2068 ms 60212 KB Time limit exceeded
17 Execution timed out 2048 ms 66936 KB Time limit exceeded
18 Correct 1943 ms 65144 KB Output is correct
19 Execution timed out 2061 ms 72756 KB Time limit exceeded
20 Execution timed out 2080 ms 188316 KB Time limit exceeded