QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#423652#8235. Top ClusterlmeowdnWA 1752ms491676kbC++143.9kb2024-05-28 14:40:472024-05-28 14:40:47

Judging History

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

  • [2024-05-28 14:40:47]
  • 评测
  • 测评结果:WA
  • 用时:1752ms
  • 内存:491676kb
  • [2024-05-28 14:40:47]
  • 提交

answer

//Shirasu Azusa 2024.5
#include <bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define mp make_pair
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
template<class T,class S>
bool chmax(T &a,const S b) {return (a<b?a=b,1:0);}
template<class T,class S>
bool chmin(T &a,const S b) {return (a>b?a=b,1:0);}
int popcnt(int x) {return __builtin_popcount(x);}
int popcnt(ll x)  {return __builtin_popcountll(x);}
int topbit(int x) {return (x==0?-1:31-__builtin_clz(x));}
int topbit(ll x)  {return (x==0?-1:63-__builtin_clzll(x));}
int lowbit(int x) {return (x==0?-1:__builtin_ctz(x));}
int lowbit(ll x)  {return (x==0?-1:__builtin_ctzll(x));}

#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
typedef pair<int,int> pii;  
typedef vector<int> vi;
typedef vector<pii> vp;
typedef tuple<int,int,int> tiii;
int read() {
  int x=0,w=1; char c=getchar(); 
  while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
  while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();} 
  return x*w;
}

const int N=5e5+5,inf=0x3f3f3f3f3f3f3f3f;
int dfn[N],tick,n,m,s[N],rr[N],f[N],g[N],cnt,x[N],k[N],id[N],a[N],ans[N],d[N];
vp e[N]; vi t[N];

bool cmp(const int &x,const int &y) {return dfn[x]<dfn[y];}
bool cmq(const int &a,const int &b) {return dfn[x[a]]<dfn[x[b]];}
int getg(int x,int y) {return dfn[x]<dfn[y]?x:y;}
int fr[22][N];
int lca(int u,int v) {
  if(u==v) return u; u=dfn[u], v=dfn[v]; if(u>v) swap(u,v);
  u++; int d=__lg(v-u+1); return getg(fr[d][u],fr[d][v-(1<<d)+1]);
}

void dfs(int u,int fa) {
  fr[0][dfn[u]=++tick]=fa;
  for(auto [v,w]:e[u]) if(v!=fa) s[v]=s[u]+w, d[v]=d[u]+1, dfs(v,u);
}
void pre() {
  rep(d,1,19) rep(i,1,n-(1<<d)+1) fr[d][i]=getg(fr[d-1][i],fr[d-1][i+(1<<d-1)]);
}
void work(vi a) {
  static int st[N]; int top=1; st[1]=1; vi use; use.eb(1);
  int sz=unique(a.begin(),a.end())-a.begin(); a.resize(sz);
  for(int x:a) if(x!=1) {
    int y=lca(x,st[top]);
    if(y==st[top]) st[++top]=x, use.eb(x);
    else {
      while(top>1&&d[st[top-1]]>d[y]) t[st[top-1]].eb(st[top]), top--;
      t[y].eb(st[top]), top--;
      if(st[top]!=y) assert(d[st[top]]<d[y]), st[++top]=y, use.eb(y);
      st[++top]=x, use.eb(x);
    }
  }
  while(top>1) t[st[top-1]].eb(st[top]), top--;
  for(int x:use) f[x]=g[x]=-inf;
  vi b=use; reverse(b.begin(),b.end());
  cnt=0; for(int x:a) if(rr[x]) g[x]=f[x]=0, cnt++;
  for(int u:b) {
    for(int v:t[u]) chmax(f[u],f[v]+s[v]-s[u]);
  }
  for(int u:use) {
    int mx=-inf, pmx=0, smx=-inf;
    for(int v:t[u]) {
      int gg=f[v]+s[v]-s[u];
      if(gg>mx) smx=mx, pmx=v, mx=gg;
      else chmax(smx,gg);
    }
    for(int v:t[u]) {
      int gg=max(g[u],(pmx==v?smx:mx))+s[v]-s[u];
      chmax(g[v],gg);
    }
  }
  for(int x:use) t[x].clear(), rr[x]=0;
}

void solve(int l,int r,vi p,vi q) {
  if(!q.size()) return;
  if(l==r) {
    for(int x:q) ans[x]=l;
    return;
  } int mid=l+r>>1; vi pl,pr,ql,qr;
  for(int x:p) {
    if(a[x]<=mid) pl.eb(x); else pr.eb(x);
  }
  if(pl.size()<mid-l+1) {
    solve(l,mid,pl,q); return;
  }
  vi ra=pl,rb,rc; for(int j:q) rb.eb(x[j]);
  rc.resize(ra.size()+rb.size());
  merge(ra.begin(),ra.end(),rb.begin(),rb.end(),rc.begin(),cmp);
  for(int x:pl) rr[x]=1; work(rc);
  for(int i:q) {
    int w=max(f[x[i]],g[x[i]]);
    if(w<=k[i]) qr.eb(i); else ql.eb(i);
  }
  solve(l,mid,pl,ql), solve(mid+1,r,pr,qr);
}

signed main() {
  n=read(), m=read();
  rep(i,1,n) a[i]=read();
  rep(i,2,n) {
    int u=read(), v=read(), w=read();
    e[u].eb(v,w), e[v].eb(u,w);
  }
  dfs(1,0); pre();
  rep(i,1,m) {
    x[i]=read(), k[i]=read(), id[i]=i;
  }
  vi ap,aq;
  rep(i,1,n) ap.eb(i);
  rep(i,1,m) aq.eb(i);
  sort(ap.begin(),ap.end(),cmp);
  sort(aq.begin(),aq.end(),cmq);
  solve(0,n,ap,aq);
  rep(i,1,m) printf("%lld\n",ans[i]);
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1752ms
memory: 491676kb

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'