QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#354990 | #8235. Top Cluster | ucup-team3160# | RE | 0ms | 20100kb | C++14 | 7.0kb | 2024-03-16 10:25:20 | 2024-03-16 10:25:21 |
Judging History
answer
/*
author: honglan0301
Sexy_goodier _ xiaoqing
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cctype>
#include <queue>
#include <map>
#include <unordered_map>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <cmath>
#include <random>
#include <set>
#include <bitset>
#include <assert.h>
using namespace std;
//namespace Fread{const int SIZE=1<<20;char buf[SIZE],*S,*T;inline char getchar(){if(S==T){T=(S=buf)+fread(buf,1,SIZE,stdin);if(S==T)return'\n';}return*S++;}}using namespace Fread;namespace Fwrite{const int SIZE=1<<20;char buf[SIZE],*S=buf,*T=buf+SIZE;inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}inline void putchar(char c){*S++=c;if(S==T)flush();}struct NTR{~NTR(){flush();}}ztr;}using namespace Fwrite;
//#define getchar Fread::getchar
//#define putchar Fwrite::putchar
namespace Fastio{struct Reader{template<typename T>Reader&operator>>(T&x){x=0;short f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();x*=f;return*this;}Reader&operator>>(double&x){x=0;double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(long double&x){x=0;long double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(__float128&x){x=0;__float128 t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(char&c){c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();return*this;}Reader&operator>>(char*str){int len=0;char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str[len++]=c,c=getchar();str[len]='\0';return*this;}Reader&operator>>(string&str){str.clear();char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str.push_back(c),c=getchar();return*this;}Reader(){}}cin;const char endl='\n';struct Writer{const int Setprecision=6;typedef int mxdouble;template<typename T>Writer&operator<<(T x){if(x==0){putchar('0');return*this;}if(x<0)putchar('-'),x=-x;static short sta[40];short top=0;while(x>0)sta[++top]=x%10,x/=10;while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(long double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(long double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(__float128 x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(__float128)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(char c){putchar(c);return*this;}Writer&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(string str){int st=0,ed=str.size();while(st<ed)putchar(str[st++]);return*this;}Writer(){}}cout;}using namespace Fastio;
#define cin Fastio::cin
#define cout Fastio::cout
#define endl Fastio::endl//;fflush(stdout)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define int long long
#define ull unsigned long long
#define mod 1000000007
mt19937 rnd(time(0));
mt19937_64 rndl(time(0));
int n,q,a[500005],wz[500005],ans[500005],u,v,w,dep[500005],qu[500005],qv[500005];
int dfn[500005],cntd,st[21][500005],lg[500005];
vector <pair<int,int>> e[500005];
void dfs1(int x,int fat)
{
st[0][dfn[x]=++cntd]=fat;
for(auto i:e[x]) {if(i.fi==fat) continue; dep[i.fi]=dep[x]+i.se; dfs1(i.fi,x);}
}
int getm(int x,int y) {return dfn[x]<dfn[y]?x:y;}
bool cmp(int x,int y) {return dfn[x]<dfn[y];}
void init()
{
for(int i=1;(1<<i)<=n;i++) for(int j=(1<<i);j<=(1<<(i+1))-1;j++) lg[j]=i;
for(int i=1;i<=lg[n];i++) for(int j=1;j+(1<<i)-1<=n;j++) st[i][j]=getm(st[i-1][j],st[i-1][j+(1<<i-1)]);
}
int lca(int x,int y)
{
if(x==y) return x; if((x=dfn[x])>(y=dfn[y])) swap(x,y);
int dd=lg[y-x++]; return getm(st[dd][x],st[dd][y-(1<<dd)+1]);
}
int dis(int x,int y) {return dep[x]+dep[y]-2*dep[lca(x,y)];}
int nd[1000005],cntb,gj[500005],dp[500005],stk[500005],top;
struct dat
{
int u,v,d;
friend dat operator+(dat x,dat y)
{
dat z; z=x; if(y.d>x.d) z=y; int dd;
dd=dis(x.u,y.u); if(dd>z.d) z={x.u,y.u,dd};
dd=dis(x.u,y.v); if(dd>z.d) z={x.u,y.v,dd};
dd=dis(x.v,y.u); if(dd>z.d) z={x.v,y.u,dd};
dd=dis(x.v,y.v); if(dd>z.d) z={x.v,y.v,dd};
return z;
}
};
void solve(int l,int r,vector <int> nq)
{
vector <int> nl,nr; int mid=(l+r)>>1;
int flag=1; for(int i=l;i<=mid;i++) if(!wz[i]) flag=0;
cntb=0; for(auto i:nq) nd[++cntb]=qu[i]; for(int i=l;i<=mid;i++) if(wz[i]) nd[++cntb]=wz[i]; nd[++cntb]=1;
sort(nd+1,nd+cntb+1,cmp); cntb=unique(nd+1,nd+cntb+1)-nd-1; stk[top=1]=nd[1];
for(int i=1;i<=cntb;i++) dp[nd[i]]=0;
//for(int i=1;i<=cntb;i++) for(int j=l;j<=mid;j++) if(wz[j]) dp[nd[i]]=max(dp[nd[i]],dis(nd[i],wz[j]));
int nf=0; dat nzj;
for(int i=l;i<=mid;i++)
{
if(!wz[i]) continue;
if(!nf) nf=1,nzj={wz[i],wz[i],0};
else nzj=nzj+(dat){wz[i],wz[i],0};
}
for(auto i:nq)
{
//cout<<i<<" "<<qu[i]<<" "<<dp[qu[i]]<<endl;
dp[qu[i]]=max(dis(qu[i],nzj.u),dis(qu[i],nzj.v));
if(flag&&dp[qu[i]]<=qv[i]) ans[i]=mid,nr.pb(i); else nl.pb(i);
}
if(l==r) return; solve(l,mid,nl); solve(mid+1,r,nr);
}
signed main()
{
cin>>n>>q;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) if(a[i]<=n) wz[a[i]]=i;
for(int i=1;i<=n-1;i++) cin>>u>>v>>w,e[u].pb(mp(v,w)),e[v].pb(mp(u,w));
dfs1(1,1); init();
vector <int> nq;
for(int i=1;i<=q;i++) cin>>qu[i]>>qv[i],nq.pb(i),ans[i]=-1;
solve(0,n-1,nq);
for(int i=1;i<=q;i++) cout<<ans[i]+1<<endl;
}//
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 20100kb
input:
5 4 3 9 0 1 2 1 2 10 3 1 4 3 4 3 3 5 2 3 0 1 0 4 6 4 7
output:
1 0 3 4
result:
ok 4 number(s): "1 0 3 4"
Test #2:
score: -100
Runtime Error
input:
500000 500000 350828 420188 171646 209344 4 999941289 289054 79183 999948352 427544 160827 138994 192204 108365 99596 999987124 292578 2949 384841 269390 999920664 315611 163146 51795 265839 34188 999939494 145387 366234 86466 220368 357231 347706 332064 279036 173185 5901 217061 112848 37915 377359...