Submission #45978

# Submission time Handle Problem Language Result Execution time Memory
45978 2018-04-16T19:33:01 Z octopuses Construction of Highway (JOI18_construction) C++17
16 / 100
1000 ms 23324 KB
#include <bits/stdc++.h>

#define ll long long
#define fr first
#define sc second
#define M -100000000000ll

using namespace std;
const int N = 100020;
map < int, int > mp;

int n, m, timer;
int a[N], in[N], tn[N], out[N], P[N][20], dep[N];
pair < int, int > g[N];
vector < pair < int, int > > c;
vector < int > G[N];
int T[4 * N];
ll t[4 * N];

void go(int v)
{
  for(int k = 1; k < 20; ++ k)
    P[v][k] = P[P[v][k - 1]][k - 1];
  dep[v] = dep[P[v][0]] + 1;
  in[v] = ++ timer;
  tn[timer] = v;
  for(auto x : G[v])
    P[x][0] = v, go(x);
  out[v] = timer + 1;
}

int lca(int v, int u)
{
  if(dep[u] < dep[v])
    swap(u, v);
  for(int k = 19; k >= 0; -- k)
    if(dep[P[u][k]] >= dep[v])
      u = P[u][k];
  if(u == v)
    return v;
  for(int k = 19; k >= 0; -- k)
    if(P[u][k] != P[v][k])
      u = P[u][k], v = P[v][k];
  return P[u][0];
}

void update(int v, int tl, int tr, int l, int x)
{
  if(tl > l || tr <= l)
    return;
  if(tl == tr - 1)
  {
    T[v] = x;
    return;
  }
  update(v + v, tl, tl + tr >> 1, l, x);
  update(v + v + 1, tl + tr >> 1, tr, l, x);
  T[v] = max(T[v + v], T[v + v + 1]);
}

int get(int v, int tl, int tr, int l, int r)
{
  if(tr <= l || r <= tl)
    return 0;
  if(l <= tl && tr <= r)
    return T[v];
  int x, y;
  x = get(v + v, tl, tl + tr >> 1, l, r);
  y = get(v + v + 1, tl + tr >> 1, tr, l, r);
  return max(x, y);
}

int right(int v, int tl, int tr, int ind, int x)
{
  if(tr <= ind)
    return 0;
  if(tl < ind)
  {
    int y = right(v + v, tl, tl + tr >> 1, ind, x);
    if(y)
      return y;
    return right(v + v + 1, tl + tr >> 1, tr, ind, x);
  }
  if(T[v] <= x)
    return 0;
  if(tl == tr - 1)
    return tn[tl];
  if(T[v + v] <= x)
    return right(v + v + 1, tl + tr >> 1, tr, ind, x);
  return right(v + v, tl, tl + tr >> 1, ind, x);
}

int left(int v, int tl, int tr, int ind, int x)
{
  if(tl >= ind)
    return 0;
  if(ind < tr)
  {
    int y = left(v + v + 1, tl + tr >> 1, tr, ind, x);
    if(y)
      return y;
    return left(v + v, tl, tl + tr >> 1, ind, x);
  }
  if(T[v] <= x)
    return 0;
  if(tl == tr - 1)
    return tn[tl];
  if(T[v + v + 1] <= x)
    return left(v + v, tl, tl + tr >> 1, ind, x);
  return left(v + v + 1, tl + tr >> 1, tr, ind, x);
}

void deep(int v)
{
  if(v == 0)
    return;
  int x = get(1, 1, n + 1, in[v], out[v]);
  //cout << v << endl;
  int r = right(1, 1, n + 1, out[v], x); r = lca(r, v);
  int l = left(1, 1, n + 1, in[v], x);   l = lca(l, v);
  l = dep[l] < dep[r] ? r : l;
  c.push_back({a[g[x].sc], dep[v] - dep[l]});
  deep(l);
}

void upd(int v, int tl, int tr, int l, ll x)
{
  if(tl > l || tr <= l)
    return;
  if(tl == tr - 1)
  {
    if(x < 0)
      t[v] = 0;
    else
      t[v] += x;
    return;
  }
  upd(v + v, tl, tl + tr >> 1, l, x);
  upd(v + v + 1, tl + tr >> 1, tr, l, x);
  t[v] = t[v + v] + t[v + v + 1];
}

