답안 #960909

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
960909 2024-04-11T08:06:23 Z danikoynov Construction of Highway (JOI18_construction) C++14
100 / 100
402 ms 30440 KB
#include <bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
     ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     cout.tie(NULL);
}

const int maxn = 1e5 + 10;

struct edge
{
     int v, u;

     edge(int _v = 0, int _u = 0)
     {
          v = _v;
          u = _u;
     }
}edges[maxn];

int n, c[maxn];
vector < int > adj[maxn];

void input()
{
     cin >> n;
     for (int i = 1; i <= n; i ++)
          cin >> c[i];
     for (int i = 1; i < n; i ++)
     {
          int v, u;
          cin >> v >> u;
          adj[v].push_back(u);
          edges[i] = edge(v, u);
     }
}

int sub[maxn], depth[maxn], heavy[maxn], par[maxn];

void calc(int v)
{
     heavy[v] = -1;
     sub[v] = 1;
     for (int u : adj[v])
     {
          par[u] = v;
          depth[u] = depth[v] + 1;
          calc(u);
          sub[v] += sub[u];
          if (heavy[v] == -1 || sub[u] > sub[heavy[v]])
               heavy[v] = u;
     }
}

int cidx[maxn], chead[maxn], cpos[maxn];
vector < int > chain[maxn];
int csz = 1;
void hld(int v, int head)
{
     cidx[v] = csz;
     chead[v] = head;
     chain[cidx[v]].push_back(v);
     if (v == head)
          cpos[v] = 1;
     else
          cpos[v] = cpos[par[v]] + 1;

     if (heavy[v] != -1)
          hld(heavy[v], head);

     for (int u : adj[v])
     {
          if (u == heavy[v])
               continue;
          csz ++;
          hld(u, u);
     }
}


set < pair < int, int > > range[maxn];

vector < pair < int, int > > get_path_values(int v)
{
     vector < pair < int, int > > path;
     while(v != 0)
     {

          int d = cpos[v];
          while(d > 0)
          {

               set < pair < int, int > > :: iterator it = range[cidx[v]].lower_bound({d, n + 1});
               /**for (pair < int, int > cur : range[cidx[v]])
               cout << cur.first << " " << cur.second << endl;
               cout << d << endl;*/
               it = prev(it);
               path.push_back({d - it -> first + 1, c[chain[cidx[v]][it -> second - 1]]});
               d = it -> first - 1;
          }
          v = par[chead[v]];
     }
     reverse(path.begin(), path.end());
     return path;
}

void clear_path(int v)
{
     int col = c[v];
     range[cidx[v]].clear();
     range[cidx[v]].insert({1, cpos[v]});
     ///cout << "here " << 1 << " " << cpos[v] << endl;
     v = par[chead[v]];
     while(v != 0)
     {
          ///cout << "back " << v << endl;
          int d = cpos[v];
          ///cout << "cidx " << cidx[v] << "  " << d << endl;
          set < pair < int, int > > :: iterator it = range[cidx[v]].lower_bound({d, n + 1});
          it = prev(it);
          ///cout << it -> first << " " << it -> second << endl;
          pair < int, int > nw = {d + 1, it -> second};
          range[cidx[v]].erase(it);
          range[cidx[v]].insert(nw);

          while(true)
          {
               set < pair < int, int > > :: iterator it = range[cidx[v]].lower_bound({d, n + 1});

               if (it == range[cidx[v]].begin())
                    {
                         ///cout << it -> first << " --- " << it -> second << endl;
                         break;
                    }
               it = prev(it);
               range[cidx[v]].erase(it);
          }

          range[cidx[v]].insert({1, d});
          c[v] = col;

          v = par[chead[v]];

     }
}


ll fen[maxn];

void update(int pos, ll val)
{
     for (int i = pos; i <= n; i += (i & -i))
          fen[i] += val;
}

ll query(int pos)
{
     ll s = 0;
     for (int i = pos; i > 0; i -= (i & -i))
          s += fen[i];
     return s;
}

