QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#761036 | #7245. Frank Sinatra | Hooch | WA | 2ms | 11900kb | C++23 | 2.8kb | 2024-11-18 20:40:49 | 2024-11-18 20:41:06 |
Judging History
answer
#include <bits/stdc++.h>
using u32 = unsigned;
using i64 = long long;
using u64 = unsigned long long;
constexpr int N = 2E5 + 5;
int n, q, val[N], id[N], dfn[N], _tot, tot, f[N], g[N];
int tofa[N];
std::vector<std::array<int, 2>> adj[N];
int st[N][20], bel[N], B;
int get(int x, int y) {
return dfn[x] < dfn[y] ? x : y;
}
void dfs(int x, int fa) {
st[dfn[x] = ++_tot][0] = fa;
id[f[x] = ++tot] = x;
for (auto [v, w] : adj[x]) {
if (v == fa) continue;
tofa[v] = w;
dfs(v, x);
}
id[g[x] = ++tot] = x;
}
int getlca(int u, int v) {
if (u == v) return u;
if ((u = dfn[u]) > (v = dfn[v])) std::swap(u, v);
int d = std::__lg(v - u++);
return get(st[u][d], st[v - (1 << d) + 1][d]);
}
int ans[N], cnt[N];
struct Query {
int l, r;
int idx;
bool operator<(const Query& w) const {
return bel[l] == bel[w.l] ? r < w.r : l < w.l;
}
} b[N];
struct Div {
int cnts[N], KB;
int pos[N], maxsz[N];
void ins(int p) {
if (p > n) return ;
++cnts[p / KB];
pos[p]++;
}
void del(int p) {
if (p > n) return ;
--cnts[p / KB];
pos[p]--;
}
void init() {
KB = std::sqrt(n + 1);
for (int i = 0; i <= n / KB; ++i)
maxsz[i / KB]++;
}
int qry() {
int i, j;
for (i = 0; i <= n / KB && cnts[i] == maxsz[i]; ++i) ;
for (j = i * KB; j <= n && pos[j]; ++j) ;
return j;
}
} dv;
bool vis[N];
void add(int pos) {
int x = id[pos];
if (vis[x]) dv.del(tofa[x]);
else dv.ins(tofa[x]);
vis[x] = !vis[x];
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n >> q;
B = std::sqrt(2 * n);
for (int i = 1; i < n; ++i) {
int u, v, w;
std::cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
dfs(1, 0);
for (int j = 1; j <= std::__lg(n); ++j) {
for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
st[i][j] = get(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
}
}
for (int i = 1; i <= q; ++i) {
int x, y;
std::cin >> x >> y;
if (f[x] > f[y]) std::swap(x, y);
if (getlca(x, y) == x) b[i] = (Query) {f[x] + 1, f[y], i};
else b[i] = (Query) {g[x], f[y], i};
}
for (int i = 1; i <= 2 * n; ++i) {
bel[i] = (i - 1) / B + 1;
}
dv.init();
std::sort(b + 1, b + 1 + q);
// for (int i = 1; i <= tot; ++i) {
// std::cout << id[i] << " \n"[i == tot];
// }
int l = 1, r = 0;
for (int i = 1; i <= q; ++i) {
// std::cerr << b[i].l << " " << b[i].r << "\n";
while (l > b[i].l) add(--l);
while (l < b[i].l) add(l++);
while (r < b[i].r) add(++r);
while (r > b[i].r) add(r--);
// for (int j = 0; j <= n; ++j) {
// std::cout << dv.pos[j] << " \n"[j == n];
// }
ans[b[i].idx] = dv.qry();
}
for (int i = 1; i <= q; ++i) {
std::cout << ans[i] << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 11748kb
input:
7 6 2 1 1 3 1 2 1 4 0 4 5 1 5 6 3 5 7 4 1 3 4 1 2 4 2 5 3 5 3 7
output:
0 1 2 2 3 3
result:
ok 6 numbers
Test #2:
score: 0
Accepted
time: 0ms
memory: 11900kb
input:
2 4 1 2 0 1 1 1 2 2 1 2 2
output:
0 1 1 0
result:
ok 4 number(s): "0 1 1 0"
Test #3:
score: -100
Wrong Answer
time: 2ms
memory: 9892kb
input:
10 100 3 7 1 3 1 0 6 1 1 1 9 0 4 1 1 5 8 1 10 6 0 1 2 1 5 3 1 6 1 4 10 6 5 3 3 5 8 9 2 1 3 8 4 8 5 10 10 5 2 7 10 2 10 8 9 5 3 9 4 6 2 1 8 4 7 3 9 2 5 3 7 10 7 2 2 6 6 6 7 1 9 7 8 6 8 7 3 5 10 5 1 1 2 10 8 8 7 4 2 2 3 7 6 2 9 5 4 10 3 2 4 10 6 3 6 7 4 5 6 10 4 8 2 1 4 9 10 7 9 3 5 9 8 7 2 1 1 7 1 7 ...
output:
0 3 3 0 0 2 1 2 0 0 3 2 3 2 0 2 0 3 3 1 3 0 2 0 0 3 1 3 2 0 2 2 0 2 3 0 2 3 2 3 3 0 1 2 3 3 3 2 0 3 3 0 2 3 0 2 0 2 0 2 3 2 2 3 1 2 2 1 3 0 0 3 2 0 0 2 2 3 3 0 3 0 2 0 2 0 0 0 0 1 2 0 2 0 1 2 3 2 2 3
result:
wrong answer 2nd numbers differ - expected: '2', found: '3'