ll gt(int v, int tl, int tr, int l)
{
  if(tl >= l)
    return 0;
  if(tr <= l)
    return t[v];
  return gt(v + v, tl, tl + tr >> 1, l) + gt(v + v + 1, tl + tr >> 1, tr, l);
}

int main()
{
  scanf("%d", &n);

  for(int i = 1; i <= n; ++ i)
    scanf("%d", &a[i]), mp[a[i]] = 1;
  m = 1;
  for(map < int, int >::iterator it = mp.begin(); it != mp.end(); it ++)
    it -> sc = m ++;
  for(int i = 1; i <= n; ++ i)
    a[i] = mp[a[i]];
  for(int i = 2; i <= n; ++ i)
  {
    scanf("%d %d", &g[i].fr, &g[i].sc);
    G[g[i].fr].push_back(g[i].sc);
  }
  go(1);
  update(1, 1, n + 1, in[1], 1);
  g[1].sc = 1;
  int S = 0;
  for(int i = 2; i <= n; ++ i)
  {
    c.clear();
    deep(g[i].fr);
    ll answer = 0;
    S += c.size();
    for(int j = 0; j < c.size(); ++ j)
    {
      answer += c[j].sc * gt(1, 1, m, c[j].fr);
      upd(1, 1, m, c[j].fr, c[j].sc);
    }
    for(int j = 0; j < c.size(); ++ j)
      upd(1, 1, m, c[j].fr, -10);
    printf("%lld\n", answer);
    update(1, 1, n + 1, in[g[i].sc], i);
    if(S > 15 * n)
      return 0;
  }
}

Compilation message

construction.cpp: In function 'void update(int, int, int, int, int)':
construction.cpp:56:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   update(v + v, tl, tl + tr >> 1, l, x);
                     ~~~^~~~
construction.cpp:57:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   update(v + v + 1, tl + tr >> 1, tr, l, x);
                     ~~~^~~~
construction.cpp: In function 'int get(int, int, int, int, int)':
construction.cpp:68:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   x = get(v + v, tl, tl + tr >> 1, l, r);
                      ~~~^~~~
construction.cpp:69:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   y = get(v + v + 1, tl + tr >> 1, tr, l, r);
                      ~~~^~~~
construction.cpp: In function 'int right(int, int, int, int, int)':
construction.cpp:79:33: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int y = right(v + v, tl, tl + tr >> 1, ind, x);
                              ~~~^~~~
construction.cpp:82:32: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     return right(v + v + 1, tl + tr >> 1, tr, ind, x);
                             ~~~^~~~
construction.cpp:89:32: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     return right(v + v + 1, tl + tr >> 1, tr, ind, x);
                             ~~~^~~~
construction.cpp:90:30: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   return right(v + v, tl, tl + tr >> 1, ind, x);
                           ~~~^~~~
construction.cpp: In function 'int left(int, int, int, int, int)':
construction.cpp:99:32: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int y = left(v + v + 1, tl + tr >> 1, tr, ind, x);
                             ~~~^~~~
construction.cpp:102:31: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     return left(v + v, tl, tl + tr >> 1, ind, x);
                            ~~~^~~~
construction.cpp:109:31: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     return left(v + v, tl, tl + tr >> 1, ind, x);
                            ~~~^~~~
construction.cpp:110:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   return left(v + v + 1, tl + tr >> 1, tr, ind, x);
                          ~~~^~~~
construction.cpp: In function 'void upd(int, int, int, int, long long int)':
construction.cpp:138:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   upd(v + v, tl, tl + tr >> 1, l, x);
                  ~~~^~~~
construction.cpp:139:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   upd(v + v + 1, tl + tr >> 1, tr, l, x);
                  ~~~^~~~
construction.cpp: In function 'long long int gt(int, int, int, int)':
construction.cpp:149:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   return gt(v + v, tl, tl + tr >> 1, l) + gt(v + v + 1, tl + tr >> 1, tr, l);
                        ~~~^~~~
