답안 #965433

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
965433 2024-04-18T14:06:25 Z 12345678 Cat Exercise (JOI23_ho_t4) C++17
100 / 100
606 ms 119512 KB
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const ll nx=2e5+5, inf=1e6, kx=18;

ll n, a[nx], u, v, in[nx], out[nx], rin[nx], t, rt, used[nx], pa[nx][kx], dp[nx], cnt, k[nx], lvl[nx];
vector<ll> d[nx], c[nx];

struct node
{
    ll mx, idx, lz;
    node(): mx(0), idx(0), lz(0){}
    node(ll mx, ll idx, ll lz): mx(mx), idx(idx), lz(lz){}
    friend node operator+(const node &a, const node &b)
    {
        if (a.mx>b.mx) return a;
        else return b;
    }
};

struct segtree
{
    node d[4*nx];
    void pushlz(ll l, ll r, ll i)
    {
        d[i].mx+=d[i].lz;
        ll md=(l+r)/2;
        if (l!=r) d[2*i].lz+=d[i].lz, d[2*i+1].lz+=d[i].lz;
        d[i].lz=0;
    }
    void build(ll l, ll r, ll i)
    {
        if (l==r) return d[i]=node(k[l], l, 0), void();
        ll md=(l+r)/2;
        build(l, md, 2*i);
        build(md+1, r, 2*i+1);
        d[i]=d[2*i]+d[2*i+1];
    }
    void update(ll l, ll r, ll i, ll ql, ll qr, ll vl)
    {
        pushlz(l, r, i);
        if (qr<l||r<ql) return;
        if (ql<=l&&r<=qr) return d[i].lz+=vl, pushlz(l, r, i), void();
        ll md=(l+r)/2;
        update(l, md, 2*i, ql ,qr, vl);
        update(md+1, r, 2*i+1, ql, qr, vl);
        d[i]=d[2*i]+d[2*i+1];
    }
    void show(int l, int r, int i)
    {
        pushlz(l, r, i);
        cout<<"debugseg "<<l<<' '<<r<<' '<<d[i].mx<<' '<<d[i].idx<<'\n';
        if (l==r) return;
        int md=(l+r)/2; 
        show(l, md, 2*i);
        show(md+1, r, 2*i+1);
        d[i]=d[2*i]+d[2*i+1];
    }
} s;
 
void dfs(ll u, ll p)
{
    lvl[u]=lvl[p]+1;
    pa[u][0]=p;
    for (int i=1; i<kx; i++) pa[u][i]=pa[pa[u][i-1]][i-1];
    in[u]=++t;
    rin[t]=u;
    for (auto v:d[u]) if (v!=p) dfs(v, u);
    out[u]=t;
}

int lca(int u, int v)
{
    if (lvl[u]>lvl[v]) swap(u, v);
    for (int i=kx-1; i>=0; i--) if (lvl[pa[v][i]]>=lvl[u]) v=pa[v][i];
    if (u==v) return v;
    for (int i=kx-1; i>=0; i--) if (pa[u][i]!=pa[v][i]) u=pa[u][i], v=pa[v][i];
    return pa[u][0];
}

ll dist(int u, int v)
{
    return lvl[u]+lvl[v]-2*lvl[lca(u, v)];
}

void updateoutrange(ll l, ll r, ll vl)
{
    if (l!=1) s.update(1, n, 1, 1, l-1, vl);
    if (r!=n) s.update(1, n, 1, r+1, n, vl);
}

void solve(ll u)
{
    used[u]=1;
    if (u!=pa[u][0]&&!used[pa[u][0]])
    {
        s.update(1, n, 1, in[u], out[u], -inf);
        c[u].push_back(rin[s.d[1].idx]);
        solve(rin[s.d[1].idx]);
        s.update(1, n, 1, in[u], out[u], inf);
    }
    for (auto v:d[u])
    {
        if (used[v]) continue;
        updateoutrange(in[v], out[v], -inf);
        //cout<<"edge "<<u<<' '<<v<<' '<<in[v]<<' '<<out[v]<<'\n';
        //s.show(1, n, 1);
        c[u].push_back(rin[s.d[1].idx]);
        solve(rin[s.d[1].idx]);
        //cout<<"debug  "<<u<<' '<<s.d[1].idx<<'\n';
        updateoutrange(in[v], out[v], inf);
    }
}

void dfs2(int u)
{
    for (auto v:c[u]) dfs2(v), dp[u]=max(dp[u], dp[v]+dist(u, v));
}

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n;
    for (ll i=1; i<=n; i++) cin>>a[i];
    for (int i=1; i<=n; i++) if (a[i]==n) rt=i;
    for (ll i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
    dfs(rt, rt);
    for (int i=1; i<=n; i++) k[in[i]]=a[i];
    s.build(1, n, 1);
    solve(rt);
    dfs2(rt);
    cout<<dp[rt];
}

