QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#283777 | #2580. 语言 | yhk1001 | 100 ✓ | 405ms | 99112kb | C++14 | 3.9kb | 2023-12-15 13:54:01 | 2023-12-15 13:54:01 |
Judging History
answer
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
using namespace std;
const int N = 1e5, E = N << 1;
const int Lg = 17;
const int SZ = N * 2 * 2 * Lg;
int n, m;
int head[N + 5], to[E + 5], nxt[E + 5], edge = 1;
void add_edge(int u, int v)
{
edge++;
to[edge] = v;
nxt[edge] = head[u];
head[u] = edge;
return ;
}
void add(int u, int v)
{
add_edge(u, v);
add_edge(v, u);
return ;
}
int dfn[N + 5], rk[N + 5], tim;
int fa[N + 5], dep[N + 5];
int st[N + 5][Lg + 5];
void dfs(int u, int father, int depth)
{
dfn[u] = ++tim, rk[tim] = u;
fa[u] = father, dep[u] = depth;
st[tim][0] = u;
for (int i = head[u]; i; i = nxt[i])
{
int v = to[i];
if (v == father)
continue;
dfs(v, u, depth + 1);
}
return ;
}
int Min(int x, int y)
{
if (dep[x] < dep[y])
return x;
return y;
}
int query(int l, int r)
{
int k = __lg(r - l + 1);
return Min(st[l][k], st[r - (1 << k) + 1][k]);
}
int LCA(int u, int v)
{
if (u == v)
return u;
u = dfn[u], v = dfn[v];
if (u > v)
swap(u, v);
return fa[query(u + 1, v)];
}
int Dis(int u, int v)
{
return dep[u] + dep[v] - 2 * dep[LCA(u, v)];
}
void build()
{
for (int k = 1; (1 << k) <= n; k++)
for (int i = 1; i + (1 << k) - 1 <= n; i++)
st[i][k] = Min(st[i][k - 1], st[i + (1 << (k - 1))][k - 1]);
return ;
}
typedef pair<int, int> pir;
vector<pir> vec[N + 5];
void push(int p, int s, int t, int val)
{
vec[p].emplace_back(dfn[s], val);
vec[p].emplace_back(dfn[t], val);
return ;
}
struct Node
{
int ls, rs;
int left, right;//node
int sum, cnt;
} node[SZ + 5];
int tot, root[N + 5];
#define ls(p) node[p].ls
#define rs(p) node[p].rs
#define left(p) node[p].left
#define right(p) node[p].right
#define sum(p) node[p].sum
#define cnt(p) node[p].cnt
void pushup(int p)
{
sum(p) = sum(ls(p)) + sum(rs(p));
if (right(ls(p)) && left(rs(p)))
sum(p) += Dis(right(ls(p)), left(rs(p)));
if (left(ls(p)))
left(p) = left(ls(p));
else
left(p) = left(rs(p));
if (right(rs(p)))
right(p) = right(rs(p));
else
right(p) = right(ls(p));
return ;
}
int merge(int x, int y, int l, int r)
{
if (!x || !y)
return x + y;
if (l == r)
{
cnt(x) += cnt(y);//sum of leaf is always 0
if (cnt(x))
left(x) = right(x) = rk[l];
else
left(x) = right(x) = 0;
return x;
}
int mid = (l + r) >> 1;
ls(x) = merge(ls(x), ls(y), l, mid);
rs(x) = merge(rs(x), rs(y), mid + 1, r);
pushup(x);
return x;
}
void modify(int p, int l, int r, int pos, int v)
{
if (l == r)
{
cnt(p) += v;
if (cnt(p))
left(p) = right(p) = rk[l];
else
left(p) = right(p) = 0;
return ;
}
int mid = (l + r) >> 1;
if (pos <= mid)
{
if (!ls(p))
ls(p) = ++tot;
modify(ls(p), l, mid, pos, v);
}
else
{
if (!rs(p))
rs(p) = ++tot;
modify(rs(p), mid + 1, r, pos, v);
}
pushup(p);
return ;
}
int query(int p)
{
int res = sum(p);
if (left(p) && right(p))
res += Dis(left(p), right(p));
return res;
}
long long ans;
void solve(int u)
{
root[u] = ++tot;
for (int i = head[u]; i; i = nxt[i])
{
int v = to[i];
if (v == fa[u])
continue;
solve(v);
root[u] = merge(root[u], root[v], 1, n);
}
for (auto info : vec[u])
modify(root[u], 1, n, info.first, info.second);
ans += query(root[u]) / 2;//each edge is counted twice
return ;
}
int main()
{
// freopen("language.in", "r", stdin);
// freopen("language.out", "w", stdout);
scanf("%d%d", &n, &m);
for (int i = 1, u, v; i < n; i++)
{
scanf("%d%d", &u, &v);
add(u, v);
}
dfs(1, 0, 1);
build();
for (int i = 1, s, t; i <= m; i++)
{
scanf("%d%d", &s, &t);
int lca = LCA(s, t);
push(s, s, t, 1);
push(t, s, t, 1);
push(lca, s, t, -1);
if (fa[lca])
push(fa[lca], s, t, -1);
}
solve(1);
ans >>= 1;//(u, v) & (v, u)
printf("%lld\n", ans);
return 0;
}
详细
Test #1:
score: 10
Accepted
time: 2ms
memory: 12264kb
input:
297 297 281 46 39 260 193 121 50 162 18 15 43 104 154 293 131 121 70 112 247 121 17 59 240 102 277 185 90 121 134 38 46 116 27 127 97 7 33 49 24 297 31 286 12 18 57 121 133 150 183 286 186 194 153 121 117 189 256 121 266 192 106 230 80 249 164 73 211 197 264 4 53 47 203 43 286 69 221 146 214 167 142...
output:
15910
result:
ok single line: '15910'
Test #2:
score: 10
Accepted
time: 0ms
memory: 12132kb
input:
293 296 286 83 144 202 108 42 70 112 85 29 291 112 14 112 97 292 123 168 247 112 143 112 238 102 174 262 252 185 63 112 137 103 64 112 15 11 282 273 248 1 100 112 159 181 226 20 47 129 258 34 72 160 189 282 50 252 145 175 13 112 250 152 106 267 30 196 22 112 43 104 17 112 105 80 95 177 34 251 259 29...
output:
15234
result:
ok single line: '15234'
Test #3:
score: 10
Accepted
time: 13ms
memory: 15124kb
input:
4858 4888 3140 3181 1664 1770 4200 493 3671 59 3209 1842 2502 2173 681 2155 2654 3278 4174 2554 3282 587 3337 4086 4287 2326 779 3708 2048 2448 3634 3148 4214 59 2083 59 325 1216 351 878 1497 3495 4642 4242 115 4419 1588 2172 1054 4610 3007 59 4821 2816 2078 1195 782 2610 2559 3187 4576 292 4000 59 ...
output:
3627298
result:
ok single line: '3627298'
Test #4:
score: 10
Accepted
time: 9ms
memory: 15092kb
input:
4983 4867 1535 1610 1899 1266 3238 2426 2405 1420 2578 4364 300 2296 4590 254 396 1242 3129 3631 324 3583 3748 1155 2771 1200 4246 4603 2331 1110 2824 1891 2544 1237 3487 3040 3687 2834 109 4636 4687 1200 1649 2420 2008 2926 2516 4852 3440 1551 765 3096 2754 1200 4879 4820 3073 4787 91 1200 2911 257...
output:
3873876
result:
ok single line: '3873876'
Test #5:
score: 10
Accepted
time: 228ms
memory: 44056kb
input:
96808 99508 40695 40696 73595 73596 13615 13616 65545 65546 19391 19392 2353 2354 26730 26731 42541 42542 28008 28009 96282 96283 76530 76531 5872 5873 61286 61287 56072 56073 87216 87217 38177 38178 50372 50373 329 330 58557 58558 43629 43630 77425 77426 8017 8018 43507 43508 35913 35914 31123 3112...
output:
907477207
result:
ok single line: '907477207'
Test #6:
score: 10
Accepted
time: 228ms
memory: 44024kb
input:
97092 99840 40695 40696 73595 73596 13615 13616 65545 65546 19391 19392 2353 2354 26730 26731 42541 42542 28008 28009 96282 96283 76530 76531 5872 5873 61286 61287 56072 56073 87216 87217 38177 38178 50372 50373 329 330 58557 58558 43629 43630 77425 77426 8017 8018 43507 43508 35913 35914 31123 3112...
output:
910321604
result:
ok single line: '910321604'
Test #7:
score: 10
Accepted
time: 389ms
memory: 97648kb
input:
99924 97275 24723 56564 44193 73644 54591 22570 56965 55653 91120 48245 25702 41871 77524 67327 80722 7653 87907 54339 86418 76838 41215 78202 48534 59032 44710 32114 38879 34955 17434 99405 37456 39328 66649 35177 76911 54339 40612 7834 70065 54339 29689 38076 48485 60166 50641 57751 97629 94661 47...
output:
1654600973
result:
ok single line: '1654600973'
Test #8:
score: 10
Accepted
time: 395ms
memory: 97608kb
input:
99498 96777 19744 18289 4605 93580 43036 31212 42702 45122 16545 73127 70905 51611 27911 61773 48069 83562 95567 11283 66495 45122 65856 15502 26003 58273 46733 45122 7043 7247 80433 55125 67878 88881 20197 45122 39212 80482 25962 45122 62830 45122 40173 39170 14714 82850 24093 66693 50604 61001 866...
output:
1503897488
result:
ok single line: '1503897488'
Test #9:
score: 10
Accepted
time: 400ms
memory: 98560kb
input:
99214 98195 8234 36494 37451 75094 31731 33067 19984 36494 52041 11042 4970 37696 26871 43916 87010 25881 37092 8701 75030 89583 4706 75209 24536 4378 37412 69085 88883 17123 79895 36494 95090 79450 95892 73714 61952 77454 81044 8337 68757 11047 73613 36494 60137 16120 41322 47605 49142 24141 79221 ...
output:
1595001829
result:
ok single line: '1595001829'
Test #10:
score: 10
Accepted
time: 405ms
memory: 99112kb
input:
98930 97863 31939 63734 42943 69741 55957 86464 76683 63177 19844 89880 92738 35520 42184 7020 98581 89880 73665 19084 91904 89880 32101 31332 16923 89880 64777 72859 39842 59094 18714 87378 5513 21706 54019 89880 16688 80816 1260 35635 83182 52628 32044 61306 26440 39530 77024 69004 20199 64299 434...
output:
1481951671
result:
ok single line: '1481951671'
Extra Test:
score: 0
Extra Test Passed