답안 #960903

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
960903 2024-04-11T08:00:27 Z danikoynov Construction of Highway (JOI18_construction) C++14
16 / 100
2000 ms 29184 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 solve()
{
     input();
     calc(1);
     hld(1, 1);
     simulate();
}

int main()
{
     solve();
     return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13912 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 4 ms 13656 KB Output is correct
6 Correct 3 ms 13508 KB Output is correct
7 Correct 4 ms 13660 KB Output is correct
8 Correct 3 ms 13656 KB Output is correct
9 Correct 4 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13660 KB Output is correct
12 Correct 3 ms 13660 KB Output is correct
13 Correct 3 ms 13716 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 13660 KB Output is correct
17 Correct 4 ms 13724 KB Output is correct
18 Correct 4 ms 13660 KB Output is correct
19 Correct 3 ms 13660 KB Output is correct
20 Correct 4 ms 13660 KB Output is correct
21 Correct 3 ms 13660 KB Output is correct
22 Correct 3 ms 13660 KB Output is correct
23 Correct 3 ms 13656 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 13660 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 13712 KB Output is correct
33 Correct 3 ms 13660 KB Output is correct
34 Correct 4 ms 13700 KB Output is correct
35 Correct 4 ms 13660 KB Output is correct
36 Correct 4 ms 13660 KB Output is correct
37 Correct 3 ms 13660 KB Output is correct
38 Correct 4 ms 13660 KB Output is correct
39 Correct 3 ms 13516 KB Output is correct
40 Correct 3 ms 13660 KB Output is correct
41 Correct 3 ms 13660 KB Output is correct
42 Correct 4 ms 13716 KB Output is correct
43 Correct 3 ms 13656 KB Output is correct
44 Correct 4 ms 13660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13912 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 4 ms 13656 KB Output is correct
6 Correct 3 ms 13508 KB Output is correct
7 Correct 4 ms 13660 KB Output is correct
8 Correct 3 ms 13656 KB Output is correct
9 Correct 4 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13660 KB Output is correct
12 Correct 3 ms 13660 KB Output is correct
13 Correct 3 ms 13716 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 13660 KB Output is correct
17 Correct 4 ms 13724 KB Output is correct
18 Correct 4 ms 13660 KB Output is correct
19 Correct 3 ms 13660 KB Output is correct
20 Correct 4 ms 13660 KB Output is correct
21 Correct 3 ms 13660 KB Output is correct
22 Correct 3 ms 13660 KB Output is correct
23 Correct 3 ms 13656 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 13660 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 13712 KB Output is correct
33 Correct 3 ms 13660 KB Output is correct
34 Correct 4 ms 13700 KB Output is correct
35 Correct 4 ms 13660 KB Output is correct
36 Correct 4 ms 13660 KB Output is correct
37 Correct 3 ms 13660 KB Output is correct
38 Correct 4 ms 13660 KB Output is correct
39 Correct 3 ms 13516 KB Output is correct
40 Correct 3 ms 13660 KB Output is correct
41 Correct 3 ms 13660 KB Output is correct
42 Correct 4 ms 13716 KB Output is correct
43 Correct 3 ms 13656 KB Output is correct
44 Correct 4 ms 13660 KB Output is correct
45 Correct 4 ms 13660 KB Output is correct
46 Correct 9 ms 13916 KB Output is correct
47 Correct 9 ms 13916 KB Output is correct
48 Correct 9 ms 13916 KB Output is correct
49 Correct 6 ms 14236 KB Output is correct
50 Correct 6 ms 14232 KB Output is correct
51 Correct 6 ms 14168 KB Output is correct
52 Correct 8 ms 14168 KB Output is correct
53 Correct 11 ms 14288 KB Output is correct
54 Correct 13 ms 14428 KB Output is correct
55 Correct 12 ms 14328 KB Output is correct
56 Correct 7 ms 14168 KB Output is correct
57 Correct 10 ms 13952 KB Output is correct
58 Correct 11 ms 13976 KB Output is correct
59 Correct 11 ms 13916 KB Output is correct
60 Correct 10 ms 13932 KB Output is correct
61 Correct 7 ms 14172 KB Output is correct
62 Correct 7 ms 14172 KB Output is correct
63 Correct 7 ms 14172 KB Output is correct
64 Correct 10 ms 13916 KB Output is correct
65 Correct 9 ms 13908 KB Output is correct
66 Correct 8 ms 13916 KB Output is correct
67 Correct 9 ms 13916 KB Output is correct
68 Correct 6 ms 14172 KB Output is correct
69 Correct 9 ms 14276 KB Output is correct
70 Correct 11 ms 14236 KB Output is correct
71 Correct 7 ms 14172 KB Output is correct
72 Correct 10 ms 14000 KB Output is correct
73 Correct 10 ms 13916 KB Output is correct
74 Correct 6 ms 14040 KB Output is correct
75 Correct 7 ms 13916 KB Output is correct
76 Correct 8 ms 13892 KB Output is correct
77 Correct 6 ms 13916 KB Output is correct
78 Correct 7 ms 13912 KB Output is correct
79 Correct 6 ms 13916 KB Output is correct
80 Correct 6 ms 14020 KB Output is correct
81 Correct 7 ms 13916 KB Output is correct
82 Correct 7 ms 13916 KB Output is correct
83 Correct 7 ms 13916 KB Output is correct
84 Correct 6 ms 14068 KB Output is correct
85 Correct 7 ms 13916 KB Output is correct
86 Correct 6 ms 13916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13912 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 4 ms 13656 KB Output is correct
6 Correct 3 ms 13508 KB Output is correct
7 Correct 4 ms 13660 KB Output is correct
8 Correct 3 ms 13656 KB Output is correct
9 Correct 4 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13660 KB Output is correct
12 Correct 3 ms 13660 KB Output is correct
13 Correct 3 ms 13716 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 13660 KB Output is correct
17 Correct 4 ms 13724 KB Output is correct
18 Correct 4 ms 13660 KB Output is correct
19 Correct 3 ms 13660 KB Output is correct
20 Correct 4 ms 13660 KB Output is correct
21 Correct 3 ms 13660 KB Output is correct
22 Correct 3 ms 13660 KB Output is correct
23 Correct 3 ms 13656 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 13660 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 13712 KB Output is correct
33 Correct 3 ms 13660 KB Output is correct
34 Correct 4 ms 13700 KB Output is correct
35 Correct 4 ms 13660 KB Output is correct
36 Correct 4 ms 13660 KB Output is correct
37 Correct 3 ms 13660 KB Output is correct
38 Correct 4 ms 13660 KB Output is correct
39 Correct 3 ms 13516 KB Output is correct
40 Correct 3 ms 13660 KB Output is correct
41 Correct 3 ms 13660 KB Output is correct
42 Correct 4 ms 13716 KB Output is correct
43 Correct 3 ms 13656 KB Output is correct
44 Correct 4 ms 13660 KB Output is correct
45 Correct 4 ms 13660 KB Output is correct
46 Correct 9 ms 13916 KB Output is correct
47 Correct 9 ms 13916 KB Output is correct
48 Correct 9 ms 13916 KB Output is correct
49 Correct 6 ms 14236 KB Output is correct
50 Correct 6 ms 14232 KB Output is correct
51 Correct 6 ms 14168 KB Output is correct
52 Correct 8 ms 14168 KB Output is correct
53 Correct 11 ms 14288 KB Output is correct
54 Correct 13 ms 14428 KB Output is correct
55 Correct 12 ms 14328 KB Output is correct
56 Correct 7 ms 14168 KB Output is correct
57 Correct 10 ms 13952 KB Output is correct
58 Correct 11 ms 13976 KB Output is correct
59 Correct 11 ms 13916 KB Output is correct
60 Correct 10 ms 13932 KB Output is correct
61 Correct 7 ms 14172 KB Output is correct
62 Correct 7 ms 14172 KB Output is correct
63 Correct 7 ms 14172 KB Output is correct
64 Correct 10 ms 13916 KB Output is correct
65 Correct 9 ms 13908 KB Output is correct
66 Correct 8 ms 13916 KB Output is correct
67 Correct 9 ms 13916 KB Output is correct
68 Correct 6 ms 14172 KB Output is correct
69 Correct 9 ms 14276 KB Output is correct
70 Correct 11 ms 14236 KB Output is correct
71 Correct 7 ms 14172 KB Output is correct
72 Correct 10 ms 14000 KB Output is correct
73 Correct 10 ms 13916 KB Output is correct
74 Correct 6 ms 14040 KB Output is correct
75 Correct 7 ms 13916 KB Output is correct
76 Correct 8 ms 13892 KB Output is correct
77 Correct 6 ms 13916 KB Output is correct
78 Correct 7 ms 13912 KB Output is correct
79 Correct 6 ms 13916 KB Output is correct
80 Correct 6 ms 14020 KB Output is correct
81 Correct 7 ms 13916 KB Output is correct
82 Correct 7 ms 13916 KB Output is correct
83 Correct 7 ms 13916 KB Output is correct
84 Correct 6 ms 14068 KB Output is correct
85 Correct 7 ms 13916 KB Output is correct
86 Correct 6 ms 13916 KB Output is correct
87 Correct 19 ms 14428 KB Output is correct
88 Correct 58 ms 16164 KB Output is correct
89 Correct 236 ms 22476 KB Output is correct
90 Correct 225 ms 22436 KB Output is correct
91 Correct 232 ms 22384 KB Output is correct
92 Correct 109 ms 28848 KB Output is correct
93 Correct 105 ms 28884 KB Output is correct
94 Correct 118 ms 29144 KB Output is correct
95 Correct 1113 ms 28836 KB Output is correct
96 Execution timed out 2058 ms 29184 KB Time limit exceeded
97 Halted 0 ms 0 KB -