QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#773929#5439. Meet in the MiddleWaterSunWA 29ms23928kbC++143.8kb2024-11-23 10:52:582024-11-23 10:53:01

Judging History

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

  • [2024-11-23 10:53:01]
  • 评测
  • 测评结果:WA
  • 用时:29ms
  • 内存:23928kb
  • [2024-11-23 10:52:58]
  • 提交

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;
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{
    #define pot(x) (1 << (x))

    const int M = 24;
    int tim,lg[N],dfn[N],dep[N],dp[N][M];

    inline void dfs(int u,int fa){
        dp[dfn[u] = ++tim][0] = dep[u];
        for (pair<int,int> v:e2[u]){
            if (v.first == fa) continue;
            dep[v.first] = dep[u] + v.second;
            dfs(v.first,u);
        }
        dp[++tim][0] = dep[u];
    }

    inline void init(){
        dfs(1,0);
        for (int i = 2;i <= tim;i++) lg[i] = lg[i >> 1] + 1;
        for (int j = 1;j <= lg[tim];j++){
            for (int i = 1;i + pot(j) - 1 <= tim;i++) dp[i][j] = min(dp[i][j - 1],dp[i + pot(j - 1)][j - 1]);
        }
    }

    inline int LCA(int x,int y){
        int sx = dfn[x],sy = dfn[y];
        if (sx > sy) swap(sx,sy);
        int t = lg[sy - sx + 1];
        return min(dp[sx][t],dp[sy - pot(t) + 1][t]);
    }

    inline int dis(int x,int y){
        return (dep[x] + dep[y] - 2 * LCA(x,y));
    }

    #undef pot
}
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 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);
   mx[u]=mx[ls]+mx[rs];
}
void update(int u,int L,int R,int l,int r,ll k) {
   if(l<=L&&R<=r) return void((tag[u]+=k,mx[u].wa+=k,mx[u].wb+=k));
   if(l<=mid) update(ls,L,mid,l,r,k);
   if(r> mid) update(rs,mid+1,R,l,r,k);
   mx[u]=mx[ls]+mx[rs];
   mx[u].wa+=tag[u],mx[u].wb+=tag[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[v],ed[v],-w-w);
      dfs1(v,u);
      SGT::update(1,1,n,1,n,-w);
      SGT::update(1,1,n,dfn[v],ed[v],+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]);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 17380kb

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: 0ms
memory: 18548kb

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: 29ms
memory: 23928kb

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:

370323323316
280536273149
271349636781
383939747069
281161682225
394097658208
350276358642
312111839254
371092499475
505116571695
322979626724
401645428777
410017125377
506242114780
279418253728
317078082663
365685235112
363837002219
322232489773
436642492981
348262010357
355118210303
521407819282
3...

result:

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