Submission #1116939

# Submission time Handle Problem Language Result Execution time Memory
1116939 2024-11-22T15:42:46 Z ntdaccode Cat Exercise (JOI23_ho_t4) C++17
100 / 100
478 ms 203108 KB
#include<bits/stdc++.h>
#define int long long

using namespace std;

typedef pair<int,int> ii;

const int M=5e5+10;

int n,p[M];
vector<int> ke[M];
int num[M],tail[M],cnt = 0,d[M],par[M];
ii rmq[M][20];
int lg[M],pos[M],id = 0;
int arr[M];
void dfs(int u,int p)
{
   num[u] = ++ cnt;
   arr[cnt] = u;
   pos[u] = ++id;
  rmq[id][0] = {d[u], u};
   for(int v : ke[u]) {
      if(v == p) continue;
      d[v] = d[u] + 1;
      par[v] = u;
      dfs(v,u);
      rmq[++id][0] = {d[u], u};
   }
   tail[u] = cnt;
}

bool anc(int u ,int v)
{
   return num[u] <= num[v] && tail[u] >= tail[v];
}

int lca(int u, int v){
  int l = pos[u], r = pos[v];
  if(l > r) swap(l, r);
  int k = lg[r - l + 1];
  return min(rmq[l][k], rmq[r-(1<<k)+1][k]).second;
}

int dist(int u,int v)
{
   int x = lca(u,v);
   return d[u] + d[v] - 2 * d[x];
}


ii t[4 * M];
int lz[4 * M];
void lazy(int s)
{
   t[s * 2].first += lz[s];
   t[s * 2 + 1].first += lz[s];
   lz[s * 2] += lz[s];
   lz[s * 2 + 1] += lz[s];
   lz[s] = 0;
   return ;
}
void build(int s,int l,int r)
{
   if(l == r) {
      t[s] = {p[arr[l]],arr[l]};
      return ;
   }
   int mid = (l + r)/2;
   build(s * 2,l,mid);
   build(s * 2 + 1,mid + 1,r);
   t[s] = max(t[s * 2], t[s * 2 + 1]);
}

void upd(int s,int l,int r,int u,int v,int val)
{
   if(v < l || r < u) return ;
   if(u <= l && r <= v) {
      t[s].first += val;
      lz[s] += val;
      return ;
   }
   int mid = (l + r)/2;
   if(l != r && lz[s]) lazy(s);
   upd(s * 2,l,mid,u,v,val);
   upd(s * 2 + 1,mid + 1,r,u,v,val);
   t[s] = max(t[s * 2],t[s * 2 + 1]);
}
int f[M];
bool dd[M];
void solve(int u)
{
   dd[u] = true;
   upd(1,1,n,num[u],tail[u],-1e9);

   int nxt = t[1].second;
   if(t[1].first > 0 && !dd[nxt]) {
   solve(nxt);
   f[u] = max(f[u],f[nxt] + dist(u,nxt));
   }

   upd(1,1,n,num[u],tail[u],1e9);
   upd(1,1,n,1,n,-1e9);

   for(int v : ke[u]) {
      if(v == par[u] || dd[v]) continue;
      if(p[v] == t[1].first) continue;
      upd(1,1,n,num[v],tail[v],1e9);

      nxt = t[1].second;
      if(t[1].first > 0) {
         solve(nxt);
         f[u] = max(f[u],f[nxt] + dist(u,nxt));
      }

      upd(1,1,n,num[v],tail[v],-1e9);
   }
   upd(1,1,n,1,n,1e9);
}

int32_t main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if(fopen("1.inp","r"))
  {
    freopen("1.inp","r",stdin);
    freopen("1.out","w",stdout);
  }
  #define task ""
  if(fopen(task".inp","r"))
  {
    freopen(task".inp","r",stdin);
    freopen(task".out","w",stdout);
  }

  cin >> n ;
  for(int i =1;i <= n; i++) cin >> p[i];
  for(int i =1;i <= n - 1; i++) {
      int u,v;
      cin >> u >> v;
      ke[u].push_back(v);
      ke[v].push_back(u);
  }
  dfs(1,0);
   for(int j = 1; j <= 19; j++)
    for(int i = 1; i + (1 << j) - 1 <= id; i++)
      rmq[i][j] = min(rmq[i][j-1], rmq[i+(1<<(j-1))][j-1]);

  for(int i = 2; i <= id; i++) lg[i] = lg[i >> 1] + 1;
  build(1,1,n);
  int x = t[1].second;
  solve(x);
  cout << f[x];

}

Compilation message

