Submission #961426

#TimeUsernameProblemLanguageResultExecution timeMemory
961426vjudge1Collecting Mushrooms (NOI18_collectmushrooms)C++17
100 / 100
35 ms15744 KiB
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #include <x86intrin.h>

#include <bits/stdc++.h>
#include <chrono>
#include <random>

// @author: Vlapos

namespace operators
{
	template <typename T1, typename T2>
	std::istream &operator>>(std::istream &in, std::pair<T1, T2> &x)
	{
		in >> x.first >> x.second;
		return in;
	}

	template <typename T1, typename T2>
	std::ostream &operator<<(std::ostream &out, std::pair<T1, T2> x)
	{
		out << x.first << " " << x.second;
		return out;
	}

	template <typename T1>
	std::istream &operator>>(std::istream &in, std::vector<T1> &x)
	{
		for (auto &i : x)
			in >> i;
		return in;
	}

	template <typename T1>
	std::ostream &operator<<(std::ostream &out, std::vector<T1> &x)
	{
		for (auto &i : x)
			out << i << " ";
		return out;
	}

	template <typename T1>
	std::ostream &operator<<(std::ostream &out, std::set<T1> &x)
	{
		for (auto &i : x)
			out << i << " ";
		return out;
	}
}

// name spaces
using namespace std;
using namespace operators;
// end of name spaces

// defines
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define f first
#define s second
#define uint unsigned int
#define all(vc) vc.begin(), vc.end()
// end of defines

// usefull stuff

void boost()
{
	ios_base ::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}

inline int getbit(int &x, int &bt) { return (x >> bt) & 1; }

const int dx4[4] = {-1, 0, 0, 1};
const int dy4[4] = {0, -1, 1, 0};
const int dx8[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy8[8] = {-1, -0, 1, -1, 1, -1, 0, 1};

const ll INF = 2e18;
const int BIG = (1e9) * 2 + 100;
const int MAXN = (1e5) + 5;
const int MOD7 = (1e9) + 7;
const int MOD9 = (1e9) + 9;
const uint MODFFT = 998244353;

// #define int ll`

struct segTreeSum
{
	vector<ll> tree;
	int sz;

	void init(int n)
	{
		sz = n;
		tree.resize(2 * sz);
	}

	void build(vector<int> &a)
	{
		init(a.size());
		for (int i = 0; i < a.size(); ++i)
			tree[i + sz] = a[sz];

		for (int i = sz - 1; i > 0; --i)
			tree[i] = tree[i << 1] + tree[(i << 1) + 1];
	}

	void add(int l, int r, int val) // [l, r)
	{
		l += sz;
		r += sz;

		while (l < r)
		{
			if (l & 1)
				tree[l++] += val;
			if (r & 1)
				tree[--r] += val;
			l >>= 1;
			r >>= 1;
		}
	}

	int get(int pos)
	{
		pos += sz;
		int res = 0;
		while (pos)
		{
			res += tree[pos];
			pos >>= 1;
		}
		return res;
	}
};

struct test
{
	void solve(int testcase)
	{
		boost();
		int n, m, d, k;
		cin >> n >> m >> d >> k;
		vector<vector<pair<pii, int>>> rls(n + 1);
		vector<vector<int>> qrs(n);
		for (int i = 0; i < n; ++i)
		{
			for (int j = 0; j < m; ++j)
			{
				char x;
				cin >> x;
				if (x == 'M')
					qrs[i].pb(j);
				else if (x == 'S')
				{
					rls[max(i - d, 0)].pb({{max(j - d, 0), min(j + d + 1, m)}, 1});
					rls[min(i + d + 1, n)].pb({{max(j - d, 0), min(j + d + 1, m)}, -1});
				}
			}
		}
		segTreeSum tree;
		tree.init(m);
		int cnt = 0;
		for (int i = 0; i < n; ++i)
		{
			for (auto [seg, v] : rls[i])
				tree.add(seg.f, seg.s, v);
			for (auto pos : qrs[i])
				if (tree.get(pos) >= k)
					cnt++;
		}
		cout << cnt << "\n";
	}
};

main()
{
	boost();
	int q = 1;
	// cin >> q;
	for (int i = 0; i < q; i++)
	{
		test t;
		t.solve(i);
	}
	return 0;
}
//[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]//
//                                                                                    //
//                               Coded by Der_Vlἀpos                                  //
//                                                                                    //
//[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]//

Compilation message (stderr)

mushrooms.cpp: In member function 'void segTreeSum::build(std::vector<int>&)':
mushrooms.cpp:109:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |   for (int i = 0; i < a.size(); ++i)
      |                   ~~^~~~~~~~~~
mushrooms.cpp: At global scope:
mushrooms.cpp:184:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  184 | main()
      | ^~~~
#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...