Submission #440213

#TimeUsernameProblemLanguageResultExecution timeMemory
440213rainboyHighway Tolls (IOI18_highway)C++11
51 / 100
380 ms9392 KiB
#include "highway.h"
#include <stdlib.h>

using namespace std;

typedef vector<int> vi;

const int N = 90000, M = 130000;

int ii[M], jj[M];
int *eh[N], eo[N];

void append(int i, int h) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
	eh[i][o] = h;
}

int dd[N], qu[N], idx[N];

void bfs(int n, int s) {
	int i, head, cnt;

	for (i = 0; i < n; i++)
		dd[i] = n;
	head = cnt = 0;
	dd[s] = 0, qu[head + cnt++] = s;
	while (cnt) {
		int d, o;

		i = qu[cnt--, head], idx[i] = head++, d = dd[i] + 1;
		for (o = eo[i]; o--; ) {
			int h = eh[i][o], j = i ^ ii[h] ^ jj[h];

			if (dd[j] > d)
				dd[j] = d, qu[head + cnt++] = j;
		}
	}
}

void find_pair(int n, vi ii_, vi jj_, int a, int b) {
	int m = ii_.size(), h, i, j, lower, upper;
	vi ww(m, 1);
	long long d;

	for (i = 0; i < n; i++)
		eh[i] = (int *) malloc(2 * sizeof *eh[i]);
	for (h = 0; h < m; h++) {
		i = ii[h] = ii_[h], j = jj[h] = jj_[h];
		append(i, h), append(j, h);
	}
	d = ask(ww);
	lower = -1, upper = n;
	while (upper - lower > 1) {
		i = (lower + upper) / 2;
		for (h = 0; h < m; h++)
			ww[h] = ii[h] > i && jj[h] > i;
		if (ask(ww) < d)
			upper = i;
		else
			lower = i;
	}
	i = upper, bfs(n, i);
	lower = -1, upper = n;
	while (upper - lower > 1) {
		int h_ = (lower + upper) / 2;

		for (h = 0; h < m; h++)
			ww[h] = idx[ii[h]] >= h_ || idx[jj[h]] >= h_;
		if (ask(ww) > d / b * a)
			lower = h_;
		else
			upper = h_;
	}
	i = qu[lower], bfs(n, i);
	lower = -1, upper = n;
	while (upper - lower > 1) {
		int h_ = (lower + upper) / 2;

		for (h = 0; h < m; h++)
			ww[h] = idx[ii[h]] >= h_ || idx[jj[h]] >= h_;
		if (ask(ww) > d / b * a)
			lower = h_;
		else
			upper = h_;
	}
	j = qu[lower];
	answer(i, j);
}

Compilation message (stderr)

highway.cpp: In function 'void append(int, int)':
highway.cpp:16:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   16 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
#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...