답안 #751185

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
751185 2023-05-31T07:38:45 Z bgnbvnbv Janjetina (COCI21_janjetina) C++14
110 / 110
809 ms 74100 KB
#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=int;
const ll maxN=1e5+10;
const ll inf=1e8;
const ll mod=1e9+7;
ll sz[maxN];
vector<ll>G[maxN];
bool r[maxN];
int dfs(int u, int p) {
  sz[u]=1;
  for(int v:G[u])
  {
      if(r[v]==false&&v!=p)
      {
          sz[u]+=dfs(v,u);
      }
  }
  return sz[u];
}
int centroid(int u, int p, int siz) {
    for(int v:G[u])
    {
        if(v!=p&&r[v]==false)
        {
            if(sz[v]>siz/2) return centroid(v,u,siz);
        }
    }
    return u;
}
ll pa[maxN];

void build(int u, int p) {
    ll C=centroid(u,u,dfs(u,u));
    r[C]=true;
    pa[C]=p;
    //adj[p].pb(C);
    for(int v:G[C])
    {
        if(r[v]==false)
        {
            build(v,C);
        }
    }
}
vector<ll>val;
ll bit[maxN];
void update(ll i)
{
    while(i<val.size()+5)
    {
        bit[i]++;
        i+=i&(-i);
    }
}
ll get(ll i)
{
    ll c=0;
    while(i>0)
    {
        c+=bit[i];
        i-=(i&(-i));
    }
    return c;
}
ll n,k;
vector<pli>g[maxN];
ll P[maxN][18],mx[maxN][18],h[maxN];
void DFS(ll u=1,ll p=1)
{
    P[u][0]=p;
    for(int i=1;i<=17;i++) P[u][i]=P[P[u][i-1]][i-1];
    for(int i=1;i<=17;i++)
    {
        mx[u][i]=max(mx[u][i-1],mx[P[u][i-1]][i-1]);
    }
    for(auto vv:g[u])
    {
        int v=vv.fi;
        int w=vv.se;
        if(v!=p)
        {
            h[v]=h[u]+1;
            mx[v][0]=w;
            DFS(v,u);
        }
    }
}
struct qq
{
    ll dis,mx;
    bool operator<(const qq&o)
    {
        return mx<o.mx;
    }
};
ll lca(ll u,ll v)
{
    if(h[u]<h[v]) swap(u,v);
    for(int i=17;i>=0;i--) if(h[u]-(1<<i)>=h[v]) u=P[u][i];
    if(u==v) return u;
    for(int i=17;i>=0;i--)
    {
        if(P[u][i]!=P[v][i]) u=P[u][i],v=P[v][i];
    }
    return P[u][0];
}
ll dis(ll u,ll v)
{
    return h[u]+h[v]-2*h[lca(u,v)];
}
ll maxx(ll u,ll v)
{
    ll k=h[u]-h[v];
    ll aa=-inf;
    for(int i=17;i>=0;i--)
    {
        if(k>>i&1) aa=max(aa,mx[u][i]),u=P[u][i];
    }
    return aa;
}
ll get(ll u,ll v)
{
    ll x=lca(u,v);
    return max(maxx(u,x),maxx(v,x));
}
vector<qq>vec[maxN],vec2[maxN];
void solve()
{
    cin >> n >> k;
    for(int i=1;i<n;i++)
    {
        ll u,v,w;
        cin >> u >> v >> w;
        g[u].pb({v,w});
        g[v].pb({u,w});
        G[u].pb(v);
        G[v].pb(u);
    }
    DFS();
    build(1,0);
    for(int i=1;i<=n;i++)
    {
        ll u=i;
        while(pa[u]!=0)
        {
            qq a={dis(i,pa[u]),get(i,pa[u])};
            vec[pa[u]].pb(a);
            vec2[u].pb(a);
            u=pa[u];
        }
    }
    for(int i=1;i<=n;i++) vec[i].pb({0,-inf}),sort(vec[i].begin(),vec[i].end()),sort(vec2[i].begin(),vec2[i].end());
    long long ans=0;
    for(int i=1;i<=n;i++)
    {
        val.clear();
        val.pb(-1);
        for(int j=0;j<vec[i].size();j++)
        {
            val.pb(vec[i][j].dis);
        }
        sort(val.begin(),val.end());
        for(int j=0;j<vec[i].size();j++)
        {
            ll pos=upper_bound(val.begin(),val.end(),vec[i][j].mx-vec[i][j].dis-k)-val.begin()-1;
            ans+=get(pos);
            pos=lower_bound(val.begin(),val.end(),vec[i][j].dis)-val.begin();
            update(pos);
        }
        fill(bit,bit+val.size()+5,0);
    }
    for(int i=1;i<=n;i++)
    {
        swap(vec[i],vec2[i]);
        val.clear();
        val.pb(-1);
        for(int j=0;j<vec[i].size();j++)
        {
            val.pb(vec[i][j].dis);
        }
        sort(val.begin(),val.end());
        for(int j=0;j<vec[i].size();j++)
        {
            ll pos=upper_bound(val.begin(),val.end(),vec[i][j].mx-vec[i][j].dis-k)-val.begin()-1;
            ans-=get(pos);
            pos=lower_bound(val.begin(),val.end(),vec[i][j].dis)-val.begin();
            update(pos);
        }
        fill(bit,bit+val.size()+5,0);
    }
    cout << ans*2;
}
int main()
{
    fastio
    //freopen(TASKNAME".INP","r",stdin);
    //freopen(TASKNAME".OUT","w",stdout);
    solve();
}

