Submission #780217

# Submission time Handle Problem Language Result Execution time Memory
780217 2023-07-12T07:20:11 Z vjudge1 Factories (JOI14_factories) C++14
100 / 100
7919 ms 518960 KB
#include "factories.h"
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef stack<ll> sll;
typedef queue<ll> qll;
typedef deque<ll> dll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
 
// #define int long long
#define endl '\n'
#define pb push_back
#define FOR(i,a,b) for(int i = a; i <= b; i++)
#define BACK(i,a,b) for(int i = a; i >= b; i--)
#define fastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fi first
#define se second
#define bp __builtin_popcountll
#define gcd __gcd
#define bit(i,n) ((n>>i)&1)
#define setmin(x,y) x=min((x),(y))
#define setmax(x,y) x=max((x),(y))
 
const int MAXN = (5e5)+5;
// const ll SQRT = 4;
const long long inf = 1e18;
const long long MOD = 1e9+7;

pair<ll,ll> st[2*MAXN][25],a[MAXN],nd[MAXN];
pair<ll,ll> f[MAXN];
int n,timer,tin[MAXN],tout[MAXN],c,s,t,x[MAXN],y[MAXN],color[MAXN],vtn[MAXN],nn,vtcolor[MAXN],dep[MAXN];
ll ans,dis[MAXN];
vector<pair<int,ll>> g[MAXN],vtg[MAXN];
stack<pair<int,int>> stk;

void dfs(const int& u,const int& p)
{
  st[timer][0]={dep[u],u};
  tin[u]=timer++;
  for(auto v:g[u]) if(v.fi!=p) 
  {
    dep[v.fi]=dep[u]+1; 
    dis[v.fi]=dis[u]+v.se; 
    dfs(v.fi,u);
    st[timer++][0]={dep[u],u};
  }
  tout[u]=timer;
}

void build()
{
  dfs(0,-1);
  int t=2*n-1;
  FOR(i,1,31-__builtin_clz(t)) FOR(j,0,t-(1<<i)) st[j][i]=min(st[j][i-1],st[j+(1ll<<i-1)][i-1]);
}

pair<int,int> lca(int u,int v)
{
  if(tin[u]>tin[v]) swap(u,v);
  int t=__lg(tin[v]-tin[u]+1);
  return min(st[tin[u]][t],st[tin[v]-(1<<t)+1][t]);
}

void buildvt()
{
  FOR(i,0,c-1) nd[i]=a[i];
  FOR(i,0,c-2) 
  {
    ll l=lca(a[i].se,a[i+1].se).se;
    nd[c+i]={tin[l],l};
  }
  sort(nd,nd+2*c-1);
  nn=0;
  vtg[nn].clear();
  vtcolor[nn]=color[nd[0].se];
  vtn[nn++]=nd[0].se;
  while(!stk.empty()) stk.pop();
  stk.push({nd[0].se,0});
  FOR(i,1,2*c-2)
  {
    if(nd[i].se==stk.top().fi) continue;
    while(!stk.empty()&&!(tin[nd[i].se]>tin[stk.top().fi]&&tout[nd[i].se]<=tout[stk.top().fi])) stk.pop();
    vtcolor[nn]=color[nd[i].se]; vtn[nn]=nd[i].se;
    vtg[nn].clear();
    vtg[nn].pb({stk.top().se,dis[nd[i].se]-dis[stk.top().fi]});
    vtg[stk.top().se].pb({nn,dis[nd[i].se]-dis[stk.top().fi]});
    stk.push({nd[i].se,nn++});
  }
}

pair<ll,ll> solve(const int& u,const int& p)
{
  if(f[u]!=make_pair(1ll*-1,1ll*-1)) return f[u];
  pair<ll,ll> res={inf,inf};
  if(vtcolor[u]==-1) res.se=0;
  else if(vtcolor[u]==1) res.fi=0;
  for(auto v:vtg[u]) if(v.fi!=p) {setmin(res.fi,solve(v.fi,u).fi+v.se); setmin(res.se,solve(v.fi,u).se+v.se);}
  return f[u]=res;
}

