Submission #44042

# Submission time Handle Problem Language Result Execution time Memory
44042 2018-03-29T12:45:23 Z wxh010910 Construction of Highway (JOI18_construction) C++17
100 / 100
294 ms 81272 KB
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define Debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef long double LD;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef unsigned long long uLL;

template <typename T> inline void Read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}

template <typename T> inline bool CheckMax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <typename T> inline bool CheckMin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

const int N = 100005;

int n, top, c[N], d[N], f[N], siz[N], sta[N], val[N], chd[N][2];
vector <pii> seq;
LL ans, b[N];

inline bool IsRoot(int x) {
  return chd[f[x]][0] != x && chd[f[x]][1] != x;
}

inline void AddTag(int x, int y) {
  val[x] = c[x] = y;
}

inline void PushUp(int x) {
  siz[x] = siz[chd[x][0]] + siz[chd[x][1]] + 1;
}

inline void PushDown(int x) {
  if (val[x]) {
    AddTag(chd[x][0], val[x]), AddTag(chd[x][1], val[x]), val[x] = 0;
  }
}

inline void Rotate(int x) {
  int y = f[x], z = f[y], k = chd[y][1] == x;
  if (!IsRoot(y)) {
    chd[z][chd[z][1] == y] = x;
  }
  f[chd[y][k] = chd[x][!k]] = y;
  f[f[chd[x][!k] = y] = x] = z;
  PushUp(y), PushUp(x);
}

inline void Splay(int x) {
  sta[top = 1] = x;
  for (int i = x; !IsRoot(i); sta[++top] = i = f[i]);
  for (; top; PushDown(sta[top--]));
  for (int y = f[x], z = f[y]; !IsRoot(x); Rotate(x), y = f[x], z = f[y]) {
    if (!IsRoot(y)) {
      Rotate((chd[y][1] == x) == (chd[z][1] == y) ? y : x);
    }
  }
}

inline void Access(int x) {
  for (int t = 0; x; x = f[x]) {
    Splay(x), chd[x][1] = t, t = x, PushUp(x), seq.pb(mp(c[x], siz[x]));
  }
}

inline void Modify(int x, int y) {
  for (; x <= n; x += x & -x) {
    b[x] += y;
  }
}

inline LL Query(int x) {
  LL r = 0;
  for (; x; x -= x & -x) {
    r += b[x];
  }
  return r;
}

inline void Solve() {
  ans = 0;
  for (int i = seq.size() - 1; i; --i) {
    seq[i].Y -= seq[i - 1].Y;
  }
  for (int i = 0; i < seq.size(); ++i) {
    ans += 1LL * seq[i].Y * Query(seq[i].X - 1);
    Modify(seq[i].X, seq[i].Y);
  }
  printf("%lld\n", ans);
  for (int i = 0; i < seq.size(); ++i) {
    Modify(seq[i].X, -seq[i].Y);
  }
  seq.clear();
}

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  Read(n);
  for (int i = 1; i <= n; ++i) {
    Read(c[i]), d[i] = c[i], siz[i] = 1;
  }
  sort(d + 1, d + n + 1);
  for (int i = 1; i <= n; ++i) {
    c[i] = lower_bound(d + 1, d + n + 1, c[i]) - d;
  }
  for (int i = 1, x, y; i < n; ++i) {
    Read(x), Read(y);
    Access(x), Splay(x);
    AddTag(x, c[y]), f[y] = x, Solve();
  }
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}

Compilation message