Compilation message

Main.cpp: In function 'void update(ll)':
Main.cpp:56:12: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |     while(i<val.size()+5)
      |           ~^~~~~~~~~~~~~
Main.cpp: In function 'void solve()':
Main.cpp:165:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<qq>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  165 |         for(int j=0;j<vec[i].size();j++)
      |                     ~^~~~~~~~~~~~~~
Main.cpp:170:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<qq>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  170 |         for(int j=0;j<vec[i].size();j++)
      |                     ~^~~~~~~~~~~~~~
Main.cpp:184:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<qq>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  184 |         for(int j=0;j<vec[i].size();j++)
      |                     ~^~~~~~~~~~~~~~
Main.cpp:189:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<qq>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  189 |         for(int j=0;j<vec[i].size();j++)
      |                     ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 7 ms 9684 KB Output is correct
4 Correct 9 ms 10184 KB Output is correct
5 Correct 8 ms 10196 KB Output is correct
6 Correct 8 ms 10152 KB Output is correct
7 Correct 8 ms 10196 KB Output is correct
8 Correct 9 ms 10152 KB Output is correct
9 Correct 6 ms 10104 KB Output is correct
10 Correct 6 ms 9992 KB Output is correct
11 Correct 6 ms 10068 KB Output is correct
12 Correct 8 ms 10068 KB Output is correct
13 Correct 9 ms 10068 KB Output is correct
14 Correct 8 ms 10028 KB Output is correct
15 Correct 7 ms 10068 KB Output is correct
16 Correct 8 ms 10068 KB Output is correct
17 Correct 8 ms 10068 KB Output is correct
18 Correct 7 ms 10056 KB Output is correct
19 Correct 10 ms 10072 KB Output is correct
20 Correct 7 ms 10068 KB Output is correct
21 Correct 8 ms 10068 KB Output is correct
22 Correct 8 ms 10068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 6 ms 9780 KB Output is correct
4 Correct 9 ms 10196 KB Output is correct
5 Correct 60 ms 15744 KB Output is correct
6 Correct 353 ms 40140 KB Output is correct
7 Correct 726 ms 73600 KB Output is correct
8 Correct 726 ms 73952 KB Output is correct
9 Correct 718 ms 73520 KB Output is correct
10 Correct 743 ms 73816 KB Output is correct
11 Correct 712 ms 73508 KB Output is correct
12 Correct 781 ms 74100 KB Output is correct
13 Correct 752 ms 73516 KB Output is correct
14 Correct 763 ms 73892 KB Output is correct
15 Correct 779 ms 73740 KB Output is correct
16 Correct 737 ms 73980 KB Output is correct
17 Correct 727 ms 73852 KB Output is correct
18 Correct 732 ms 73680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 7 ms 9684 KB Output is correct
4 Correct 9 ms 10184 KB Output is correct
5 Correct 8 ms 10196 KB Output is correct
6 Correct 8 ms 10152 KB Output is correct
7 Correct 8 ms 10196 KB Output is correct
8 Correct 9 ms 10152 KB Output is correct
9 Correct 6 ms 10104 KB Output is correct
10 Correct 6 ms 9992 KB Output is correct
11 Correct 6 ms 10068 KB Output is correct
12 Correct 8 ms 10068 KB Output is correct
13 Correct 9 ms 10068 KB Output is correct
14 Correct 8 ms 10028 KB Output is correct
15 Correct 7 ms 10068 KB Output is correct
16 Correct 8 ms 10068 KB Output is correct
17 Correct 8 ms 10068 KB Output is correct
18 Correct 7 ms 10056 KB Output is correct
19 Correct 10 ms 10072 KB Output is correct
20 Correct 7 ms 10068 KB Output is correct
21 Correct 8 ms 10068 KB Output is correct
22 Correct 8 ms 10068 KB Output is correct
23 Correct 5 ms 9684 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Correct 6 ms 9780 KB Output is correct
26 Correct 9 ms 10196 KB Output is correct
27 Correct 60 ms 15744 KB Output is correct
28 Correct 353 ms 40140 KB Output is correct
29 Correct 726 ms 73600 KB Output is correct
30 Correct 726 ms 73952 KB Output is correct
31 Correct 718 ms 73520 KB Output is correct
32 Correct 743 ms 73816 KB Output is correct
33 Correct 712 ms 73508 KB Output is correct
34 Correct 781 ms 74100 KB Output is correct
35 Correct 752 ms 73516 KB Output is correct
36 Correct 763 ms 73892 KB Output is correct
37 Correct 779 ms 73740 KB Output is correct
38 Correct 737 ms 73980 KB Output is correct
39 Correct 727 ms 73852 KB Output is correct
40 Correct 732 ms 73680 KB Output is correct
41 Correct 6 ms 9684 KB Output is correct
42 Correct 735 ms 73528 KB Output is correct
43 Correct 719 ms 73992 KB Output is correct
44 Correct 734 ms 73652 KB Output is correct
45 Correct 730 ms 73996 KB Output is correct
46 Correct 710 ms 73544 KB Output is correct
47 Correct 713 ms 74020 KB Output is correct
48 Correct 704 ms 73588 KB Output is correct
49 Correct 743 ms 73832 KB Output is correct
50 Correct 778 ms 73748 KB Output is correct
51 Correct 809 ms 73848 KB Output is correct
52 Correct 234 ms 48304 KB Output is correct
53 Correct 251 ms 48680 KB Output is correct
54 Correct 247 ms 48240 KB Output is correct
55 Correct 253 ms 48644 KB Output is correct
56 Correct 272 ms 48612 KB Output is correct
57 Correct 750 ms 58516 KB Output is correct
58 Correct 658 ms 57892 KB Output is correct
59 Correct 707 ms 58796 KB Output is correct
60 Correct 759 ms 58436 KB Output is correct
61 Correct 717 ms 58524 KB Output is correct
62 Correct 550 ms 55188 KB Output is correct
63 Correct 591 ms 55892 KB Output is correct
64 Correct 595 ms 56052 KB Output is correct
65 Correct 25 ms 11888 KB Output is correct
66 Correct 5 ms 9684 KB Output is correct