void find_inversions(vector < pair < int, int > > &path)
{
     ll ans = 0;
     int sz = path.size();
     for (int i = sz - 1; i >= 0; i --)
     {
          ans = ans + query(path[i].second - 1) * (ll)(path[i].first);
          update(path[i].second, path[i].first);
     }
     for (int i = 0; i < sz; i ++)
          update(path[i].second, -path[i].first);
     /**for (int i = 0; i < sz; i ++)
          for (int j = i + 1; j < sz; j ++)
          {
               if (path[i].second > path[j].second)
               {
                    ans = ans + (ll)(path[i].first) * (ll)(path[j].first);
               }
          }*/

     cout << ans << endl;

}
void simulate()
{
     //for (int i = 1; i <= n; i ++)
       //cout << i << " : " << cidx[i] <<  " " << cpos[i] << " " << heavy[i] << " " << sub[i] << endl;
     //exit(0);
     range[cidx[1]].insert({1, 1});
     for (int i = 1; i < n; i ++)
     {
          ///cout << "pass " << i << endl;
          vector < pair < int, int > > path = get_path_values(edges[i].v);
          //cout << "survive " << i << endl;
          //cout << "inversions  ";
          find_inversions(path);
          //cout << "path " << endl;
          //for (pair < int, int > cur : path)
               //cout << cur.first << " " << cur.second << endl;
          //cout << "path" << endl;
          //for (pair <int, int > cur : path)
            //   cout << cur.first << " " << cur.second << endl;
          clear_path(edges[i].u);
          /**cout << "edge " << i << endl;
          for (int i = 1; i <= csz; i ++)
          {
               cout << "chain " << i << endl;
               for (pair < int, int > cur : range[i])
                    cout << cur.first << " " << cur.second << endl;
          }
          if (i == 4)
               break;*/
     }
}

void compress_data()
{
     vector < int > d;
     for (int i = 1; i <= n; i ++)
          d.push_back(c[i]);
     sort(d.begin(), d.end());
     unordered_map < int, int > mp;
     for (int i = 0; i < n; i ++)
          mp[d[i]] = i + 1;
     for (int i = 1; i <= n; i ++)
          c[i] = mp[c[i]];
}
void solve()
{
     input();
     compress_data();
     calc(1);
     hld(1, 1);
     simulate();
}

