QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#109523 | #5828. 游戏 | fireince | 60 | 754ms | 47028kb | C++14 | 4.8kb | 2023-05-29 16:41:57 | 2023-05-29 16:41:59 |
Judging History
answer
#include<iostream>
#include<cassert>
#include<algorithm>
#include<vector>
#define endl '\n'
using namespace std;
typedef long long ll;
const int N=2e5+500;
const int H=19;
int fa[N][H],dep[N];
vector<int> G[N];
void lca_pre(int u,int f){
fa[u][0]=f;
dep[u]=dep[f]+1;
for(int i=1;i<H;i++)fa[u][i]=fa[fa[u][i-1]][i-1];
for(int v:G[u]){
if(v==f)continue;
lca_pre(v,u);
}
}
int lca(int a,int b){
if(dep[a]<dep[b])swap(a,b);
for(int i=H-1;i>=0;i--)
if(dep[fa[a][i]]>=dep[b])a=fa[a][i];
if(a==b)return a;
for(int i=H-1;i>=0;i--)
if(fa[a][i]!=fa[b][i])a=fa[a][i],b=fa[b][i];
return fa[a][0];
}
int jump_up(int u,int d){
for(int i=H-1;i>=0;i--)
if(dep[fa[u][i]]>=d)u=fa[u][i];
return u;
}
int find_subtree(int u,int v){
return jump_up(v,dep[u]+1);
}
const int TN=5e6;
struct Seg{
static int scnt;
static Seg segs[TN];
int lc,rc,val;
Seg& ch(bool k,bool c=false){
int& w=k?rc:lc;
if(!w)w=++scnt;
return segs[w];
}
void refresh(){
val=max(ch(0).val,ch(1).val);
}
void insert(int l,int r,int idx,int value){
if(l==r){
val=value;
return;
}
int mid=(l+r)>>1;
if(idx<=mid)ch(0,true).insert(l,mid,idx,value);
else ch(1,true).insert(mid+1,r,idx,value);
refresh();
}
int query(int l,int r,int ql,int qr){
if(ql<=l&&qr>=r)
return val;
int mid=(l+r)>>1,ans=0;
if(ql<=mid)ans=max(ans,ch(0).query(l,mid,ql,qr));
if(qr>mid)ans=max(ans,ch(1).query(mid+1,r,ql,qr));
return ans;
}
};
Seg Seg::segs[TN];
int Seg::scnt=0;
Seg t[N];
int n;
void add(int x,int y,int v){
x=n-x+1;
for(;x<=n+1;x+=x&-x)
t[x].insert(0,n,y,v);
}
int ask(int x,int y){
int ans=0;
x=n-x+1;
for(;x;x-=x&-x)
ans=max(ans,t[x].query(0,n,y,n));
return ans;
}
int far[N][3],md[N],mu[N];
bool cmp1(int a,int b){
return dep[a]>dep[b];
}
void dfs1(int u,int f){
vector<int> tmp;
tmp.push_back(u);
tmp.push_back(u);
tmp.push_back(u);
for(int v:G[u]){
if(v==f)continue;
dfs1(v,u);
if(far[v][0])tmp.push_back(far[v][0]);
}
sort(tmp.begin(),tmp.end(),cmp1);
for(int i=0;i<tmp.size()&&i<3;i++)far[u][i]=tmp[i];
}
int dis(int a,int b){
assert(a);
assert(b);
return dep[a]+dep[b]-2*dep[lca(a,b)];
}
void dfs2(int u,int f){
if(f){
for(int i=0;i<3;i++)
if(find_subtree(f,far[f][i])!=u){
mu[u]=far[f][i];
break;
}
mu[u]=dis(u,mu[u])>dis(u,mu[f])?mu[u]:mu[f];
}else{
mu[u]=u;
}
for(int v:G[u]){
if(v==f)continue;
dfs2(v,u);
}
int k=mu[u];
for(int i=0;i<3;i++)
if(dis(u,k)>dis(u,far[u][i]))swap(k,far[u][i]);
}
struct P{
int u;
int a[3];
} ps[N];
struct Query{
int a[3],b[3],id;
} qs[N];
int reses[N][3];
bool cmp2(Query a,Query b){
return a.b[0]>b.b[0];
}
bool cmp3(P a,P b){
return a.a[0]>b.a[0];
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<n;i++){
int u,v;
cin>>u>>v;
G[u].push_back(v);
G[v].push_back(u);
}
lca_pre(1,0);
dfs1(1,0);
dfs2(1,0);
for(int i=1;i<=n;i++){
ps[i].u=i;
for(int j=0;j<3;j++)ps[i].a[j]=dis(i,far[i][j]);
}
sort(ps+1,ps+1+n,cmp3);
int q;
cin>>q;
for(int i=1;i<=q;i++){
cin>>qs[i].a[0]>>qs[i].a[1]>>qs[i].a[2];
int t=(qs[i].a[0]+qs[i].a[1]+qs[i].a[2])/2;
for(int j=0;j<3;j++)qs[i].b[j]=t-qs[i].a[j];
sort(qs[i].b,qs[i].b+3,greater<int>());
qs[i].id=i;
}
sort(qs+1,qs+1+q,cmp2);
int p=1;
for(int i=1;i<=q;i++){
while(p<=n&&ps[p].a[0]>=qs[i].b[0]){
add(ps[p].a[1],ps[p].a[2],ps[p].u);
p++;
}
int u=ask(qs[i].b[1],qs[i].b[2]);
int *r=reses[qs[i].id];
for(int j=0;j<3;j++){
if(fa[find_subtree(u,far[u][j])][0]==u){
r[j]=jump_up(far[u][j],dep[u]+qs[i].b[j]);
}else{
int l=lca(far[u][j],u);
if(qs[i].b[j]>dep[u]-dep[l])r[j]=jump_up(far[u][j],dep[l]+qs[i].b[j]-(dep[u]-dep[l]));
else r[j]=jump_up(u,dep[u]-qs[i].b[j]);
}
}
for(int j=0;j<6;j++){
if(dis(r[0],r[1])==qs[i].a[0]&&dis(r[0],r[2])==qs[i].a[1]&&dis(r[1],r[2])==qs[i].a[2])break;
next_permutation(r,r+3);
}
}
for(int i=1;i<=q;i++){
cout<<reses[i][0]<<" "<<reses[i][1]<<" "<<reses[i][2]<<endl;
}
return 0;
}
详细
Test #1:
score: 10
Accepted
time: 6ms
memory: 19964kb
input:
500 279 196 182 105 400 14 278 217 198 411 379 318 120 421 314 95 437 325 23 433 71 485 192 154 90 224 180 71 328 93 183 101 404 349 223 285 492 100 366 480 29 328 200 448 365 156 226 231 129 167 246 273 171 452 284 6 131 471 229 90 480 48 448 157 240 221 7 36 401 200 255 438 436 110 281 489 388 258...
output:
125 285 94 28 222 451 478 342 285 126 480 101 69 395 421 207 492 343 126 48 2 178 311 497 458 69 215 190 345 174 306 497 356 183 74 270 154 492 311 124 342 399 17 333 285 497 48 356 355 345 28 345 35 456 154 35 253 1 200 138 497 125 492 227 262 78 68 399 332 136 210 344 390 73 78 492 342 92 242 125 ...
result:
ok Accepted!
Test #2:
score: 10
Accepted
time: 9ms
memory: 20136kb
input:
2000 643 1871 689 23 1822 1443 1031 1027 1655 845 189 771 1561 498 19 1778 576 1080 904 717 1690 291 1387 1077 398 811 1310 1231 645 1291 480 927 330 170 1464 1057 1033 894 1308 288 1292 1529 1212 122 1108 401 89 1118 1058 1088 1764 1314 981 1255 1893 864 180 1887 1903 843 734 1412 883 1013 1739 124...
output:
890 1985 1416 474 546 688 1454 1897 409 25 1826 1985 1686 581 1992 1371 1093 913 798 1262 1738 287 1094 1179 696 1047 1083 38 158 118 866 1686 1238 1495 261 1201 42 873 298 524 1803 1565 1757 1803 414 1104 1992 362 1700 1992 581 1127 1094 121 564 1090 87 1445 1134 722 581 1237 875 295 1122 727 1751 ...
result:
ok Accepted!
Test #3:
score: 10
Accepted
time: 732ms
memory: 46040kb
input:
200000 56968 132021 105656 107536 123627 58349 119191 138198 133708 142638 114350 24980 137784 40346 124158 130204 80684 183207 78156 94449 21893 157560 54464 73571 145830 57756 160288 32120 178632 142663 26565 185985 70576 24906 32217 115826 185756 137673 54280 179613 77826 144447 66005 29955 11745...
output:
49216 166995 114956
result:
ok Accepted!
Test #4:
score: 10
Accepted
time: 711ms
memory: 43108kb
input:
200000 41999 100683 85781 129266 122431 179332 162416 44814 24405 42267 154161 12483 178049 159964 67625 152391 133072 25866 178005 14695 94384 170290 54701 40323 66280 128515 159022 55057 14985 12920 182805 40659 173117 67973 99771 26102 198919 94543 23608 187601 61125 5759 89437 47647 18142 192402...
output:
175810 46532 145940
result:
ok Accepted!
Test #5:
score: 10
Accepted
time: 726ms
memory: 47028kb
input:
200000 195072 75458 31991 127114 60943 49502 186375 1130 45394 147217 168455 84307 132752 188952 101108 130004 107490 22003 16275 187645 111002 42669 138880 137115 112688 172751 81697 99037 166996 18495 2234 56119 170807 101349 105736 23180 148159 40863 136678 11849 190707 91245 61779 120740 157115 ...
output:
180520 9346 135354 158646 80535 198034 89804 171254 94007 123714 184299 81244 148655 16000 5609 58386 126517 158508 97550 172287 96137 74130 81905 96258 50011 196948 137585 75063 93583 169593
result:
ok Accepted!
Test #6:
score: 10
Accepted
time: 754ms
memory: 46248kb
input:
200000 48556 78408 155026 9376 8983 61211 150393 85068 90892 109283 75746 89742 6760 187245 168658 130735 68602 127646 60802 149828 22898 59471 172845 100274 42303 190696 7612 134905 94702 59800 129633 192496 19903 64869 51318 63358 34692 66030 98535 176606 153647 177529 157903 147292 106273 107278 ...
output:
172960 7414 113000 105926 5911 67376 145762 163725 149803 150834 80567 138788 13622 59883 167844 71639 15272 162792 98031 123390 198221 88030 46058 177036 19508 69264 76493 154594 187048 86508
result:
ok Accepted!
Test #7:
score: 0
Time Limit Exceeded
input:
200000 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 52 5...
output:
result:
Test #8:
score: 0
Time Limit Exceeded
input:
200000 5732 55198 128966 25317 114737 116391 150003 18274 43867 70745 76222 4169 55976 114951 198396 72896 38647 19711 12756 172119 73197 117994 117512 14177 130965 126990 119440 183341 142023 60829 111893 57350 122754 123305 36525 79077 36447 91967 135405 170456 165839 147481 66074 175822 22238 264...
output:
199602 199602 101960 199602 199602 116854 199602 199602 181923 199602 199602 121667 199602 199602 186490 199602 199602 124494 199602 199602 45118 199602 199602 30403 199602 199602 153560 199602 199602 129388 199602 199602 36035 199602 199602 14824 199602 199602 165111 199602 199602 58548 199602 1996...
result:
Test #9:
score: 0
Time Limit Exceeded
input:
200000 185063 17064 180987 114492 88071 71803 158363 135918 60910 54848 97338 6734 192937 9410 49617 199068 82499 63554 188791 188152 178767 40866 11304 27212 144234 138097 42236 3946 103355 12683 50992 20598 145723 48620 11709 115688 123172 121379 70541 130844 147827 39431 139372 61280 42705 54015 ...
output:
result:
Test #10:
score: 0
Time Limit Exceeded
input:
200000 197244 185999 18639 124754 154223 12099 53676 167616 22710 22294 150412 66132 19320 75478 170410 122661 130961 175554 171586 85572 188386 81238 120249 117687 43214 126266 8744 165654 164725 189519 124144 170329 86605 100197 130545 17030 113301 96665 67733 187286 37846 146399 75352 117550 3235...