construction.cpp: In function 'void Solve()':
construction.cpp:111:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < seq.size(); ++i) {
                   ~~^~~~~~~~~~~~
construction.cpp:116:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < seq.size(); ++i) {
                   ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 520 KB Output is correct
6 Correct 2 ms 732 KB Output is correct
7 Correct 2 ms 732 KB Output is correct
8 Correct 2 ms 732 KB Output is correct
9 Correct 3 ms 732 KB Output is correct
10 Correct 2 ms 732 KB Output is correct
11 Correct 2 ms 732 KB Output is correct
12 Correct 2 ms 732 KB Output is correct
13 Correct 2 ms 732 KB Output is correct
14 Correct 2 ms 732 KB Output is correct
15 Correct 2 ms 760 KB Output is correct
16 Correct 2 ms 764 KB Output is correct
17 Correct 2 ms 776 KB Output is correct
18 Correct 2 ms 788 KB Output is correct
19 Correct 3 ms 788 KB Output is correct
20 Correct 3 ms 832 KB Output is correct
21 Correct 2 ms 832 KB Output is correct
22 Correct 2 ms 836 KB Output is correct
23 Correct 2 ms 844 KB Output is correct
24 Correct 2 ms 928 KB Output is correct
25 Correct 2 ms 928 KB Output is correct
26 Correct 2 ms 928 KB Output is correct
27 Correct 2 ms 928 KB Output is correct
28 Correct 2 ms 964 KB Output is correct
29 Correct 2 ms 964 KB Output is correct
30 Correct 2 ms 964 KB Output is correct
31 Correct 2 ms 964 KB Output is correct
32 Correct 2 ms 964 KB Output is correct
33 Correct 2 ms 996 KB Output is correct
34 Correct 2 ms 1004 KB Output is correct
35 Correct 2 ms 1012 KB Output is correct
36 Correct 2 ms 1020 KB Output is correct
37 Correct 2 ms 1036 KB Output is correct
38 Correct 2 ms 1044 KB Output is correct
39 Correct 2 ms 1056 KB Output is correct
40 Correct 2 ms 1060 KB Output is correct
41 Correct 3 ms 1196 KB Output is correct
42 Correct 2 ms 1196 KB Output is correct
43 Correct 2 ms 1196 KB Output is correct
44 Correct 2 ms 1196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 520 KB Output is correct
6 Correct 2 ms 732 KB Output is correct
7 Correct 2 ms 732 KB Output is correct
8 Correct 2 ms 732 KB Output is correct
9 Correct 3 ms 732 KB Output is correct
10 Correct 2 ms 732 KB Output is correct
11 Correct 2 ms 732 KB Output is correct
12 Correct 2 ms 732 KB Output is correct
13 Correct 2 ms 732 KB Output is correct
14 Correct 2 ms 732 KB Output is correct
15 Correct 2 ms 760 KB Output is correct
16 Correct 2 ms 764 KB Output is correct
17 Correct 2 ms 776 KB Output is correct
18 Correct 2 ms 788 KB Output is correct
19 Correct 3 ms 788 KB Output is correct
20 Correct 3 ms 832 KB Output is correct
21 Correct 2 ms 832 KB Output is correct
22 Correct 2 ms 836 KB Output is correct
23 Correct 2 ms 844 KB Output is correct
24 Correct 2 ms 928 KB Output is correct
25 Correct 2 ms 928 KB Output is correct
26 Correct 2 ms 928 KB Output is correct
27 Correct 2 ms 928 KB Output is correct
28 Correct 2 ms 964 KB Output is correct
29 Correct 2 ms 964 KB Output is correct
30 Correct 2 ms 964 KB Output is correct
31 Correct 2 ms 964 KB Output is correct
32 Correct 2 ms 964 KB Output is correct
33 Correct 2 ms 996 KB Output is correct
34 Correct 2 ms 1004 KB Output is correct
35 Correct 2 ms 1012 KB Output is correct
36 Correct 2 ms 1020 KB Output is correct
37 Correct 2 ms 1036 KB Output is correct
38 Correct 2 ms 1044 KB Output is correct
39 Correct 2 ms 1056 KB Output is correct
40 Correct 2 ms 1060 KB Output is correct
41 Correct 3 ms 1196 KB Output is correct
42 Correct 2 ms 1196 KB Output is correct
43 Correct 2 ms 1196 KB Output is correct
44 Correct 2 ms 1196 KB Output is correct
45 Correct 4 ms 1196 KB Output is correct
46 Correct 6 ms 1388 KB Output is correct
47 Correct 6 ms 1388 KB Output is correct
48 Correct 8 ms 1548 KB Output is correct
49 Correct 5 ms 1548 KB Output is correct
50 Correct 4 ms 1592 KB Output is correct
51 Correct 4 ms 1668 KB Output is correct
52 Correct 6 ms 1872 KB Output is correct
53 Correct 5 ms 1932 KB Output is correct
54 Correct 6 ms 2008 KB Output is correct
55 Correct 5 ms 2084 KB Output is correct
56 Correct 6 ms 2128 KB Output is correct
57 Correct 10 ms 2204 KB Output is correct
58 Correct 8 ms 2284 KB Output is correct
59 Correct 7 ms 2364 KB Output is correct
60 Correct 7 ms 2444 KB Output is correct
61 Correct 5 ms 2520 KB Output is correct
62 Correct 6 ms 2520 KB Output is correct
63 Correct 5 ms 2560 KB Output is correct
64 Correct 7 ms 2636 KB Output is correct
65 Correct 5 ms 2736 KB Output is correct
66 Correct 6 ms 2736 KB Output is correct
67 Correct 6 ms 2788 KB Output is correct
68 Correct 4 ms 2840 KB Output is correct
69 Correct 5 ms 3020 KB Output is correct
70 Correct 4 ms 3068 KB Output is correct
71 Correct 4 ms 3068 KB Output is correct
72 Correct 7 ms 3068 KB Output is correct
73 Correct 7 ms 3100 KB Output is correct
74 Correct 5 ms 3180 KB Output is correct
75 Correct 4 ms 3196 KB Output is correct
76 Correct 4 ms 3268 KB Output is correct
77 Correct 4 ms 3324 KB Output is correct
78 Correct 4 ms 3388 KB Output is correct
79 Correct 5 ms 3436 KB Output is correct
80 Correct 4 ms 3516 KB Output is correct
81 Correct 5 ms 3600 KB Output is correct
82 Correct 5 ms 3676 KB Output is correct
83 Correct 5 ms 3880 KB Output is correct
84 Correct 5 ms 3880 KB Output is correct
85 Correct 5 ms 3880 KB Output is correct
86 Correct 5 ms 3880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 520 KB Output is correct
6 Correct 2 ms 732 KB Output is correct
7 Correct 2 ms 732 KB Output is correct
8 Correct 2 ms 732 KB Output is correct
9 Correct 3 ms 732 KB Output is correct
10 Correct 2 ms 732 KB Output is correct
11 Correct 2 ms 732 KB Output is correct
12 Correct 2 ms 732 KB Output is correct
13 Correct 2 ms 732 KB Output is correct
14 Correct 2 ms 732 KB Output is correct
15 Correct 2 ms 760 KB Output is correct
16 Correct 2 ms 764 KB Output is correct
17 Correct 2 ms 776 KB Output is correct
18 Correct 2 ms 788 KB Output is correct
19 Correct 3 ms 788 KB Output is correct
20 Correct 3 ms 832 KB Output is correct
21 Correct 2 ms 832 KB Output is correct
22 Correct 2 ms 836 KB Output is correct
23 Correct 2 ms 844 KB Output is correct
24 Correct 2 ms 928 KB Output is correct
25 Correct 2 ms 928 KB Output is correct
26 Correct 2 ms 928 KB Output is correct
27 Correct 2 ms 928 KB Output is correct
28 Correct 2 ms 964 KB Output is correct
29 Correct 2 ms 964 KB Output is correct
30 Correct 2 ms 964 KB Output is correct
31 Correct 2 ms 964 KB Output is correct
32 Correct 2 ms 964 KB Output is correct
33 Correct 2 ms 996 KB Output is correct
34 Correct 2 ms 1004 KB Output is correct
35 Correct 2 ms 1012 KB Output is correct
36 Correct 2 ms 1020 KB Output is correct
37 Correct 2 ms 1036 KB Output is correct
38 Correct 2 ms 1044 KB Output is correct
39 Correct 2 ms 1056 KB Output is correct
40 Correct 2 ms 1060 KB Output is correct
41 Correct 3 ms 1196 KB Output is correct
42 Correct 2 ms 1196 KB Output is correct
43 Correct 2 ms 1196 KB Output is correct
44 Correct 2 ms 1196 KB Output is correct
45 Correct 4 ms 1196 KB Output is correct
46 Correct 6 ms 1388 KB Output is correct
47 Correct 6 ms 1388 KB Output is correct
48 Correct 8 ms 1548 KB Output is correct
49 Correct 5 ms 1548 KB Output is correct
50 Correct 4 ms 1592 KB Output is correct
51 Correct 4 ms 1668 KB Output is correct
52 Correct 6 ms 1872 KB Output is correct
53 Correct 5 ms 1932 KB Output is correct
54 Correct 6 ms 2008 KB Output is correct
55 Correct 5 ms 2084 KB Output is correct
56 Correct 6 ms 2128 KB Output is correct
57 Correct 10 ms 2204 KB Output is correct
58 Correct 8 ms 2284 KB Output is correct
59 Correct 7 ms 2364 KB Output is correct
60 Correct 7 ms 2444 KB Output is correct
61 Correct 5 ms 2520 KB Output is correct
62 Correct 6 ms 2520 KB Output is correct
63 Correct 5 ms 2560 KB Output is correct
64 Correct 7 ms 2636 KB Output is correct
65 Correct 5 ms 2736 KB Output is correct
66 Correct 6 ms 2736 KB Output is correct
67 Correct 6 ms 2788 KB Output is correct
68 Correct 4 ms 2840 KB Output is correct
69 Correct 5 ms 3020 KB Output is correct
70 Correct 4 ms 3068 KB Output is correct
71 Correct 4 ms 3068 KB Output is correct
72 Correct 7 ms 3068 KB Output is correct
73 Correct 7 ms 3100 KB Output is correct
74 Correct 5 ms 3180 KB Output is correct
75 Correct 4 ms 3196 KB Output is correct
76 Correct 4 ms 3268 KB Output is correct
77 Correct 4 ms 3324 KB Output is correct
78 Correct 4 ms 3388 KB Output is correct
79 Correct 5 ms 3436 KB Output is correct
80 Correct 4 ms 3516 KB Output is correct
81 Correct 5 ms 3600 KB Output is correct
82 Correct 5 ms 3676 KB Output is correct
83 Correct 5 ms 3880 KB Output is correct
84 Correct 5 ms 3880 KB Output is correct
85 Correct 5 ms 3880 KB Output is correct
86 Correct 5 ms 3880 KB Output is correct
87 Correct 14 ms 4252 KB Output is correct
88 Correct 46 ms 5596 KB Output is correct
89 Correct 189 ms 10568 KB Output is correct
90 Correct 200 ms 12560 KB Output is correct
91 Correct 194 ms 14788 KB Output is correct
92 Correct 108 ms 16664 KB Output is correct
93 Correct 77 ms 18840 KB Output is correct
94 Correct 86 ms 21028 KB Output is correct
95 Correct 94 ms 23372 KB Output is correct
96 Correct 87 ms 25524 KB Output is correct
97 Correct 98 ms 27632 KB Output is correct
98 Correct 94 ms 29680 KB Output is correct
99 Correct 90 ms 31496 KB Output is correct
100 Correct 249 ms 33044 KB Output is correct
101 Correct 286 ms 35080 KB Output is correct
102 Correct 282 ms 37128 KB Output is correct
103 Correct 294 ms 39448 KB Output is correct
104 Correct 158 ms 41560 KB Output is correct
105 Correct 156 ms 43740 KB Output is correct
106 Correct 165 ms 45708 KB Output is correct
107 Correct 151 ms 46376 KB Output is correct
108 Correct 156 ms 48216 KB Output is correct
109 Correct 177 ms 50136 KB Output is correct
110 Correct 71 ms 51584 KB Output is correct
111 Correct 108 ms 53960 KB Output is correct
112 Correct 77 ms 55532 KB Output is correct
113 Correct 77 ms 56756 KB Output is correct
114 Correct 257 ms 58172 KB Output is correct
115 Correct 277 ms 59772 KB Output is correct
116 Correct 145 ms 61396 KB Output is correct
117 Correct 83 ms 63044 KB Output is correct
118 Correct 91 ms 64816 KB Output is correct
119 Correct 119 ms 66820 KB Output is correct
120 Correct 78 ms 68148 KB Output is correct
121 Correct 83 ms 69800 KB Output is correct
122 Correct 96 ms 71180 KB Output is correct
123 Correct 169 ms 73200 KB Output is correct
124 Correct 166 ms 75068 KB Output is correct
125 Correct 172 ms 76820 KB Output is correct
126 Correct 198 ms 78340 KB Output is correct
127 Correct 165 ms 79848 KB Output is correct
128 Correct 149 ms 81272 KB Output is correct