Yellow_and_Strong @ 2022-10-01 14:27:12
rt,代码厌氧,不开O2能A,开O2就全T了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 1e5 + 1e2;
int n, m;
struct edge
{
int to, next;
}e[MAXN << 1]; int head[MAXN], cnt;
int fa[MAXN], d[MAXN], size[MAXN], hson[MAXN];
int TCS_cnt, dfn[MAXN], rnk[MAXN], top[MAXN];
struct tree
{
int l, r;
long long pre, add;
}t[MAXN << 2];
inline int read()
{
int x = 0; char ch = getchar();
while ( !isdigit(ch) ) ch = getchar();
while ( isdigit(ch) ) { x = x * 10 + (ch - '0'); ch = getchar(); }
return x;
}
void add (int u, int v) { e[++ cnt].to = v; e[cnt].next = head[u]; head[u] = cnt; }
void input()
{
n = read(); m = read();
for (register int i = 1; i < n; i ++)
{
int u, v;
u = read(); v = read();
add (u, v); add (v, u);
}
}
void dfs1_init() { d[1] = 1; }
void dfs1 (int u)
{
size[u] = 1;
for (register int i = head[u]; i; i = e[i].next)
{
int v = e[i].to;
if (v == fa[u]) continue;
fa[v] = u;
d[v] = d[u] + 1;
dfs1 (v);
size[u] += size[v];
if (!hson[u] or size[v] > size[ hson[u] ]) hson[u] = v;
}
}
void dfs2 (int u, int t)
{
dfn[u] = ++ TCS_cnt;
rnk[TCS_cnt] = u;
top[u] = t;
if (!hson[u]) return;
dfs2 (hson[u], t);
for (register int i = head[u]; i; i = e[i].next)
{
int v = e[i].to;
if (v != fa[u] and v != hson[u]) dfs2 (v, v);
}
}
void build (int p, int l, int r)
{
t[p].l = l, t[p].r = r;
if (l == r) return;
int mid = (l + r) >> 1;
build (p << 1, l, mid); build (p << 1 | 1, mid + 1, r);
}
void spread (int p)
{
if (!t[p].add) return;
t[p << 1].pre += t[p].add * (t[p << 1].r - t[p << 1].l + 1);
t[p << 1 | 1].pre += t[p].add * (t[p << 1 | 1].r - t[p << 1 | 1].l + 1);
t[p << 1].add += t[p].add;
t[p << 1 | 1].add += t[p].add;
t[p].add = 0;
}
void SeT_update (int p, int l, int r, int k)
{
if (l <= t[p].l and t[p].r <= r)
{
t[p].pre += k * (t[p].r - t[p].l + 1);
t[p].add += k;
return;
}
spread (p);
int mid = (t[p].l + t[p].r) >> 1;
if (l <= mid) SeT_update (p << 1, l, r, k);
if (r > mid) SeT_update (p << 1 | 1, l, r, k);
t[p].pre = t[p << 1].pre + t[p << 1 | 1].pre;
}
void TCS_update (int u, int v, int k)
{
while (top[u] != top[v])
{
if (d[ top[v] ] < d[ top[u] ]) swap (u, v);
SeT_update (1, dfn[ top[v] ], dfn[v], k);
v = fa[ top[v] ];
}
if (d[v] < d[u]) swap (u, v);
SeT_update (1, dfn[u] + 1, dfn[v], k);
// SeT_update (1, dfn[u], dfn[u], -k);
}
long long SeT_query (int p, int l, int r)
{
if (l <= t[p].l and t[p].r <= r) return t[p].pre;
spread (p);
int mid = (t[p].l + t[p].r) >> 1, res = 0;
if (l <= mid) res = SeT_query (p << 1, l, r);
if (r > mid) res += SeT_query (p << 1 | 1, l, r);
return res;
}
long long TCS_query (int u, int v)
{
long long ans = 0;
/*while (top[u] != top[v])
{
if (d[ top[v] ] < d[ top[u] ]) swap (u, v);
ans += SeT_query (1, dfn[ top[v] ], dfn[v]);
v = fa[ top[v] ];
}*/
if (d[v] < d[u]) swap (u, v);
ans += SeT_query (1, dfn[v], dfn[v]);
// ans -= SeT_query (1, dfn[u], dfn[u]);
}
void work()
{
for (register int i = 1; i <= m; i ++)
{
char op; int u, v;
cin >> op; u = read(); v = read();
if (op == 'P') TCS_update (u, v, 1);
else printf ("%lld\n", TCS_query(u, v) );
}
}
int main()
{
input();
dfs1_init();
dfs1 (1);
dfs2 (1, 1);
build (1, 1, n);
work();
return 0;
}
by interestingLSY @ 2022-10-01 14:29:47
大概率是未定义行为
by ud2_ @ 2022-10-01 14:54:01
main.cpp: In function 'long long int TCS_query(int, int)':
main.cpp:141:1: warning: no return statement in function returning non-void [-Wreturn-type]
141 | }
| ^
by Yellow_and_Strong @ 2022-10-01 14:55:30
@interestingLSY 我在 TCS_query() 函数里没有打 return ans ,sb了,谢老师(逃
by Yellow_and_Strong @ 2022-10-01 14:56:02
@ud2_ 谢巨佬