Submission #684272

#TimeUsernameProblemLanguageResultExecution timeMemory
684272rainboyCity (JOI17_city)C++17
8 / 100
292 ms83540 KiB
#include "Encoder.h"
#include <stdlib.h>

static const int N = 250000, H = 18;

static int len[N + 1][H + 1], offset[N + 1][H + 1];

static void InitEncoder() {
	for (int n = 1; n <= N; n++)
		for (int h = 0; h <= H; h++)
			len[n][h] = n * (h + 1);
	int x = 0;
	for (int n = 1; n <= N; n++)
		for (int h = 0; h <= H; h++)
			offset[n][h] = x, x += len[N][H] / n;
}

static int *ej[N], eo[N], sz[N], hh[N];

static void append(int i, int j) {
	int o = eo[i]++;
	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j;
}

static void dfs1(int p, int i, int h) {
	sz[i] = 1, hh[i] = h;
	for (int o = eo[i]; o--; ) {
		int j = ej[i][o];
		if (j != p) {
			dfs1(i, j, h);
			sz[i] += sz[j];
		}
	}
}

static void dfs2(int p, int i, int x) {
	Code(i, offset[sz[i]][hh[i]] + x / sz[i]);
	for (int o = eo[i]; o--; ) {
		int j = ej[i][o];
		if (j != p) {
			x = (x + sz[j] - 1) / sz[j] * sz[j];
			dfs2(i, j, x), x += len[sz[j]][hh[j]];
		}
	}
}

void Encode(int n, int *ii, int *jj) {
	InitEncoder();
	for (int i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (int h = 0; h < n - 1; h++)
		append(ii[h], jj[h]), append(jj[h], ii[h]);
	dfs1(-1, 0, H);
	dfs2(-1, 0, 0);
}
#include "Device.h"
 
static const int N = 250000, H = 18;
 
static int len[N + 1][H + 1], offset[N + 1][H + 1];

void InitDevice() {
	for (int n = 1; n <= N; n++)
		for (int h = 0; h <= H; h++)
			len[n][h] = n * (h + 1);
	int x = 0;
	for (int n = 1; n <= N; n++)
		for (int h = 0; h <= H; h++)
			offset[n][h] = x, x += len[N][H] / n;
}

static void Decode(long long x, int *a, int *b) {
	int lower = 1 * (H + 1) + 0, upper = (N + 1) * (H + 1);

	while (upper - lower > 1) {
		int nh = (lower + upper) / 2, n = nh / (H + 1), h = nh % (H + 1);

		if (x < offset[n][h])
			upper = nh;
		else
			lower = nh;
	}
	int nh = lower, n = nh / (H + 1), h = nh % (H + 1);
	*a = (x - offset[n][h]) * n, *b = *a + len[n][h];
}

int Answer(long long x, long long y) {
	int tai, tbi, taj, tbj;
	Decode(x, &tai, &tbi), Decode(y, &taj, &tbj);
	if (taj <= tai && tbi <= tbj)
		return 0;
	if (tai <= taj && tbj <= tbi)
		return 1;
	return 2;
}

Compilation message (stderr)

Encoder.cpp: In function 'void append(int, int)':
Encoder.cpp:22:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   22 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...