QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#544344 | #8235. Top Cluster | yanshanjiahong | WA | 830ms | 252964kb | C++20 | 2.8kb | 2024-09-02 15:22:16 | 2024-09-02 15:22:18 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define repp(i,j,k) for(int i=j;i>=k;i--)
#define ls(x) (x<<1)
#define rs(x) ((x<<1)|1)
#define mp make_pair
#define sec second
#define fir first
#define pii pair<int,int>
#define lowbit(i) i&-i
#define int long long
using namespace std;
typedef long long ll;
const int N=5e5+5,M=1e6+5,S=(1<<20)+5,inf=2e9+7,mo=998244853,base=620071201;
void read(int &p){
int x=0,w=1;
char ch=0;
while(!isdigit(ch)){
if(ch=='-')w=-1;
ch=getchar();
}
while(isdigit(ch)){
x=(x<<1)+(x<<3)+ch-'0';
ch=getchar();
}
p=x*w;
}
int n,q;
vector<pii>e[N];
pii a[N];
struct dir{
int x,y,len;
}d[N];
int eular[N*2],cnte,firp[N],dis[N];
struct ST_form{
int st[N*2][22],lg[N];
void init(){
rep(i,2,cnte)
lg[i]=lg[i/2]+1;
}
void build(){
init();
rep(i,1,cnte)
st[i][0]=eular[i];
rep(j,1,20){
rep(i,1,cnte){
st[i][j]=st[i][j-1];
if(i+(1<<(j-1))<=n)st[i][j]=min(st[i][j],st[i+(1<<(j-1))][j-1]);
}
}
}
int query(int x,int y){
if(x>y)swap(x,y);
int len=(y-x+1);
return min(st[x][lg[len]],st[y-(1<<lg[len])+1][lg[len]]);
}
}ST;
void dfs(int x,int f){
eular[++cnte]=x,firp[x]=cnte;
for(auto j:e[x]){
if(j.fir==f)continue;
dis[j.fir]=dis[x]+j.sec;
dfs(j.fir,x);
eular[++cnte]=x;
}
}
int lca(int x,int y){
return ST.query(firp[x],firp[y]);
}
int getdis(int x,int y){
return dis[x]+dis[y]-2ll*dis[lca(x,y)];
}
int upp=0;
bool check(int val,int x,int k){
int dx=d[val].x,dy=d[val].y;
if(n==500000&&val==1)printf("%lld %lld\n",dx,dy);
if(getdis(x,dx)>k||getdis(x,dy)>k)return 1;
return 0;
}
signed main(){
read(n),read(q);
rep(i,1,n)
read(a[i].fir),a[i].sec=i;
sort(a+1,a+n+1);
rep(i,1,n){
if(a[i].fir==i-1)upp=i;
else break;
}
rep(i,1,n-1){
int x,y,v;
read(x),read(y),read(v);
e[x].push_back(mp(y,v)),e[y].push_back(mp(x,v));
}
dfs(1,0);
ST.build();
d[1]=(dir){a[1].sec,a[1].sec,0};
rep(i,2,n){
int x=d[i-1].x,y=d[i-1].y,l=d[i-1].len;
d[i]=d[i-1];
int v1=getdis(x,a[i].sec),v2=getdis(y,a[i].sec);
if(v1>v2&&v1>l)d[i]=(dir){x,a[i].sec,v1};
else if(v2>=v1&&v2>l)d[i]=(dir){y,a[i].sec,v2};
}
while(q--){
int x,k;
read(x),read(k);
if(n==500000)printf("%lld %lld\n",x,k);
int le=1,ri=upp,ans=upp;
while(le<=ri){
int mid=(le+ri)>>1;
if(check(mid,x,k))ans=mid-1,ri=mid-1;
else le=mid+1;
}
printf("%lld\n",ans);
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 10036kb
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
Wrong Answer
time: 830ms
memory: 252964kb
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:
496531 0 133725 133725 1 315907 500000 249999 302983 46798 249999 94618 65440 249999 497379 107501 249999 87467 224492 249999 318784 270683 249999 60187 370988 249999 224933 302394 249999 410518 318001 249999 445087 281266 249999 223786 211602 249999 325886 161729 249999 138663 150663 249999 58486 3...
result:
wrong answer 1st numbers differ - expected: '0', found: '496531'