QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#518729 | #3847. Airline | wisniewskij | WA | 318ms | 27744kb | C++20 | 3.9kb | 2024-08-14 03:14:33 | 2024-08-14 03:14:34 |
Judging History
answer
#include <bits/stdc++.h>
#define ndl '\n'
#define ll long long
#define INF 1000000007
#define st first
#define nd second
#define debug(x) cout << #x << ": " << x << ndl
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define all(x) (x).begin(), (x).end()
using namespace std;
void dfs_sub_tree_size(ll v, const vector<vector<ll>> &G, vector<ll> &sub_tree_size, vector<ll> &parent) {
sub_tree_size[v] = 1;
for(auto x: G[v]) {
if(x != parent[v]) {
parent[x] = v;
dfs_sub_tree_size(x, G, sub_tree_size, parent);
sub_tree_size[v] += sub_tree_size[x];
}
}
}
void fill_depth_and_jump_table(vector<ll> &depth, vector<vector<ll>> &jump_table, const vector<ll> &parent) {
for(ll j=0; j<jump_table[0].size(); j++) {
for(ll i=2; i<jump_table.size(); i++) {
jump_table[i][j] = (j == 0) ? parent[i] : jump_table[jump_table[i][j-1]][j-1];
depth[i] = depth[jump_table[i][0]] + 1;
}
}
}
ll find_lca(ll x, ll y, const vector<ll> &depth, const vector<vector<ll>> &jump_table) {
if(depth[x] < depth[y]) swap(x, y);
ll depth_difference = depth[x] - depth[y];
for(ll i=0; (1<<i) <= depth_difference; i++)
if((1<<i)&depth_difference) x = jump_table[x][i];
if(x == y) return x;
for(ll i=jump_table[0].size()-1; i>=0; i--) {
while(jump_table[x][i] != jump_table[y][i]) {
x = jump_table[x][i];
y = jump_table[y][i];
}
}
return jump_table[x][0];
}
void fill_path(ll x, ll y, ll lca, const vector<ll> &depth, const vector<vector<ll>> &jump_table, vector<ll> &path) {
if(depth[x] < depth[y]) swap(x, y);
while (x != lca) {
path.push_back(x);
x = jump_table[x][0];
}
path.push_back(lca);
if(lca != y) {
vector<ll> stack;
while (y != lca) {
stack.push_back(y);
y = jump_table[y][0];
}
for(ll i=stack.size()-1; i>=0; i--)
path.push_back(stack[i]);
}
}
void fill_appendices(const ll lca, const vector<vector<ll>> &G, const vector<ll> &path, const vector<ll> &subtree_sizes, const vector<vector<ll>> &jump_table, vector<ll> &appendices) {
for(ll i=0; i<path.size(); i++) {
ll v = path[i], lower = (i != 0 ? path[i-1] : -1), upper = (i != path.size()-1 ? path[i+1] : -1);
appendices.push_back(1);
if(v == lca) {
appendices.back() += subtree_sizes[1] - subtree_sizes[lca];
}
for(auto x:G[v]) {
if(x != lower && x != upper && x != jump_table[v][0])
appendices.back() += subtree_sizes[x];
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
ll n, q; cin>>n>>q;
vector<vector<ll>> G(n+1);
for(ll i=0;i<n-1;i++) {
ll a, b; cin>>a>>b;
G[a].push_back(b);
G[b].push_back(a);
}
vector<ll> parent(n+1);
vector<ll> sub_tree_size(n+1);
dfs_sub_tree_size(1, G, sub_tree_size, parent);
vector<ll> depth(n+1);
vector<vector<ll>> jump_table(n+1, vector<ll>(log2(n)+1));
fill_depth_and_jump_table(depth, jump_table, parent);
while(q--) {
ll a, b; cin>>a>>b;
ll lca = find_lca(a, b, depth, jump_table);
vector<ll> path, appendices;
fill_path(a, b, lca, depth, jump_table, path);
fill_appendices(lca, G, path, sub_tree_size, jump_table, appendices);
ll ans = 0, left_sum = 0, half_cycle_length = ((path.size()-1)/2);
for(ll i=0; i<half_cycle_length; i++) left_sum += appendices[i];
for(ll i=0; i<half_cycle_length; i++) {
ans += left_sum * appendices[appendices.size()-1-i];
left_sum -= appendices[half_cycle_length-1-i];
}
cout<<ans<<'\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 68ms
memory: 4140kb
input:
1000 100000 552 9 456 720 540 790 628 695 848 478 66 268 798 360 773 277 116 471 874 792 912 784 502 831 359 965 925 434 677 629 271 670 76 755 92 200 814 422 922 266 617 44 480 331 18 662 153 753 669 491 368 187 99 867 476 808 774 509 98 147 724 478 447 182 923 469 881 665 674 589 770 613 436 310 8...
output:
8905 976 2924 5646 3837 5966 5530 2221 2394 2570 2238 4823 4034 2458 2582 1259 4751 2694 456 1380 3199 5788 2214 3854 3098 4086 5734 4148 3065 3428 3937 4461 2845 5935 5122 5625 2614 704 6312 3180 2480 7481 9410 4642 1660 4875 5767 2976 5717 5766 4783 4553 2483 3635 4125 4093 6074 8675 3988 4695 313...
result:
ok 100000 lines
Test #2:
score: 0
Accepted
time: 130ms
memory: 5672kb
input:
9392 100000 4521 1973 362 7795 6505 6798 6079 8431 5691 9228 774 4060 2800 6246 6995 4890 5981 7196 6747 6781 8642 1970 4609 7891 6692 3764 1918 8164 8889 4386 6997 2266 8080 2062 1474 2612 5128 1618 6763 604 6611 4768 9265 2462 9323 9067 4086 7292 7661 5091 19 8937 3209 5829 4079 6058 1912 4137 443...
output:
25865 20795 47691 36294 117742 45172 57330 89573 117973 132250 35330 292130 28222 23630 79047 38816 217653 54722 121448 46997 75338 16394 24369 137591 17869 186807 34649 20072 15377 82113 24090 260999 67202 15795 51210 24248 33377 18702 163416 58397 29846 104190 78268 21985 58729 60089 77200 27965 8...
result:
ok 100000 lines
Test #3:
score: 0
Accepted
time: 141ms
memory: 5724kb
input:
10000 100000 9893 2063 2863 2642 8661 4723 6164 9513 6820 6207 4488 2221 1325 5718 1556 7921 5625 8519 6390 5649 418 8063 6480 8380 7514 2373 9617 726 1487 6460 2513 2370 6802 7614 2530 2877 4146 7723 2545 9741 2921 4690 7311 6782 7396 3695 9619 6495 4591 1657 935 9454 6828 4754 3923 9077 725 8709 4...
output:
301945 54742 255647 42833 42190 115868 126805 72842 58873 37057 117958 75501 36147 52590 56534 48946 45216 179027 51981 54150 34061 94817 83797 72851 130990 160292 30221 81990 111442 26773 73980 38218 70294 303262 52096 204388 26876 216714 73098 230567 180645 96737 124516 25837 103832 27278 37849 93...
result:
ok 100000 lines
Test #4:
score: -100
Wrong Answer
time: 318ms
memory: 27744kb
input:
98549 100000 82689 70448 77356 39505 7527 5072 89160 36614 1539 37083 3256 15723 91761 73823 9152 52058 79202 73747 47554 40484 3207 62832 96291 76455 85804 74263 75592 40690 16876 90408 48161 77604 73371 80844 40649 70129 87295 60940 65605 26715 32778 50672 40569 69825 13624 47206 15834 81212 62890...
output:
2268380 926599 2183002 1702915 836581 177673 1413412 918753 6124931 4566130 1089211 9862469 1721224 2320780 601961 574275 7243947 1626922 130095 3762731 583708 2859944 306238 1100329 1124119 773908 8423096 1233858 1789556 2075624 3582238 3224532 10322578 606003 575441 1432311 1752507 1493374 225848 ...
result:
wrong answer 99th lines differ - expected: '515287', found: '93870634'