QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#688457#8235. Top Clusterxxk2006RE 4ms15780kbC++232.6kb2024-10-30 09:32:052024-10-30 09:32:09

Judging History

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

  • [2024-10-30 09:32:09]
  • 评测
  • 测评结果:RE
  • 用时:4ms
  • 内存:15780kb
  • [2024-10-30 09:32:05]
  • 提交

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;
    int z=lg[fi[y]-fi[x]+1];
    z=min(st[fi[x]][z],st[fi[y]-(1<<z)+1][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<=2*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);
        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;
}

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 15780kb

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:


result: