QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#770492#5439. Meet in the MiddleASnownWA 34ms25544kbC++173.6kb2024-11-21 22:05:242024-11-21 22:05:25

Judging History

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

  • [2024-11-21 22:05:25]
  • 评测
  • 测评结果:WA
  • 用时:34ms
  • 内存:25544kb
  • [2024-11-21 22:05:24]
  • 提交

answer

#include<bits/stdc++.h>
#define file(F) freopen(#F".in","r",stdin),freopen(#F".out","w",stdout)
#define lowbit(x) ((x)&-(x))
#define ALL(x) (x).begin(),(x).end()
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define popc(x) (__builtin_popcountll((x)))
#define abs(x) ((x)>=0?(x):-(x))
using namespace std;
#define int long long
using ll=long long;
using uint=uint32_t;
template<typename T>
inline bool Max(T &x,T y) { return std::less<T>()(x,y)&&(x=y,true); }
template<typename T>
inline bool Min(T &x,T y) { return std::less<T>()(y,x)&&(x=y,true); }
void Solve();
const int N = 1e5+15;
int n,Q;
vector<pair<int,int>> e1[N],e2[N];
namespace GETLCA {
int dfn[N],dfx=0;
ll dep[N];
int st[N][20];
void dfs(int u,int fa) {
   st[dfn[u]=++dfx][0]=fa;
   for(auto [v,w]:e2[u]) if(v!=fa) 
      dep[v]=dep[u]+w,dfs(v,u);
}
void init() {
   dfs(1,0);
   for(int l=0;l<19;l++) for(int i=1;i+(1<<l+1)-1<=n;i++)
      st[i][l+1]=min(st[i][l],st[i+(1<<l)][l],
       [&](int a,int b){ return dfn[a]<dfn[b]; });
}
int LCA(int x,int y) {
   if(x==y) return x;
   if((x=dfn[x])>(y=dfn[y])) swap(x,y);
   int l=__lg(y-x++);
   return min(st[x][l],st[y-(1<<l)+1][l],
    [&](int a,int b){ return dfn[a]<dfn[b]; });
}
inline ll dis(int x,int y) { return dep[x]+dep[y]-2*dep[LCA(x,y)]; }
} using GETLCA::LCA; using GETLCA::dis;
int dfn[N],pos[N],dfx,ed[N];
ll dep[N];
void dfs(int u,int fa) {
   pos[dfn[u]=++dfx]=u;
   for(auto [v,w]:e1[u]) if(v!=fa)
      dep[v]=dep[u]+w,dfs(v,u);
   ed[u]=dfx;
}
namespace SGT {
const int N = ::N<<2;
#define mid ((L+R)>>1)
#define ls (u<<1)
#define rs (ls|1)
struct Data {
   int a,b; ll wa,wb;
   static inline auto calc(const Data &x) {
      return x.a==x.b?0:dis(x.a,x.b)+x.wa+x.wb;
   };
   inline bool operator<(const Data &p) const {
      return calc(*this)<calc(p);
   }
   friend auto operator+(const Data &p,const Data &q) {
      Data T=max(p,q);
      Max(T,{p.a,q.a,p.wa,q.wa});
      Max(T,{p.a,q.b,p.wa,q.wb});
      Max(T,{p.b,q.a,p.wb,q.wa});
      Max(T,{p.b,q.b,p.wb,q.wb});
      return T;
   }
} mx[N];
ll tag[N];
void pushup(int u) { mx[u]=mx[ls]+mx[rs]; }
void clac(int u,ll k) { tag[u]+=k,mx[u].wa+=k,mx[u].wb+=k; }
void pushdown(int u) { clac(ls,tag[u]),clac(rs,tag[u]),tag[u]=0; }
void build(int u,int L,int R) {
   if(L==R) {
      int x=pos[L];
      mx[u]={x,x,dep[x],dep[x]};
      return ;
   }
   build(ls,L,mid),build(rs,mid+1,R);
   pushup(u);
}
void update(int u,int L,int R,int l,int r,ll k) {
   if(l<=L&&R<=r) return clac(u,k);
   pushdown(u);
   if(l<=mid) update(ls,L,mid,l,r,k);
   if(r> mid) update(rs,mid+1,R,l,r,k);
   pushup(u);
}
}
vector<pair<int,int>> q[N];
ll ans[N*5];
void dfs1(int u,int fa) {
   auto res=SGT::mx[1];
   for(auto [v,id]:q[u]) ans[id]=max(dis(v,res.a)+res.wa,dis(v,res.b)+res.wb);
   for(auto [v,w]:e1[u]) if(v!=fa) {
      SGT::update(1,1,n,1,n,+w);
      SGT::update(1,1,n,dfn[u],ed[u],-w-w);
      dfs1(v,u);
      SGT::update(1,1,n,1,n,-w);
      SGT::update(1,1,n,dfn[u],ed[u],+w+w);
   }
}
signed main() {
#ifndef ONLINE_JUDGE
#endif
   cin.tie(nullptr)->sync_with_stdio(false);
   Solve();
}
void Solve() {
   cin>>n>>Q;
   for(int i=1;i<n;i++) {
      int u,v,w; cin>>u>>v>>w;
      e1[u].emplace_back(v,w),e1[v].emplace_back(u,w);
   }
   for(int i=1;i<n;i++) {
      int u,v,w; cin>>u>>v>>w;
      e2[u].emplace_back(v,w),e2[v].emplace_back(u,w);
   }
   for(int i=1;i<=Q;i++) {
      int a,b; cin>>a>>b;
      q[a].emplace_back(b,i);
   }
   GETLCA::init();
   dfs(1,0);
   SGT::build(1,1,n);
   dfs1(1,0);
   for(int i=1;i<=Q;i++) printf("%lld\n",ans[i]);
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 17920kb

input:

3 4
1 2 1
2 3 2
1 2 2
2 3 1
1 1
1 2
2 1
2 2

output:

6
4
5
3

result:

ok 4 number(s): "6 4 5 3"

Test #2:

score: 0
Accepted
time: 4ms
memory: 16900kb

input:

2 1
1 2 1
1 2 1
1 1

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 3ms
memory: 19992kb

input:

2 1
1 2 1
1 2 1
1 2

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: -100
Wrong Answer
time: 34ms
memory: 25544kb

input:

10000 50000
8101 5996 108427744
5996 7605 870838849
5996 5599 603303696
8101 3006 339377417
8101 6463 442516687
6463 5560 109174079
5560 4063 127596224
3006 1682 947915262
5996 1986 130416311
6463 5455 279771516
6463 2634 516688796
4063 3034 217886863
7605 5092 742375061
5599 2266 193804402
5092 140...

output:

647769169470
625497854878
514273941704
635075564184
471040550366
643852087006
640842849682
573604504956
644012360096
801884622512
673675540410
732773217962
744167805865
763083383940
552804875584
526048815183
609678709938
688855237466
548466293246
724820609206
652439234544
665719927466
699584565018
6...

result:

wrong answer 1st numbers differ - expected: '647838384844', found: '647769169470'