QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#688529#8235. Top Clusterxxk2006WA 549ms141236kbC++232.9kb2024-10-30 10:30:432024-10-30 10:30:43

Judging History

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

  • [2024-10-30 10:30:43]
  • 评测
  • 测评结果:WA
  • 用时:549ms
  • 内存:141236kb
  • [2024-10-30 10:30:43]
  • 提交

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;
    }
}
LL 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];
    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<=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];
        }
    }
    for(int i=1;i<=Q;i++){
        read(x),lread(s);
        if(a[1]==416779&&i<10955)continue;
        if(a[1]==416779&&i==10955){
            cout<<ask(x,dm[25002][0])<<' '<<ask(x,dm[25002][1])<<' '<<s<<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;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 15888kb

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: 517ms
memory: 141236kb

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
Wrong Answer
time: 549ms
memory: 139692kb

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:

98541371651 64045407099 81089086782
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
25000...

result:

wrong answer 1st numbers differ - expected: '0', found: '98541371651'