답안 #960904

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
960904 2024-04-11T08:03:02 Z danikoynov Construction of Highway (JOI18_construction) C++14
16 / 100
2000 ms 29704 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]];

     }
}


void find_inversions(vector < pair < int, int > > &path)
{
     ll ans = 0;
     int sz = path.size();
     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()
{
     solve();
     return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13656 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 4 ms 13660 KB Output is correct
4 Correct 3 ms 13688 KB Output is correct
5 Correct 3 ms 13736 KB Output is correct
6 Correct 4 ms 13660 KB Output is correct
7 Correct 3 ms 13660 KB Output is correct
8 Correct 3 ms 13656 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 4 ms 13660 KB Output is correct
12 Correct 4 ms 13660 KB Output is correct
13 Correct 4 ms 13660 KB Output is correct
14 Correct 3 ms 13660 KB Output is correct
15 Correct 4 ms 13660 KB Output is correct
16 Correct 5 ms 13656 KB Output is correct
17 Correct 3 ms 13660 KB Output is correct
18 Correct 4 ms 13660 KB Output is correct
19 Correct 3 ms 13660 KB Output is correct
20 Correct 3 ms 13660 KB Output is correct
21 Correct 4 ms 13756 KB Output is correct
22 Correct 3 ms 13660 KB Output is correct
23 Correct 4 ms 13660 KB Output is correct
24 Correct 3 ms 13660 KB Output is correct
25 Correct 3 ms 13660 KB Output is correct
26 Correct 3 ms 13656 KB Output is correct
27 Correct 3 ms 13660 KB Output is correct
28 Correct 3 ms 13660 KB Output is correct
29 Correct 3 ms 13660 KB Output is correct
30 Correct 4 ms 13660 KB Output is correct
31 Correct 4 ms 13660 KB Output is correct
32 Correct 4 ms 13692 KB Output is correct
33 Correct 3 ms 13512 KB Output is correct
34 Correct 3 ms 13660 KB Output is correct
35 Correct 3 ms 13912 KB Output is correct
36 Correct 4 ms 13660 KB Output is correct
37 Correct 4 ms 13656 KB Output is correct
38 Correct 3 ms 13660 KB Output is correct
39 Correct 4 ms 13660 KB Output is correct
40 Correct 3 ms 13660 KB Output is correct
41 Correct 3 ms 13660 KB Output is correct
42 Correct 3 ms 13660 KB Output is correct
43 Correct 4 ms 13512 KB Output is correct
44 Correct 3 ms 13660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13656 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 4 ms 13660 KB Output is correct
4 Correct 3 ms 13688 KB Output is correct
5 Correct 3 ms 13736 KB Output is correct
6 Correct 4 ms 13660 KB Output is correct
7 Correct 3 ms 13660 KB Output is correct
8 Correct 3 ms 13656 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 4 ms 13660 KB Output is correct
12 Correct 4 ms 13660 KB Output is correct
13 Correct 4 ms 13660 KB Output is correct
14 Correct 3 ms 13660 KB Output is correct
15 Correct 4 ms 13660 KB Output is correct
16 Correct 5 ms 13656 KB Output is correct
17 Correct 3 ms 13660 KB Output is correct
18 Correct 4 ms 13660 KB Output is correct
19 Correct 3 ms 13660 KB Output is correct
20 Correct 3 ms 13660 KB Output is correct
21 Correct 4 ms 13756 KB Output is correct
22 Correct 3 ms 13660 KB Output is correct
23 Correct 4 ms 13660 KB Output is correct
24 Correct 3 ms 13660 KB Output is correct
25 Correct 3 ms 13660 KB Output is correct
26 Correct 3 ms 13656 KB Output is correct
27 Correct 3 ms 13660 KB Output is correct
28 Correct 3 ms 13660 KB Output is correct
29 Correct 3 ms 13660 KB Output is correct
30 Correct 4 ms 13660 KB Output is correct
31 Correct 4 ms 13660 KB Output is correct
32 Correct 4 ms 13692 KB Output is correct
33 Correct 3 ms 13512 KB Output is correct
34 Correct 3 ms 13660 KB Output is correct
35 Correct 3 ms 13912 KB Output is correct
36 Correct 4 ms 13660 KB Output is correct
37 Correct 4 ms 13656 KB Output is correct
38 Correct 3 ms 13660 KB Output is correct
39 Correct 4 ms 13660 KB Output is correct
40 Correct 3 ms 13660 KB Output is correct
41 Correct 3 ms 13660 KB Output is correct
42 Correct 3 ms 13660 KB Output is correct
43 Correct 4 ms 13512 KB Output is correct
44 Correct 3 ms 13660 KB Output is correct
45 Correct 4 ms 13660 KB Output is correct
46 Correct 12 ms 13916 KB Output is correct
47 Correct 12 ms 13916 KB Output is correct
48 Correct 11 ms 14168 KB Output is correct
49 Correct 9 ms 14168 KB Output is correct
50 Correct 7 ms 14216 KB Output is correct
51 Correct 7 ms 14172 KB Output is correct
52 Correct 10 ms 14168 KB Output is correct
53 Correct 15 ms 14172 KB Output is correct
54 Correct 14 ms 14248 KB Output is correct
55 Correct 12 ms 14168 KB Output is correct
56 Correct 7 ms 13912 KB Output is correct
57 Correct 11 ms 13960 KB Output is correct
58 Correct 11 ms 13916 KB Output is correct
59 Correct 13 ms 14016 KB Output is correct
60 Correct 11 ms 13916 KB Output is correct
61 Correct 7 ms 13916 KB Output is correct
62 Correct 8 ms 13916 KB Output is correct
63 Correct 7 ms 13916 KB Output is correct
64 Correct 10 ms 13960 KB Output is correct
65 Correct 10 ms 13916 KB Output is correct
66 Correct 10 ms 13916 KB Output is correct
67 Correct 9 ms 13952 KB Output is correct
68 Correct 6 ms 14168 KB Output is correct
69 Correct 8 ms 14172 KB Output is correct
70 Correct 11 ms 14172 KB Output is correct
71 Correct 6 ms 13964 KB Output is correct
72 Correct 11 ms 13916 KB Output is correct
73 Correct 10 ms 13916 KB Output is correct
74 Correct 6 ms 13912 KB Output is correct
75 Correct 7 ms 13916 KB Output is correct
76 Correct 7 ms 13916 KB Output is correct
77 Correct 7 ms 13996 KB Output is correct
78 Correct 7 ms 13980 KB Output is correct
79 Correct 6 ms 13916 KB Output is correct
80 Correct 7 ms 13916 KB Output is correct
81 Correct 11 ms 13916 KB Output is correct
82 Correct 8 ms 13956 KB Output is correct
83 Correct 8 ms 13912 KB Output is correct
84 Correct 8 ms 13916 KB Output is correct
85 Correct 7 ms 13920 KB Output is correct
86 Correct 7 ms 13916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13656 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 4 ms 13660 KB Output is correct
4 Correct 3 ms 13688 KB Output is correct
5 Correct 3 ms 13736 KB Output is correct
6 Correct 4 ms 13660 KB Output is correct
7 Correct 3 ms 13660 KB Output is correct
8 Correct 3 ms 13656 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 4 ms 13660 KB Output is correct
12 Correct 4 ms 13660 KB Output is correct
13 Correct 4 ms 13660 KB Output is correct
14 Correct 3 ms 13660 KB Output is correct
15 Correct 4 ms 13660 KB Output is correct
16 Correct 5 ms 13656 KB Output is correct
17 Correct 3 ms 13660 KB Output is correct
18 Correct 4 ms 13660 KB Output is correct
19 Correct 3 ms 13660 KB Output is correct
20 Correct 3 ms 13660 KB Output is correct
21 Correct 4 ms 13756 KB Output is correct
22 Correct 3 ms 13660 KB Output is correct
23 Correct 4 ms 13660 KB Output is correct
24 Correct 3 ms 13660 KB Output is correct
25 Correct 3 ms 13660 KB Output is correct
26 Correct 3 ms 13656 KB Output is correct
27 Correct 3 ms 13660 KB Output is correct
28 Correct 3 ms 13660 KB Output is correct
29 Correct 3 ms 13660 KB Output is correct
30 Correct 4 ms 13660 KB Output is correct
31 Correct 4 ms 13660 KB Output is correct
32 Correct 4 ms 13692 KB Output is correct
33 Correct 3 ms 13512 KB Output is correct
34 Correct 3 ms 13660 KB Output is correct
35 Correct 3 ms 13912 KB Output is correct
36 Correct 4 ms 13660 KB Output is correct
37 Correct 4 ms 13656 KB Output is correct
38 Correct 3 ms 13660 KB Output is correct
39 Correct 4 ms 13660 KB Output is correct
40 Correct 3 ms 13660 KB Output is correct
41 Correct 3 ms 13660 KB Output is correct
42 Correct 3 ms 13660 KB Output is correct
43 Correct 4 ms 13512 KB Output is correct
44 Correct 3 ms 13660 KB Output is correct
45 Correct 4 ms 13660 KB Output is correct
46 Correct 12 ms 13916 KB Output is correct
47 Correct 12 ms 13916 KB Output is correct
48 Correct 11 ms 14168 KB Output is correct
49 Correct 9 ms 14168 KB Output is correct
50 Correct 7 ms 14216 KB Output is correct
51 Correct 7 ms 14172 KB Output is correct
52 Correct 10 ms 14168 KB Output is correct
53 Correct 15 ms 14172 KB Output is correct
54 Correct 14 ms 14248 KB Output is correct
55 Correct 12 ms 14168 KB Output is correct
56 Correct 7 ms 13912 KB Output is correct
57 Correct 11 ms 13960 KB Output is correct
58 Correct 11 ms 13916 KB Output is correct
59 Correct 13 ms 14016 KB Output is correct
60 Correct 11 ms 13916 KB Output is correct
61 Correct 7 ms 13916 KB Output is correct
62 Correct 8 ms 13916 KB Output is correct
63 Correct 7 ms 13916 KB Output is correct
64 Correct 10 ms 13960 KB Output is correct
65 Correct 10 ms 13916 KB Output is correct
66 Correct 10 ms 13916 KB Output is correct
67 Correct 9 ms 13952 KB Output is correct
68 Correct 6 ms 14168 KB Output is correct
69 Correct 8 ms 14172 KB Output is correct
70 Correct 11 ms 14172 KB Output is correct
71 Correct 6 ms 13964 KB Output is correct
72 Correct 11 ms 13916 KB Output is correct
73 Correct 10 ms 13916 KB Output is correct
74 Correct 6 ms 13912 KB Output is correct
75 Correct 7 ms 13916 KB Output is correct
76 Correct 7 ms 13916 KB Output is correct
77 Correct 7 ms 13996 KB Output is correct
78 Correct 7 ms 13980 KB Output is correct
79 Correct 6 ms 13916 KB Output is correct
80 Correct 7 ms 13916 KB Output is correct
81 Correct 11 ms 13916 KB Output is correct
82 Correct 8 ms 13956 KB Output is correct
83 Correct 8 ms 13912 KB Output is correct
84 Correct 8 ms 13916 KB Output is correct
85 Correct 7 ms 13920 KB Output is correct
86 Correct 7 ms 13916 KB Output is correct
87 Correct 22 ms 14296 KB Output is correct
88 Correct 64 ms 15544 KB Output is correct
89 Correct 244 ms 20408 KB Output is correct
90 Correct 268 ms 20352 KB Output is correct
91 Correct 253 ms 20364 KB Output is correct
92 Correct 140 ms 29704 KB Output is correct
93 Correct 125 ms 29524 KB Output is correct
94 Correct 138 ms 29444 KB Output is correct
95 Correct 1136 ms 27612 KB Output is correct
96 Execution timed out 2013 ms 27444 KB Time limit exceeded
97 Halted 0 ms 0 KB -