Submission #128588

# Submission time Handle Problem Language Result Execution time Memory
128588 2019-07-11T07:13:33 Z 조승현(#3164) None (JOI16_ho_t4) C++14
100 / 100
266 ms 25772 KB
#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
#define pii pair<int,int>
#define pll pair<long long, long long>
int n, K;
char p[101000];
map<pii, int>Map;
struct point {
	int x, y;
};
int cnt = 0;
vector<int>G[101000];
map<pll, int>vis;
vector<point> TP;
point U[101000];
long long XX, YY, Mod;
pll Make(long long a, long long b) {
	return { -YY * a + XX * b, XX*a + YY * b };
}
pll Make2(long long a, long long b) {
	return { -YY * a + XX * b, (XX*a + YY * b) % Mod };
}
struct Range {
	int b, e;
};
vector<Range>GG[101000];
long long Ans;
void Go(vector<Range> *P) {
	int i, pv[4] = { 0,0,0,0 }, sz[4];
	for (i = 0; i < 4; i++)sz[i] = P[i].size();
	while (1) {
		int r1 = 2e9, r2 = -1e9;
		for (i = 0; i < 4; i++) {
			r1 = min(r1, P[i][pv[i]].e);
			r2 = max(r2, P[i][pv[i]].b);
		}
		if (r1 >= r2)Ans += r1 - r2 + 1;
		int Mn = 2e9, x = -1;
		for (i = 0; i < 4; i++) {
			if (pv[i] < sz[i] - 1 && P[i][pv[i]].e < Mn) {
				Mn = P[i][pv[i]].e;
				x = i;
			}
		}
		if (x == -1)break;
		pv[x]++;
	}
}
void Calc(vector<int> &P, vector<Range> &Q) {
	int i;
	int b = -1, e = -1;
	for (auto &t : P) {
		if (e < t) {
			if (e != -1)Q.push_back({ b,e });
			b = t;
			e = t + K - 1;
		}
		e = max(e, t + K - 1);
	}
	Q.push_back({ b,e });
}
int main() {
	int i, j, x = 0, y = 0;
	scanf("%d%d", &n, &K);
	scanf("%s", p);
	Map[{0, 0}] = 1;
	for (i = 0; i < n; i++) {
		if (p[i] == 'E')x++;
		if (p[i] == 'W')x--;
		if (p[i] == 'N')y++;
		if (p[i] == 'S')y--;
		Map[{x, y}] = 1;
	}
	if (x == 0 && y == 0) {
		int res = 0;
		for (auto &t : Map) {
			int x = t.first.first, y = t.first.second;
			if (Map[{x, y}] == 1 && Map[{x + 1, y}] == 1 && Map[{x, y + 1}] == 1 && Map[{x + 1, y + 1}] == 1)res++;
		}
		printf("%d\n", res);
		return 0;
	}
	for (auto &t : Map)TP.push_back({ t.first.first, t.first.second });
	if (x < 0) {
		for (auto &t : TP)t.x = -t.x;
		x = -x;
	}
	if (y < 0) {
		for (auto &t : TP)t.y = -t.y;
		y = -y;
	}
	for (auto &t : TP)t.x += n + 1, t.y += n + 1;
	Mod = 1ll * x*x + 1ll * y*y;
	XX = x, YY = y;
	for (auto &t : TP) {
		pll pp = Make(t.x, t.y);
		pll pp2 = { pp.first, pp.second%Mod };
		if (!vis.count(pp2)) {
			vis[pp2] = ++cnt;
			U[cnt] = t;
		}
		G[vis[pp2]].push_back(pp.second / Mod);
	}
	for (i = 1; i <= cnt; i++) {
		sort(G[i].begin(), G[i].end());
		Calc(G[i], GG[i]);
	}
	for (i = 1; i <= cnt; i++) {
		point t = U[i];
		pll pp1 = Make2(t.x, t.y);
		pll pp2 = Make2(t.x + 1, t.y);
		pll pp3 = Make2(t.x, t.y + 1);
		pll pp4 = Make2(t.x + 1, t.y + 1);
		if (!vis.count(pp1) || !vis.count(pp2) || !vis.count(pp3) || !vis.count(pp4))continue;
		long long a1 = Make(t.x, t.y).second / Mod;
		long long a2 = Make(t.x + 1, t.y).second / Mod;
		long long a3 = Make(t.x, t.y + 1).second / Mod;
		long long a4 = Make(t.x + 1, t.y + 1).second / Mod;
		vector<Range>P[4];
		P[0] = GG[vis[pp1]];
		P[1] = GG[vis[pp2]];
		P[2] = GG[vis[pp3]];
		P[3] = GG[vis[pp4]];
		for (auto &t : P[1])t.b -= a2 - a1, t.e -= a2 - a1;
		for (auto &t : P[2])t.b -= a3 - a1, t.e -= a3 - a1;
		for (auto &t : P[3])t.b -= a4 - a1, t.e -= a4 - a1;
		Go(P);
	}
	printf("%lld\n", Ans);
}

Compilation message

2016_ho_t4.cpp: In function 'void Calc(std::vector<int>&, std::vector<Range>&)':
2016_ho_t4.cpp:53:6: warning: unused variable 'i' [-Wunused-variable]
  int i;
      ^
2016_ho_t4.cpp: In function 'int main()':
2016_ho_t4.cpp:66:9: warning: unused variable 'j' [-Wunused-variable]
  int i, j, x = 0, y = 0;
         ^
2016_ho_t4.cpp:67:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &K);
  ~~~~~^~~~~~~~~~~~~~~~
2016_ho_t4.cpp:68:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", p);
  ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 6 ms 5112 KB Output is correct
5 Correct 6 ms 4984 KB Output is correct
6 Correct 6 ms 4980 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 4984 KB Output is correct
13 Correct 6 ms 5112 KB Output is correct
14 Correct 6 ms 5112 KB Output is correct
15 Correct 6 ms 5112 KB Output is correct
16 Correct 6 ms 5112 KB Output is correct
17 Correct 6 ms 5112 KB Output is correct
18 Correct 6 ms 5112 KB Output is correct
19 Correct 6 ms 4984 KB Output is correct
20 Correct 6 ms 4984 KB Output is correct
21 Correct 6 ms 5112 KB Output is correct
22 Correct 6 ms 5112 KB Output is correct
23 Correct 6 ms 4984 KB Output is correct
24 Correct 6 ms 5112 KB Output is correct
25 Correct 6 ms 4988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 6 ms 5112 KB Output is correct
5 Correct 6 ms 4984 KB Output is correct
6 Correct 6 ms 4980 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 4984 KB Output is correct
13 Correct 6 ms 5112 KB Output is correct
14 Correct 6 ms 5112 KB Output is correct
15 Correct 6 ms 5112 KB Output is correct
16 Correct 6 ms 5112 KB Output is correct
17 Correct 6 ms 5112 KB Output is correct
18 Correct 6 ms 5112 KB Output is correct
19 Correct 6 ms 4984 KB Output is correct
20 Correct 6 ms 4984 KB Output is correct
21 Correct 6 ms 5112 KB Output is correct
22 Correct 6 ms 5112 KB Output is correct
23 Correct 6 ms 4984 KB Output is correct
24 Correct 6 ms 5112 KB Output is correct
25 Correct 6 ms 4988 KB Output is correct
26 Correct 6 ms 5112 KB Output is correct
27 Correct 8 ms 5240 KB Output is correct
28 Correct 9 ms 5368 KB Output is correct
29 Correct 37 ms 7980 KB Output is correct
30 Correct 36 ms 7800 KB Output is correct
31 Correct 150 ms 22092 KB Output is correct
32 Correct 25 ms 6652 KB Output is correct
33 Correct 61 ms 10232 KB Output is correct
34 Correct 63 ms 10360 KB Output is correct
35 Correct 61 ms 9868 KB Output is correct
36 Correct 40 ms 8436 KB Output is correct
37 Correct 27 ms 7032 KB Output is correct
38 Correct 59 ms 9584 KB Output is correct
39 Correct 50 ms 9844 KB Output is correct
40 Correct 25 ms 6648 KB Output is correct
41 Correct 33 ms 7412 KB Output is correct
42 Correct 41 ms 8056 KB Output is correct
43 Correct 61 ms 9844 KB Output is correct
44 Correct 59 ms 9848 KB Output is correct
45 Correct 47 ms 8952 KB Output is correct
46 Correct 83 ms 12148 KB Output is correct
47 Correct 126 ms 15348 KB Output is correct
48 Correct 55 ms 15084 KB Output is correct
49 Correct 178 ms 25576 KB Output is correct
50 Correct 169 ms 25772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 6 ms 5112 KB Output is correct
5 Correct 6 ms 4984 KB Output is correct
6 Correct 6 ms 4980 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 4984 KB Output is correct
13 Correct 6 ms 5112 KB Output is correct
14 Correct 6 ms 5112 KB Output is correct
15 Correct 6 ms 5112 KB Output is correct
16 Correct 6 ms 5112 KB Output is correct
17 Correct 6 ms 5112 KB Output is correct
18 Correct 6 ms 5112 KB Output is correct
19 Correct 6 ms 4984 KB Output is correct
20 Correct 6 ms 4984 KB Output is correct
21 Correct 6 ms 5112 KB Output is correct
22 Correct 6 ms 5112 KB Output is correct
23 Correct 6 ms 4984 KB Output is correct
24 Correct 6 ms 5112 KB Output is correct
25 Correct 6 ms 4988 KB Output is correct
26 Correct 6 ms 5112 KB Output is correct
27 Correct 6 ms 5112 KB Output is correct
28 Correct 6 ms 5112 KB Output is correct
29 Correct 6 ms 5112 KB Output is correct
30 Correct 6 ms 5048 KB Output is correct
31 Correct 6 ms 5112 KB Output is correct
32 Correct 6 ms 5112 KB Output is correct
33 Correct 7 ms 5112 KB Output is correct
34 Correct 6 ms 5112 KB Output is correct
35 Correct 6 ms 5112 KB Output is correct
36 Correct 6 ms 4984 KB Output is correct
37 Correct 6 ms 5112 KB Output is correct
38 Correct 6 ms 4984 KB Output is correct
39 Correct 6 ms 4984 KB Output is correct
40 Correct 6 ms 5112 KB Output is correct
41 Correct 6 ms 4984 KB Output is correct
42 Correct 6 ms 5112 KB Output is correct
43 Correct 6 ms 5136 KB Output is correct
44 Correct 6 ms 5112 KB Output is correct
45 Correct 6 ms 5112 KB Output is correct
46 Correct 6 ms 5112 KB Output is correct
47 Correct 6 ms 5112 KB Output is correct
48 Correct 6 ms 5112 KB Output is correct
49 Correct 6 ms 5112 KB Output is correct
50 Correct 6 ms 4984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 6 ms 5112 KB Output is correct
5 Correct 6 ms 4984 KB Output is correct
6 Correct 6 ms 4980 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 4984 KB Output is correct
13 Correct 6 ms 5112 KB Output is correct
14 Correct 6 ms 5112 KB Output is correct
15 Correct 6 ms 5112 KB Output is correct
16 Correct 6 ms 5112 KB Output is correct
17 Correct 6 ms 5112 KB Output is correct
18 Correct 6 ms 5112 KB Output is correct
19 Correct 6 ms 4984 KB Output is correct
20 Correct 6 ms 4984 KB Output is correct
21 Correct 6 ms 5112 KB Output is correct
22 Correct 6 ms 5112 KB Output is correct
23 Correct 6 ms 4984 KB Output is correct
24 Correct 6 ms 5112 KB Output is correct
25 Correct 6 ms 4988 KB Output is correct
26 Correct 6 ms 5112 KB Output is correct
27 Correct 8 ms 5240 KB Output is correct
28 Correct 9 ms 5368 KB Output is correct
29 Correct 37 ms 7980 KB Output is correct
30 Correct 36 ms 7800 KB Output is correct
31 Correct 150 ms 22092 KB Output is correct
32 Correct 25 ms 6652 KB Output is correct
33 Correct 61 ms 10232 KB Output is correct
34 Correct 63 ms 10360 KB Output is correct
35 Correct 61 ms 9868 KB Output is correct
36 Correct 40 ms 8436 KB Output is correct
37 Correct 27 ms 7032 KB Output is correct
38 Correct 59 ms 9584 KB Output is correct
39 Correct 50 ms 9844 KB Output is correct
40 Correct 25 ms 6648 KB Output is correct
41 Correct 33 ms 7412 KB Output is correct
42 Correct 41 ms 8056 KB Output is correct
43 Correct 61 ms 9844 KB Output is correct
44 Correct 59 ms 9848 KB Output is correct
45 Correct 47 ms 8952 KB Output is correct
46 Correct 83 ms 12148 KB Output is correct
47 Correct 126 ms 15348 KB Output is correct
48 Correct 55 ms 15084 KB Output is correct
49 Correct 178 ms 25576 KB Output is correct
50 Correct 169 ms 25772 KB Output is correct
51 Correct 6 ms 5112 KB Output is correct
52 Correct 6 ms 5112 KB Output is correct
53 Correct 6 ms 5112 KB Output is correct
54 Correct 6 ms 5112 KB Output is correct
55 Correct 6 ms 5048 KB Output is correct
56 Correct 6 ms 5112 KB Output is correct
57 Correct 6 ms 5112 KB Output is correct
58 Correct 7 ms 5112 KB Output is correct
59 Correct 6 ms 5112 KB Output is correct
60 Correct 6 ms 5112 KB Output is correct
61 Correct 6 ms 4984 KB Output is correct
62 Correct 6 ms 5112 KB Output is correct
63 Correct 6 ms 4984 KB Output is correct
64 Correct 6 ms 4984 KB Output is correct
65 Correct 6 ms 5112 KB Output is correct
66 Correct 6 ms 4984 KB Output is correct
67 Correct 6 ms 5112 KB Output is correct
68 Correct 6 ms 5136 KB Output is correct
69 Correct 6 ms 5112 KB Output is correct
70 Correct 6 ms 5112 KB Output is correct
71 Correct 6 ms 5112 KB Output is correct
72 Correct 6 ms 5112 KB Output is correct
73 Correct 6 ms 5112 KB Output is correct
74 Correct 6 ms 5112 KB Output is correct
75 Correct 6 ms 4984 KB Output is correct
76 Correct 8 ms 5368 KB Output is correct
77 Correct 8 ms 5240 KB Output is correct
78 Correct 26 ms 7932 KB Output is correct
79 Correct 74 ms 14928 KB Output is correct
80 Correct 39 ms 8056 KB Output is correct
81 Correct 29 ms 7160 KB Output is correct
82 Correct 57 ms 9460 KB Output is correct
83 Correct 30 ms 7160 KB Output is correct
84 Correct 60 ms 9844 KB Output is correct
85 Correct 71 ms 9844 KB Output is correct
86 Correct 107 ms 15852 KB Output is correct
87 Correct 67 ms 10872 KB Output is correct
88 Correct 53 ms 9336 KB Output is correct
89 Correct 60 ms 9464 KB Output is correct
90 Correct 28 ms 7160 KB Output is correct
91 Correct 72 ms 10232 KB Output is correct
92 Correct 63 ms 10368 KB Output is correct
93 Correct 59 ms 10260 KB Output is correct
94 Correct 40 ms 8692 KB Output is correct
95 Correct 45 ms 8696 KB Output is correct
96 Correct 84 ms 12168 KB Output is correct
97 Correct 126 ms 15584 KB Output is correct
98 Correct 47 ms 13032 KB Output is correct
99 Correct 178 ms 25580 KB Output is correct
100 Correct 266 ms 25592 KB Output is correct