QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#688482 | #8235. Top Cluster | xxk2006 | 0 | 529ms | 141240kb | C++23 | 2.9kb | 2024-10-30 09:54:54 | 2024-10-30 09:54:55 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define Enter putchar('\n')
#define spc putchar(' ')
#define pb push_back
#define fi first
#define se second
inline void read(int &num){num=0;int f=1;char ch=getchar();while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){num=(num<<1)+(num<<3)+(ch^48);ch=getchar();}num*=f;}
inline void lread(long long &num){num=0;int f=1;char ch=getchar();while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){num=(num<<1)+(num<<3)+(ch^48);ch=getchar();}num*=f;}
void print(long long num){if(num<0){putchar('-');num=-num;}if(num>9){print(num/10);}putchar((num%10)^48);}
const int N=5e5+9;
int a[N],id[N],cnt,fi[N],st[N<<1][20],lg[N<<1],dm[N][2],vi[N<<1];
struct node{
int v;
LL w;
};
LL dp[N];
vector<node> g[N];
bool cmp(const int &x,const int &y){
return a[x]<a[y];
}
void dfs(int u,int fr){
fi[u]=++cnt;
st[cnt][0]=cnt;
vi[cnt]=u;
for(int i=0;i<g[u].size();i++){
if(g[u][i].v==fr)continue;
dp[g[u][i].v]=dp[u]+g[u][i].w;
dfs(g[u][i].v,u);
st[++cnt][0]=fi[u];
vi[cnt]=u;
}
}
int ask(int x,int y){
if(!x||!y)return 0;
if(fi[x]>fi[y])swap(x,y);
int z=lg[fi[y]-fi[x]+1];
if(a[1]==416779)cout<<fi[y]<<' '<<fi[x]<<' '<<z<<':';
z=min(st[fi[x]][z],st[fi[y]-(1<<z)+1][z]);
if(a[1]==416779)cout<<z<<'#'<<dp[x]+dp[y]-dp[vi[z]]-dp[vi[z]];
return dp[x]+dp[y]-dp[vi[z]]-dp[vi[z]];
}
int main(){
int n,Q,x,y,z,mx=0;
LL s;
read(n),read(Q);
for(int i=1;i<=n;i++)read(a[i]),id[i]=i;
sort(id+1,id+n+1,cmp);
for(int i=1;i<=n;i++){
if(a[id[i]]==mx)++mx;
else break;
}
for(int i=1;i<n;i++){
read(x),read(y),read(z);
g[x].pb({y,z});
g[y].pb({x,z});
}
dfs(1,0);
for(int i=1,j=1;i<=19;i++,j<<=1){
for(int k=1;k+j+j<=n+n;k++){
st[k][i]=min(st[k][i-1],st[k+j][i-1]);
}
}
for(int i=2;i<=1e6;i++)lg[i]=lg[i>>1]+1;
dm[2][0]=dm[1][0]=id[1];
dm[2][1]=id[2];
for(int i=3;i<=mx;i++){
x=ask(dm[i-1][0],dm[i-1][1]);
y=ask(dm[i-1][0],id[i]);
z=ask(dm[i-1][1],id[i]);
if(x>=y&&x>=z){
dm[i][0]=dm[i-1][0],dm[i][1]=dm[i-1][1];
continue;
}
if(y>z){
dm[i][0]=dm[i-1][0],dm[i][1]=id[i];
}
else{
dm[i][1]=dm[i-1][1],dm[i][0]=id[i];
}
}
while(Q--){
read(x),lread(s);
if(a[1]==416779){
ask(x,id[1]);
cout<<endl;
}
int l=0,r=mx,mid;
while(l<r){
mid=(l+r+1)>>1;
if(max(ask(x,dm[mid][0]),ask(x,dm[mid][1]))<=s){
l=mid;
}
else r=mid-1;
}
print(l),Enter;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15920kb
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: 0
Accepted
time: 529ms
memory: 141240kb
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:
0 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 2499...
result:
ok 500000 numbers
Test #3:
score: -100
Output Limit Exceeded
input:
500000 500000 416779 59604 366180 195604 4 30957 999969109 7476 352690 368624 121597 999960303 999933891 13 14 138579 294015 227392 106760 117837 208506 999997971 34770 40258 182765 65889 206246 233051 130491 182099 117381 241945 449750 155921 356191 999955435 2243 450904 242106 178163 148523 75648 ...
output:
951395 270592 19:168258#92717434682951395 377304 19:168258#68116783970377304 270592 16:168258#67668204822951395 377304 19:168258#68116783970951395 60152 19:25987#84458348075377304 60152 18:25987#59409118215951395 377304 19:168258#68116783970951395 842602 16:748257#66399075189842602 377304 18:168258#...