답안 #97232

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
97232 2019-02-14T14:26:58 Z E869120 무지개나라 (APIO17_rainbow) C++14
50 / 100
3000 ms 205372 KB
#include "rainbow.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

const long long BASE = 10000000LL;

vector<bool> used[2009]; int H, W, minx = (1 << 30), miny = (1 << 30), maxx = 0, maxy = 0;
vector<int> A[4][2009], A2[2009], A3[2009], A4[2009];
vector<pair<int, int>> G[4];

map<int, int> M[4][200009];

void add(int ty, int px, int py, int r) {
	int X = px, Y = py;
	while (X <= H) {
		Y = py;
		while (Y <= W) {
			M[ty][X][Y] += r;
			Y += (Y&-Y);
		}
		X += (X&-X);
	}
}
int sum(int ty, int px, int py) {
	int s = 0, X = px, Y = py;
	while (X >= 1) {
		Y = py;
		while (Y >= 1) {
			s += M[ty][X][Y];
			Y -= (Y&-Y);
		}
		X -= (X&-X);
	}
	return s;
}

void putvector(int px, int py) {
	G[0].push_back(make_pair(px, py));
	G[1].push_back(make_pair(px, py - 1));
	G[1].push_back(make_pair(px, py));
	G[2].push_back(make_pair(px - 1, py));
	G[2].push_back(make_pair(px, py));
	G[3].push_back(make_pair(px - 1, py - 1));
	G[3].push_back(make_pair(px - 1, py));
	G[3].push_back(make_pair(px, py - 1));
	G[3].push_back(make_pair(px, py));
}

void init(int R, int C, int sr, int sc, int M, char *S) {
	H = R; W = C;
	if (1LL * H*W <= BASE) {
		for (int i = 0; i <= H; i++) used[i].resize(W + 1, true);
		for (int i = 0; i <= H; i++) { for (int j = 0; j < 4; j++) A[j][i].resize(W + 1, 0); }

		int cx = sr, cy = sc; used[cx][cy] = false; minx = cx; miny = cy; maxx = cx; maxy = cy;
		for (int i = 0; i < M; i++) {
			if (S[i] == 'E') cy++;
			if (S[i] == 'W') cy--;
			if (S[i] == 'N') cx--;
			if (S[i] == 'S') cx++;
			used[cx][cy] = false;
			minx = min(minx, cx); maxx = max(maxx, cx);
			miny = min(miny, cy); maxy = max(maxy, cy);
		}
		for (int i = 1; i <= H; i++) {
			for (int j = 1; j <= W; j++) { if (used[i][j] == true) A[0][i][j] = 1; }
		}
		for (int i = 1; i <= H; i++) {
			for (int j = 1; j <= W - 1; j++) { if (used[i][j] == true && used[i][j + 1] == true) A[1][i][j] = 1; }
		}
		for (int i = 1; i <= H - 1; i++) {
			for (int j = 1; j <= W; j++) { if (used[i][j] == true && used[i + 1][j] == true) A[2][i][j] = 1; }
		}
		for (int i = 1; i <= H - 1; i++) {
			for (int j = 1; j <= W - 1; j++) { if (used[i][j] == true && used[i][j + 1] == true && used[i + 1][j] == true && used[i + 1][j + 1] == true) A[3][i][j] = 1; }
		}
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j <= H; j++) {
				for (int k = 1; k <= W; k++) A[i][j][k] += A[i][j][k - 1];
			}
			for (int j = 1; j <= H; j++) {
				for (int k = 0; k <= W; k++) A[i][j][k] += A[i][j - 1][k];
			}
		}
	}
	else {
		int cx = sr, cy = sc; putvector(cx, cy);
		for (int i = 0; i < M; i++) {
			if (S[i] == 'E') cy++;
			if (S[i] == 'W') cy--;
			if (S[i] == 'N') cx--;
			if (S[i] == 'S') cx++;
			putvector(cx, cy);
			minx = min(minx, cx); maxx = max(maxx, cx);
			miny = min(miny, cy); maxy = max(maxy, cy);
		}
		for (int i = 0; i < 4; i++) {
			sort(G[i].begin(), G[i].end());
			G[i].erase(unique(G[i].begin(), G[i].end()), G[i].end());
		}
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < G[i].size(); j++) {
				if (G[i][j].first == 0 || G[i][j].second == 0) continue;
				add(i, G[i][j].first, G[i][j].second, 1);
			}
		}
	}
}

