QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#57994 | #4839. Smaller LCA | MIT01# | WA | 1443ms | 70240kb | C++17 | 3.4kb | 2022-10-24 08:06:46 | 2022-10-24 08:06:46 |
Judging History
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 contrib[maxn + 5];
int ans[maxn + 5];
vector< pair<int, int> > pairs;
int fen0[maxn + 5];
int fen1[maxn + 5];
inline void add(int *fen, int x, int y)
{
for (int i = x + 1; i <= n; i += i & -i)
fen[i] += y;
}
inline int query(int *fen, 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(fen0, 0, sizeof fen0);
memset(fen1, 0, sizeof fen1);
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))
{
int tmp = query(fen0, End[cur - 1]) - query(fen0, dfn[cur - 1]);
ans[cur - 1] += tmp;
for (auto z : adj[cur - 1])
if (z != fa[0][cur - 1])
{
contrib[z] = tmp - (query(fen1, End[z]) - query(fen1, dfn[z]));
}
++cur;
}
add(fen0, dfn[x], 1);
add(fen0, dfn[y], 1);
add(fen1, dfn[x], 1);
add(fen1, dfn[y], 1);
int tmp = lca(x, y);
add(fen0, dfn[tmp], -1);
add(fen1, dfn[tmp], -2);
if (~fa[0][tmp])
{
add(fen0, dfn[fa[0][tmp]], -1);
}
}
REP(i, 0, n)
{
int x = seq[i];
if (~fa[0][x]) contrib[x] += contrib[fa[0][x]];
}
REP(i, 0, n) ans[i] += contrib[i];
REP(i, 0, n)
printf("%lld\n", ((LL)n * (n + 1) >> 1) - ans[i]);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 6ms
memory: 14588kb
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: 1443ms
memory: 70240kb
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 44999925170 44999927760 44999927757 45000014066 44999929448 45000048206 45000040365 45000014854 45000062169 44999926677 45000062861 45000054322 44999557096 44999930080 44999904595 45000061228 45000047124 45000047820 45000061175 44999923478 45000047096 44999904840 45000040350 44999537961 ...
result:
wrong answer 1st numbers differ - expected: '44999437117', found: '44999499571'