답안 #961426

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
961426 2024-04-12T05:32:14 Z vjudge1 Collecting Mushrooms (NOI18_collectmushrooms) C++17
100 / 100
35 ms 15744 KB
// #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

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()
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 600 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 528 KB Output is correct
4 Correct 0 ms 400 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
3 Correct 3 ms 344 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 11200 KB Output is correct
2 Correct 16 ms 10364 KB Output is correct
3 Correct 16 ms 10452 KB Output is correct
4 Correct 35 ms 15744 KB Output is correct
5 Correct 26 ms 11832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 600 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 528 KB Output is correct
14 Correct 0 ms 400 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 344 KB Output is correct
17 Correct 2 ms 344 KB Output is correct
18 Correct 3 ms 344 KB Output is correct
19 Correct 2 ms 348 KB Output is correct
20 Correct 2 ms 348 KB Output is correct
21 Correct 21 ms 11200 KB Output is correct
22 Correct 16 ms 10364 KB Output is correct
23 Correct 16 ms 10452 KB Output is correct
24 Correct 35 ms 15744 KB Output is correct
25 Correct 26 ms 11832 KB Output is correct
26 Correct 14 ms 5100 KB Output is correct
27 Correct 12 ms 2712 KB Output is correct
28 Correct 21 ms 6276 KB Output is correct
29 Correct 15 ms 3144 KB Output is correct
30 Correct 13 ms 4116 KB Output is correct