int ranged(int ty, int px, int py, int qx, int qy) {
	if (px > qx || py > qy) return 0;
	return A[ty][px - 1][py - 1] + A[ty][qx][qy] - A[ty][px - 1][qy] - A[ty][qx][py - 1];
}
long long big_ranged_attack(int ty, int px, int py, int qx, int qy) {
	if (px > qx || py > py) return 0;

	int V1 = sum(ty, px - 1, py - 1);
	int V2 = sum(ty, px - 1, qy);
	int V3 = sum(ty, qx, py - 1);
	int V4 = sum(ty, qx, qy);
	return V1 - V2 - V3 + V4;
	/*int rem = 0;
	for (int i = 0; i < G[ty].size(); i++) {
		if (px <= G[ty][i].first && G[ty][i].first <= qx && py <= G[ty][i].second && G[ty][i].second <= qy) rem++;
	}
	return rem;*/
}

int colour(int ar, int ac, int br, int bc) {
	if (1LL * H*W <= BASE) {
		int Z1 = ranged(0, ar, ac, br, bc);
		int Z2 = ranged(1, ar, ac, br, bc - 1);
		int Z3 = ranged(2, ar, ac, br - 1, bc);
		int Z4 = ranged(3, ar, ac, br - 1, bc - 1);
		int BONUS = 0; if (ar < minx && maxx < br && ac < miny && maxy < bc) BONUS = 1;
		return (Z1 - Z2 - Z3 + Z4 + BONUS);
	}
	else {
		long long V1 = br - ar + 1, V2 = bc - ac + 1;
		long long Z1 = V1 * V2 - big_ranged_attack(0, ar, ac, br, bc);
		long long Z2 = V1 * (V2 - 1) - big_ranged_attack(1, ar, ac, br, bc - 1);
		long long Z3 = (V1 - 1) * V2 - big_ranged_attack(2, ar, ac, br - 1, bc);
		long long Z4 = (V1 - 1) * (V2 - 1) - big_ranged_attack(3, ar, ac, br - 1, bc - 1);
		long long BONUS = 0; if (ar < minx && maxx < br && ac < miny && maxy < bc) BONUS = 1;
		return (Z1 - Z2 - Z3 + Z4 + BONUS);
	}
}

Compilation message

