QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#519693 | #3847. Airline | wisniewskij | TL | 935ms | 184012kb | C++20 | 3.9kb | 2024-08-14 23:56:58 | 2024-08-14 23:56:59 |
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;
inline void dfs_fill_depths_and_subtree_sizes(int v, vector<vector<int>> &jump_table, vector<int> &subtree_size, vector<int> &depth, const vector<vector<int>> &G){
subtree_size[v] = 1;
for(auto x:G[v]){
if(x==jump_table[v][0]) continue;
jump_table[x][0] = v;
depth[x] = depth[v] + 1;
dfs_fill_depths_and_subtree_sizes(x, jump_table, subtree_size, depth, G);
subtree_size[v] += subtree_size[x];
}
}
inline void fill_jump_table(vector<vector<int>> &jump_table){
for(int j=1;j<jump_table[0].size();j++)
for(int i=2;i<jump_table.size();i++)
jump_table[i][j] = jump_table[jump_table[i][j-1]][j-1];
}
int find_lca(const vector<vector<int>> &jump_table, const vector<int> &depth, int a, int b){
if(depth[a] < depth[b]) swap(a, b);
int depth_diff = depth[a] - depth[b];
for(int i=0;(1<<i)<=depth_diff;i++)
if((1<<i)&depth_diff)
a = jump_table[a][i];
if(a == b) return a;
for(int i=jump_table[0].size()-1;i>=0;i--)
while(jump_table[a][i] != jump_table[b][i]) {
a = jump_table[a][i];
b = jump_table[b][i];
}
return jump_table[a][0];
}
vector<ll> find_subtree_sizes_for_cycle(int a, int b, const int lca, const vector<vector<int>> &jump_table,
const vector<int> &subtree_size, const vector<int> &depth, const vector<vector<int>> &G){
vector<int> cycle_indices;
vector<ll> cycle_subtrees;
if(depth[a] < depth[b]) swap(a, b);
while(a != lca){
cycle_indices.push_back(a);
a = jump_table[a][0];
}
cycle_indices.push_back(lca);
int a_side_size = cycle_indices.size();
while(b != lca){
cycle_indices.push_back(b);
b = jump_table[b][0];
}
reverse(cycle_indices.begin() + a_side_size, cycle_indices.end());
cycle_subtrees.assign(cycle_indices.size(), 0);
for(int i=0;i<cycle_indices.size();i++){
int lower = (i != 0 ? cycle_indices[i-1] : -1), upper = (i != cycle_indices.size()-1 ? cycle_indices[i+1] : -1);
cycle_subtrees[i] = 1;
if(cycle_indices[i] == lca)
cycle_subtrees[i] += subtree_size[1] - subtree_size[cycle_indices[i]];
for(auto x: G[cycle_indices[i]])
if(x != upper && x != lower && x != jump_table[cycle_indices[i]][0])
cycle_subtrees[i] += subtree_size[x];
}
return cycle_subtrees;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n, q; cin>>n>>q;
vector<vector<int>> G(n+1);
for(int i=0;i<n-1;i++){
int x, y; cin>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
vector<int> depth(n+1), subtree_size(n+1);
vector<vector<int>> jump_table(n+1, vector<int>(log2(n)+1));
dfs_fill_depths_and_subtree_sizes(1, jump_table, subtree_size, depth, G);
fill_jump_table(jump_table);
while(q--){
int x, y, lca; cin>>x>>y; lca = find_lca(jump_table, depth, x, y);
vector<ll> subtree_cycle = find_subtree_sizes_for_cycle(x, y, lca, jump_table, subtree_size, depth, G);
ll left_sum = 0, ans = 0;
int cycle_half_len = (subtree_cycle.size()-1)/2;
for(int i=0;i<cycle_half_len;i++) left_sum += subtree_cycle[i];
for(int i=0;cycle_half_len-1-i>=0;i++){
ans += left_sum * subtree_cycle[subtree_cycle.size()-1-i];
left_sum -= subtree_cycle[cycle_half_len-1-i];
}
cout<<ans<<'\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 49ms
memory: 3836kb
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: 97ms
memory: 4736kb
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: 97ms
memory: 4664kb
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: 0
Accepted
time: 236ms
memory: 19524kb
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:
ok 100000 lines
Test #5:
score: 0
Accepted
time: 235ms
memory: 19860kb
input:
100000 100000 6485 28819 91949 30150 52641 17533 10545 52999 42821 43631 52176 83277 66684 9548 29337 74462 90920 25011 47466 13053 83527 7709 12707 25795 90508 82266 46872 57123 60150 97514 76184 41073 73305 90274 51117 72775 21957 89242 21849 96322 59134 86949 15879 40843 71163 17671 99618 96690 9...
output:
200430 4076423 813976 6802322 3547614 3149979 5727130 2110580 3217987 217799 1416755 1046962 1097030 351235 810715 367530 162075 934424 461963 3098238 773882 1336893 3309131 717298 1717283 193899 116706 5542089 380797 1296714 1323500 2692618 447799 701192 116204 622110 236020 1279977 297384 547912 2...
result:
ok 100000 lines
Test #6:
score: 0
Accepted
time: 931ms
memory: 179140kb
input:
971873 100000 436749 616077 202840 410896 455619 967072 103439 822434 475653 398112 461031 486647 343026 341908 861220 641003 796134 726337 481939 80955 727467 87328 439111 929971 614634 944496 593211 114711 119419 855657 201230 264491 210286 147802 32084 285406 718264 254462 390417 57890 684402 868...
output:
151895839 5567962 2048780 7755912 3298118 291200668 2026946 3803072 10195185 25852053 8564427 37758355 4632161 3646199 10536917 31895919 41481500 16188147 10264801 5882102 20896091 5441699 48493636 8893505 44641595 51487773 25764094 32152214 4989029 10363357 8539733 15998581 44503767 10306841 307950...
result:
ok 100000 lines
Test #7:
score: 0
Accepted
time: 935ms
memory: 184012kb
input:
1000000 100000 803384 460095 915180 745831 32349 23515 580947 448659 188631 274157 934991 711308 419176 808090 309146 586352 991644 637573 755008 737123 65329 299447 724012 469040 421444 502777 633955 629892 912207 342364 736852 674360 755495 502086 993988 249100 479859 362258 359743 424921 379910 2...
output:
40080441 32130667 38441771 8989143 54574184 34475496 14887507 7644291 6840414 96392883 71876437 61536099 109314471 1835776 65681185 190466381 3115293 5730318 28144459 27307083 4219651 14991979 10338001 4149617 96025528 26892158 4216803 11507748 3715762 38929181 75754148 36061602 16200031 15557529 35...
result:
ok 100000 lines
Test #8:
score: -100
Time Limit Exceeded
input:
1000000 100000 762104 839878 68400 601674 322741 68400 797458 681633 979286 121964 541137 30448 408270 173352 726998 993520 235077 267868 68400 650976 68400 678723 68400 87880 68400 736923 68400 599313 55248 68400 632321 986186 111629 831883 708807 701035 334748 68400 217493 661224 948145 973190 661...
output:
1000000 13 7 999998 6 1999992 10 33 7 20 6 9 7 999995 999996 3 999998 3 18 9 7 4 3 4 1999993 5 10 1999989 999997 6 999993 6 999995 3 9 8 999999 1000000 1000002 999996 999996 6 999997 999996 999999 2999987 10 999996 999996 2999985 1000001 13 16 999998 14 1000004 7 999998 1999990 7 11 6 5 1000010 9999...