QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#149112 | #5249. Bandits | Forever_Young | WA | 494ms | 30748kb | C++14 | 4.4kb | 2023-08-24 01:21:26 | 2023-08-24 01:21:27 |
Judging History
answer
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
typedef long long LL;
const int N = 100011;
int vst[N];
int tim = 0;
const int inf = 1e9 + 7;
struct E {
int y, z, n;
} e[N * 2];
vector<int> vec[N];
int q[N][3];
LL dep[N];
int blg[N], idx[N], son[N], siz[N], ans[N], x[N], y[N];
int l = 1;
typedef tree<pair<LL, int> , null_type, less<pair<LL, int> >, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
ordered_set mp, mps[N];
void get_size(int v, int fa = 0) {
siz[v] = 1;
son[v] = 0;
int mx = -1;
for(int p = idx[v]; p; p = e[p].n) {
int y = e[p].y;
if(y == fa) continue;
get_size(y, v);
siz[v] += siz[y];
if(siz[y] > mx) {
mx = siz[y];
son[v] = y;
}
}
}
int get_root(int st) {
get_size(st);
int tot = siz[st];
while(siz[son[st]] > tot / 2) {
st = son[st];
}
return st;
}
void dfs(int v, int fa = 0) {
for(int p = idx[v]; p; p = e[p].n) {
int y = e[p].y;
if(y == fa) continue;
int z = e[p].z;
dep[y] = dep[v] + z;
blg[y] = fa == 0 ? y : blg[v];
dfs(y, v);
}
}
void build(int x, int y, int z) {
e[++l].y = y;
e[l].z = z;
e[l].n = idx[x];
idx[x] = l;
}
mt19937 gene(233);
void dvcq(int st, vector<int> qids) {
int rt = get_root(st);
//printf("st = %d, rt = %d, queries = %d, siz = %d\n", st, rt, qids.size(), siz[st]);
dep[rt] = 0;
dfs(rt);
int cnt = 0;
for(int id : qids) {
if(q[id][0] == '+') {
int v = q[id][1];
int len = q[id][2];
if(dep[v] <= len) {
if(siz[st] == 2) {
cnt++;
}else {
mp.insert({len - dep[v], id});
if(v == rt) {
}else {
mps[blg[v]].insert({len - dep[v], id});
vec[blg[v]].pb(id);
}
}
}else {
if(siz[st] == 2) {
}else {
vec[blg[v]].pb(id);
}
}
}else {
if(siz[st] == 2) {
ans[id] += cnt;
}else {
int eid = q[id][1];
int x = ::x[eid];
int y = ::y[eid];
if(dep[x] < dep[y]) {
x = y;
}
int b = x == rt ? blg[y] : blg[x];
ans[id] += mp.size() - mp.order_of_key({dep[x], -1});
ans[id] -= mps[b].size() - mps[b].order_of_key({dep[x], -1});
if(ans[id] == 1 && id == 852) {
printf("%d %d %lld %lld\n", x, y, dep[x], dep[y]);
}
vec[b].pb(id);
}
}
}
if(siz[st] == 2) return;
mp.clear();
for(int p = idx[rt]; p; p = e[p].n) {
int y = e[p].y;
mps[y].clear();
}
for(int p = idx[rt]; p; p = e[p].n) {
int y = e[p].y;
int bak = idx[rt];
int nxt = e[p].n;
idx[rt] = p;
e[p].n = 0;
dvcq(y, move(vec[y]));
idx[rt] = bak;
e[p].n = nxt;
}
}
int main() {
int n, Q;
scanf("%d", &n);
//n = 100000;
for(int i = 0; i < n - 1; i++) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
//x = i + 2;
//y = gene() % (i + 1) + 1;
//z = gene() % (int)(1e9 + 1);
build(x, y, z);
build(y, x, z);
::x[i] = x;
::y[i] = y;
}
scanf("%d", &Q);
//Q = 100000;
vector<int> queries;
for(int i = 0; i < Q; i++) {
queries.pb(i);
static char st[111];
scanf("%s", st);
//st[0] = gene() % 2 ? '+' : '?';
q[i][0] = st[0];
if(q[i][0] == '+') {
scanf("%d%d", &q[i][1], &q[i][2]);
//q[i][1] = gene() % n + 1;
//q[i][2] = gene() % (int)(1e9 + 1);
}else {
scanf("%d", &q[i][1]);
//q[i][1] = gene() % (n - 1) + 1;
q[i][1] -= 1;
}
}
dvcq(1, queries);
for(int i = 0; i < Q; i++) {
if(q[i][0] == '?') {
printf("%d\n", ans[i]);
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 494ms
memory: 30748kb
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 2 2 5 2 2 3 4 4 7 8 9 11 10 14 12 12 10 11 10 10 9 10 11 11 9 15 11 14 13 14 16 11 17 15 13 15 14 14 20 15 20 22 22 20 17 23 23 24 29 24 26 30 31 36 28 37 39 35 34 45 39 46 45 43 46 42 49 44 50 43 47 52 50 49 57 51 56 61 58 68 66 69 69 61 63 67 63 72 74 78 72 73 78 77 73 85 76 86 82 85 76 82 8...
result:
ok 50000 lines
Test #2:
score: 0
Accepted
time: 291ms
memory: 29596kb
input:
100000 30038 18547 1594 65857 34063 4575 36600 72585 2328 99199 77595 1590 64257 48199 589 72301 40302 5083 69474 97536 606 60079 67381 9331 65982 39033 205 84122 65285 8508 18167 44905 3704 93490 94986 5941 27155 46374 6616 36232 62969 2212 79807 68652 7199 87352 59101 6880 94571 53224 3552 63610 8...
output:
0 1 3 3 3 3 4 6 10 10 12 14 14 16 18 19 19 19 19 22 22 23 23 23 23 24 25 26 26 26 26 26 26 28 31 31 31 31 31 34 34 36 40 41 41 42 42 42 42 42 44 44 44 47 48 48 48 48 48 48 48 48 48 49 50 50 53 54 54 55 55 56 56 56 56 56 59 62 62 62 62 62 62 62 62 62 62 63 65 67 69 69 69 73 73 73 74 76 76 76 76 76 79...
result:
ok 50000 lines
Test #3:
score: -100
Wrong Answer
time: 185ms
memory: 23908kb
input:
100000 61878 94907 27495452 40498 66053 163324081 9987 91760 269034612 88613 6169 634714395 87422 83687 263182872 22328 64374 595886322 57437 38976 201931120 75020 26926 516189886 88639 96262 269100132 88285 44915 173237252 88407 91931 174315082 12843 50312 641940581 13297 52746 120211351 89089 4638...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
wrong answer 853rd lines differ - expected: '0', found: '1'