Submission #1222763

#TimeUsernameProblemLanguageResultExecution timeMemory
1222763sleepntsheepPrize (CEOI22_prize)C++17
0 / 100
1303 ms190060 KiB
#include <cstdio>
#include <cassert>
#include <algorithm>

using namespace std;


#define Q 2000000
#define N 2000000

int n, k, q, t, dfn[N], dfc, nfd[N], fa[N], hd[N], e[N * 2]
, Ln[N * 2], ii, rt, hld[N], lv[N], dis[N], sz[N]
, hd2[N], e2[N * 2], Ln2[N * 2], ii2, dfn2[N], dfc2, fa2[N]
, hld2[N], lv2[N], sz2[N], V[N + N], tp, dis2[N], sta[N]
, e3[N * 2], Ln3[N * 2], ii3, hd3[N], dis3[N], cst[N * 2], lv3[N]
, dfn3[N], dfc3, sz3[N], hld3[N], fa3[N]
, q1[Q], q2[Q]
;

int cmp(int i, int j) { return dfn2[i] < dfn2[j]; }

void add(int u, int v) { ++ii; e[ii] = v, Ln[ii] = hd[u]; hd[u] = ii; }
void add2(int u, int v) {++ii2; e2[ii2] = v, Ln2[ii2] = hd2[u]; hd2[u] = ii2;}
void add3(int u, int v, int w) {
	++ii3; cst[ii3] = w; e3[ii3] = v, Ln3[ii3] = hd3[u]; hd3[u] = ii3; }

void dfs(int u) {
	sz[u] = 1;
	for (int v, j = hd[u]; j; j = Ln[j]) {
		v = e[j];
		if (v == fa[u]) continue;
		fa[v] = u, lv[v] = lv[u] + 1, dfs(v);
		sz[u] += sz[v];
		if (e[hd[u]]==fa[u]||sz[v] > sz[e[hd[u]]]) e[j] = e[hd[u]], e[hd[u]] = v;
	}
}

void dfs2(int u) {
	nfd[dfn[u] = dfc++] = u;
	for (int v, j = hd[u]; j; j = Ln[j]) {
		v = e[j];
		if(v == fa[u]) continue;
		hld[v] = j == hd[u] ? hld[u]: v;
		dfs2(v);
	}
}

void efs(int u) {
	sz2[u] = 1;
	for (int v, j = hd2[u]; j; j = Ln2[j]) {
		v = e2[j];
		if (v == fa2[u]) continue;
		lv2[v] = lv2[u] + 1;
		fa2[v] = u;
		efs(v);
		sz2[u] += sz2[v];
		if (e2[hd2[u]]==fa2[u] || sz2[v] > sz2[e2[hd2[u]]]) e2[j] = e2[hd2[u]], e2[hd2[u]] = v;
	}
}

void efs2(int u) {
	dfn2[u] = dfc2++;
	for (int v, j = hd2[u]; j; j = Ln2[j]) {
		v = e2[j];
		if (v == fa2[u]) continue;
		hld2[v] = j == hd2[u] ? hld2[u]: v;
		efs2(v);
	}
}

void gfs(int u) {
	sz3[u] = 1;
	for (int v, j = hd3[u]; j; j = Ln3[j]) {
		v = e3[j];
		lv3[v] = lv3[u] + cst[j], fa3[v] = u, gfs(v);
		sz3[u] += sz3[v];
		if (sz3[v] > sz3[e3[hd3[u]]]) e3[j] = e3[hd3[u]], e3[hd3[u]] = v;
	}
}

void gfs2(int u) {
	dfn3[u] = dfc3++;
	for (int v, j = hd3[u]; j; j = Ln3[j]) {
		v = e3[j];
		hld3[v] = j == hd3[u] ? hld3[u]: v;
		gfs2(v);
	}
}

int lca(int u, int v) {
	while (hld[u] != hld[v])
		if (lv[hld[u]] > lv[hld[v]]) u = fa[hld[u]];
		else v = fa[hld[v]];
	return dfn[u] < dfn[v] ? u: v;
}