rainbow.cpp: In function 'void init(int, int, int, int, int, char*)':
rainbow.cpp:105:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int j = 0; j < G[i].size(); j++) {
                    ~~^~~~~~~~~~~~~
rainbow.cpp: In function 'long long int big_ranged_attack(int, int, int, int, int)':
rainbow.cpp:118:20: warning: self-comparison always evaluates to false [-Wtautological-compare]
  if (px > qx || py > py) return 0;
                 ~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 38364 KB Output is correct
2 Correct 40 ms 38392 KB Output is correct
3 Correct 38 ms 38392 KB Output is correct
4 Correct 37 ms 38392 KB Output is correct
5 Correct 37 ms 38392 KB Output is correct
6 Correct 42 ms 38264 KB Output is correct
7 Correct 28 ms 38264 KB Output is correct
8 Correct 36 ms 38264 KB Output is correct
9 Correct 39 ms 38264 KB Output is correct
10 Correct 38 ms 38264 KB Output is correct
11 Correct 39 ms 38392 KB Output is correct
12 Correct 37 ms 38392 KB Output is correct
13 Correct 37 ms 38292 KB Output is correct
14 Correct 45 ms 38372 KB Output is correct
15 Correct 40 ms 38392 KB Output is correct
16 Correct 38 ms 38272 KB Output is correct
17 Correct 37 ms 38272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 38272 KB Output is correct
2 Correct 37 ms 38272 KB Output is correct
3 Correct 188 ms 48504 KB Output is correct
4 Correct 172 ms 48592 KB Output is correct
5 Correct 148 ms 48636 KB Output is correct
6 Correct 185 ms 48568 KB Output is correct
7 Correct 179 ms 48760 KB Output is correct
8 Correct 162 ms 48696 KB Output is correct
9 Correct 192 ms 48632 KB Output is correct
10 Correct 152 ms 48632 KB Output is correct
11 Correct 130 ms 48632 KB Output is correct
12 Correct 116 ms 48672 KB Output is correct
13 Correct 117 ms 48708 KB Output is correct
14 Correct 117 ms 48704 KB Output is correct
15 Correct 106 ms 48632 KB Output is correct
16 Correct 164 ms 48660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 38392 KB Output is correct
2 Execution timed out 3037 ms 205372 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 38364 KB Output is correct
2 Correct 40 ms 38392 KB Output is correct
3 Correct 38 ms 38392 KB Output is correct
4 Correct 37 ms 38392 KB Output is correct
5 Correct 37 ms 38392 KB Output is correct
6 Correct 42 ms 38264 KB Output is correct
7 Correct 28 ms 38264 KB Output is correct
8 Correct 36 ms 38264 KB Output is correct
9 Correct 39 ms 38264 KB Output is correct
10 Correct 38 ms 38264 KB Output is correct
11 Correct 39 ms 38392 KB Output is correct
12 Correct 37 ms 38392 KB Output is correct
13 Correct 37 ms 38292 KB Output is correct
14 Correct 45 ms 38372 KB Output is correct
15 Correct 40 ms 38392 KB Output is correct
16 Correct 38 ms 38272 KB Output is correct
17 Correct 37 ms 38272 KB Output is correct
18 Correct 191 ms 55040 KB Output is correct
19 Correct 142 ms 40568 KB Output is correct
20 Correct 97 ms 39332 KB Output is correct
21 Correct 111 ms 39644 KB Output is correct
22 Correct 133 ms 40544 KB Output is correct
23 Correct 127 ms 40588 KB Output is correct
24 Correct 87 ms 39160 KB Output is correct
25 Correct 92 ms 39676 KB Output is correct
26 Correct 107 ms 40568 KB Output is correct
27 Correct 198 ms 55048 KB Output is correct
28 Correct 176 ms 55032 KB Output is correct
29 Correct 169 ms 55152 KB Output is correct
30 Correct 193 ms 55032 KB Output is correct
31 Correct 40 ms 38264 KB Output is correct
32 Correct 181 ms 54940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 38364 KB Output is correct
2 Correct 40 ms 38392 KB Output is correct
3 Correct 38 ms 38392 KB Output is correct
4 Correct 37 ms 38392 KB Output is correct
5 Correct 37 ms 38392 KB Output is correct
6 Correct 42 ms 38264 KB Output is correct
7 Correct 28 ms 38264 KB Output is correct
8 Correct 36 ms 38264 KB Output is correct
9 Correct 39 ms 38264 KB Output is correct
10 Correct 38 ms 38264 KB Output is correct
11 Correct 39 ms 38392 KB Output is correct
12 Correct 37 ms 38392 KB Output is correct
13 Correct 37 ms 38292 KB Output is correct
14 Correct 45 ms 38372 KB Output is correct
15 Correct 40 ms 38392 KB Output is correct
16 Correct 38 ms 38272 KB Output is correct
17 Correct 37 ms 38272 KB Output is correct
18 Correct 191 ms 55040 KB Output is correct
19 Correct 142 ms 40568 KB Output is correct
20 Correct 97 ms 39332 KB Output is correct
21 Correct 111 ms 39644 KB Output is correct
22 Correct 133 ms 40544 KB Output is correct
23 Correct 127 ms 40588 KB Output is correct
24 Correct 87 ms 39160 KB Output is correct
25 Correct 92 ms 39676 KB Output is correct
26 Correct 107 ms 40568 KB Output is correct
27 Correct 198 ms 55048 KB Output is correct
28 Correct 176 ms 55032 KB Output is correct
29 Correct 169 ms 55152 KB Output is correct
30 Correct 193 ms 55032 KB Output is correct
31 Correct 40 ms 38264 KB Output is correct
32 Correct 181 ms 54940 KB Output is correct
33 Execution timed out 3037 ms 205372 KB Time limit exceeded
34 Halted 0 ms 0 KB -