Main.cpp: In function 'int32_t main()':
Main.cpp:127:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |     freopen("1.inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp:128:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |     freopen("1.out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:133:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  133 |     freopen(task".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:134:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  134 |     freopen(task".out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 24912 KB Output is correct
2 Correct 4 ms 24912 KB Output is correct
3 Correct 6 ms 24912 KB Output is correct
4 Correct 4 ms 26960 KB Output is correct
5 Correct 5 ms 24920 KB Output is correct
6 Correct 4 ms 26960 KB Output is correct
7 Correct 4 ms 24912 KB Output is correct
8 Correct 5 ms 25048 KB Output is correct
9 Correct 5 ms 26960 KB Output is correct
10 Correct 5 ms 24912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 24912 KB Output is correct
2 Correct 4 ms 24912 KB Output is correct
3 Correct 6 ms 24912 KB Output is correct
4 Correct 4 ms 26960 KB Output is correct
5 Correct 5 ms 24920 KB Output is correct
6 Correct 4 ms 26960 KB Output is correct
7 Correct 4 ms 24912 KB Output is correct
8 Correct 5 ms 25048 KB Output is correct
9 Correct 5 ms 26960 KB Output is correct
10 Correct 5 ms 24912 KB Output is correct
11 Correct 5 ms 27216 KB Output is correct
12 Correct 5 ms 25168 KB Output is correct
13 Correct 5 ms 25168 KB Output is correct
14 Correct 4 ms 25168 KB Output is correct
15 Correct 5 ms 25168 KB Output is correct
16 Correct 5 ms 25092 KB Output is correct
17 Correct 6 ms 25168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 24912 KB Output is correct
2 Correct 4 ms 24912 KB Output is correct
3 Correct 6 ms 24912 KB Output is correct
4 Correct 4 ms 26960 KB Output is correct
5 Correct 5 ms 24920 KB Output is correct
6 Correct 4 ms 26960 KB Output is correct
7 Correct 4 ms 24912 KB Output is correct
8 Correct 5 ms 25048 KB Output is correct
9 Correct 5 ms 26960 KB Output is correct
10 Correct 5 ms 24912 KB Output is correct
11 Correct 5 ms 27216 KB Output is correct
12 Correct 5 ms 25168 KB Output is correct
13 Correct 5 ms 25168 KB Output is correct
14 Correct 4 ms 25168 KB Output is correct
15 Correct 5 ms 25168 KB Output is correct
16 Correct 5 ms 25092 KB Output is correct
17 Correct 6 ms 25168 KB Output is correct
18 Correct 9 ms 31824 KB Output is correct
19 Correct 9 ms 30032 KB Output is correct
20 Correct 8 ms 30032 KB Output is correct
21 Correct 13 ms 30032 KB Output is correct
22 Correct 8 ms 30032 KB Output is correct
23 Correct 12 ms 30032 KB Output is correct
24 Correct 13 ms 30032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 24912 KB Output is correct
2 Correct 4 ms 24912 KB Output is correct
3 Correct 6 ms 24912 KB Output is correct
4 Correct 4 ms 26960 KB Output is correct
5 Correct 5 ms 24920 KB Output is correct
6 Correct 4 ms 26960 KB Output is correct
7 Correct 4 ms 24912 KB Output is correct
8 Correct 5 ms 25048 KB Output is correct
9 Correct 5 ms 26960 KB Output is correct
10 Correct 5 ms 24912 KB Output is correct
11 Correct 5 ms 27216 KB Output is correct
12 Correct 5 ms 25168 KB Output is correct
13 Correct 5 ms 25168 KB Output is correct
14 Correct 4 ms 25168 KB Output is correct
15 Correct 5 ms 25168 KB Output is correct
16 Correct 5 ms 25092 KB Output is correct
17 Correct 6 ms 25168 KB Output is correct
18 Correct 9 ms 31824 KB Output is correct
19 Correct 9 ms 30032 KB Output is correct
20 Correct 8 ms 30032 KB Output is correct
21 Correct 13 ms 30032 KB Output is correct
22 Correct 8 ms 30032 KB Output is correct
23 Correct 12 ms 30032 KB Output is correct
24 Correct 13 ms 30032 KB Output is correct
25 Correct 6 ms 24912 KB Output is correct
26 Correct 9 ms 30032 KB Output is correct
27 Correct 10 ms 30200 KB Output is correct
28 Correct 9 ms 30288 KB Output is correct
29 Correct 11 ms 30032 KB Output is correct
30 Correct 9 ms 29760 KB Output is correct
31 Correct 10 ms 29688 KB Output is correct
32 Correct 9 ms 29776 KB Output is correct
33 Correct 14 ms 29776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 24912 KB Output is correct
2 Correct 4 ms 24912 KB Output is correct
3 Correct 6 ms 24912 KB Output is correct
4 Correct 4 ms 26960 KB Output is correct
5 Correct 5 ms 24920 KB Output is correct
6 Correct 4 ms 26960 KB Output is correct
7 Correct 4 ms 24912 KB Output is correct
8 Correct 5 ms 25048 KB Output is correct
9 Correct 5 ms 26960 KB Output is correct
10 Correct 5 ms 24912 KB Output is correct
11 Correct 5 ms 27216 KB Output is correct
12 Correct 5 ms 25168 KB Output is correct
13 Correct 5 ms 25168 KB Output is correct
14 Correct 4 ms 25168 KB Output is correct
15 Correct 5 ms 25168 KB Output is correct
16 Correct 5 ms 25092 KB Output is correct
17 Correct 6 ms 25168 KB Output is correct
18 Correct 9 ms 31824 KB Output is correct
19 Correct 9 ms 30032 KB Output is correct
20 Correct 8 ms 30032 KB Output is correct
21 Correct 13 ms 30032 KB Output is correct
22 Correct 8 ms 30032 KB Output is correct
23 Correct 12 ms 30032 KB Output is correct
24 Correct 13 ms 30032 KB Output is correct
25 Correct 336 ms 201372 KB Output is correct
26 Correct 285 ms 202740 KB Output is correct
27 Correct 310 ms 202796 KB Output is correct
28 Correct 286 ms 202972 KB Output is correct
29 Correct 309 ms 202076 KB Output is correct
30 Correct 295 ms 203108 KB Output is correct
31 Correct 274 ms 201180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 24912 KB Output is correct
2 Correct 5 ms 24924 KB Output is correct
3 Correct 333 ms 188600 KB Output is correct
4 Correct 315 ms 188588 KB Output is correct
5 Correct 334 ms 188496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 24912 KB Output is correct
2 Correct 4 ms 24912 KB Output is correct
3 Correct 6 ms 24912 KB Output is correct
4 Correct 4 ms 26960 KB Output is correct
5 Correct 5 ms 24920 KB Output is correct
6 Correct 4 ms 26960 KB Output is correct
7 Correct 4 ms 24912 KB Output is correct
8 Correct 5 ms 25048 KB Output is correct
9 Correct 5 ms 26960 KB Output is correct
10 Correct 5 ms 24912 KB Output is correct
11 Correct 5 ms 27216 KB Output is correct
12 Correct 5 ms 25168 KB Output is correct
13 Correct 5 ms 25168 KB Output is correct
14 Correct 4 ms 25168 KB Output is correct
15 Correct 5 ms 25168 KB Output is correct
16 Correct 5 ms 25092 KB Output is correct
17 Correct 6 ms 25168 KB Output is correct
18 Correct 9 ms 31824 KB Output is correct
19 Correct 9 ms 30032 KB Output is correct
20 Correct 8 ms 30032 KB Output is correct
21 Correct 13 ms 30032 KB Output is correct
22 Correct 8 ms 30032 KB Output is correct
23 Correct 12 ms 30032 KB Output is correct
24 Correct 13 ms 30032 KB Output is correct
25 Correct 6 ms 24912 KB Output is correct
26 Correct 9 ms 30032 KB Output is correct
27 Correct 10 ms 30200 KB Output is correct
28 Correct 9 ms 30288 KB Output is correct
29 Correct 11 ms 30032 KB Output is correct
30 Correct 9 ms 29760 KB Output is correct
31 Correct 10 ms 29688 KB Output is correct
32 Correct 9 ms 29776 KB Output is correct
33 Correct 14 ms 29776 KB Output is correct
34 Correct 336 ms 201372 KB Output is correct
35 Correct 285 ms 202740 KB Output is correct
36 Correct 310 ms 202796 KB Output is correct
37 Correct 286 ms 202972 KB Output is correct
38 Correct 309 ms 202076 KB Output is correct
39 Correct 295 ms 203108 KB Output is correct
40 Correct 274 ms 201180 KB Output is correct
41 Correct 5 ms 24912 KB Output is correct
42 Correct 5 ms 24924 KB Output is correct
43 Correct 333 ms 188600 KB Output is correct
44 Correct 315 ms 188588 KB Output is correct
45 Correct 334 ms 188496 KB Output is correct
46 Correct 423 ms 202296 KB Output is correct
47 Correct 437 ms 201096 KB Output is correct
48 Correct 478 ms 201032 KB Output is correct
49 Correct 393 ms 201032 KB Output is correct
50 Correct 401 ms 190792 KB Output is correct
51 Correct 393 ms 190808 KB Output is correct
52 Correct 413 ms 190652 KB Output is correct
53 Correct 434 ms 190792 KB Output is correct