QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#544354#8235. Top ClusteryanshanjiahongWA 850ms256784kbC++202.7kb2024-09-02 15:29:522024-09-02 15:29:52

Judging History

你现在查看的是最新测评结果

  • [2024-09-02 15:29:52]
  • 评测
  • 测评结果:WA
  • 用时:850ms
  • 内存:256784kb
  • [2024-09-02 15:29:52]
  • 提交

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*2];
    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))<=cnte)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(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);
        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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 9900kb

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: 850ms
memory: 256784kb

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:

wrong answer 6721st numbers differ - expected: '0', found: '249999'