construction.cpp:149:60: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   return gt(v + v, tl, tl + tr >> 1, l) + gt(v + v + 1, tl + tr >> 1, tr, l);
                                                         ~~~^~~~
construction.cpp: In function 'int main()':
construction.cpp:178:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j = 0; j < c.size(); ++ j)
                    ~~^~~~~~~~~~
construction.cpp:183:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j = 0; j < c.size(); ++ j)
                    ~~^~~~~~~~~~
construction.cpp:154:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &n);
   ~~~~~^~~~~~~~~~
construction.cpp:157:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &a[i]), mp[a[i]] = 1;
     ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
construction.cpp:165:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &g[i].fr, &g[i].sc);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2796 KB Output is correct
3 Correct 5 ms 2860 KB Output is correct
4 Correct 5 ms 2916 KB Output is correct
5 Correct 7 ms 2968 KB Output is correct
6 Correct 6 ms 3016 KB Output is correct
7 Correct 6 ms 3020 KB Output is correct
8 Correct 5 ms 3020 KB Output is correct
9 Correct 5 ms 3020 KB Output is correct
10 Correct 6 ms 3020 KB Output is correct
11 Correct 6 ms 3024 KB Output is correct
12 Correct 6 ms 3024 KB Output is correct
13 Correct 6 ms 3024 KB Output is correct
14 Correct 6 ms 3024 KB Output is correct
15 Correct 7 ms 3024 KB Output is correct
16 Correct 8 ms 3028 KB Output is correct
17 Correct 10 ms 3156 KB Output is correct
18 Correct 10 ms 3156 KB Output is correct
19 Correct 6 ms 3156 KB Output is correct
20 Correct 6 ms 3156 KB Output is correct
21 Correct 5 ms 3156 KB Output is correct
22 Correct 5 ms 3156 KB Output is correct
23 Correct 7 ms 3156 KB Output is correct
24 Correct 5 ms 3156 KB Output is correct
25 Correct 5 ms 3156 KB Output is correct
26 Correct 5 ms 3156 KB Output is correct
27 Correct 6 ms 3156 KB Output is correct
28 Correct 6 ms 3156 KB Output is correct
29 Correct 6 ms 3156 KB Output is correct
30 Correct 7 ms 3156 KB Output is correct
31 Correct 9 ms 3156 KB Output is correct
32 Correct 6 ms 3156 KB Output is correct
33 Correct 6 ms 3156 KB Output is correct
34 Correct 5 ms 3156 KB Output is correct
35 Correct 5 ms 3156 KB Output is correct
36 Correct 6 ms 3156 KB Output is correct
37 Correct 4 ms 3156 KB Output is correct
38 Correct 6 ms 3156 KB Output is correct
39 Correct 7 ms 3156 KB Output is correct
40 Correct 6 ms 3156 KB Output is correct
41 Correct 5 ms 3156 KB Output is correct
42 Correct 4 ms 3156 KB Output is correct
43 Correct 5 ms 3156 KB Output is correct
44 Correct 6 ms 3156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2796 KB Output is correct
3 Correct 5 ms 2860 KB Output is correct
4 Correct 5 ms 2916 KB Output is correct
5 Correct 7 ms 2968 KB Output is correct
6 Correct 6 ms 3016 KB Output is correct
7 Correct 6 ms 3020 KB Output is correct
8 Correct 5 ms 3020 KB Output is correct
9 Correct 5 ms 3020 KB Output is correct
10 Correct 6 ms 3020 KB Output is correct
11 Correct 6 ms 3024 KB Output is correct
12 Correct 6 ms 3024 KB Output is correct
13 Correct 6 ms 3024 KB Output is correct
14 Correct 6 ms 3024 KB Output is correct
15 Correct 7 ms 3024 KB Output is correct
16 Correct 8 ms 3028 KB Output is correct
17 Correct 10 ms 3156 KB Output is correct
18 Correct 10 ms 3156 KB Output is correct
19 Correct 6 ms 3156 KB Output is correct
20 Correct 6 ms 3156 KB Output is correct
21 Correct 5 ms 3156 KB Output is correct
22 Correct 5 ms 3156 KB Output is correct
23 Correct 7 ms 3156 KB Output is correct
24 Correct 5 ms 3156 KB Output is correct
25 Correct 5 ms 3156 KB Output is correct
26 Correct 5 ms 3156 KB Output is correct
27 Correct 6 ms 3156 KB Output is correct
28 Correct 6 ms 3156 KB Output is correct
29 Correct 6 ms 3156 KB Output is correct
30 Correct 7 ms 3156 KB Output is correct
31 Correct 9 ms 3156 KB Output is correct
32 Correct 6 ms 3156 KB Output is correct
33 Correct 6 ms 3156 KB Output is correct
34 Correct 5 ms 3156 KB Output is correct
35 Correct 5 ms 3156 KB Output is correct
36 Correct 6 ms 3156 KB Output is correct
37 Correct 4 ms 3156 KB Output is correct
38 Correct 6 ms 3156 KB Output is correct
39 Correct 7 ms 3156 KB Output is correct
40 Correct 6 ms 3156 KB Output is correct
41 Correct 5 ms 3156 KB Output is correct
42 Correct 4 ms 3156 KB Output is correct
43 Correct 5 ms 3156 KB Output is correct
44 Correct 6 ms 3156 KB Output is correct
45 Correct 11 ms 3196 KB Output is correct
46 Correct 31 ms 3708 KB Output is correct
47 Correct 39 ms 3708 KB Output is correct
48 Correct 34 ms 3852 KB Output is correct
49 Correct 13 ms 3984 KB Output is correct
50 Correct 19 ms 3996 KB Output is correct
51 Correct 13 ms 4012 KB Output is correct
52 Correct 14 ms 4012 KB Output is correct
53 Correct 15 ms 4012 KB Output is correct
54 Correct 16 ms 4012 KB Output is correct
55 Correct 18 ms 4012 KB Output is correct
56 Correct 16 ms 4012 KB Output is correct
57 Correct 52 ms 4012 KB Output is correct
58 Correct 55 ms 4012 KB Output is correct
59 Correct 60 ms 4012 KB Output is correct
60 Correct 55 ms 4012 KB Output is correct
61 Correct 16 ms 4012 KB Output is correct
62 Correct 16 ms 4012 KB Output is correct
63 Correct 16 ms 4012 KB Output is correct
64 Correct 22 ms 4012 KB Output is correct
65 Correct 23 ms 4012 KB Output is correct
66 Correct 27 ms 4012 KB Output is correct
67 Correct 38 ms 4012 KB Output is correct
68 Correct 10 ms 4012 KB Output is correct
69 Correct 14 ms 4012 KB Output is correct
70 Correct 12 ms 4012 KB Output is correct
71 Correct 12 ms 4012 KB Output is correct
72 Correct 66 ms 4012 KB Output is correct
73 Correct 45 ms 4012 KB Output is correct
74 Correct 13 ms 4012 KB Output is correct
75 Correct 16 ms 4012 KB Output is correct
76 Correct 14 ms 4012 KB Output is correct
77 Correct 16 ms 4012 KB Output is correct
78 Correct 12 ms 4012 KB Output is correct
79 Correct 12 ms 4012 KB Output is correct
80 Correct 12 ms 4012 KB Output is correct
81 Correct 17 ms 4012 KB Output is correct
82 Correct 17 ms 4012 KB Output is correct
83 Correct 19 ms 4012 KB Output is correct
84 Correct 14 ms 4012 KB Output is correct
85 Correct 14 ms 4012 KB Output is correct
86 Correct 14 ms 4012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2796 KB Output is correct
3 Correct 5 ms 2860 KB Output is correct
4 Correct 5 ms 2916 KB Output is correct
5 Correct 7 ms 2968 KB Output is correct
6 Correct 6 ms 3016 KB Output is correct
7 Correct 6 ms 3020 KB Output is correct
8 Correct 5 ms 3020 KB Output is correct
9 Correct 5 ms 3020 KB Output is correct
10 Correct 6 ms 3020 KB Output is correct
11 Correct 6 ms 3024 KB Output is correct
12 Correct 6 ms 3024 KB Output is correct
13 Correct 6 ms 3024 KB Output is correct
14 Correct 6 ms 3024 KB Output is correct
15 Correct 7 ms 3024 KB Output is correct
16 Correct 8 ms 3028 KB Output is correct
17 Correct 10 ms 3156 KB Output is correct
18 Correct 10 ms 3156 KB Output is correct
19 Correct 6 ms 3156 KB Output is correct
20 Correct 6 ms 3156 KB Output is correct
21 Correct 5 ms 3156 KB Output is correct
22 Correct 5 ms 3156 KB Output is correct
23 Correct 7 ms 3156 KB Output is correct
24 Correct 5 ms 3156 KB Output is correct
25 Correct 5 ms 3156 KB Output is correct
26 Correct 5 ms 3156 KB Output is correct
27 Correct 6 ms 3156 KB Output is correct
28 Correct 6 ms 3156 KB Output is correct
29 Correct 6 ms 3156 KB Output is correct
30 Correct 7 ms 3156 KB Output is correct
31 Correct 9 ms 3156 KB Output is correct
32 Correct 6 ms 3156 KB Output is correct
33 Correct 6 ms 3156 KB Output is correct
34 Correct 5 ms 3156 KB Output is correct
35 Correct 5 ms 3156 KB Output is correct
36 Correct 6 ms 3156 KB Output is correct
37 Correct 4 ms 3156 KB Output is correct
38 Correct 6 ms 3156 KB Output is correct
39 Correct 7 ms 3156 KB Output is correct
40 Correct 6 ms 3156 KB Output is correct
41 Correct 5 ms 3156 KB Output is correct
42 Correct 4 ms 3156 KB Output is correct
43 Correct 5 ms 3156 KB Output is correct
44 Correct 6 ms 3156 KB Output is correct
45 Correct 11 ms 3196 KB Output is correct
46 Correct 31 ms 3708 KB Output is correct
47 Correct 39 ms 3708 KB Output is correct
48 Correct 34 ms 3852 KB Output is correct
49 Correct 13 ms 3984 KB Output is correct
50 Correct 19 ms 3996 KB Output is correct
51 Correct 13 ms 4012 KB Output is correct
52 Correct 14 ms 4012 KB Output is correct
53 Correct 15 ms 4012 KB Output is correct
54 Correct 16 ms 4012 KB Output is correct
55 Correct 18 ms 4012 KB Output is correct
56 Correct 16 ms 4012 KB Output is correct
57 Correct 52 ms 4012 KB Output is correct
58 Correct 55 ms 4012 KB Output is correct
59 Correct 60 ms 4012 KB Output is correct
60 Correct 55 ms 4012 KB Output is correct
61 Correct 16 ms 4012 KB Output is correct
62 Correct 16 ms 4012 KB Output is correct
63 Correct 16 ms 4012 KB Output is correct
64 Correct 22 ms 4012 KB Output is correct
65 Correct 23 ms 4012 KB Output is correct
66 Correct 27 ms 4012 KB Output is correct
67 Correct 38 ms 4012 KB Output is correct
68 Correct 10 ms 4012 KB Output is correct
69 Correct 14 ms 4012 KB Output is correct
70 Correct 12 ms 4012 KB Output is correct
71 Correct 12 ms 4012 KB Output is correct
72 Correct 66 ms 4012 KB Output is correct
73 Correct 45 ms 4012 KB Output is correct
74 Correct 13 ms 4012 KB Output is correct
75 Correct 16 ms 4012 KB Output is correct
76 Correct 14 ms 4012 KB Output is correct
77 Correct 16 ms 4012 KB Output is correct
78 Correct 12 ms 4012 KB Output is correct
79 Correct 12 ms 4012 KB Output is correct
80 Correct 12 ms 4012 KB Output is correct
81 Correct 17 ms 4012 KB Output is correct
82 Correct 17 ms 4012 KB Output is correct
83 Correct 19 ms 4012 KB Output is correct
84 Correct 14 ms 4012 KB Output is correct
85 Correct 14 ms 4012 KB Output is correct
86 Correct 14 ms 4012 KB Output is correct
87 Correct 93 ms 5172 KB Output is correct
88 Correct 315 ms 9020 KB Output is correct
89 Execution timed out 1085 ms 23324 KB Time limit exceeded
90 Halted 0 ms 0 KB -