QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#327642 | #3307. Query on a Tree 17 | JWRuixi | WA | 5ms | 10052kb | C++20 | 3.7kb | 2024-02-15 11:41:31 | 2024-02-15 11:41:33 |
Judging History
answer
#include <bits/stdc++.h>
#define IL inline
#define LL long long
#define eb emplace_back
#define L(i, j, k) for (int i = (j); i <= (k); ++i)
#define R(i, j, k) for (int i = (j); i >= (k); --i)
using namespace std;
namespace IO {
char ibuf[1 << 20], *iS, *iT;
#ifndef LOCAL
#define gh() (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, 1 << 20, stdin), (iS == iT ? EOF : *iS++) : *iS++)
#else
#define gh() getchar()
#endif
template<typename T = int>
IL T read () {
T x = 0;
bool f = 0;
char c = gh();
while (c < '0' || '9' < c) f |= c == '-', c = gh();
while ('0' <= c && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = gh();
return f ? ~(x - 1) : x;
}
}
using IO::read;
using ci = const int;
using vi = vector<int>;
constexpr int N = 1e5 + 9;
constexpr int M = 20;
int n, m, dep[N], sz[N], son[N], top[N], dfn[N], id[N], dfc, par[N], st[N][M];
vi g[N];
namespace SGT {
LL tr[N << 2], tg[N << 2];
#define ls (p << 1)
#define rs (p << 1 | 1)
IL void pushup (int p) {
tr[p] = tr[ls] + tr[rs];
}
IL void push (int p, int v) {
tr[p] += v;
tg[p] += v;
}
IL void pushdown (int p) {
auto &o = tg[p];
if (o) {
push(ls, o);
push(rs, o);
o = 0;
}
}
IL void upd (int ql, int qr, int l, int r, int p) {
if (ql <= l && r <= qr) {
push(p, 1);
return;
}
pushdown(p);
ci mid = (l + r) >> 1;
if (ql <= mid) upd(ql, qr, l, mid, ls);
if (mid < qr) upd(ql, qr, mid + 1, r, rs);
pushup(p);
}
IL void upd (int l, int r) {
upd(l, r, 1, n, 1);
}
IL LL qry (int ql, int qr, int l, int r, int p) {
if (ql <= l && r <= qr) {
return tr[p];
}
pushdown(p);
ci mid = (l + r) >> 1;
LL R = 0;
if (ql <= mid) R += qry(ql, qr, l, mid, ls);
if (mid < qr) R += qry(ql, qr, mid + 1, r, rs);
return R;
}
IL LL qry (int l, int r) {
return qry(l, r, 1, n, 1);
}
}
IL void dfs1 (int u, int fa) {
sz[u] = 1;
par[u] = fa;
dep[u] = dep[fa] + 1;
for (int v : g[u]) {
if (v == fa) continue;
dfs1(v, u);
sz[u] += sz[v];
if (sz[v] > sz[son[u]]) son[u] = v;
}
}
IL void dfs2 (int u, int tp) {
top[u] = tp;
id[dfn[u] = ++dfc] = u;
if (son[u]) {
dfs2(son[u], tp);
}
for (int v : g[u]) {
if (v == son[u] || v == par[u]) continue;
dfs2(v, v);
}
}
void init () {
dfs1(1, 0);
dfs2(1, 1);
L (i, 1, n) {
int k = id[i];
st[k][0] = par[k];
L (j, 1, 17) st[k][j] = st[st[k][j - 1]][j - 1];
}
}
IL LL siz (int u) {
return SGT::qry(dfn[u], dfn[u] + sz[u] - 1);
}
int main () {
n = read();
L (i, 1, n - 1) {
int u = read(), v = read();
g[u].eb(v);
g[v].eb(u);
}
init();
m = read();
LL all = 0;
while (m--) {
int o = read();
if (o == 1) {
int x = read();
SGT::upd(dfn[x], dfn[x] + sz[x] - 1);
all += sz[x];
} else {
int x = read(), y = read();
all += dep[x] + dep[y] + 1;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
SGT::upd(dfn[top[x]], dfn[x]);
x = par[top[x]];
}
if (dep[x] < dep[y]) swap(x, y);
SGT::upd(dep[y], dfn[x]);
all -= 2 * dep[y];
}
int l = 1, r = n, md, p = 0;
while (l <= r) {
md = (l + r) >> 1;
SGT::qry(1, md) * 2 >= all ? p = md, r = md - 1 : l = md + 1;
}
p = id[p];
R (i, 17, 0) {
int q = st[p][i];
if (q && siz(q) <= all / 2) {
p = q;
}
}
while (par[p] && siz(p) * 2 <= all) p = par[p];
printf("%d\n", p);
}
}
// I love WHQ!
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 7960kb
input:
7 1 6 1 7 7 3 3 2 7 5 5 4 4 1 2 1 4 1 6 2 6 7
output:
2 7 7 1
result:
ok 4 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 7944kb
input:
15 1 2 1 3 2 4 3 7 6 2 3 8 4 9 2 5 5 11 7 13 10 4 11 15 12 5 14 7 11 2 6 11 1 6 2 10 9 1 9 2 2 6 1 4 2 7 6 1 2 2 8 13 1 10 2 8 15
output:
2 2 2 2 2 2 2 2 2 2 2
result:
ok 11 lines
Test #3:
score: 0
Accepted
time: 1ms
memory: 10052kb
input:
2 1 2 1 1 1
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 1ms
memory: 9916kb
input:
2 1 2 1 2 1 2
output:
1
result:
ok single line: '1'
Test #5:
score: -100
Wrong Answer
time: 5ms
memory: 7940kb
input:
100 1 2 58 2 2 87 63 87 65 63 65 19 6 87 58 21 87 8 87 74 91 6 95 65 2 61 87 59 3 61 37 87 67 87 23 2 63 9 87 46 40 67 70 2 12 58 46 75 75 36 28 3 12 33 72 87 39 6 75 52 85 70 1 76 26 40 47 28 2 49 41 65 66 28 51 37 15 47 86 51 8 60 97 19 48 58 72 90 33 83 97 54 36 5 23 14 78 52 90 7 99 2 48 82 48 6...
output:
72 2 2 87 87 2 2 2 2 87 2 2 87 87 87 87 87 2 87 87 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 2 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 ...
result:
wrong answer 2nd lines differ - expected: '3', found: '2'