Submission #961018

# Submission time Handle Problem Language Result Execution time Memory
961018 2024-04-11T11:55:20 Z tudor_costin Cat Exercise (JOI23_ho_t4) C++11
100 / 100
421 ms 64448 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int Nmax=3e5;
int a[Nmax];
int nodes[Nmax],pos[Nmax],p[Nmax];
pair<int,int> maxi[Nmax];
int dp[Nmax];
int nodedepth[Nmax];
int aint[4*Nmax];
vector<int> euler;
vector<int> depths;
vector<int> v[Nmax];
int n;
int sol=0;
int sef(int x)
{
    if(p[x]==x) return x;
    p[x]=sef(p[x]);
    return p[x];
}
bool cmp(int x,int y)
{
    return a[x]<a[y];
}
void build(int nod,int l,int r)
{
    if(l==r)
    {
        aint[nod]=euler[l];
        return;
    }
    int mid=(l+r)/2;
    build(2*nod,l,mid);
    build(2*nod+1,mid+1,r);
    if (nodedepth[aint[2*nod]]<nodedepth[aint[2*nod+1]]) aint[nod]=aint[2*nod];
    else aint[nod]=aint[2*nod+1];
    return;
}
int query(int nod,int l,int r,int st,int dr)
{
    if(st<=l && r<=dr) return aint[nod];
    int mid=(l+r)/2;
    if(dr<=mid) return query(2*nod,l,mid,st,dr);
    if(mid<st) return query(2*nod+1,mid+1,r,st,dr);
    int op1=query(2*nod,l,mid,st,mid),op2=query(2*nod+1,mid+1,r,mid+1,dr);
    if(nodedepth[op1]<nodedepth[op2]) return op1;
    return op2;
}
void dfs(int nod,int prev,int depth)
{
    euler.push_back(nod);
    depths.push_back(depth);
    nodedepth[nod]=depth;
    pos[nod]=euler.size()-1;
    for(auto u:v[nod])
    {
        if(u!=prev)
        {
            dfs(u,nod,depth+1);
            euler.push_back(nod);
            depths.push_back(depth);
        }
    }
}
int lca(int u,int v)
{
    if(pos[u]>pos[v]) swap(u,v);
    return query(1,0,euler.size()-1,pos[u],pos[v]);
}
void calclca()
{
    dfs(1,0,0);
    build(1,0,euler.size()-1);
}
int dist(int u,int v)
{
    int nod=lca(u,v);
    return nodedepth[u]+nodedepth[v]-2*nodedepth[nod];
}
void unite(int x,int y)
{
    int sefx=sef(x);
    int sefy=sef(y);
    p[sefy]=sefx;
    maxi[sefx]=max(maxi[sefx],maxi[sefy]);
    return;
}
void activate(int nod)
{
    p[nod]=nod;
    maxi[nod]={a[nod],nod};
    for(auto u:v[nod])
    {
        if(p[u]!=0)
        {
            int nxt=maxi[sef(u)].second;
            dp[nod]=max(dp[nod],dist(nod,nxt)+dp[nxt]);
            unite(nod,u);
        }
    }
}
signed main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        nodes[i]=i;
    }
    sort(nodes+1,nodes+n+1,cmp);
    for(int i=1;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    calclca();
    for(int i=1;i<=n;i++)
    {
        activate(nodes[i]);
    }
    cout<<dp[nodes[n]]<<'\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 21080 KB Output is correct
2 Correct 5 ms 21092 KB Output is correct
3 Correct 4 ms 21084 KB Output is correct
4 Correct 3 ms 21084 KB Output is correct
5 Correct 4 ms 21084 KB Output is correct
6 Correct 3 ms 21084 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Correct 4 ms 21080 KB Output is correct
9 Correct 4 ms 21080 KB Output is correct
10 Correct 3 ms 21084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 21080 KB Output is correct
2 Correct 5 ms 21092 KB Output is correct
3 Correct 4 ms 21084 KB Output is correct
4 Correct 3 ms 21084 KB Output is correct
5 Correct 4 ms 21084 KB Output is correct
6 Correct 3 ms 21084 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Correct 4 ms 21080 KB Output is correct
9 Correct 4 ms 21080 KB Output is correct
10 Correct 3 ms 21084 KB Output is correct
11 Correct 4 ms 21084 KB Output is correct
12 Correct 5 ms 21084 KB Output is correct
13 Correct 4 ms 21084 KB Output is correct
14 Correct 4 ms 21084 KB Output is correct
15 Correct 4 ms 21084 KB Output is correct
16 Correct 4 ms 21084 KB Output is correct
17 Correct 4 ms 21084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 21080 KB Output is correct
2 Correct 5 ms 21092 KB Output is correct
3 Correct 4 ms 21084 KB Output is correct
4 Correct 3 ms 21084 KB Output is correct
5 Correct 4 ms 21084 KB Output is correct
6 Correct 3 ms 21084 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Correct 4 ms 21080 KB Output is correct
9 Correct 4 ms 21080 KB Output is correct
10 Correct 3 ms 21084 KB Output is correct
11 Correct 4 ms 21084 KB Output is correct
12 Correct 5 ms 21084 KB Output is correct
13 Correct 4 ms 21084 KB Output is correct
14 Correct 4 ms 21084 KB Output is correct
15 Correct 4 ms 21084 KB Output is correct
16 Correct 4 ms 21084 KB Output is correct
17 Correct 4 ms 21084 KB Output is correct
18 Correct 8 ms 21852 KB Output is correct
19 Correct 10 ms 21904 KB Output is correct
20 Correct 8 ms 21848 KB Output is correct
21 Correct 8 ms 21852 KB Output is correct
22 Correct 8 ms 21848 KB Output is correct
23 Correct 8 ms 21852 KB Output is correct
24 Correct 8 ms 21852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 21080 KB Output is correct
2 Correct 5 ms 21092 KB Output is correct
3 Correct 4 ms 21084 KB Output is correct
4 Correct 3 ms 21084 KB Output is correct
5 Correct 4 ms 21084 KB Output is correct
6 Correct 3 ms 21084 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Correct 4 ms 21080 KB Output is correct
9 Correct 4 ms 21080 KB Output is correct
10 Correct 3 ms 21084 KB Output is correct
11 Correct 4 ms 21084 KB Output is correct
12 Correct 5 ms 21084 KB Output is correct
13 Correct 4 ms 21084 KB Output is correct
14 Correct 4 ms 21084 KB Output is correct
15 Correct 4 ms 21084 KB Output is correct
16 Correct 4 ms 21084 KB Output is correct
17 Correct 4 ms 21084 KB Output is correct
18 Correct 8 ms 21852 KB Output is correct
19 Correct 10 ms 21904 KB Output is correct
20 Correct 8 ms 21848 KB Output is correct
21 Correct 8 ms 21852 KB Output is correct
22 Correct 8 ms 21848 KB Output is correct
23 Correct 8 ms 21852 KB Output is correct
24 Correct 8 ms 21852 KB Output is correct
25 Correct 3 ms 21084 KB Output is correct
26 Correct 8 ms 21596 KB Output is correct
27 Correct 8 ms 21848 KB Output is correct
28 Correct 8 ms 21592 KB Output is correct
29 Correct 8 ms 21632 KB Output is correct
30 Correct 9 ms 21616 KB Output is correct
31 Correct 8 ms 21596 KB Output is correct
32 Correct 9 ms 21596 KB Output is correct
33 Correct 8 ms 21592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 21080 KB Output is correct
2 Correct 5 ms 21092 KB Output is correct
3 Correct 4 ms 21084 KB Output is correct
4 Correct 3 ms 21084 KB Output is correct
5 Correct 4 ms 21084 KB Output is correct
6 Correct 3 ms 21084 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Correct 4 ms 21080 KB Output is correct
9 Correct 4 ms 21080 KB Output is correct
10 Correct 3 ms 21084 KB Output is correct
11 Correct 4 ms 21084 KB Output is correct
12 Correct 5 ms 21084 KB Output is correct
13 Correct 4 ms 21084 KB Output is correct
14 Correct 4 ms 21084 KB Output is correct
15 Correct 4 ms 21084 KB Output is correct
16 Correct 4 ms 21084 KB Output is correct
17 Correct 4 ms 21084 KB Output is correct
18 Correct 8 ms 21852 KB Output is correct
19 Correct 10 ms 21904 KB Output is correct
20 Correct 8 ms 21848 KB Output is correct
21 Correct 8 ms 21852 KB Output is correct
22 Correct 8 ms 21848 KB Output is correct
23 Correct 8 ms 21852 KB Output is correct
24 Correct 8 ms 21852 KB Output is correct
25 Correct 226 ms 60688 KB Output is correct
26 Correct 213 ms 62732 KB Output is correct
27 Correct 202 ms 63648 KB Output is correct
28 Correct 248 ms 64448 KB Output is correct
29 Correct 287 ms 63988 KB Output is correct
30 Correct 253 ms 64084 KB Output is correct
31 Correct 255 ms 64124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 21080 KB Output is correct
2 Correct 4 ms 21084 KB Output is correct
3 Correct 278 ms 48992 KB Output is correct
4 Correct 292 ms 51312 KB Output is correct
5 Correct 279 ms 52496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 21080 KB Output is correct
2 Correct 5 ms 21092 KB Output is correct
3 Correct 4 ms 21084 KB Output is correct
4 Correct 3 ms 21084 KB Output is correct
5 Correct 4 ms 21084 KB Output is correct
6 Correct 3 ms 21084 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Correct 4 ms 21080 KB Output is correct
9 Correct 4 ms 21080 KB Output is correct
10 Correct 3 ms 21084 KB Output is correct
11 Correct 4 ms 21084 KB Output is correct
12 Correct 5 ms 21084 KB Output is correct
13 Correct 4 ms 21084 KB Output is correct
14 Correct 4 ms 21084 KB Output is correct
15 Correct 4 ms 21084 KB Output is correct
16 Correct 4 ms 21084 KB Output is correct
17 Correct 4 ms 21084 KB Output is correct
18 Correct 8 ms 21852 KB Output is correct
19 Correct 10 ms 21904 KB Output is correct
20 Correct 8 ms 21848 KB Output is correct
21 Correct 8 ms 21852 KB Output is correct
22 Correct 8 ms 21848 KB Output is correct
23 Correct 8 ms 21852 KB Output is correct
24 Correct 8 ms 21852 KB Output is correct
25 Correct 3 ms 21084 KB Output is correct
26 Correct 8 ms 21596 KB Output is correct
27 Correct 8 ms 21848 KB Output is correct
28 Correct 8 ms 21592 KB Output is correct
29 Correct 8 ms 21632 KB Output is correct
30 Correct 9 ms 21616 KB Output is correct
31 Correct 8 ms 21596 KB Output is correct
32 Correct 9 ms 21596 KB Output is correct
33 Correct 8 ms 21592 KB Output is correct
34 Correct 226 ms 60688 KB Output is correct
35 Correct 213 ms 62732 KB Output is correct
36 Correct 202 ms 63648 KB Output is correct
37 Correct 248 ms 64448 KB Output is correct
38 Correct 287 ms 63988 KB Output is correct
39 Correct 253 ms 64084 KB Output is correct
40 Correct 255 ms 64124 KB Output is correct
41 Correct 4 ms 21080 KB Output is correct
42 Correct 4 ms 21084 KB Output is correct
43 Correct 278 ms 48992 KB Output is correct
44 Correct 292 ms 51312 KB Output is correct
45 Correct 279 ms 52496 KB Output is correct
46 Correct 300 ms 58492 KB Output is correct
47 Correct 315 ms 57688 KB Output is correct
48 Correct 321 ms 57444 KB Output is correct
49 Correct 339 ms 55844 KB Output is correct
50 Correct 421 ms 51184 KB Output is correct
51 Correct 341 ms 52928 KB Output is correct
52 Correct 403 ms 52540 KB Output is correct
53 Correct 364 ms 51804 KB Output is correct