ll Query(int S,int X[],int T,int Y[])
{
  s=S; t=T; c=s+t;
  FOR(i,0,n-1) color[i]=vtcolor[i]=0;
  FOR(i,0,s-1) {x[i]=X[i]; color[x[i]]=-1; a[i]={tin[x[i]],x[i]};}
  FOR(i,0,t-1) {y[i]=Y[i]; color[y[i]]=1; a[s+i]={tin[y[i]],y[i]};}
  sort(a,a+c);
  buildvt();
  FOR(i,0,nn-1) f[i]={-1,-1};
  ans=LLONG_MAX;
  FOR(i,0,nn-1) setmin(ans,solve(i,-1).fi+solve(i,-1).se);
  return ans;
}

void Init(int N,int A[],int B[],int D[])
{
  n=N;
  FOR(i,0,n-1) g[i].clear();
  FOR(i,0,n-2)
  {
    g[A[i]].pb({B[i],D[i]});
    g[B[i]].pb({A[i],D[i]});
  }
  build();
}

Compilation message

factories.cpp: In function 'void build()':
factories.cpp:61:86: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   61 |   FOR(i,1,31-__builtin_clz(t)) FOR(j,0,t-(1<<i)) st[j][i]=min(st[j][i-1],st[j+(1ll<<i-1)][i-1]);
      |                                                                                     ~^~
# Verdict Execution time Memory Grader output
1 Correct 25 ms 24648 KB Output is correct
2 Correct 670 ms 37144 KB Output is correct
3 Correct 635 ms 36412 KB Output is correct
4 Correct 643 ms 37276 KB Output is correct
5 Correct 561 ms 37032 KB Output is correct
6 Correct 419 ms 36660 KB Output is correct
7 Correct 597 ms 36368 KB Output is correct
8 Correct 615 ms 37412 KB Output is correct
9 Correct 558 ms 37028 KB Output is correct
10 Correct 403 ms 36632 KB Output is correct
11 Correct 600 ms 36368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 24404 KB Output is correct
2 Correct 7150 ms 467400 KB Output is correct
3 Correct 4749 ms 473072 KB Output is correct
4 Correct 6904 ms 464976 KB Output is correct
5 Correct 7661 ms 515136 KB Output is correct
6 Correct 7512 ms 474144 KB Output is correct
7 Correct 1431 ms 121772 KB Output is correct
8 Correct 1769 ms 120980 KB Output is correct
9 Correct 1805 ms 127612 KB Output is correct
10 Correct 1949 ms 122976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 24648 KB Output is correct
2 Correct 670 ms 37144 KB Output is correct
3 Correct 635 ms 36412 KB Output is correct
4 Correct 643 ms 37276 KB Output is correct
5 Correct 561 ms 37032 KB Output is correct
6 Correct 419 ms 36660 KB Output is correct
7 Correct 597 ms 36368 KB Output is correct
8 Correct 615 ms 37412 KB Output is correct
9 Correct 558 ms 37028 KB Output is correct
10 Correct 403 ms 36632 KB Output is correct
11 Correct 600 ms 36368 KB Output is correct
12 Correct 15 ms 24404 KB Output is correct
13 Correct 7150 ms 467400 KB Output is correct
14 Correct 4749 ms 473072 KB Output is correct
15 Correct 6904 ms 464976 KB Output is correct
16 Correct 7661 ms 515136 KB Output is correct
17 Correct 7512 ms 474144 KB Output is correct
18 Correct 1431 ms 121772 KB Output is correct
19 Correct 1769 ms 120980 KB Output is correct
20 Correct 1805 ms 127612 KB Output is correct
21 Correct 1949 ms 122976 KB Output is correct
22 Correct 2607 ms 490728 KB Output is correct
23 Correct 7877 ms 492156 KB Output is correct
24 Correct 1883 ms 491676 KB Output is correct
25 Correct 7919 ms 495816 KB Output is correct
26 Correct 1656 ms 475328 KB Output is correct
27 Correct 7909 ms 518960 KB Output is correct
28 Correct 7098 ms 483540 KB Output is correct
29 Correct 1759 ms 474256 KB Output is correct
30 Correct 1880 ms 473436 KB Output is correct
31 Correct 1873 ms 474264 KB Output is correct
32 Correct 2029 ms 139188 KB Output is correct
33 Correct 1840 ms 135116 KB Output is correct
34 Correct 895 ms 120244 KB Output is correct
35 Correct 892 ms 119968 KB Output is correct
36 Correct 872 ms 120648 KB Output is correct
37 Correct 867 ms 120488 KB Output is correct