답안 #435662

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
435662 2021-06-23T14:24:45 Z rainboy 늑대인간 (IOI18_werewolf) C++11
0 / 100
564 ms 57580 KB
#include "werewolf.h"
#include <stdlib.h>
#include <string.h>

using namespace std;

typedef vector<int> vi;

const int N = 200000, Q = 200000;

int t;

int *ej[N], eo[N], *fj[N], fo[N];

void append(int **ej, int *eo, 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;
}

int ds[N], cc[N];

int find(int i) {
	return ds[i] < 0 ? i : find(ds[i]);
}

void join(int i, int j, int c) {
	i = find(i);
	j = find(j);
	if (i == j)
		return;
	if (ds[i] > ds[j]) {
		ds[i] = j, cc[i] = c;
		append(fj, fo, j, i);
	} else {
		if (ds[i] == ds[j])
			ds[i]--;
		ds[j] = i, cc[j] = c;
		append(fj, fo, i, j);
	}
}

int ta[2][N], tb[2][N], time;

void dfs(int i) {
	int o;

	ta[t][i] = time++;
	for (o = 0; o < fo[i]; o++) {
		int j = fj[i][o];

		dfs(j);
	}
	tb[t][i] = time;
}

int ll_[2][Q], rr_[2][Q];

void get_segment(int i, int c, int *l, int *r) {
	int lower, upper, o;

	while (ds[i] >= 0 && cc[i] >= c)
		i = ds[i];
	lower = -1, upper = fo[i];
	while (upper - lower > 1) {
		int o = (lower + upper) / 2;

		if (cc[fj[i][o]] >= c)
			lower = o;
		else
			upper = o;
	}
	*l = ta[t][i], *r = lower == -1 ? ta[t][i] + 1 : tb[t][fj[i][lower]];
}

int ft[N];

void update(int i, int n) {
	while (i < n) {
		ft[i]++;
		i |= i + 1;
	}
}

int query(int i) {
	int x = 0;

	while (i >= 0) {
		x += ft[i];
		i &= i + 1, i--;
	}
	return x;
}

int yy[N], kk[Q];

void solve(int n) {
	int i, o;

	for (i = 0; i < n; i++) {
		update(yy[i], n);
		for (o = eo[i]; o--; ) {
			int h_ = ej[i][o], h = h_ >> 1;

			kk[h] += (query(rr_[1][h] - 1) - query(ll_[1][h] - 1)) * ((h_ & 1) == 0 ? -1 : 1);
		}
	}
}

vi check_validity(int n, vi xx, vi yy, vi ss, vi ee, vi ll, vi rr) {
	int m = xx.size(), q = ss.size(), h, h_, i, j, o, tmp;
	vi ok(q);

	for (t = 0; t < 2; t++) {
		for (i = 0; i < n; i++) {
			ej[i] = (int *) malloc(2 * sizeof *ej[i]), eo[i] = 0;
			fj[i] = (int *) malloc(2 * sizeof *fj[i]), fo[i] = 0;
		}
		for (h = 0; h < m; h++)
			append(ej, eo, xx[h], yy[h]), append(ej, eo, yy[h], xx[h]);
		memset(ds, -1, n * sizeof *ds);
		for (i = n - 1; i >= 0; i--)
			for (o = eo[i]; o--; ) {
				j = ej[i][o];
				if (i < j)
					join(i, j, i);
			}
		time = 0;
		for (i = 0; i < n; i++)
			if (ds[i] < 0)
				dfs(i);
		for (h = 0; h < m; h++)
			xx[h] = n - 1 - xx[h], yy[h] = n - 1 - yy[h];
		for (h = 0; h < q; h++)
			get_segment(ss[h], ll[h], &ll_[t][h], &rr_[t][h]);
		for (h = 0; h < q; h++) {
			tmp = ss[h], ss[h] = n - 1 - ee[h], ee[h] = n - 1 - tmp;
			tmp = ll[h], ll[h] = n - 1 - rr[h], rr[h] = n - 1 - tmp;
		}
	}
	for (i = 0, j = n - 1; i < j; i++, j--) {
		tmp = ta[1][i], ta[1][i] = ta[1][j], ta[1][j] = tmp;
		tmp = tb[1][i], tb[1][i] = tb[1][j], tb[1][j] = tmp;
	}
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]), eo[i] = 0;
	for (i = 0; i < n; i++)
		yy[ta[0][i]] = ta[1][i];
	for (h = 0; h < q; h++) {
		if (ll_[0][h] > 0)
			append(ej, eo, ll_[0][h] - 1, h << 1 | 0);
		append(ej, eo, rr_[0][h] - 1, h << 1 | 1);
	}
	solve(n);
	for (h = 0; h < q; h++)
		ok[h] = kk[h] > 0;
	return ok;
}

Compilation message

werewolf.cpp: In function 'void append(int**, int*, int, int)':
werewolf.cpp:18:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   18 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
werewolf.cpp: In function 'void get_segment(int, int, int*, int*)':
werewolf.cpp:62:20: warning: unused variable 'o' [-Wunused-variable]
   62 |  int lower, upper, o;
      |                    ^
werewolf.cpp: In function 'vi check_validity(int, vi, vi, vi, vi, vi, vi)':
werewolf.cpp:113:39: warning: unused variable 'h_' [-Wunused-variable]
  113 |  int m = xx.size(), q = ss.size(), h, h_, i, j, o, tmp;
      |                                       ^~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 564 ms 57580 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -