Submission #522554

# Submission time Handle Problem Language Result Execution time Memory
522554 2022-02-05T06:56:26 Z fcmalkcin Paths (RMI21_paths) C++17
100 / 100
578 ms 39932 KB
/*#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops, no-stack-protector")
#pragma GCC target("avx,avx2,fma")*/

#include <bits/stdc++.h>
using namespace std;

#define ll  long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
#define For(i,a,b) for (ll i=a;i<=b;i++)

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const ll maxn=1e5+100;
const ll mod=998244353  ;
const ll base=2e9+10000;

/// you will be the best but now you just are trash
/// goal 3/7

ll res[maxn];
vector<pll> adj[maxn];
pll st[4*maxn];
ll pos[maxn];
ll n, k;
pll mer(pll a,pll b)
{
    if (a.ff>=b.ff)
        return a;
    return b;
}
void update(ll id,ll left,ll right,ll x,ll diff)
{
    if (x>right||x<left)
        return ;
    if (left==right)
    {
        st[id]=make_pair(diff,left);
        return ;
    }
    ll mid=(left+right)/2;
    update(id*2,left,mid,x,diff);
    update(id*2+1,mid+1,right,x,diff);
    st[id]=mer(st[id*2],st[id*2+1]);
}
pll get(ll id,ll left,ll right,ll x,ll y)
{
    if (x>right||y<left)
        return make_pair(-1,0);
    if (x<=left&&y>=right)
        return st[id];
    ll mid=(left+right)/2;
    return mer(get(id*2,left,mid,x,y),get(id*2+1,mid+1,right,x,y));
}
struct tk
{
    pll st[4*maxn];
    ll la[4*maxn];
    tk()
    {
        for (int i=0; i<4*maxn; i++)
        {
            st[i]=make_pair(0,0);
        }
        memset(la,0,sizeof(la));
    }
    pll mer(pll a,pll b)
    {
        if (a.ff>=b.ff)
            return a;
        return b;
    }
    void dosth(ll id,ll left,ll right)
    {
        st[id*2].ff+=la[id];
        st[id*2+1].ff+=la[id];
        la[id*2]+=la[id];
        la[id*2+1]+=la[id];
        la[id]=0;
        return ;
    }
    void update(ll id,ll left,ll right,ll x,ll y,ll diff)
    {
        if (x>right||y<left)
            return ;
        if (x<=left&&y>=right)
        {
            st[id].ff+=diff;
            la[id]+=diff;
            if (left==right)
                st[id].ss=left;
            return ;
        }
        dosth(id,left,right);
        ll mid=(left+right)/2;
        update(id*2,left,mid,x,y,diff);
        update(id*2+1,mid+1,right,x,y,diff);
        st[id]=mer(st[id*2],st[id*2+1]);
    }
    pll get(ll id,ll left,ll right,ll x,ll y)
    {
        if (x>right||y<left)
            return make_pair(-1,0);
        if (x<=left&&y>=right)
            return st[id];
        dosth(id,left,right);
        ll mid=(left+right)/2;
        return mer(get(id*2,left,mid,x,y),get(id*2+1,mid+1,right,x,y));
    }
} man;
set<pll> st1;
set<pll,greater<pll>> st2;
ll ans=0;
ll cnt=0;
ll f[maxn];
ll l[maxn];
ll dep[maxn];
vector<ll> vt;
ll val[maxn];
bool dd[maxn];
ll anc[maxn];

