QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#57992#4839. Smaller LCAMIT01#WA 1002ms67800kbC++172.9kb2022-10-24 07:53:192022-10-24 07:53:20

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-24 07:53:20]
  • 评测
  • 测评结果:WA
  • 用时:1002ms
  • 内存:67800kb
  • [2022-10-24 07:53:19]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = (a), i##_end_ = (b); i < i##_end_; ++i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define x first
#define y second
#define pb emplace_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()

template<typename T> inline bool chkmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

typedef long long LL;

const int oo = 0x3f3f3f3f;

const int max0 = 19;
const int maxn = 300100;

int n;
vector<int> adj[maxn + 5];

int fa[max0][maxn + 5];

int dep[maxn + 5];

int dfs_tot = 0;
int dfn[maxn + 5];
int End[maxn + 5];

LL sum[maxn + 5];
int seq[maxn + 5];

void dfs(int x, int f = -1)
{
	seq[dfs_tot] = x;
	dfn[x] = dfs_tot++; 
	if (~f) dep[x] = dep[f] + 1;
	else dep[x] = 0;
	fa[0][x] = f;
	for (auto y : adj[x])
		if (y != f) dfs(y, x);
	End[x] = dfs_tot;
}

int lca(int x, int y)
{
	if (dep[x] > dep[y]) swap(x, y);
	for (int i = 0; dep[x] != dep[y]; ++i)
		if ((dep[y] ^ dep[x]) >> i & 1) y = fa[i][y];
	int s = max0 - 1;
	while (x != y)
	{
		for ( ; s && fa[s][x] == fa[s][y]; --s);
		x = fa[s][x];
		y = fa[s][y];
	}
	return x;
}

int cnt[maxn + 5];

int ans[maxn + 5];

vector< pair<int, int> > pairs;

int fen[maxn + 5];

inline void add(int x, int y)
{
	for (int i = x + 1; i <= n; i += i & -i)
		fen[i] += y;
}

inline int query(int x)
{
	int ret = 0;
	for (int i = x; i > 0; i -= i & -i)
		ret += fen[i];
	return ret;
}

int main()
{
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	scanf("%d", &n);
	REP(i, 0, n - 1)
	{
		int u, v;
		scanf("%d%d", &u, &v), --u, --v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	dfs_tot = 0;
	dfs(0);
	REP(i, 1, max0) REP(j, 0, n)
		if (~fa[i - 1][j]) fa[i][j] = fa[i - 1][fa[i - 1][j]];
		else fa[i][j] = fa[i - 1][j];
	for (int i = 1; i * i <= n; ++i)
		for (int j = i; i * j <= n; ++j)
		{
			pairs.pb(mp(i - 1, j - 1));
			int tmp = lca(i - 1, j - 1);
			if ((tmp + 1) > (i + 1) * (j + 1)) ++cnt[tmp];
		}
	sort(ALL(pairs), [&](const pair<int, int> &x, const pair<int, int> &y) { return x.x * x.y < y.x * y.y; } );
	memset(fen, 0, sizeof fen);
	REP(i, 0, n)
	{
		int x = seq[i];
		sum[x] = cnt[x];
		if (~fa[0][x]) sum[x] += sum[fa[0][x]];
	}
	int tot = 0;
	REP(i, 0, n) tot += cnt[i];
	REP(i, 0, n) ans[i] = tot - sum[i];

	int cur = 1;
	for (auto u : pairs)
	{
		int x = u.x, y = u.y;
		while (cur <= (x + 1) * (y + 1))
		{
			ans[cur - 1] += query(End[cur - 1]) - query(dfn[cur - 1]);
			++cur;
		}
		add(dfn[x], 1);
		add(dfn[y], 1);
		int tmp = lca(x, y);
		add(dfn[tmp], -1);
		if (~fa[0][tmp])
			add(dfn[fa[0][tmp]], -1);
	}

	REP(i, 0, n)
		printf("%lld\n", ((LL)n * (n + 1) >> 1) - ans[i]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 20284kb

input:

5
1 2
4 2
2 5
3 5

output:

15
15
15
15
14

result:

ok 5 number(s): "15 15 15 15 14"

Test #2:

score: -100
Wrong Answer
time: 1002ms
memory: 67800kb

input:

300000
40632 143306
32259 40632
225153 143306
269774 225153
289668 40632
191636 269774
85717 191636
58564 191636
156509 143306
289939 40632
247103 269774
40257 40632
98149 289668
142277 143306
291616 40257
46813 225153
56324 143306
277154 142277
53903 289668
114266 32259
152231 58564
241151 152231
4...

output:

44999499571
44999926083
44999929276
44999929279
45000042104
44999933941
45000063680
45000055397
45000044320
45000086343
44999927939
45000086410
45000069157
44999557923
44999938161
44999905638
45000085067
45000065551
45000065397
45000085037
44999925378
45000065709
44999905667
45000055227
44999538844
...

result:

wrong answer 1st numbers differ - expected: '44999437117', found: '44999499571'