int main()
{
     speed();
     solve();
     return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 4 ms 13764 KB Output is correct
4 Correct 3 ms 13916 KB Output is correct
5 Correct 4 ms 14040 KB Output is correct
6 Correct 4 ms 13916 KB Output is correct
7 Correct 4 ms 13916 KB Output is correct
8 Correct 3 ms 13912 KB Output is correct
9 Correct 3 ms 13912 KB Output is correct
10 Correct 4 ms 13916 KB Output is correct
11 Correct 3 ms 14168 KB Output is correct
12 Correct 3 ms 13912 KB Output is correct
13 Correct 3 ms 13916 KB Output is correct
14 Correct 4 ms 13916 KB Output is correct
15 Correct 3 ms 13916 KB Output is correct
16 Correct 5 ms 13912 KB Output is correct
17 Correct 4 ms 13916 KB Output is correct
18 Correct 3 ms 13916 KB Output is correct
19 Correct 3 ms 13912 KB Output is correct
20 Correct 4 ms 13916 KB Output is correct
21 Correct 3 ms 13844 KB Output is correct
22 Correct 3 ms 13916 KB Output is correct
23 Correct 4 ms 13916 KB Output is correct
24 Correct 4 ms 13916 KB Output is correct
25 Correct 4 ms 13916 KB Output is correct
26 Correct 3 ms 13916 KB Output is correct
27 Correct 3 ms 13916 KB Output is correct
28 Correct 3 ms 13916 KB Output is correct
29 Correct 3 ms 14040 KB Output is correct
30 Correct 4 ms 13916 KB Output is correct
31 Correct 4 ms 13916 KB Output is correct
32 Correct 4 ms 13916 KB Output is correct
33 Correct 3 ms 13916 KB Output is correct
34 Correct 4 ms 13916 KB Output is correct
35 Correct 4 ms 14164 KB Output is correct
36 Correct 3 ms 13916 KB Output is correct
37 Correct 3 ms 13916 KB Output is correct
38 Correct 3 ms 13916 KB Output is correct
39 Correct 4 ms 13916 KB Output is correct
40 Correct 3 ms 14068 KB Output is correct
41 Correct 3 ms 13916 KB Output is correct
42 Correct 4 ms 13916 KB Output is correct
43 Correct 3 ms 14168 KB Output is correct
44 Correct 4 ms 13916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 4 ms 13764 KB Output is correct
4 Correct 3 ms 13916 KB Output is correct
5 Correct 4 ms 14040 KB Output is correct
6 Correct 4 ms 13916 KB Output is correct
7 Correct 4 ms 13916 KB Output is correct
8 Correct 3 ms 13912 KB Output is correct
9 Correct 3 ms 13912 KB Output is correct
10 Correct 4 ms 13916 KB Output is correct
11 Correct 3 ms 14168 KB Output is correct
12 Correct 3 ms 13912 KB Output is correct
13 Correct 3 ms 13916 KB Output is correct
14 Correct 4 ms 13916 KB Output is correct
15 Correct 3 ms 13916 KB Output is correct
16 Correct 5 ms 13912 KB Output is correct
17 Correct 4 ms 13916 KB Output is correct
18 Correct 3 ms 13916 KB Output is correct
19 Correct 3 ms 13912 KB Output is correct
20 Correct 4 ms 13916 KB Output is correct
21 Correct 3 ms 13844 KB Output is correct
22 Correct 3 ms 13916 KB Output is correct
23 Correct 4 ms 13916 KB Output is correct
24 Correct 4 ms 13916 KB Output is correct
25 Correct 4 ms 13916 KB Output is correct
26 Correct 3 ms 13916 KB Output is correct
27 Correct 3 ms 13916 KB Output is correct
28 Correct 3 ms 13916 KB Output is correct
29 Correct 3 ms 14040 KB Output is correct
30 Correct 4 ms 13916 KB Output is correct
31 Correct 4 ms 13916 KB Output is correct
32 Correct 4 ms 13916 KB Output is correct
33 Correct 3 ms 13916 KB Output is correct
34 Correct 4 ms 13916 KB Output is correct
35 Correct 4 ms 14164 KB Output is correct
36 Correct 3 ms 13916 KB Output is correct
37 Correct 3 ms 13916 KB Output is correct
38 Correct 3 ms 13916 KB Output is correct
39 Correct 4 ms 13916 KB Output is correct
40 Correct 3 ms 14068 KB Output is correct
41 Correct 3 ms 13916 KB Output is correct
42 Correct 4 ms 13916 KB Output is correct
43 Correct 3 ms 14168 KB Output is correct
44 Correct 4 ms 13916 KB Output is correct
45 Correct 4 ms 13916 KB Output is correct
46 Correct 8 ms 14168 KB Output is correct
47 Correct 8 ms 14172 KB Output is correct
48 Correct 9 ms 14172 KB Output is correct
49 Correct 5 ms 14680 KB Output is correct
50 Correct 8 ms 14684 KB Output is correct
51 Correct 5 ms 14684 KB Output is correct
52 Correct 6 ms 14428 KB Output is correct
53 Correct 6 ms 14428 KB Output is correct
54 Correct 7 ms 14592 KB Output is correct
55 Correct 6 ms 14428 KB Output is correct
56 Correct 6 ms 14428 KB Output is correct
57 Correct 9 ms 14172 KB Output is correct
58 Correct 10 ms 14172 KB Output is correct
59 Correct 12 ms 14288 KB Output is correct
60 Correct 10 ms 14172 KB Output is correct
61 Correct 8 ms 14464 KB Output is correct
62 Correct 6 ms 14428 KB Output is correct
63 Correct 6 ms 14428 KB Output is correct
64 Correct 10 ms 14204 KB Output is correct
65 Correct 10 ms 14172 KB Output is correct
66 Correct 8 ms 14168 KB Output is correct
67 Correct 10 ms 14172 KB Output is correct
68 Correct 5 ms 14428 KB Output is correct
69 Correct 6 ms 14428 KB Output is correct
70 Correct 5 ms 14428 KB Output is correct
71 Correct 5 ms 14424 KB Output is correct
72 Correct 9 ms 14172 KB Output is correct
73 Correct 10 ms 14172 KB Output is correct
74 Correct 5 ms 14456 KB Output is correct
75 Correct 9 ms 14192 KB Output is correct
76 Correct 6 ms 14168 KB Output is correct
77 Correct 6 ms 14168 KB Output is correct
78 Correct 5 ms 14172 KB Output is correct
79 Correct 5 ms 14172 KB Output is correct
80 Correct 6 ms 14172 KB Output is correct
81 Correct 7 ms 14172 KB Output is correct
82 Correct 8 ms 14172 KB Output is correct
83 Correct 6 ms 14168 KB Output is correct
84 Correct 6 ms 14168 KB Output is correct
85 Correct 6 ms 14168 KB Output is correct
86 Correct 6 ms 14116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 4 ms 13764 KB Output is correct
4 Correct 3 ms 13916 KB Output is correct
5 Correct 4 ms 14040 KB Output is correct
6 Correct 4 ms 13916 KB Output is correct
7 Correct 4 ms 13916 KB Output is correct
8 Correct 3 ms 13912 KB Output is correct
9 Correct 3 ms 13912 KB Output is correct
10 Correct 4 ms 13916 KB Output is correct
11 Correct 3 ms 14168 KB Output is correct
12 Correct 3 ms 13912 KB Output is correct
13 Correct 3 ms 13916 KB Output is correct
14 Correct 4 ms 13916 KB Output is correct
15 Correct 3 ms 13916 KB Output is correct
16 Correct 5 ms 13912 KB Output is correct
17 Correct 4 ms 13916 KB Output is correct
18 Correct 3 ms 13916 KB Output is correct
19 Correct 3 ms 13912 KB Output is correct
20 Correct 4 ms 13916 KB Output is correct
21 Correct 3 ms 13844 KB Output is correct
22 Correct 3 ms 13916 KB Output is correct
23 Correct 4 ms 13916 KB Output is correct
24 Correct 4 ms 13916 KB Output is correct
25 Correct 4 ms 13916 KB Output is correct
26 Correct 3 ms 13916 KB Output is correct
27 Correct 3 ms 13916 KB Output is correct
28 Correct 3 ms 13916 KB Output is correct
29 Correct 3 ms 14040 KB Output is correct
30 Correct 4 ms 13916 KB Output is correct
31 Correct 4 ms 13916 KB Output is correct
32 Correct 4 ms 13916 KB Output is correct
33 Correct 3 ms 13916 KB Output is correct
34 Correct 4 ms 13916 KB Output is correct
35 Correct 4 ms 14164 KB Output is correct
36 Correct 3 ms 13916 KB Output is correct
37 Correct 3 ms 13916 KB Output is correct
38 Correct 3 ms 13916 KB Output is correct
39 Correct 4 ms 13916 KB Output is correct
40 Correct 3 ms 14068 KB Output is correct
41 Correct 3 ms 13916 KB Output is correct
42 Correct 4 ms 13916 KB Output is correct
43 Correct 3 ms 14168 KB Output is correct
44 Correct 4 ms 13916 KB Output is correct
45 Correct 4 ms 13916 KB Output is correct
46 Correct 8 ms 14168 KB Output is correct
47 Correct 8 ms 14172 KB Output is correct
48 Correct 9 ms 14172 KB Output is correct
49 Correct 5 ms 14680 KB Output is correct
50 Correct 8 ms 14684 KB Output is correct
51 Correct 5 ms 14684 KB Output is correct
52 Correct 6 ms 14428 KB Output is correct
53 Correct 6 ms 14428 KB Output is correct
54 Correct 7 ms 14592 KB Output is correct
55 Correct 6 ms 14428 KB Output is correct
56 Correct 6 ms 14428 KB Output is correct
57 Correct 9 ms 14172 KB Output is correct
58 Correct 10 ms 14172 KB Output is correct
59 Correct 12 ms 14288 KB Output is correct
60 Correct 10 ms 14172 KB Output is correct
61 Correct 8 ms 14464 KB Output is correct
62 Correct 6 ms 14428 KB Output is correct
63 Correct 6 ms 14428 KB Output is correct
64 Correct 10 ms 14204 KB Output is correct
65 Correct 10 ms 14172 KB Output is correct
66 Correct 8 ms 14168 KB Output is correct
67 Correct 10 ms 14172 KB Output is correct
68 Correct 5 ms 14428 KB Output is correct
69 Correct 6 ms 14428 KB Output is correct
70 Correct 5 ms 14428 KB Output is correct
71 Correct 5 ms 14424 KB Output is correct
72 Correct 9 ms 14172 KB Output is correct
73 Correct 10 ms 14172 KB Output is correct
74 Correct 5 ms 14456 KB Output is correct
75 Correct 9 ms 14192 KB Output is correct
76 Correct 6 ms 14168 KB Output is correct
77 Correct 6 ms 14168 KB Output is correct
78 Correct 5 ms 14172 KB Output is correct
79 Correct 5 ms 14172 KB Output is correct
80 Correct 6 ms 14172 KB Output is correct
81 Correct 7 ms 14172 KB Output is correct
82 Correct 8 ms 14172 KB Output is correct
83 Correct 6 ms 14168 KB Output is correct
84 Correct 6 ms 14168 KB Output is correct
85 Correct 6 ms 14168 KB Output is correct
86 Correct 6 ms 14116 KB Output is correct
87 Correct 19 ms 14680 KB Output is correct
88 Correct 57 ms 16216 KB Output is correct
89 Correct 247 ms 21216 KB Output is correct
90 Correct 251 ms 21248 KB Output is correct
91 Correct 231 ms 21124 KB Output is correct
92 Correct 87 ms 30224 KB Output is correct
93 Correct 91 ms 30240 KB Output is correct
94 Correct 79 ms 30220 KB Output is correct
95 Correct 99 ms 28168 KB Output is correct
96 Correct 120 ms 28212 KB Output is correct
97 Correct 130 ms 30440 KB Output is correct
98 Correct 128 ms 30364 KB Output is correct
99 Correct 94 ms 27396 KB Output is correct
100 Correct 295 ms 23108 KB Output is correct
101 Correct 402 ms 23244 KB Output is correct
102 Correct 331 ms 23248 KB Output is correct
103 Correct 322 ms 23108 KB Output is correct
104 Correct 110 ms 27396 KB Output is correct
105 Correct 115 ms 27408 KB Output is correct
106 Correct 103 ms 27324 KB Output is correct
107 Correct 226 ms 22188 KB Output is correct
108 Correct 207 ms 22516 KB Output is correct
109 Correct 222 ms 22692 KB Output is correct
110 Correct 59 ms 29508 KB Output is correct
111 Correct 100 ms 29964 KB Output is correct
112 Correct 96 ms 29840 KB Output is correct
113 Correct 66 ms 26680 KB Output is correct
114 Correct 300 ms 23024 KB Output is correct
115 Correct 310 ms 22600 KB Output is correct
116 Correct 81 ms 26556 KB Output is correct
117 Correct 99 ms 24600 KB Output is correct
118 Correct 93 ms 24060 KB Output is correct
119 Correct 90 ms 23476 KB Output is correct
120 Correct 70 ms 24120 KB Output is correct
121 Correct 72 ms 23536 KB Output is correct
122 Correct 78 ms 22984 KB Output is correct
123 Correct 103 ms 24580 KB Output is correct
124 Correct 115 ms 23976 KB Output is correct
125 Correct 115 ms 23532 KB Output is correct
126 Correct 88 ms 24372 KB Output is correct
127 Correct 93 ms 23512 KB Output is correct
128 Correct 102 ms 23100 KB Output is correct