Compilation message

Main.cpp: In member function 'void segtree::pushlz(long long int, long long int, long long int)':
Main.cpp:30:12: warning: unused variable 'md' [-Wunused-variable]
   30 |         ll md=(l+r)/2;
      |            ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 39516 KB Output is correct
2 Correct 7 ms 39560 KB Output is correct
3 Correct 6 ms 39516 KB Output is correct
4 Correct 8 ms 39512 KB Output is correct
5 Correct 8 ms 39512 KB Output is correct
6 Correct 6 ms 39512 KB Output is correct
7 Correct 7 ms 39512 KB Output is correct
8 Correct 6 ms 39516 KB Output is correct
9 Correct 7 ms 39560 KB Output is correct
10 Correct 6 ms 39516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 39516 KB Output is correct
2 Correct 7 ms 39560 KB Output is correct
3 Correct 6 ms 39516 KB Output is correct
4 Correct 8 ms 39512 KB Output is correct
5 Correct 8 ms 39512 KB Output is correct
6 Correct 6 ms 39512 KB Output is correct
7 Correct 7 ms 39512 KB Output is correct
8 Correct 6 ms 39516 KB Output is correct
9 Correct 7 ms 39560 KB Output is correct
10 Correct 6 ms 39516 KB Output is correct
11 Correct 7 ms 39512 KB Output is correct
12 Correct 7 ms 39516 KB Output is correct
13 Correct 7 ms 39516 KB Output is correct
14 Correct 7 ms 39544 KB Output is correct
15 Correct 8 ms 39772 KB Output is correct
16 Correct 7 ms 39516 KB Output is correct
17 Correct 7 ms 39512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 39516 KB Output is correct
2 Correct 7 ms 39560 KB Output is correct
3 Correct 6 ms 39516 KB Output is correct
4 Correct 8 ms 39512 KB Output is correct
5 Correct 8 ms 39512 KB Output is correct
6 Correct 6 ms 39512 KB Output is correct
7 Correct 7 ms 39512 KB Output is correct
8 Correct 6 ms 39516 KB Output is correct
9 Correct 7 ms 39560 KB Output is correct
10 Correct 6 ms 39516 KB Output is correct
11 Correct 7 ms 39512 KB Output is correct
12 Correct 7 ms 39516 KB Output is correct
13 Correct 7 ms 39516 KB Output is correct
14 Correct 7 ms 39544 KB Output is correct
15 Correct 8 ms 39772 KB Output is correct
16 Correct 7 ms 39516 KB Output is correct
17 Correct 7 ms 39512 KB Output is correct
18 Correct 11 ms 42844 KB Output is correct
19 Correct 12 ms 42632 KB Output is correct
20 Correct 11 ms 42844 KB Output is correct
21 Correct 11 ms 42072 KB Output is correct
22 Correct 13 ms 42108 KB Output is correct
23 Correct 12 ms 42076 KB Output is correct
24 Correct 12 ms 42076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 39516 KB Output is correct
2 Correct 7 ms 39560 KB Output is correct
3 Correct 6 ms 39516 KB Output is correct
4 Correct 8 ms 39512 KB Output is correct
5 Correct 8 ms 39512 KB Output is correct
6 Correct 6 ms 39512 KB Output is correct
7 Correct 7 ms 39512 KB Output is correct
8 Correct 6 ms 39516 KB Output is correct
9 Correct 7 ms 39560 KB Output is correct
10 Correct 6 ms 39516 KB Output is correct
11 Correct 7 ms 39512 KB Output is correct
12 Correct 7 ms 39516 KB Output is correct
13 Correct 7 ms 39516 KB Output is correct
14 Correct 7 ms 39544 KB Output is correct
15 Correct 8 ms 39772 KB Output is correct
16 Correct 7 ms 39516 KB Output is correct
17 Correct 7 ms 39512 KB Output is correct
18 Correct 11 ms 42844 KB Output is correct
19 Correct 12 ms 42632 KB Output is correct
20 Correct 11 ms 42844 KB Output is correct
21 Correct 11 ms 42072 KB Output is correct
22 Correct 13 ms 42108 KB Output is correct
23 Correct 12 ms 42076 KB Output is correct
24 Correct 12 ms 42076 KB Output is correct
25 Correct 6 ms 39516 KB Output is correct
26 Correct 13 ms 42584 KB Output is correct
27 Correct 13 ms 42840 KB Output is correct
28 Correct 13 ms 42584 KB Output is correct
29 Correct 14 ms 42584 KB Output is correct
30 Correct 16 ms 42076 KB Output is correct
31 Correct 13 ms 42076 KB Output is correct
32 Correct 13 ms 42040 KB Output is correct
33 Correct 13 ms 42076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 39516 KB Output is correct
2 Correct 7 ms 39560 KB Output is correct
3 Correct 6 ms 39516 KB Output is correct
4 Correct 8 ms 39512 KB Output is correct
5 Correct 8 ms 39512 KB Output is correct
6 Correct 6 ms 39512 KB Output is correct
7 Correct 7 ms 39512 KB Output is correct
8 Correct 6 ms 39516 KB Output is correct
9 Correct 7 ms 39560 KB Output is correct
10 Correct 6 ms 39516 KB Output is correct
11 Correct 7 ms 39512 KB Output is correct
12 Correct 7 ms 39516 KB Output is correct
13 Correct 7 ms 39516 KB Output is correct
14 Correct 7 ms 39544 KB Output is correct
15 Correct 8 ms 39772 KB Output is correct
16 Correct 7 ms 39516 KB Output is correct
17 Correct 7 ms 39512 KB Output is correct
18 Correct 11 ms 42844 KB Output is correct
19 Correct 12 ms 42632 KB Output is correct
20 Correct 11 ms 42844 KB Output is correct
21 Correct 11 ms 42072 KB Output is correct
22 Correct 13 ms 42108 KB Output is correct
23 Correct 12 ms 42076 KB Output is correct
24 Correct 12 ms 42076 KB Output is correct
25 Correct 253 ms 119512 KB Output is correct
26 Correct 252 ms 115284 KB Output is correct
27 Correct 273 ms 115224 KB Output is correct
28 Correct 254 ms 86780 KB Output is correct
29 Correct 233 ms 87892 KB Output is correct
30 Correct 243 ms 87604 KB Output is correct
31 Correct 243 ms 88188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 39516 KB Output is correct
2 Correct 7 ms 39544 KB Output is correct
3 Correct 335 ms 87948 KB Output is correct
4 Correct 327 ms 88120 KB Output is correct
5 Correct 321 ms 87784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 39516 KB Output is correct
2 Correct 7 ms 39560 KB Output is correct
3 Correct 6 ms 39516 KB Output is correct
4 Correct 8 ms 39512 KB Output is correct
5 Correct 8 ms 39512 KB Output is correct
6 Correct 6 ms 39512 KB Output is correct
7 Correct 7 ms 39512 KB Output is correct
8 Correct 6 ms 39516 KB Output is correct
9 Correct 7 ms 39560 KB Output is correct
10 Correct 6 ms 39516 KB Output is correct
11 Correct 7 ms 39512 KB Output is correct
12 Correct 7 ms 39516 KB Output is correct
13 Correct 7 ms 39516 KB Output is correct
14 Correct 7 ms 39544 KB Output is correct
15 Correct 8 ms 39772 KB Output is correct
16 Correct 7 ms 39516 KB Output is correct
17 Correct 7 ms 39512 KB Output is correct
18 Correct 11 ms 42844 KB Output is correct
19 Correct 12 ms 42632 KB Output is correct
20 Correct 11 ms 42844 KB Output is correct
21 Correct 11 ms 42072 KB Output is correct
22 Correct 13 ms 42108 KB Output is correct
23 Correct 12 ms 42076 KB Output is correct
24 Correct 12 ms 42076 KB Output is correct
25 Correct 6 ms 39516 KB Output is correct
26 Correct 13 ms 42584 KB Output is correct
27 Correct 13 ms 42840 KB Output is correct
28 Correct 13 ms 42584 KB Output is correct
29 Correct 14 ms 42584 KB Output is correct
30 Correct 16 ms 42076 KB Output is correct
31 Correct 13 ms 42076 KB Output is correct
32 Correct 13 ms 42040 KB Output is correct
33 Correct 13 ms 42076 KB Output is correct
34 Correct 253 ms 119512 KB Output is correct
35 Correct 252 ms 115284 KB Output is correct
36 Correct 273 ms 115224 KB Output is correct
37 Correct 254 ms 86780 KB Output is correct
38 Correct 233 ms 87892 KB Output is correct
39 Correct 243 ms 87604 KB Output is correct
40 Correct 243 ms 88188 KB Output is correct
41 Correct 7 ms 39516 KB Output is correct
42 Correct 7 ms 39544 KB Output is correct
43 Correct 335 ms 87948 KB Output is correct
44 Correct 327 ms 88120 KB Output is correct
45 Correct 321 ms 87784 KB Output is correct
46 Correct 582 ms 111496 KB Output is correct
47 Correct 570 ms 111384 KB Output is correct
48 Correct 606 ms 111680 KB Output is correct
49 Correct 571 ms 111444 KB Output is correct
50 Correct 503 ms 85164 KB Output is correct
51 Correct 476 ms 85848 KB Output is correct
52 Correct 486 ms 85012 KB Output is correct
53 Correct 501 ms 85272 KB Output is correct