QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#186588 | #5249. Bandits | RobeZH | WA | 991ms | 138000kb | C++14 | 4.3kb | 2023-09-24 05:08:30 | 2023-09-24 05:08:30 |
Judging History
answer
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define subnb true
#define Lnb true
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
template<class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int INF = (int)1e9;
const int N = (int)1e5 + 50, LOGN = 18;
//const int N = (int)1e3, LOGN = 18;
struct edge {
int to, cost;
};
int n, q;
vector<edge> g[N];
struct LCA {
int parent[LOGN][N];
int depth[N];
ll dr[N];
void dfs(int v, int p, int d, ll d1) {
parent[0][v] = p;
depth[v] = d;
dr[v] = d1;
for (auto e : g[v]) {
int nxt = e.to, cost = e.cost;
if(nxt != p) dfs(nxt, v, d + 1,d1 + e.cost);
}
}
void init() {
dfs(0, -1, 0, 0);
rep(k, 0, LOGN - 1) {
rep(v, 0, n) parent[k + 1][v] =
parent[k][v] < 0 ? -1 : parent[k][parent[k][v]];
}
}
int lca(int u, int v) {
if(depth[u] > depth[v]) swap(u, v);
rep(k, 0, LOGN) {
if((depth[v] - depth[u]) >> k & 1) v = parent[k][v];
}
if(u == v) return u;
for (int k = LOGN - 1; k >= 0; k--) {
if(parent[k][u] != parent[k][v]) {
u = parent[k][u];
v = parent[k][v];
}
}
return parent[0][u];
}
ll min_dis(int u, int v) {
int ca = lca(u, v);
return dr[u] + dr[v] - 2 * dr[ca];
}
} lca;
set<int> G[N];
int sub[N], par[N];
ll d1par[N], d0par[N];
Tree<pll> Sp[N], Sc[N];
int rt = -1;
pii es[N];
int dfs1(int v, int p) {
sub[v] = 1;
for (int nxt : G[v])
if(nxt != p) sub[v] += dfs1(nxt, v);
return sub[v];
}
int dfs2(int v, int p, int nn) {
for (int nxt : G[v]) {
if(nxt != p && sub[nxt] > nn / 2) return dfs2(nxt, v, nn);
}
return v;
}
void decompose(int v, int p) {
dfs1(v, -1);
int centroid = dfs2(v, -1, sub[v]);
if(p == -1) rt = centroid;
par[centroid] = p;
d1par[centroid] = lca.min_dis(centroid, p);
if(p != -1) d0par[centroid] = d0par[p] + 1;
for (int nxt : G[centroid]) {
G[nxt].erase(centroid);
decompose(nxt, centroid);
}
G[centroid].clear();
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
rep(i, 0, n - 1) {
int u, v, w; cin >> u >> v >> w; u--, v--;
G[u].insert(v);
G[v].insert(u);
g[u].push_back({v, w});
g[v].push_back({u, w});
es[i] = {u, v};
}
lca.init();
decompose(0, -1);
// rep(i, 0, n) {
// cout << i + 1 << " par: " << par[i] + 1 << " d0:" << d0par[i] << " d1:" << d1par[i] << endl;
// }
cin >> q;
rep(qid, 0, q) {
char c; cin >> c;
if(c == '+') {
int v; ll w; cin >> v >> w; v--;
int pv = -1;
while(w >= 0) {
Sp[v].insert({w, qid});
if(pv != -1) Sc[v].insert({w, qid});
// cout << "adding " << w << ", " << qid << " to " << v + 1 << ", " << pv + 1 << endl;
if(v == rt) break;
w -= d1par[v];
pv = v;
v = par[v];
}
} else {
int eid; cin >> eid; eid--;
int u, v;
tie(u, v) = es[eid];
if(d0par[u] < d0par[v]) swap(u, v);
int cur = u, pv = -1;
// assert(cur == v);
ll dissum = 0;
ll res = 0;
while(true) {
ll c0 = sz(Sp[cur]) - Sp[cur].order_of_key({dissum, -INF});
ll c1 = pv == -1 ? 0LL : sz(Sc[pv]) - Sc[pv].order_of_key({dissum, -INF});
res += c0 - c1;
if(cur == rt) break;
dissum += d1par[cur];
pv = cur;
cur = par[cur];
}
cout << res << '\n';
}
}
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 991ms
memory: 138000kb
input:
100000 2670 75097 4080 87477 75802 1712 51835 36626 2883 19412 25923 5852 23976 19312 2520 82536 19514 2492 27160 66601 4483 99087 15088 3504 47050 58820 2964 37063 5696 9901 7717 1496 4891 79136 5448 4340 22575 81285 9289 96280 3803 9877 41980 32139 2855 44236 64938 3298 5983 99947 9666 95856 62545...
output:
0 0 0 1 1 3 1 1 2 3 3 5 7 7 7 8 9 10 10 8 8 9 7 8 9 8 7 8 10 9 8 8 8 11 10 11 12 9 12 11 12 17 14 13 17 17 14 15 15 19 17 21 20 18 21 21 25 23 25 28 26 27 30 30 31 30 29 31 32 32 32 35 33 33 37 35 35 42 40 39 44 45 47 47 48 49 47 49 48 49 52 54 53 56 57 56 59 58 60 56 60 59 59 59 58 58 57 59 59 65 6...
result:
wrong answer 4th lines differ - expected: '2', found: '1'