版本比较
题解:P13824 「Diligent-OI R2 D」在水一方
正文
增加 25 行删除 25 行
24 行未改动25262728293031323344 行未改动78798081828384858687888990919293949596979829 行未改动12812913013113213313413513619 行未改动15615715815916016116216316414 行未改动1791801811821831841851861871881891901911921931941951961971981992002012025 行未改动2082092102112122132142152162 行未改动21922022122222322422522622711 行未改动2392402412422432442452462474 行未改动2522532542552562572582592602612622632642652662672682692707 行未改动2782792802812822832842852862872882892902912925 行未改动2982993003013023033043053063073083093103113123139 行未改动32332432532632732832933033124 行未改动void dfs2(ll x, ll root) {str[x] = root;if (x != 1)H[x] = H[fa[x]] + (A[x].x - A[fa[x]].x) * (A[x].x - A[fa[x]].x) + (A[x].y - A[fa[x]].y) * (A[x].y - A[fa[x]].y); // 维护前缀和if (son[x]) dfs2(son[x], root);for (int i = 0; i < V[x].size(); i++) {if (V[x][i] == son[x] || V[x][i] == fa[x]) continue;dfs2(V[x][i], V[x][i]);44 行未改动u = dfn[i];dp[u][u] = 0;for (int j = 1; j <= dfncnt; j++) {v = dfn[j];dp[u][v] = (A[u].x - A[v].x) * (A[u].x - A[v].x) + (A[u].y - A[v].y) * (A[u].y - A[v].y); // 把dp值初始化为点对距离q = (1ull << 63) - 1; // 找到更新该点对距离最大值的最小值if (u != lca[u][v]) {q = min(dp[fa[u]][v], q);}if (v != lca[u][v]) {q = min(dp[u][fa[v]], q);}if (u != lca[u][v] && v != lca[u][v]) {q = min(q, dp[fa[u]][fa[v]]);}dp[u][v] = max(dp[u][v], q);if (dp[u][v] == (1ull << 63) - 1) dp[u][v] = 0; // 无法更新代表u、v是同一个点,赋值为0}}```29 行未改动v = dfn[j];F.push_back((node1){u, v, dp[u][v]});}}ll flag; // 下面的重载小于号巧妙的用这个 flag 实现了排序模式的转换struct node3{ll p, q, i, d;bool operator<(const node3 &x) const{19 行未改动}D[i].p = Q.top().a;D[i].q = Q.top().b;}flag = 1; // 切换排序模式sort(D + 1, D + 1 + t);for (int i = 1; i <= t; i++) {cout << D[i].p << ' ' << D[i].q << endl;}14 行未改动const ll M = 1e5 + 10;struct node{ll x, y;} A[N]; // 存点vector<ll> V[N]; // 存树ll n, t;ll dp[N][N], dfn[N], dfncnt; // dp部分(信息2)相关,不用多说了ll son[N], siz[N], fa[N], deep[N], str[N], H[N], dis[N][N], lca[N][N]; // 求活动路径距离(信息1)相关// son、siz、fa、deep、str分别是树剖用到的重儿子、子树大小、父节点、深度和所属重链// H是求距离用到的树上前缀和// dis、lca分别存点对(p,q)的活动路径距离和最近公共祖先void dfs1(ll x, ll f) { // 求重儿子,以及通过dfs处理dfs序、父节点、深度等信息dfncnt++;dfn[dfncnt] = x;fa[x] = f;deep[x] = deep[f] + 1;siz[x] = 1;for (int i = 0; i < V[x].size(); i++) {5 行未改动}}}void dfs2(ll x, ll root) { // 剖重链str[x] = root;if (x != 1)H[x] = H[fa[x]] + (A[x].x - A[fa[x]].x) * (A[x].x - A[fa[x]].x) + (A[x].y - A[fa[x]].y) * (A[x].y - A[fa[x]].y);if (son[x]) dfs2(son[x], root);2 行未改动dfs2(V[x][i], V[x][i]);}}ll str_lca(ll x, ll y) { // 求两点活动路径距离,以及lcall a = x, b = y;while (str[x] != str[y]) {if (deep[str[x]] < deep[str[y]]) {swap(x, y);11 行未改动return v < x.v;}};vector<node1> F; // 存储点对信息,用于获取答案struct node2{ll a, b, v;bool operator<(const node2 &x) const{4 行未改动return v < x.v;}};priority_queue<node2> Q; // 优先队列,用于获取答案ll flag;struct node3{ll p, q, i, d;bool operator<(const node3 &x) const{if (!flag) return d < x.d;else return i < x.i;}} D[M]; // 存储询问与答案的数组int main() {scanf("%lld%lld", &n, &t);for (int i = 1; i <= n; i++) {7 行未改动}dfs1(1, 0);H[1] = 0;dfs2(1, 1);for (int i = 1; i <= n; i++) { // 活动路径长度处理部分for (int j = 1; j <= n; j++) {lca[i][j] = str_lca(i, j);}}ll q;for (int i = 1; i <= dfncnt; i++) { // dp部分u = dfn[i];dp[u][u] = 0;for (int j = 1; j <= dfncnt; j++) {v = dfn[j];5 行未改动if (v != lca[u][v]) {q = min(dp[u][fa[v]], q);}if (u != lca[u][v] && v != lca[u][v]) {q = min(q, dp[fa[u]][fa[v]]);}dp[u][v] = max(dp[u][v], q);if (dp[u][v] == (1ull << 63) - 1) dp[u][v] = 0;F.push_back((node1){u, v, dp[u][v]});}}sort(F.begin(), F.end()); // 答案获取部分for (int i = 1; i <= t; i++) {scanf("%lld", &D[i].d);D[i].i = i;}9 行未改动}flag = 1;sort(D + 1, D + 1 + t);for (int i = 1; i <= t; i++) {printf("%lld %lld\n", D[i].p, D[i].q);} // 输出,愉快的AC qwqreturn 0;}```24 行未改动25262728293031323344 行未改动78798081828384858687888990919293949596979829 行未改动12812913013113213313413513619 行未改动15615715815916016116216316414 行未改动1791801811821831841851861871881891901911921931941951961971981992002012025 行未改动2082092102112122132142152162 行未改动21922022122222322422522622711 行未改动2392402412422432442452462474 行未改动2522532542552562572582592602612622632642652662672682692707 行未改动2782792802812822832842852862872882892902912925 行未改动2982993003013023033043053063073083093103113123139 行未改动32332432532632732832933033124 行未改动void dfs2(ll x, ll root) {str[x] = root;if (x != 1)H[x] = H[fa[x]] + (A[x].x - A[fa[x]].x) * (A[x].x - A[fa[x]].x) + (A[x].y - A[fa[x]].y) * (A[x].y - A[fa[x]].y); // 维护前缀和if (son[x]) dfs2(son[x], root);for (int i = 0; i < V[x].size(); i++) {if (V[x][i] == son[x] || V[x][i] == fa[x]) continue;dfs2(V[x][i], V[x][i]);44 行未改动u = dfn[i];dp[u][u] = 0;for (int j = 1; j <= dfncnt; j++) {v = dfn[j];dp[u][v] = (A[u].x - A[v].x) * (A[u].x - A[v].x) + (A[u].y - A[v].y) * (A[u].y - A[v].y); // 把dp值初始化为点对距离q = (1ull << 63) - 1; // 找到更新该点对距离最大值的最小值if (u != lca[u][v]) {q = min(dp[fa[u]][v], q);}if (v != lca[u][v]) {q = min(dp[u][fa[v]], q);}if (u != lca[u][v] && v != lca[u][v]) {q = min(q, dp[fa[u]][fa[v]]);}dp[u][v] = max(dp[u][v], q);if (dp[u][v] == (1ull << 63) - 1) dp[u][v] = 0; // 无法更新代表u、v是同一个点,赋值为0}}```29 行未改动v = dfn[j];F.push_back((node1){u, v, dp[u][v]});}}ll flag; // 下面的重载小于号巧妙的用这个 flag 实现了排序模式的转换struct node3{ll p, q, i, d;bool operator<(const node3 &x) const{19 行未改动}D[i].p = Q.top().a;D[i].q = Q.top().b;}flag = 1; // 切换排序模式sort(D + 1, D + 1 + t);for (int i = 1; i <= t; i++) {cout << D[i].p << ' ' << D[i].q << endl;}14 行未改动const ll M = 1e5 + 10;struct node{ll x, y;} A[N]; // 存点vector<ll> V[N]; // 存树ll n, t;ll dp[N][N], dfn[N], dfncnt; // dp部分(信息2)相关,不用多说了ll son[N], siz[N], fa[N], deep[N], str[N], H[N], dis[N][N], lca[N][N]; // 求活动路径距离(信息1)相关// son、siz、fa、deep、str分别是树剖用到的重儿子、子树大小、父节点、深度和所属重链// H是求距离用到的树上前缀和// dis、lca分别存点对(p,q)的活动路径距离和最近公共祖先void dfs1(ll x, ll f) { // 求重儿子,以及通过dfs处理dfs序、父节点、深度等信息dfncnt++;dfn[dfncnt] = x;fa[x] = f;deep[x] = deep[f] + 1;siz[x] = 1;for (int i = 0; i < V[x].size(); i++) {5 行未改动}}}void dfs2(ll x, ll root) { // 剖重链str[x] = root;if (x != 1)H[x] = H[fa[x]] + (A[x].x - A[fa[x]].x) * (A[x].x - A[fa[x]].x) + (A[x].y - A[fa[x]].y) * (A[x].y - A[fa[x]].y);if (son[x]) dfs2(son[x], root);2 行未改动dfs2(V[x][i], V[x][i]);}}ll str_lca(ll x, ll y) { // 求两点活动路径距离,以及lcall a = x, b = y;while (str[x] != str[y]) {if (deep[str[x]] < deep[str[y]]) {swap(x, y);11 行未改动return v < x.v;}};vector<node1> F; // 存储点对信息,用于获取答案struct node2{ll a, b, v;bool operator<(const node2 &x) const{4 行未改动return v < x.v;}};priority_queue<node2> Q; // 优先队列,用于获取答案ll flag;struct node3{ll p, q, i, d;bool operator<(const node3 &x) const{if (!flag) return d < x.d;else return i < x.i;}} D[M]; // 存储询问与答案的数组int main() {scanf("%lld%lld", &n, &t);for (int i = 1; i <= n; i++) {7 行未改动}dfs1(1, 0);H[1] = 0;dfs2(1, 1);for (int i = 1; i <= n; i++) { // 活动路径长度处理部分for (int j = 1; j <= n; j++) {lca[i][j] = str_lca(i, j);}}ll q;for (int i = 1; i <= dfncnt; i++) { // dp部分u = dfn[i];dp[u][u] = 0;for (int j = 1; j <= dfncnt; j++) {v = dfn[j];5 行未改动if (v != lca[u][v]) {q = min(dp[u][fa[v]], q);}if (u != lca[u][v] && v != lca[u][v]) {q = min(q, dp[fa[u]][fa[v]]);}dp[u][v] = max(dp[u][v], q);if (dp[u][v] == (1ull << 63) - 1) dp[u][v] = 0;F.push_back((node1){u, v, dp[u][v]});}}sort(F.begin(), F.end()); // 答案获取部分for (int i = 1; i <= t; i++) {scanf("%lld", &D[i].d);D[i].i = i;}9 行未改动}flag = 1;sort(D + 1, D + 1 + t);for (int i = 1; i <= t; i++) {printf("%lld %lld\n", D[i].p, D[i].q);} // 输出,愉快的AC qwqreturn 0;}```