QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#364490 | #8236. Snake Move | ucup-team1525# | WA | 7ms | 32392kb | C++17 | 4.7kb | 2024-03-24 14:49:14 | 2024-03-24 14:49:15 |
Judging History
answer
#include <bits/stdc++.h>
typedef long long ll;
constexpr ll MAX_DIS = 1e15;
struct Path {
int top;
int value;
};
auto cmpValue = [](const Path& a, const Path& b) { return a.value < b.value; };
struct Ans {
ll dis;
Path v[2];
};
auto cmpDis = [](const Ans& a, const Ans& b) { return a.dis < b.dis; };
Ans Merge(Ans a, Ans b) {
std::array<Path, 4> arr = {a.v[0], a.v[1], b.v[0], b.v[1]};
std::sort(arr.begin(), arr.end(), cmpValue);
return {
.dis = a.dis,
.v = {
arr[0],
*std::find_if(arr.begin() + 1, arr.end(), [&](Path p) { return p.top != arr[0].top; })
}
};
}
int Mex(const std::vector<int>& v) {
for (int i = 0; i < v.size(); i++) {
if (v[i] != i) { return i; }
}
return v.size();
}
const int N = 5e5 + 10;
int w[N];
struct L {
int to;
ll len;
};
std::vector<L> tr[N];
int globalMex;
struct Q {
int x;
ll k;
int ans;
};
Q q[N];
std::vector<int> qId[N];
namespace top_cluster {
bool removed[N];
namespace center {
int size[N];
int center;
int centerMaxSonSize;
int totalSize;
void DfsSize(int x, int f) {
size[x] = 1;
for (auto [to, len]: tr[x]) {
if (removed[to]) { continue; }
if (to == f) { continue;}
DfsSize(to, x);
size[x] += size[to];
}
}
void DfsCenter(int x, int f) {
int maxSonSize = totalSize - size[x];
for (auto [to, len]: tr[x]) {
if (removed[to]) { continue; }
if (to == f) { continue;}
DfsCenter(to, x);
maxSonSize = std::max(maxSonSize, size[to]);
}
if (maxSonSize < centerMaxSonSize) {
center = x;
centerMaxSonSize = maxSonSize;
}
}
int GetCenter(int x) {
DfsSize(x, 0);
totalSize = size[x];
centerMaxSonSize = size[x];
DfsCenter(x, 0);
return center;
}
}
std::vector<Ans> ans;
void DfsAdd(int x, int f, ll dis, int top) {
ans.push_back({
.dis = dis,
.v = {
(Path) {.top = top, .value = w[x]},
(Path) {.top = 0, .value = globalMex},
},
});
if (cmpValue(ans.back().v[1], ans.back().v[0])) { std::swap(ans.back().v[0], ans.back().v[1]); }
for (auto [to, len]: tr[x]) {
if (removed[to]) { continue; }
if (to == f) { continue;}
DfsAdd(to, x, dis + len, top);
}
}
void DfsCalc(int x, int f, ll dis, int top) {
for (auto id: qId[x]) {
auto it = std::upper_bound(ans.begin(), ans.end(), (Ans) {.dis = dis}, cmpDis);
if (it == ans.end()) { continue; }
if (it->v[0].top != top) {
q[id].ans = it->v[0].value;
continue;
}
q[id].ans = it->v[1].value;
}
for (auto [to, len]: tr[x]) {
if (removed[to]) { continue; }
if (to == f) { continue;}
DfsCalc(to, x, dis + len, top);
}
}
void Solve(int x) {
x = center::GetCenter(x);
removed[x] = true;
for (auto [to, len]: tr[x]) {
if (removed[to]) { continue; }
DfsAdd(to, x, len, to);
}
std::sort(ans.begin(), ans.end(), cmpDis);
for (int i = ans.size() - 1; i > 0 ; i--) {
ans[i - 1] = Merge(ans[i - 1], ans[i]);
}
for (auto [to, len]: tr[x]) {
if (removed[to]) { continue; }
DfsCalc(to, x, len, to);
}
ans.clear();
for (auto [to, len]: tr[x]) {
if (removed[to]) { continue; }
Solve(to);
}
}
}
int main() {
std::ios::sync_with_stdio(false);
int n, m;
std::cin >> n >> m;
for (int i = 1; i <= n; i++) { std::cin >> w[i]; }
globalMex = ({
std::vector<int> v(w + 1, w + n + 1);
std::sort(v.begin(), v.end());
Mex(v);
});
for (int i = 1; i < n; i++) {
int u, v;
ll len;
std::cin >> u >> v >> len;
tr[u].push_back((L) {.to = v, .len = len});
tr[v].push_back((L) {.to = u, .len = len});
}
for (int i = 1; i <= m; i++) {
std::cin >> q[i].x >> q[i].k;
q[i].ans = globalMex;
qId[q[i].x].push_back(i);
}
top_cluster::Solve(1);
for (int i = 1; i <= m; i++) {
std::cout << q[i].ans << "\n";
}
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 7ms
memory: 32392kb
input:
4 5 5 3 5 3 4 3 3 3 2 4 2 ..... ..... ..... .....
output:
0 0 0 0 0
result:
wrong answer 1st lines differ - expected: '293', found: '0'