QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#602639 | #8235. Top Cluster | xlwang | RE | 0ms | 20092kb | C++14 | 3.5kb | 2024-10-01 11:26:47 | 2024-10-01 11:26:51 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define int long long
#define fr(i,j,k) for(register int i=j;i<=k;++i)
#define rf(i,j,k) for(register int i=j;i>=k;--i)
#define foredge(i,j) for(register int i=head[j];i;i=e[i].nxt)
#define pb push_back
#define Times printf("Time:%.3lf\n",clock()/CLOCKS_PER_SEC)
#define pii pair<int,int>
#define mk make_pair
using namespace std;
inline int read(){
int x=0;
bool f=0;
char c=getchar();
while(!isdigit(c)) f|=(c=='-'),c=getchar();
while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
return f?-x:x;
}
inline void write(int x){
if(x<0){putchar('-');x=-x;}
if(x>9)write(x/10);
putchar(x%10+'0');
}
inline void writeln(int x){write(x); puts("");}
inline void writepl(int x){write(x); putchar(' ');}
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline int randfind(int l,int r){return rnd()%(r-l+1)+l;}
//inline void init(){
// int t=read();
// while(t--) work();
//}
const int Maxn=5e5+10;
int n,q;
struct node{
int nxt,to,d;
}e[Maxn<<1];
int head[Maxn],cnt;
inline void add(int u,int v,int w){++cnt;e[cnt]=(node){head[u],v,w};head[u]=cnt;}
namespace LCA{
int dfn[Maxn],f[25][Maxn<<2],dep[Maxn];
int lg[Maxn<<2],idx,s[Maxn];
inline int getmin(int x,int y){if(dep[x]<dep[y]) return x;return y;}
inline void dfs(int x,int fa){
dep[x]=dep[fa]+1;dfn[x]=++idx;f[0][idx]=x;
for(register int i=head[x];i;i=e[i].nxt){
int y;y=e[i].to;
if(y!=fa) s[y]=s[x]+e[i].d,dfs(y,x),f[0][++idx]=x;
}
}
inline int getlca(int x,int y){
x=dfn[x],y=dfn[y];
if(x>y) swap(x,y);
int ln=lg[y-x+1];
return getmin(f[ln][x],f[ln][y-(1<<ln)+1]);
}
inline int getdis(int x,int y){
if(x==0 || y==0) return 0;
return s[x]+s[y]-2*s[getlca(x,y)];
}
inline void init(){
dfs(1,0);
if(n==500000) cout<<idx<<endl;
lg[0]=-1;fr(i,1,idx) lg[i]=lg[i/2]+1;
fr(j,1,lg[idx]) fr(i,1,idx){
if(i+(1<<j)-1>idx) break;
f[j][i]=getmin(f[j-1][i],f[j-1][i+(1<<(j-1))]);
}
}
}
int ans[Maxn];
int vis[Maxn];
int now;
inline int getmex(){while(vis[now]) ++now;return now;}
struct point{int val,id;}p[Maxn];
inline bool cmp(point a,point b){return a.val<b.val;}
inline void init(){
n=read();q=read();
fr(i,1,n) p[i].val=read(),p[i].id=i;
fr(i,1,n-1){
int x,y,z;
x=read();y=read();z=read();
add(x,y,z);add(y,x,z);
}LCA::init();
sort(p+1,p+n+1,cmp);
}
struct line{int id1,id2;}pr[Maxn];
bool operator < (line A,line B){return LCA::getdis(A.id1,A.id2)<LCA::getdis(B.id1,B.id2);}
inline bool check(int x,int id,int k){
return LCA::getdis(x,pr[id].id1)<=k && LCA::getdis(x,pr[id].id2)<=k;
}
inline void work(){
fr(i,1,n) vis[p[i].val]=1,ans[i]=getmex();
// fr(i,)
fr(i,1,n) pr[i].id1=p[i].id;
fr(i,2,n){
pr[i]=pr[i-1];
line now=(line){pr[i].id1,p[i].id};
if(pr[i]<now) pr[i]=now;
now=(line){pr[i].id2,p[i].id};
if(pr[i]<now) pr[i]=now;
}
while(q--){
int x,k;
x=read();k=read();
int l=1,r=n,answer=0;
while(l<=r){
int mid;mid=(l+r)>>1;
if(check(x,mid,k)) answer=mid,l=mid+1;
else r=mid-1;
}writeln(ans[answer]);
}
}
signed main(){
// freopen("input.in","r",stdin);
// freopen("output.out","w",stdout);
init();work();
// printf("\nTIME:%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 20092kb
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...
output:
999999