Submission #523365

# Submission time Handle Problem Language Result Execution time Memory
523365 2022-02-07T14:38:37 Z CPSC Construction of Highway (JOI18_construction) C++14
100 / 100
388 ms 93800 KB
# include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define pii pair <int, int >
using namespace std;
const int N = 1e5 + 5;
int lv[N],sz[N],par[N],tree[N],in[N],out[N],tin,sizeofchain[N];
map <int, int> shes;
vector <int> v[N],tocomp;
deque < pii > vec[N];
int bigchild[N],chain[N],n,a[N],b[N],c[N];
void update(int idx, int val) {
    for (int i = idx; i < N; i += i&(-i)) {
        tree[i] += val;
    }
}
int getans(int idx) {
    int pas = 0;
    for (int i = idx; i > 0; i-=i&(-i)) {
        pas += tree[i];
    }
    return pas;
}
void dfs(int a, int p) {
    sz[a] = 1;
    lv[a] = lv[p] + 1;
    par[a] = p;
    for (int i = 0; i < v[a].size(); i++) {
        int to = v[a][i];
        if (to == p) continue;
        dfs(to,a);
        sz[a] += sz[to];
        if (!bigchild[a] || sz[bigchild[a]] < sz[to]) {
            bigchild[a] = to;
        }
    }
}
void dfs1(int a, int p) {
    tin++;
    in[a] = tin;
    if (bigchild[a]) dfs1(bigchild[a],a);
    for (int i = 0; i < v[a].size(); i++) {
        int to = v[a][i];
        if (to == p || to == bigchild[a]) continue;
        dfs1(to,a);
    }
    out[a] = tin;
}
void dfs_chain(int a,int p) {
    if (bigchild[a]) {
        chain[bigchild[a]] = chain[a];
    }
    sizeofchain[a]++;
    if (!vec[chain[a]].size()) {
        vec[chain[a]].pb({c[a],1});
    } else {
        if (vec[chain[a]].back().f == c[a]) {
            vec[chain[a]].back().s++;
        } else vec[chain[a]].pb({c[a],1});
    }
    for (int i = 0; i < v[a].size(); i++) {
        int to = v[a][i];
        if (to == p) continue;
        dfs_chain(to,a);
    }
}
int getans(int a, int b) {
    int ans = 0;
    int lst = chain[a];
    int cur = a;
    vector <pii> pas;
    //if (a == 2 && b == 3) cout<<lst<<"\n",prnt(vec[lst]);
    while (true) {
        lst = chain[cur];
        int x = lv[cur] - lv[lst] + 1;
        //if (a == 5 && b == 9) cout<<lst<<" "<<cur<<endl;
        vector <pii> vv;
        int sum = 0;
        for (int i = 0; i < vec[lst].size(); i++) {
            if(vec[lst][i].s + sum > x) {
               // if (a == 5 && b == 9 && lst == 1) cout<<x<<" "<<sum<<" "<< (x - sum)<<endl;
                vv.pb({vec[lst][i].f, (x - sum)});
                break;
            } 
            
            sum += vec[lst][i].s;
            vv.pb({vec[lst][i].f,vec[lst][i].s});
            if (sum == x) break;
        }
        reverse(vv.begin(),vv.end());
        for (int i = 0; i < vv.size(); i++) {
            pas.pb({vv[i].f,vv[i].s});
        }
        cur = par[lst];
        if (cur == 0) break;
    }
    //if (a == 2 && b == 3)cout<<endl<<endl,
    //prnt(pas);
   // cout<<a<<"   "<<b<<endl;
   // prnt(pas);
    for (int i = 0; i < pas.size(); i++) {
        ans += getans(pas[i].f - 1)*pas[i].s;
        update(pas[i].f, pas[i].s);
    }
    for (int i = 0; i < pas.size(); i++) update(pas[i].f, -pas[i].s);
    return ans;
}
void toadd(int a, int b) {
    int lst = chain[a];
    int cur = a;
    while (true) {
        lst = chain[cur];
        int x = lv[cur] - lv[lst] + 1;
        int sum = 0; int idx = 0;
        vector <pii> vv;
        
        while (vec[lst].size()) {
            int i = 0;
            if (vec[lst][i].s + sum > x) {
                vec[lst][i].s = vec[lst][i].s - (x - sum);
                break;
            }
            sum += vec[lst][i].s;
            vec[lst].pop_front();
            if (sum == x) break;
        }
        vec[lst].push_front({c[b],x});
        cur = par[lst];
        if (cur == 0) break;
    }
}
main() {
	std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	#define endl '\n'
    cin>>n;
    for (int i = 1; i <= n; i++) {
        cin>>c[i];
        tocomp.pb(c[i]);
    }
    sort(tocomp.begin(),tocomp.end());
    int cnt = 1;
    shes[tocomp[0]] = 1;
    for (int i = 1; i < tocomp.size(); i++) {
        if (tocomp[i] != tocomp[i - 1]) cnt++;
        shes[tocomp[i]] = cnt;
    }
    for (int i = 1; i <= n; i++) c[i] = shes[c[i]];
    for (int i = 1; i <= n - 1; i++) {
        cin>>a[i]>>b[i];
        v[a[i]].pb(b[i]);
        v[b[i]].pb(a[i]);
    }
    for (int i = 1; i <= n; i++) {
        chain[i] = i;
    }
    dfs(1,0);
    dfs1(1,0);
    dfs_chain(1,0);
    for (int i = 1; i <= n - 1; i++) {
        int ss  = getans(a[i],b[i]);
        toadd(a[i],b[i]);
        cout<<ss<<endl;
    }
}