int lca2(int u, int v) {
	while (hld2[u] != hld2[v])
		if (lv2[hld2[u]] > lv2[hld2[v]]) u = fa2[hld2[u]];
		else v = fa2[hld2[v]];
	return dfn2[u] < dfn2[v] ? u: v;
}

int lca3(int u, int v) {
	while (hld3[u] != hld3[v])
		if (lv3[hld3[u]] > lv3[hld3[v]]) u = fa3[hld3[u]];
		else v = fa3[hld3[v]];
	return dfn3[u] < dfn3[v] ? u: v;
}

int main() {
	scanf("%d%d%d%d", &n, &k, &q, &t);

	for (int j, i = 0; i < n; ++i) {
		scanf("%d", &j);
		if (j > -1)
			add(--j, i), add(i, j);
	}

	for (int i = 0, j; i < n; ++i) {
		scanf("%d", &j);
		if (j > -1)
			add2(--j, i), add2(i, j);
		else
			rt = i;
	}

	fa[rt] = rt;	hld[rt] = rt;
	fa2[rt] = rt; hld2[rt] = rt;
	fa3[rt] = rt; hld3[rt] = rt;
	dfs(rt);
	dfs2(rt);
	efs(rt);
	efs2(rt);

	for (int i = 0; i < k; ++i) V[tp++] = nfd[i];
	sort(V, V + tp, cmp);

	for (int i = 0; i < k; ++i) printf("%d ", 1 + nfd[i]);
	puts(""), fflush(stdout);
	for (int i = 1; i < k; ++i) printf("? %d %d\n", 1 + rt, 1 + nfd[i]);
	for (int i = 1; i < tp; ++i) printf("? %d %d\n", 1 + V[i - 1], 1 + V[i]);

	puts("!"), fflush(stdout);

	for (int i = 1, _, __; i < k; ++i) {
		scanf("%d%d%d%d", &_, dis + nfd[i], &__, dis2 + nfd[i]);
		dis[nfd[i]] += _;
	}

	for (int d1, d2, i = 1, _; i < tp; ++i) {
		scanf("%d%d%d%d", &_, &_, &d1, &d2);
		assert(dis2[V[i - 1]] - d1 == dis2[V[i]] - d2);
		dis2[lca2(V[i - 1], V[i])] = dis2[V[i]] - d2;
	}

	for (int oldtp = tp, i = 1; i < oldtp; ++i)
		V[tp++] = lca2(V[i - 1], V[i]);
	sort(V, V + tp, cmp);
	tp = unique(V, V + tp) - V;

	sta[++tp] = V[0];
	for (int i = 1; i < tp; ++i) {
		while (tp > 0 && ! (dfn2[sta[tp]] <= dfn2[V[i]] &&
					dfn2[V[i]] + sz2[V[i]] <= dfn2[sta[tp]] + sz2[sta[tp]])
				)
			--tp;
		add3(sta[tp], V[i], dis2[V[i]] - dis2[sta[tp]]);
	}

	gfs(rt);
	gfs2(rt);

	return 0;

	for (int i = 0; i < t; ++i)
		scanf("%d%d", &q1[i], &q2[i]), --q1[i], --q2[i];

	for (int i = 0; i < t; ++i) {
		int u, v, x, y;
		u = q1[i], v = q2[i];
		x = dis[u] + dis[v] - 2 * dis[lca(u, v)];
		y = lv3[u] + lv3[v] - 2 * lv3[lca3(u, v)];

		printf("%d %d\n", x, y);
	}
	fflush(stdout);

	return 0;
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:112:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |         scanf("%d%d%d%d", &n, &k, &q, &t);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:115:22: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  115 |                 scanf("%d", &j);
      |                 ~~~~~^~~~~~~~~~
Main.cpp:121:22: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |                 scanf("%d", &j);
      |                 ~~~~~^~~~~~~~~~
Main.cpp:147:22: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  147 |                 scanf("%d%d%d%d", &_, dis + nfd[i], &__, dis2 + nfd[i]);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:152:22: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  152 |                 scanf("%d%d%d%d", &_, &_, &d1, &d2);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:177:22: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  177 |                 scanf("%d%d", &q1[i], &q2[i]), --q1[i], --q2[i];
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#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...