void add(ll u)
{
    if (st1.size()<k)
    {
        st1.insert(make_pair(val[u],u));
        ans+=val[u];
    }
    else
    {
        if ((*st1.begin())<make_pair(val[u],u))
        {
            auto p=(*st1.begin());
            st1.erase(st1.begin());
            st1.insert(make_pair(val[u],u));
            ans+=val[u];
            ans-=p.ff;
            st2.insert(p);
        }
        else
        {
            st2.insert(make_pair(val[u],u));
        }
    }
}
void ers(ll u)
{
    if (st1.count(make_pair(val[u],u)))
    {
        st1.erase(make_pair(val[u],u));
        ans-=val[u];
        if (st2.size())
        {
            auto p=(*st2.begin());
            st2.erase(p);
            st1.insert(p);
            ans+=p.ff;
        }
    }
    else
    {
        st2.erase(make_pair(val[u],u));
    }
}
void change(ll u,ll val1)
{
    if (val[u])
    {
        ers(u);
    }
    update(1,1,n,f[u],val1);
    val[u]=val1;
    add(u);
}
void dfs(ll u,ll par)
{
    anc[u]=par;
    cnt++;
    f[u]=cnt;
    pos[cnt]=u;
    for (auto p:adj[u])
    {
        ll to=p.ff;
        ll w=p.ss;
        if (to==par)
            continue;
        dep[to]=dep[u]+w;
        dfs(to,u);
    }
    l[u]=cnt;
    if (l[u]==f[u])
    {
        vt.pb(u);
    }
}
ll mxl[maxn];
void dfs1(ll u,ll par)
{
    mxl[u]=u;
    for (auto p:adj[u])
    {
        ll to=p.ff;
        ll w=p.ss;
        if (to==par)
            continue;
        dfs1(to,u);
        if (val[mxl[to]]>val[mxl[u]])
            mxl[u]=mxl[to];
    }
}
void dfs2(ll u,ll par,pll mxpre)
{
    res[u]=ans;
    for (auto p1:adj[u])
    {
        ll to=p1.ff;
        ll w=p1.ss;
        if (to==par)
            continue;
        ll posnxt=mxl[to];
        ll valnw=dep[posnxt]-dep[u];
        change(posnxt,valnw-w);
        auto p=mer(get(1,1,n,f[u],f[to]-1),get(1,1,n,l[to]+1,l[u]));
        if (p.ff>mxpre.ff)
        {
            p=make_pair(p.ff,pos[p.ss]);
        }
        else
        {
            p=mxpre;
        }
        change(p.ss,p.ff+w);
        dfs2(to,u,make_pair(p.ff+w,p.ss));
        change(posnxt,valnw);
        change(p.ss,p.ff);

    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    if (fopen("t.inp", "r"))
    {
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }
    cin>> n>> k;
    pll root=make_pair(0,1);
    for (int i=1; i<=n-1; i++)
    {
        ll x, y, w;
        cin>>x>> y>> w;
        adj[x].pb(make_pair(y,w));
        adj[y].pb(make_pair(x,w));
        root=max(root,make_pair((ll)adj[x].size(),x));
        root=max(root,make_pair((ll)adj[y].size(),y));
    }
    if (n==1)
    {
        cout <<0;
        return 0;
    }
    dd[0]=1;
    dfs(root.ss,0);
   // cout <<root.ss<<endl;
    k=min(k,(ll)vt.size());
    for (auto to:vt)
    {
        man.update(1,1,n,f[to],l[to],dep[to]);
        // cout <<to<<" "<<dep[to]<<endl;
    }
    for (int i=1; i<=vt.size(); i++)
    {
        auto p=man.st[1];
        ll u=pos[p.ss];
        change(u,p.ff);
        ll nw=u;
        while (!dd[nw])
        {
            dd[nw]=1;
            man.update(1,1,n,f[nw],l[nw],-(dep[nw]-dep[anc[nw]]));
            nw=anc[nw];
        }
    }
    dfs1(root.ss,0);
    dfs2(root.ss,0,make_pair(-1,0));
    for (int i=1;i<=n;i++)
    {
        cout <<res[i]<<endl;
    }
}

Compilation message

Main.cpp: In function 'void add(long long int)':
Main.cpp:129:19: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  129 |     if (st1.size()<k)
      |         ~~~~~~~~~~^~
Main.cpp: In function 'void dfs1(long long int, long long int)':
Main.cpp:208:12: warning: unused variable 'w' [-Wunused-variable]
  208 |         ll w=p.ss;
      |            ^
Main.cpp: In function 'int main()':
Main.cpp:279:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  279 |     for (int i=1; i<=vt.size(); i++)
      |                   ~^~~~~~~~~~~
Main.cpp:251:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  251 |         freopen("test.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:252:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  252 |         freopen("test.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12108 KB Output is correct
2 Correct 6 ms 12108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12108 KB Output is correct
2 Correct 6 ms 12108 KB Output is correct
3 Correct 6 ms 12100 KB Output is correct
4 Correct 6 ms 12108 KB Output is correct
5 Correct 7 ms 12120 KB Output is correct
6 Correct 6 ms 12096 KB Output is correct
7 Correct 7 ms 12140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12108 KB Output is correct
2 Correct 6 ms 12108 KB Output is correct
3 Correct 6 ms 12100 KB Output is correct
4 Correct 6 ms 12108 KB Output is correct
5 Correct 7 ms 12120 KB Output is correct
6 Correct 6 ms 12096 KB Output is correct
7 Correct 7 ms 12140 KB Output is correct
8 Correct 9 ms 12364 KB Output is correct
9 Correct 8 ms 12356 KB Output is correct
10 Correct 8 ms 12236 KB Output is correct
11 Correct 9 ms 12340 KB Output is correct
12 Correct 10 ms 12236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12108 KB Output is correct
2 Correct 6 ms 12108 KB Output is correct
3 Correct 6 ms 12100 KB Output is correct
4 Correct 6 ms 12108 KB Output is correct
5 Correct 7 ms 12120 KB Output is correct
6 Correct 6 ms 12096 KB Output is correct
7 Correct 7 ms 12140 KB Output is correct
8 Correct 9 ms 12364 KB Output is correct
9 Correct 8 ms 12356 KB Output is correct
10 Correct 8 ms 12236 KB Output is correct
11 Correct 9 ms 12340 KB Output is correct
12 Correct 10 ms 12236 KB Output is correct
13 Correct 13 ms 12620 KB Output is correct
14 Correct 12 ms 12692 KB Output is correct
15 Correct 10 ms 12496 KB Output is correct
16 Correct 12 ms 12600 KB Output is correct
17 Correct 16 ms 12492 KB Output is correct
18 Correct 12 ms 12496 KB Output is correct
19 Correct 14 ms 12620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 457 ms 35552 KB Output is correct
2 Correct 452 ms 37940 KB Output is correct
3 Correct 328 ms 30008 KB Output is correct
4 Correct 458 ms 35764 KB Output is correct
5 Correct 479 ms 37436 KB Output is correct
6 Correct 487 ms 36116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12108 KB Output is correct
2 Correct 6 ms 12108 KB Output is correct
3 Correct 6 ms 12100 KB Output is correct
4 Correct 6 ms 12108 KB Output is correct
5 Correct 7 ms 12120 KB Output is correct
6 Correct 6 ms 12096 KB Output is correct
7 Correct 7 ms 12140 KB Output is correct
8 Correct 9 ms 12364 KB Output is correct
9 Correct 8 ms 12356 KB Output is correct
10 Correct 8 ms 12236 KB Output is correct
11 Correct 9 ms 12340 KB Output is correct
12 Correct 10 ms 12236 KB Output is correct
13 Correct 13 ms 12620 KB Output is correct
14 Correct 12 ms 12692 KB Output is correct
15 Correct 10 ms 12496 KB Output is correct
16 Correct 12 ms 12600 KB Output is correct
17 Correct 16 ms 12492 KB Output is correct
18 Correct 12 ms 12496 KB Output is correct
19 Correct 14 ms 12620 KB Output is correct
20 Correct 457 ms 35552 KB Output is correct
21 Correct 452 ms 37940 KB Output is correct
22 Correct 328 ms 30008 KB Output is correct
23 Correct 458 ms 35764 KB Output is correct
24 Correct 479 ms 37436 KB Output is correct
25 Correct 487 ms 36116 KB Output is correct
26 Correct 538 ms 36132 KB Output is correct
27 Correct 488 ms 37048 KB Output is correct
28 Correct 493 ms 38260 KB Output is correct
29 Correct 318 ms 30148 KB Output is correct
30 Correct 519 ms 36240 KB Output is correct
31 Correct 448 ms 34960 KB Output is correct
32 Correct 578 ms 39932 KB Output is correct
33 Correct 517 ms 38176 KB Output is correct
34 Correct 280 ms 31376 KB Output is correct
35 Correct 522 ms 38236 KB Output is correct
36 Correct 449 ms 37688 KB Output is correct