Compilation message

construction.cpp: In function 'void dfs(int, int)':
construction.cpp:29:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for (int i = 0; i < v[a].size(); i++) {
      |                     ~~^~~~~~~~~~~~~
construction.cpp: In function 'void dfs1(int, int)':
construction.cpp:43:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |     for (int i = 0; i < v[a].size(); i++) {
      |                     ~~^~~~~~~~~~~~~
construction.cpp: In function 'void dfs_chain(int, int)':
construction.cpp:62:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |     for (int i = 0; i < v[a].size(); i++) {
      |                     ~~^~~~~~~~~~~~~
construction.cpp: In function 'int getans(int, int)':
construction.cpp:80:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |         for (int i = 0; i < vec[lst].size(); i++) {
      |                         ~~^~~~~~~~~~~~~~~~~
construction.cpp:92:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |         for (int i = 0; i < vv.size(); i++) {
      |                         ~~^~~~~~~~~~~
construction.cpp:102:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |     for (int i = 0; i < pas.size(); i++) {
      |                     ~~^~~~~~~~~~~~
construction.cpp:106:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |     for (int i = 0; i < pas.size(); i++) update(pas[i].f, -pas[i].s);
      |                     ~~^~~~~~~~~~~~
construction.cpp: In function 'void toadd(int, int)':
construction.cpp:115:26: warning: unused variable 'idx' [-Wunused-variable]
  115 |         int sum = 0; int idx = 0;
      |                          ^~~
construction.cpp: At global scope:
construction.cpp:133:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  133 | main() {
      | ^~~~
construction.cpp: In function 'int main()':
construction.cpp:144:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  144 |     for (int i = 1; i < tocomp.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 42 ms 69964 KB Output is correct
2 Correct 43 ms 69976 KB Output is correct
3 Correct 46 ms 70060 KB Output is correct
4 Correct 44 ms 70100 KB Output is correct
5 Correct 45 ms 70136 KB Output is correct
6 Correct 45 ms 70208 KB Output is correct
7 Correct 43 ms 70148 KB Output is correct
8 Correct 46 ms 70084 KB Output is correct
9 Correct 45 ms 70168 KB Output is correct
10 Correct 45 ms 70112 KB Output is correct
11 Correct 44 ms 70116 KB Output is correct
12 Correct 46 ms 70140 KB Output is correct
13 Correct 43 ms 70124 KB Output is correct
14 Correct 43 ms 70084 KB Output is correct
15 Correct 46 ms 70052 KB Output is correct
16 Correct 44 ms 70132 KB Output is correct
17 Correct 44 ms 70044 KB Output is correct
18 Correct 43 ms 70076 KB Output is correct
19 Correct 44 ms 70092 KB Output is correct
20 Correct 44 ms 70116 KB Output is correct
21 Correct 44 ms 70080 KB Output is correct
22 Correct 49 ms 70108 KB Output is correct
23 Correct 48 ms 70052 KB Output is correct
24 Correct 44 ms 70084 KB Output is correct
25 Correct 47 ms 70028 KB Output is correct
26 Correct 44 ms 70040 KB Output is correct
27 Correct 44 ms 70124 KB Output is correct
28 Correct 47 ms 70144 KB Output is correct
29 Correct 45 ms 70060 KB Output is correct
30 Correct 47 ms 70156 KB Output is correct
31 Correct 45 ms 70136 KB Output is correct
32 Correct 44 ms 70208 KB Output is correct
33 Correct 44 ms 70032 KB Output is correct
34 Correct 48 ms 70024 KB Output is correct
35 Correct 45 ms 70048 KB Output is correct
36 Correct 45 ms 70084 KB Output is correct
37 Correct 45 ms 70032 KB Output is correct
38 Correct 47 ms 70064 KB Output is correct
39 Correct 46 ms 70112 KB Output is correct
40 Correct 47 ms 70020 KB Output is correct
41 Correct 45 ms 70108 KB Output is correct
42 Correct 42 ms 70036 KB Output is correct
43 Correct 43 ms 70012 KB Output is correct
44 Correct 43 ms 70124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 69964 KB Output is correct
2 Correct 43 ms 69976 KB Output is correct
3 Correct 46 ms 70060 KB Output is correct
4 Correct 44 ms 70100 KB Output is correct
5 Correct 45 ms 70136 KB Output is correct
6 Correct 45 ms 70208 KB Output is correct
7 Correct 43 ms 70148 KB Output is correct
8 Correct 46 ms 70084 KB Output is correct
9 Correct 45 ms 70168 KB Output is correct
10 Correct 45 ms 70112 KB Output is correct
11 Correct 44 ms 70116 KB Output is correct
12 Correct 46 ms 70140 KB Output is correct
13 Correct 43 ms 70124 KB Output is correct
14 Correct 43 ms 70084 KB Output is correct
15 Correct 46 ms 70052 KB Output is correct
16 Correct 44 ms 70132 KB Output is correct
17 Correct 44 ms 70044 KB Output is correct
18 Correct 43 ms 70076 KB Output is correct
19 Correct 44 ms 70092 KB Output is correct
20 Correct 44 ms 70116 KB Output is correct
21 Correct 44 ms 70080 KB Output is correct
22 Correct 49 ms 70108 KB Output is correct
23 Correct 48 ms 70052 KB Output is correct
24 Correct 44 ms 70084 KB Output is correct
25 Correct 47 ms 70028 KB Output is correct
26 Correct 44 ms 70040 KB Output is correct
27 Correct 44 ms 70124 KB Output is correct
28 Correct 47 ms 70144 KB Output is correct
29 Correct 45 ms 70060 KB Output is correct
30 Correct 47 ms 70156 KB Output is correct
31 Correct 45 ms 70136 KB Output is correct
32 Correct 44 ms 70208 KB Output is correct
33 Correct 44 ms 70032 KB Output is correct
34 Correct 48 ms 70024 KB Output is correct
35 Correct 45 ms 70048 KB Output is correct
36 Correct 45 ms 70084 KB Output is correct
37 Correct 45 ms 70032 KB Output is correct
38 Correct 47 ms 70064 KB Output is correct
39 Correct 46 ms 70112 KB Output is correct
40 Correct 47 ms 70020 KB Output is correct
41 Correct 45 ms 70108 KB Output is correct
42 Correct 42 ms 70036 KB Output is correct
43 Correct 43 ms 70012 KB Output is correct
44 Correct 43 ms 70124 KB Output is correct
45 Correct 46 ms 70104 KB Output is correct
46 Correct 49 ms 70652 KB Output is correct
47 Correct 51 ms 70620 KB Output is correct
48 Correct 50 ms 70640 KB Output is correct
49 Correct 48 ms 70980 KB Output is correct
50 Correct 48 ms 70888 KB Output is correct
51 Correct 49 ms 70980 KB Output is correct
52 Correct 48 ms 70852 KB Output is correct
53 Correct 49 ms 70852 KB Output is correct
54 Correct 50 ms 70856 KB Output is correct
55 Correct 54 ms 70860 KB Output is correct
56 Correct 50 ms 70796 KB Output is correct
57 Correct 52 ms 70532 KB Output is correct
58 Correct 51 ms 70548 KB Output is correct
59 Correct 52 ms 70596 KB Output is correct
60 Correct 54 ms 70596 KB Output is correct
61 Correct 47 ms 70736 KB Output is correct
62 Correct 45 ms 70736 KB Output is correct
63 Correct 48 ms 70708 KB Output is correct
64 Correct 56 ms 70604 KB Output is correct
65 Correct 54 ms 70392 KB Output is correct
66 Correct 50 ms 70476 KB Output is correct
67 Correct 48 ms 70480 KB Output is correct
68 Correct 49 ms 70700 KB Output is correct
69 Correct 48 ms 70824 KB Output is correct
70 Correct 47 ms 70736 KB Output is correct
71 Correct 52 ms 70604 KB Output is correct
72 Correct 50 ms 70604 KB Output is correct
73 Correct 56 ms 70328 KB Output is correct
74 Correct 47 ms 70596 KB Output is correct
75 Correct 46 ms 70728 KB Output is correct
76 Correct 49 ms 70628 KB Output is correct
77 Correct 47 ms 70680 KB Output is correct
78 Correct 48 ms 70512 KB Output is correct
79 Correct 46 ms 70476 KB Output is correct
80 Correct 47 ms 70468 KB Output is correct
81 Correct 47 ms 70612 KB Output is correct
82 Correct 46 ms 70596 KB Output is correct
83 Correct 53 ms 70588 KB Output is correct
84 Correct 47 ms 70408 KB Output is correct
85 Correct 47 ms 70492 KB Output is correct
86 Correct 49 ms 70456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 69964 KB Output is correct
2 Correct 43 ms 69976 KB Output is correct
3 Correct 46 ms 70060 KB Output is correct
4 Correct 44 ms 70100 KB Output is correct
5 Correct 45 ms 70136 KB Output is correct
6 Correct 45 ms 70208 KB Output is correct
7 Correct 43 ms 70148 KB Output is correct
8 Correct 46 ms 70084 KB Output is correct
9 Correct 45 ms 70168 KB Output is correct
10 Correct 45 ms 70112 KB Output is correct
11 Correct 44 ms 70116 KB Output is correct
12 Correct 46 ms 70140 KB Output is correct
13 Correct 43 ms 70124 KB Output is correct
14 Correct 43 ms 70084 KB Output is correct
15 Correct 46 ms 70052 KB Output is correct
16 Correct 44 ms 70132 KB Output is correct
17 Correct 44 ms 70044 KB Output is correct
18 Correct 43 ms 70076 KB Output is correct
19 Correct 44 ms 70092 KB Output is correct
20 Correct 44 ms 70116 KB Output is correct
21 Correct 44 ms 70080 KB Output is correct
22 Correct 49 ms 70108 KB Output is correct
23 Correct 48 ms 70052 KB Output is correct
24 Correct 44 ms 70084 KB Output is correct
25 Correct 47 ms 70028 KB Output is correct
26 Correct 44 ms 70040 KB Output is correct
27 Correct 44 ms 70124 KB Output is correct
28 Correct 47 ms 70144 KB Output is correct
29 Correct 45 ms 70060 KB Output is correct
30 Correct 47 ms 70156 KB Output is correct
31 Correct 45 ms 70136 KB Output is correct
32 Correct 44 ms 70208 KB Output is correct
33 Correct 44 ms 70032 KB Output is correct
34 Correct 48 ms 70024 KB Output is correct
35 Correct 45 ms 70048 KB Output is correct
36 Correct 45 ms 70084 KB Output is correct
37 Correct 45 ms 70032 KB Output is correct
38 Correct 47 ms 70064 KB Output is correct
39 Correct 46 ms 70112 KB Output is correct
40 Correct 47 ms 70020 KB Output is correct
41 Correct 45 ms 70108 KB Output is correct
42 Correct 42 ms 70036 KB Output is correct
43 Correct 43 ms 70012 KB Output is correct
44 Correct 43 ms 70124 KB Output is correct
45 Correct 46 ms 70104 KB Output is correct
46 Correct 49 ms 70652 KB Output is correct
47 Correct 51 ms 70620 KB Output is correct
48 Correct 50 ms 70640 KB Output is correct
49 Correct 48 ms 70980 KB Output is correct
50 Correct 48 ms 70888 KB Output is correct
51 Correct 49 ms 70980 KB Output is correct
52 Correct 48 ms 70852 KB Output is correct
53 Correct 49 ms 70852 KB Output is correct
54 Correct 50 ms 70856 KB Output is correct
55 Correct 54 ms 70860 KB Output is correct
56 Correct 50 ms 70796 KB Output is correct
57 Correct 52 ms 70532 KB Output is correct
58 Correct 51 ms 70548 KB Output is correct
59 Correct 52 ms 70596 KB Output is correct
60 Correct 54 ms 70596 KB Output is correct
61 Correct 47 ms 70736 KB Output is correct
62 Correct 45 ms 70736 KB Output is correct
63 Correct 48 ms 70708 KB Output is correct
64 Correct 56 ms 70604 KB Output is correct
65 Correct 54 ms 70392 KB Output is correct
66 Correct 50 ms 70476 KB Output is correct
67 Correct 48 ms 70480 KB Output is correct
68 Correct 49 ms 70700 KB Output is correct
69 Correct 48 ms 70824 KB Output is correct
70 Correct 47 ms 70736 KB Output is correct
71 Correct 52 ms 70604 KB Output is correct
72 Correct 50 ms 70604 KB Output is correct
73 Correct 56 ms 70328 KB Output is correct
74 Correct 47 ms 70596 KB Output is correct
75 Correct 46 ms 70728 KB Output is correct
76 Correct 49 ms 70628 KB Output is correct
77 Correct 47 ms 70680 KB Output is correct
78 Correct 48 ms 70512 KB Output is correct
79 Correct 46 ms 70476 KB Output is correct
80 Correct 47 ms 70468 KB Output is correct
81 Correct 47 ms 70612 KB Output is correct
82 Correct 46 ms 70596 KB Output is correct
83 Correct 53 ms 70588 KB Output is correct
84 Correct 47 ms 70408 KB Output is correct
85 Correct 47 ms 70492 KB Output is correct
86 Correct 49 ms 70456 KB Output is correct
87 Correct 70 ms 71512 KB Output is correct
88 Correct 103 ms 74396 KB Output is correct
89 Correct 284 ms 83916 KB Output is correct
90 Correct 308 ms 83804 KB Output is correct
91 Correct 283 ms 83848 KB Output is correct
92 Correct 194 ms 92452 KB Output is correct
93 Correct 179 ms 93800 KB Output is correct
94 Correct 178 ms 93792 KB Output is correct
95 Correct 183 ms 90356 KB Output is correct
96 Correct 181 ms 90632 KB Output is correct
97 Correct 177 ms 90596 KB Output is correct
98 Correct 183 ms 90824 KB Output is correct
99 Correct 180 ms 89660 KB Output is correct
100 Correct 368 ms 85084 KB Output is correct
101 Correct 375 ms 85252 KB Output is correct
102 Correct 384 ms 85312 KB Output is correct
103 Correct 388 ms 85264 KB Output is correct
104 Correct 196 ms 89572 KB Output is correct
105 Correct 214 ms 89556 KB Output is correct
106 Correct 209 ms 89628 KB Output is correct
107 Correct 275 ms 84016 KB Output is correct
108 Correct 252 ms 79676 KB Output is correct
109 Correct 263 ms 81564 KB Output is correct
110 Correct 151 ms 88308 KB Output is correct
111 Correct 173 ms 90280 KB Output is correct
112 Correct 150 ms 84944 KB Output is correct
113 Correct 153 ms 84060 KB Output is correct
114 Correct 354 ms 84924 KB Output is correct
115 Correct 374 ms 79884 KB Output is correct
116 Correct 179 ms 84016 KB Output is correct
117 Correct 182 ms 87100 KB Output is correct
118 Correct 181 ms 86500 KB Output is correct
119 Correct 184 ms 86128 KB Output is correct
120 Correct 150 ms 81904 KB Output is correct
121 Correct 157 ms 81272 KB Output is correct
122 Correct 165 ms 81056 KB Output is correct
123 Correct 204 ms 87136 KB Output is correct
124 Correct 197 ms 86460 KB Output is correct
125 Correct 204 ms 86200 KB Output is correct
126 Correct 163 ms 81980 KB Output is correct
127 Correct 169 ms 81388 KB Output is correct
128 Correct 170 ms 81116 KB Output is correct