//Let's join Kaede Takagaki Fan Club !! #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include #include #include using namespace std; typedef long long ll; typedef pair P; typedef pair P1; typedef pair P2; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define fi first #define sc second #define rep(i,x) for(int i=0;i void dmp(T a){ rep(i,a.size()) cout << a[i] << " "; cout << endl; } template bool chmax(T&a, T b){ if(a < b){ a = b; return 1; } return 0; } template bool chmin(T&a, T b){ if(a > b){ a = b; return 1; } return 0; } template void g(T &a){ cin >> a; } template void o(const T &a,bool space=false){ cout << a << (space?' ':'\n'); } //ios::sync_with_stdio(false); const ll mod = 1000000007;//998244353 template void add(T&a,T b){ a+=b; if(a >= mod) a-=mod; } int n, k, c[100005], d[100005]; vectoredge[1000005]; ll sum[2][100005]; int vt[2][100005]; #define SZ 100005 int up[20][SZ],dep[SZ]; struct dbling{ bool ready = 0; void dfs(int v,int u,int d){ up[0][v] = u; dep[v] = d; rep(i,edge[v].size()){ if(edge[v][i] == u) continue; dfs(edge[v][i], v, d+1); } } void prepare(){ dfs(1,-1,0); rep(j,19){ rep(i,SZ){ if(up[j][i] == -1) up[j+1][i] = up[j][i]; else up[j+1][i] = up[j][up[j][i]]; } } ready = 1; } int get(int a,int b){ if(dep[a] > dep[b]) swap(a,b); int D = dep[b]-dep[a]; rep(i,20){ if(((D>>i)&1)) b = up[i][b]; } if(a == b) return a; for(int i=19;i>=0;i--){ if(up[i][a] != up[i][b]){ a = up[i][a]; b = up[i][b]; } } return up[0][a]; } int dist(int a,int b){ int c = get(a,b); return dep[a]+dep[b]-2*dep[c]; } int dist(int a,int b,int c){ //assuming c is lca of (a,b) return dep[a]+dep[b]-2*dep[c]; } int go_up(int v,int a){ if(dep[v] < a) return -1; rep(i,20) if(((a>>i)&1)) v = up[i][v]; return v; } //a ---- b //と並べたときのx番目 (1-indexed) //存在しない場合は-1を返す //verified : opencup Warsaw I int v_on_path(int a,int b,int x){ int c = get(a,b); int d = dist(a,b,c); if(x <= 0 || x > d+1) return -1; if(x <= dep[a]-dep[c]+1){ return go_up(a, x-1); } else{ x = d+2-x; return go_up(b, x-1); } } }kaede; struct BIT { int bit[200005], sz; void init(int nn = 200005){ sz = nn; rep(i, sz) bit[i] = 0; } int f(int x) { return x&-x; } void add(int i,int x) { for(int s=i;s0;s-=f(s)) res+=bit[s]; return res; } }shiki; ll rui[100005]; vectorza; ll interval(ll num){ ll ret = 0; shiki.init(); shiki.add(POSL(za, 0)+1, 1); //cout << num << endl; for(int i=1;i<=k;i++){ ll x = rui[i]-num; int pos = POSL(za, x); ret += i-shiki.sum(pos); pos = POSL(za, rui[i]) + 1; shiki.add(pos, 1); } //cout << ret << endl; return ret; } vectorQ[100005]; bool rem[100005]; int sub[100005]; int make(int v,int u){ int cnt=1; for(int i=0;icand, cand2; void make(int v, int u, int d){ for(const auto a:Q[v]) cand.pb(a-d); rep(i, edge[v].size()){ if(rem[edge[v][i]] || edge[v][i] == u) continue; make(edge[v][i], v, d+1); } } void make2(int v, int u, int d){ for(const auto a:Q[v]){ ll x = B-a+d; int k = POSU(cand, x); C += shiki.sum(k); cand2.pb(a-d); } rep(i, edge[v].size()){ if(rem[edge[v][i]] || edge[v][i] == u) continue; make2(edge[v][i], v, d+1); } } void solve(int v){ make(v,-1); int cent=search(v,-1,sub[v]).sc; rem[cent]=true; cand.clear(); for(const auto a:Q[cent]) cand.pb(a); for(int i=0;iA, B; repn(i, k){ sum[0][i] = sum[0][i-1] + d[i]; if(i == 1) vt[0][i] = c[i]; else vt[0][i] = kaede.get(vt[0][i-1], c[i]); A.pb(sum[0][i]+dep[vt[0][i]]); //cout << sum[0][i] << " " << vt[0][i] << endl; } repn(i, k){ sum[1][i] = sum[1][i-1] + d[k+1-i]; if(i == 1) vt[1][i] = c[k+1-i]; else vt[1][i] = kaede.get(vt[1][i-1], c[k+1-i]); B.pb(sum[1][i]+dep[vt[1][i]]); } SORT(A); SORT(B); ll num = 1LL*k*(k+1)/2+1; num = (num+1)/2; //(lb, ub] ll lb = -2e15, ub = 2e15; while(ub-lb > 1){ ll mid = (lb+ub)/2; //1, 何もしない ll cnt = 1; //k*k //if(mid == 3) cout << essence(mid) << endl; cnt += essence(mid); //2*k cnt += POSU(A, mid) + POSU(B, mid); //all if(sum[0][k]+dep[vt[0][k]] <= mid) cnt -= k; cnt -= interval(mid - sum[0][k] - dep[vt[0][k]]); assert(cnt >= 0); if(cnt < num) lb = mid; else ub = mid; } printf("%lld\n", ub); }