QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#382263 | #4945. 暴力写挂 | bachbeo2007 | 100 ✓ | 1588ms | 170488kb | C++23 | 5.4kb | 2024-04-08 09:59:17 | 2024-04-08 09:59:18 |
Judging History
answer
// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- insert(x)
- find_by_order(k): return iterator to the k-th smallest element
- order_of_key(x): the number of elements that are strictly smaller
*/
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define ll long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define fi first
#define se second
const ll inf=1e18;
const int maxn=400005;
const int maxl=20;
struct node{
pair<ll,int> Max1={-inf,-1},Max2={-inf,-1};
node(){}
friend node operator+(node a,pair<ll,int> x){
if(x>a.Max1){
if(x.se==a.Max1.se) a.Max1=x;
else{a.Max2=a.Max1;a.Max1=x;}
}
else if(x>a.Max2 && x.se!=a.Max1.se) a.Max2=x;
return a;
}
ll query(int p){
if(p==Max1.se) return Max2.fi;
else return Max1.fi;
}
};
int n;
ll ans=-inf;
namespace Centroid{
vector<pii> edge[maxn];
int child[maxn],sz,dad[maxn];
int Min[2*maxn][maxl],lg2[2*maxn],pos,L[maxn];
ll dep[maxn];
node Max[maxn];
bool used[maxn];
int cmp(int u,int v){
return (L[u]<L[v]?u:v);
}
void pre_dfs(int u,int par){
Min[++pos][0]=u;L[u]=pos;
for(pii v:edge[u]){
if(v.fi==par) continue;
dep[v.fi]=dep[u]+v.se;
pre_dfs(v.fi,u);
Min[++pos][0]=u;
}
}
int lca(int u,int v){
u=L[u];v=L[v];
if(u>v) swap(u,v);
int p=lg2[v-u+1];
return cmp(Min[u][p],Min[v-(1<<p)+1][p]);
}
ll dist(int u,int v){
return dep[u]+dep[v]-2*dep[lca(u,v)];
}
void dfs(int u,int par){
child[u]=1;
for(pii v:edge[u]){
if(v.fi==par || used[v.fi]) continue;
dfs(v.fi,u);child[u]+=child[v.fi];
}
}
int findcen(int u,int par){
for(pii v:edge[u]){
if(v.fi==par || used[v.fi]) continue;
if(child[v.fi]>sz/2) return findcen(v.fi,u);
}
return u;
}
void decompose(int u,int par){
dfs(u,par);sz=child[u];
int cen=findcen(u,par);
dad[cen]=par;used[cen]=true;
for(pii v:edge[cen]){
if(v.fi==par || used[v.fi]) continue;
decompose(v.fi,cen);
}
}
void del(int u){
while(u){Max[u]=node();u=dad[u];}
}
void add(int u){
int x=u,pre=u;
while(x){
Max[x]=Max[x]+make_pair(dist(u,x)+dep[u],pre);
pre=x;x=dad[x];
}
}
ll query(int u){
ll res=-inf;
int x=u,pre=u;
while(x){
res=max(res,dist(u,x)+Max[x].query(pre));
pre=x;x=dad[x];
}
return (res+dep[u])/2;
}
void build(){
for(int i=1;i<n;i++){
int u,v,w;cin >> u >> v >> w;
edge[u].push_back({v,w});
edge[v].push_back({u,w});
}
pre_dfs(1,0);
decompose(1,0);
for(int i=2;i<=pos;i++) lg2[i]=lg2[i/2]+1;
for(int i=1;i<20;i++){
for(int j=1;j<=(pos-(1<<i)+1);j++) Min[j][i]=cmp(Min[j][i-1],Min[j+(1<<(i-1))][i-1]);
}
}
}
ll dep[maxn],cur;
int child[maxn],son[maxn];
vector<pii> edge[maxn];
vector<int> ver;
void pre_dfs(int u,int par){
child[u]=1;
for(pii v:edge[u]){
if(v.fi==par) continue;
dep[v.fi]=dep[u]+v.se;
pre_dfs(v.fi,u);child[u]+=child[v.fi];
if(child[v.fi]>child[son[u]]) son[u]=v.fi;
}
}
void cal(int u,int par){
ans=max(ans,Centroid::query(u)-cur);
ver.push_back(u);
for(pii v:edge[u]){
if(v.fi==par) continue;
cal(v.fi,u);
}
}
void del(int u,int par){
Centroid::del(u);
for(pii v:edge[u]){
if(v.fi==par) continue;
del(v.fi,u);
}
}
void dfs(int u,int par,int t){
for(pii v:edge[u]){
if(v.fi==par || v.fi==son[u]) continue;
dfs(v.fi,u,1);
}
if(son[u]) dfs(son[u],u,0);
cur=dep[u];
for(pii v:edge[u]){
if(v.fi==par || v.fi==son[u]) continue;
ver.clear();cal(v.fi,u);
for(int x:ver) Centroid::add(x);
}
ans=max(ans,Centroid::dep[u]-cur);
ans=max(ans,Centroid::query(u)-cur);
Centroid::add(u);
if(t) del(u,par);
}
void solve(){
cin >> n;
Centroid::build();
for(int i=1;i<n;i++){
int u,v,w;cin >> u >> v >> w;
edge[u].push_back({v,w});
edge[v].push_back({u,w});
}
pre_dfs(1,0);
dfs(1,0,0);
cout << ans << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int test=1;//cin >> test;
while(test--) solve();
}
详细
Test #1:
score: 5
Accepted
time: 3ms
memory: 28248kb
input:
36 35 34 1 16 17 1 26 27 1 4 5 1 2 1 1 30 29 1 9 10 1 13 14 1 14 36 1 29 28 1 31 16 1 33 34 1 18 17 1 6 7 1 22 23 1 25 24 1 25 26 1 22 21 1 24 23 1 14 15 1 19 20 1 4 2 1 31 32 1 32 33 1 11 12 1 21 20 1 5 6 1 4 9 1 16 15 1 2 3 1 28 27 1 12 13 1 11 10 1 19 18 1 7 8 1 19 18 1 18 17 1 22 21 1 7 8 1 35 3...
output:
28
result:
ok 1 number(s): "28"
Test #2:
score: 5
Accepted
time: 2ms
memory: 24152kb
input:
366 302 300 1 281 284 1 205 213 1 38 56 1 280 278 1 215 218 1 287 289 1 337 336 1 32 19 1 161 135 1 154 152 1 339 343 1 333 336 1 316 320 1 190 203 1 235 229 1 238 253 1 162 188 1 125 128 1 104 100 1 255 275 1 141 139 1 249 231 1 322 318 1 320 323 1 2 49 1 122 123 1 2 3 1 252 229 1 93 96 1 13 83 1 4...
output:
66
result:
ok 1 number(s): "66"
Test #3:
score: 5
Accepted
time: 3ms
memory: 28444kb
input:
1388 233 230 2010117013 466 465 517864445 273 277 468266280 3 33 1876495368 523 525 393728142 1290 1288 964611218 456 453 291234141 1106 862 1491931739 748 752 1273412570 702 706 91665845 1194 862 955991116 1006 863 1683051927 862 1165 1155264346 372 370 1224015059 544 548 667577990 2 148 1198631891...
output:
328592252066
result:
ok 1 number(s): "328592252066"
Test #4:
score: 5
Accepted
time: 6ms
memory: 26568kb
input:
1999 247 25 793109451 1026 1023 1869940234 1132 832 612508077 773 1136 163935570 750 752 843387799 1244 982 1777108778 38 131 1018040742 1273 943 1734553409 96 28 2014569809 14 18 1736377610 1558 1536 1818734353 1507 1395 1869434908 1471 1838 516216128 1115 1051 938285248 1889 1570 1781082422 111 47...
output:
40944563637
result:
ok 1 number(s): "40944563637"
Test #5:
score: 5
Accepted
time: 3ms
memory: 24596kb
input:
2666 1914 1909 1900304480 1896 1827 46829052 1462 461 1034104716 1993 2208 1476581917 534 306 1587894016 2318 2334 54406485 407 1523 1108845815 118 58 1464204605 739 1127 1082834408 1333 1240 1326511079 22 851 1562784465 874 204 31232643 2157 2230 1432494307 2269 2494 1877799642 2525 2547 767349491 ...
output:
46319083678
result:
ok 1 number(s): "46319083678"
Test #6:
score: 5
Accepted
time: 7ms
memory: 29224kb
input:
5666 5394 4025 1009479564 1480 2237 -1429591010 1404 2085 856916845 5289 3972 742726642 2981 2818 1314010296 1457 1090 -733492464 819 771 1384454828 2244 1483 1063195272 1292 1007 -1861141429 4640 3648 -1921175128 2368 1545 -1581683837 2 90 1858875740 3 666 935624153 1121 922 190864513 1186 954 4355...
output:
77595942376
result:
ok 1 number(s): "77595942376"
Test #7:
score: 5
Accepted
time: 19ms
memory: 31596kb
input:
8666 165 1201 1038443784 2507 1934 -734585573 1843 2444 1510685986 125 255 -371342030 7397 7180 1436849724 4391 3562 1721269684 8169 8406 -769044783 7346 7183 1040271094 2484 2407 -1780590520 6059 6591 1083318614 1947 3267 1411381807 1532 1465 1760339062 774 654 -1082029949 7969 7683 -1138237500 368...
output:
43632978289
result:
ok 1 number(s): "43632978289"
Test #8:
score: 5
Accepted
time: 26ms
memory: 30360kb
input:
11111 5475 5474 1264346617 5400 5399 479067219 6768 6767 1666857947 4681 4680 -1300739728 9511 9512 501265531 7280 7281 -641351804 5261 5262 -915253126 8278 8279 607718841 2624 2625 272191602 69 70 1522014920 9336 9335 -1301642281 10833 10834 -1327316854 9991 9990 1428368830 4841 4840 -424194350 398...
output:
29894787715
result:
ok 1 number(s): "29894787715"
Test #9:
score: 5
Accepted
time: 32ms
memory: 32388kb
input:
12345 8243 8244 -1053057901 12050 12051 -1633460666 10904 10905 52815170 9562 9561 -1045255643 861 860 1207725542 2964 2965 -1535983715 11858 11859 477008449 3541 3542 -612566097 11514 11513 1670385301 5072 5073 283074153 11471 11472 -1911022368 7286 7285 911877895 3455 3454 113595854 1357 1358 -114...
output:
33297530419
result:
ok 1 number(s): "33297530419"
Test #10:
score: 5
Accepted
time: 300ms
memory: 101412kb
input:
188888 76753 76752 608781106 173345 173344 1385300362 52402 52401 616008356 164674 164673 1465319105 8389 8388 1711299259 106391 106390 1915795430 140922 140921 1191918051 23209 23210 1554648596 144489 144490 940708893 113066 113067 1845664846 156111 156112 1032902039 47174 47173 948212630 7550 7551...
output:
190352672270703
result:
ok 1 number(s): "190352672270703"
Test #11:
score: 5
Accepted
time: 661ms
memory: 166064kb
input:
366666 130393 130392 1745739051 2909 2908 648703068 287682 287683 214392793 34303 34304 -1178470735 201947 201948 1633041545 71534 71535 -1898563484 364105 364106 3121060 245712 245713 1970744166 332266 332265 -1399804510 350346 350347 1258576095 297078 297079 1846269167 353774 353773 -1654457930 14...
output:
262853684155
result:
ok 1 number(s): "262853684155"
Test #12:
score: 5
Accepted
time: 463ms
memory: 99816kb
input:
188888 63165 63166 1837585977 186944 186943 418930609 158449 158448 353605203 81350 81349 818330358 46400 46401 1797408919 86471 86470 2009543790 134326 134327 1744525828 182974 182973 1801286949 159896 159897 829675145 105703 105704 1813431214 75191 75192 290150959 183962 183961 1946851254 154410 1...
output:
190262840337216
result:
ok 1 number(s): "190262840337216"
Test #13:
score: 5
Accepted
time: 1588ms
memory: 164620kb
input:
366666 326151 326150 431983823 102666 102665 709407221 282506 282505 249567611 103228 103229 1484554698 55108 55107 1022198226 43915 43914 423843667 252667 252666 1949908115 314746 314747 736646600 127181 127180 1851447045 321033 321032 379403997 356758 356757 56556585 286283 286282 1171907991 19794...
output:
370227457628347
result:
ok 1 number(s): "370227457628347"
Test #14:
score: 5
Accepted
time: 1192ms
memory: 162572kb
input:
366666 333246 333245 -1455666363 209725 209726 411942300 137733 137734 -1036880356 282418 282419 1257507163 31970 31969 68984509 245443 245442 640608996 17646 17647 1484211578 352864 352865 -1957781539 119808 119809 1994115839 233773 233774 733202407 50580 50579 -645159820 234978 234977 -1253386771 ...
output:
1087912926406
result:
ok 1 number(s): "1087912926406"
Test #15:
score: 5
Accepted
time: 223ms
memory: 102040kb
input:
188888 68419 68522 303955801 42623 14462 1286912953 25459 33619 717260553 21744 21793 1190600040 128869 112300 587562905 112298 118721 1704852203 149867 112300 457066097 17095 48549 177299469 23649 41641 725059649 85230 77024 757651257 112298 183254 1590484170 112298 132572 1791449084 8766 29333 115...
output:
46886180770
result:
ok 1 number(s): "46886180770"
Test #16:
score: 5
Accepted
time: 569ms
memory: 168764kb
input:
366666 86275 75039 1457813126 189352 226927 1915597621 147287 48158 1290734574 31893 123322 712745103 302921 273732 123061183 302056 286799 1551449832 41543 41058 600034432 94194 106399 1771598529 43183 11406 1070539898 28883 24429 201958100 203676 255576 9754959 32408 64152 246897921 15567 64399 14...
output:
222227692617
result:
ok 1 number(s): "222227692617"
Test #17:
score: 5
Accepted
time: 474ms
memory: 170488kb
input:
366666 129619 5198 -973656183 266812 231272 1567664157 5196 88382 785954950 14850 5196 118061314 5197 185242 1088219202 5197 73335 -1334128788 5198 50710 764178009 5196 104181 -539107013 216830 235373 667432679 186324 186419 -1550436917 186139 257465 319261589 5197 28980 -1399296087 5198 20052 -6614...
output:
582086506117
result:
ok 1 number(s): "582086506117"
Test #18:
score: 5
Accepted
time: 730ms
memory: 141020kb
input:
366666 221087 261714 -1157424755 275319 221086 1146814470 322529 221086 -792303321 4 14915 -879002672 221087 347873 -782761546 78013 85363 1827554750 221085 271698 1986551213 134063 78013 -216058810 221087 286211 987545442 126222 78012 128783110 17264 2 490291481 292957 221085 -1076233944 230128 221...
output:
126836005122
result:
ok 1 number(s): "126836005122"
Test #19:
score: 5
Accepted
time: 1395ms
memory: 140256kb
input:
366666 278666 284245 -656119512 86406 110657 -502085363 144075 128040 776809184 69336 174347 -804165979 67547 156366 -1848058246 64373 71724 -715071830 19658 5225 -1361227618 315145 291521 701585949 303323 267128 -1560410812 300079 306255 -929824651 286404 249489 -1944484045 68182 173207 -786742825 ...
output:
247695762186
result:
ok 1 number(s): "247695762186"
Test #20:
score: 5
Accepted
time: 1296ms
memory: 137960kb
input:
366666 213470 213298 -1206709900 93209 46605 -1438620736 82357 164714 -1234024344 69683 139365 -215225542 234896 213857 -1140909086 54821 109642 -559041306 92759 185518 925676817 37465 74929 -912279904 85738 171476 -1123336314 96113 192226 680464365 55163 110326 -1286101794 334453 349377 -1841466041...
output:
45736064383
result:
ok 1 number(s): "45736064383"
Extra Test:
score: 0
Extra Test Passed