Submission #523362

# Submission time Handle Problem Language Result Execution time Memory
523362 2022-02-07T14:34:01 Z CPSC Construction of Highway (JOI18_construction) C++14
16 / 100
2000 ms 28928 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;
vector < 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;
        vv.pb({c[b],x});
        for (int i = 0; i < vec[lst].size(); i++) {
            if (vec[lst][i].s + sum > x) {
                vv.pb({vec[lst][i].f,vec[lst][i].s - (x - sum)});
                idx = i + 1;
                break;
            }
            sum += vec[lst][i].s;
            idx = i + 1;
            if (sum == x) break;
        }
        
        for (int i = idx; i < vec[lst].size(); i++) {
            vv.pb({vec[lst][i].f,vec[lst][i].s});
        }
        vec[lst].clear();
        for (int i = 0; i < vv.size(); i++) {
            if (!vec[lst].size()) vec[lst].pb(vv[i]);
            else {
                if (vec[lst].back().f == vv[i].f) vec[lst].back().s += vv[i].s;
                else vec[lst].pb({vv[i].f,vv[i].s});
            }
        }
        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::vector<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:118: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]
  118 |         for (int i = 0; i < vec[lst].size(); i++) {
      |                         ~~^~~~~~~~~~~~~~~~~
construction.cpp:129:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |         for (int i = idx; i < vec[lst].size(); i++) {
      |                           ~~^~~~~~~~~~~~~~~~~
construction.cpp:133: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]
  133 |         for (int i = 0; i < vv.size(); i++) {
      |                         ~~^~~~~~~~~~~
construction.cpp: At global scope:
construction.cpp:144:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  144 | main() {
      | ^~~~
construction.cpp: In function 'int main()':
construction.cpp:155:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  155 |     for (int i = 1; i < tocomp.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5068 KB Output is correct
4 Correct 3 ms 5032 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 3 ms 5068 KB Output is correct
8 Correct 6 ms 5196 KB Output is correct
9 Correct 7 ms 5196 KB Output is correct
10 Correct 6 ms 5220 KB Output is correct
11 Correct 5 ms 5204 KB Output is correct
12 Correct 5 ms 5160 KB Output is correct
13 Correct 5 ms 5196 KB Output is correct
14 Correct 4 ms 5164 KB Output is correct
15 Correct 5 ms 5068 KB Output is correct
16 Correct 6 ms 5160 KB Output is correct
17 Correct 4 ms 5068 KB Output is correct
18 Correct 4 ms 5160 KB Output is correct
19 Correct 4 ms 5196 KB Output is correct
20 Correct 4 ms 5196 KB Output is correct
21 Correct 5 ms 5168 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 4 ms 5068 KB Output is correct
24 Correct 3 ms 5068 KB Output is correct
25 Correct 3 ms 5068 KB Output is correct
26 Correct 5 ms 5068 KB Output is correct
27 Correct 4 ms 5196 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5068 KB Output is correct
30 Correct 4 ms 5068 KB Output is correct
31 Correct 4 ms 5068 KB Output is correct
32 Correct 4 ms 5068 KB Output is correct
33 Correct 5 ms 5068 KB Output is correct
34 Correct 4 ms 5068 KB Output is correct
35 Correct 5 ms 5156 KB Output is correct
36 Correct 4 ms 5068 KB Output is correct
37 Correct 4 ms 5028 KB Output is correct
38 Correct 3 ms 5068 KB Output is correct
39 Correct 4 ms 5068 KB Output is correct
40 Correct 4 ms 5068 KB Output is correct
41 Correct 3 ms 5068 KB Output is correct
42 Correct 3 ms 5068 KB Output is correct
43 Correct 4 ms 5196 KB Output is correct
44 Correct 3 ms 5028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5068 KB Output is correct
4 Correct 3 ms 5032 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 3 ms 5068 KB Output is correct
8 Correct 6 ms 5196 KB Output is correct
9 Correct 7 ms 5196 KB Output is correct
10 Correct 6 ms 5220 KB Output is correct
11 Correct 5 ms 5204 KB Output is correct
12 Correct 5 ms 5160 KB Output is correct
13 Correct 5 ms 5196 KB Output is correct
14 Correct 4 ms 5164 KB Output is correct
15 Correct 5 ms 5068 KB Output is correct
16 Correct 6 ms 5160 KB Output is correct
17 Correct 4 ms 5068 KB Output is correct
18 Correct 4 ms 5160 KB Output is correct
19 Correct 4 ms 5196 KB Output is correct
20 Correct 4 ms 5196 KB Output is correct
21 Correct 5 ms 5168 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 4 ms 5068 KB Output is correct
24 Correct 3 ms 5068 KB Output is correct
25 Correct 3 ms 5068 KB Output is correct
26 Correct 5 ms 5068 KB Output is correct
27 Correct 4 ms 5196 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5068 KB Output is correct
30 Correct 4 ms 5068 KB Output is correct
31 Correct 4 ms 5068 KB Output is correct
32 Correct 4 ms 5068 KB Output is correct
33 Correct 5 ms 5068 KB Output is correct
34 Correct 4 ms 5068 KB Output is correct
35 Correct 5 ms 5156 KB Output is correct
36 Correct 4 ms 5068 KB Output is correct
37 Correct 4 ms 5028 KB Output is correct
38 Correct 3 ms 5068 KB Output is correct
39 Correct 4 ms 5068 KB Output is correct
40 Correct 4 ms 5068 KB Output is correct
41 Correct 3 ms 5068 KB Output is correct
42 Correct 3 ms 5068 KB Output is correct
43 Correct 4 ms 5196 KB Output is correct
44 Correct 3 ms 5028 KB Output is correct
45 Correct 5 ms 5268 KB Output is correct
46 Correct 11 ms 5720 KB Output is correct
47 Correct 10 ms 5760 KB Output is correct
48 Correct 10 ms 5708 KB Output is correct
49 Correct 173 ms 6056 KB Output is correct
50 Correct 184 ms 6068 KB Output is correct
51 Correct 191 ms 6084 KB Output is correct
52 Correct 94 ms 5896 KB Output is correct
53 Correct 92 ms 5956 KB Output is correct
54 Correct 87 ms 5936 KB Output is correct
55 Correct 94 ms 5916 KB Output is correct
56 Correct 54 ms 5912 KB Output is correct
57 Correct 18 ms 5708 KB Output is correct
58 Correct 13 ms 5676 KB Output is correct
59 Correct 13 ms 5708 KB Output is correct
60 Correct 13 ms 5708 KB Output is correct
61 Correct 48 ms 5924 KB Output is correct
62 Correct 49 ms 5920 KB Output is correct
63 Correct 49 ms 5928 KB Output is correct
64 Correct 9 ms 5544 KB Output is correct
65 Correct 10 ms 5452 KB Output is correct
66 Correct 11 ms 5548 KB Output is correct
67 Correct 11 ms 5632 KB Output is correct
68 Correct 174 ms 5848 KB Output is correct
69 Correct 88 ms 5936 KB Output is correct
70 Correct 90 ms 5708 KB Output is correct
71 Correct 48 ms 5696 KB Output is correct
72 Correct 15 ms 5708 KB Output is correct
73 Correct 14 ms 5452 KB Output is correct
74 Correct 49 ms 5708 KB Output is correct
75 Correct 27 ms 5808 KB Output is correct
76 Correct 24 ms 5808 KB Output is correct
77 Correct 18 ms 5780 KB Output is correct
78 Correct 30 ms 5580 KB Output is correct
79 Correct 20 ms 5580 KB Output is correct
80 Correct 18 ms 5572 KB Output is correct
81 Correct 32 ms 5828 KB Output is correct
82 Correct 22 ms 5796 KB Output is correct
83 Correct 19 ms 5780 KB Output is correct
84 Correct 28 ms 5624 KB Output is correct
85 Correct 21 ms 5604 KB Output is correct
86 Correct 17 ms 5580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5068 KB Output is correct
4 Correct 3 ms 5032 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 3 ms 5068 KB Output is correct
8 Correct 6 ms 5196 KB Output is correct
9 Correct 7 ms 5196 KB Output is correct
10 Correct 6 ms 5220 KB Output is correct
11 Correct 5 ms 5204 KB Output is correct
12 Correct 5 ms 5160 KB Output is correct
13 Correct 5 ms 5196 KB Output is correct
14 Correct 4 ms 5164 KB Output is correct
15 Correct 5 ms 5068 KB Output is correct
16 Correct 6 ms 5160 KB Output is correct
17 Correct 4 ms 5068 KB Output is correct
18 Correct 4 ms 5160 KB Output is correct
19 Correct 4 ms 5196 KB Output is correct
20 Correct 4 ms 5196 KB Output is correct
21 Correct 5 ms 5168 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 4 ms 5068 KB Output is correct
24 Correct 3 ms 5068 KB Output is correct
25 Correct 3 ms 5068 KB Output is correct
26 Correct 5 ms 5068 KB Output is correct
27 Correct 4 ms 5196 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5068 KB Output is correct
30 Correct 4 ms 5068 KB Output is correct
31 Correct 4 ms 5068 KB Output is correct
32 Correct 4 ms 5068 KB Output is correct
33 Correct 5 ms 5068 KB Output is correct
34 Correct 4 ms 5068 KB Output is correct
35 Correct 5 ms 5156 KB Output is correct
36 Correct 4 ms 5068 KB Output is correct
37 Correct 4 ms 5028 KB Output is correct
38 Correct 3 ms 5068 KB Output is correct
39 Correct 4 ms 5068 KB Output is correct
40 Correct 4 ms 5068 KB Output is correct
41 Correct 3 ms 5068 KB Output is correct
42 Correct 3 ms 5068 KB Output is correct
43 Correct 4 ms 5196 KB Output is correct
44 Correct 3 ms 5028 KB Output is correct
45 Correct 5 ms 5268 KB Output is correct
46 Correct 11 ms 5720 KB Output is correct
47 Correct 10 ms 5760 KB Output is correct
48 Correct 10 ms 5708 KB Output is correct
49 Correct 173 ms 6056 KB Output is correct
50 Correct 184 ms 6068 KB Output is correct
51 Correct 191 ms 6084 KB Output is correct
52 Correct 94 ms 5896 KB Output is correct
53 Correct 92 ms 5956 KB Output is correct
54 Correct 87 ms 5936 KB Output is correct
55 Correct 94 ms 5916 KB Output is correct
56 Correct 54 ms 5912 KB Output is correct
57 Correct 18 ms 5708 KB Output is correct
58 Correct 13 ms 5676 KB Output is correct
59 Correct 13 ms 5708 KB Output is correct
60 Correct 13 ms 5708 KB Output is correct
61 Correct 48 ms 5924 KB Output is correct
62 Correct 49 ms 5920 KB Output is correct
63 Correct 49 ms 5928 KB Output is correct
64 Correct 9 ms 5544 KB Output is correct
65 Correct 10 ms 5452 KB Output is correct
66 Correct 11 ms 5548 KB Output is correct
67 Correct 11 ms 5632 KB Output is correct
68 Correct 174 ms 5848 KB Output is correct
69 Correct 88 ms 5936 KB Output is correct
70 Correct 90 ms 5708 KB Output is correct
71 Correct 48 ms 5696 KB Output is correct
72 Correct 15 ms 5708 KB Output is correct
73 Correct 14 ms 5452 KB Output is correct
74 Correct 49 ms 5708 KB Output is correct
75 Correct 27 ms 5808 KB Output is correct
76 Correct 24 ms 5808 KB Output is correct
77 Correct 18 ms 5780 KB Output is correct
78 Correct 30 ms 5580 KB Output is correct
79 Correct 20 ms 5580 KB Output is correct
80 Correct 18 ms 5572 KB Output is correct
81 Correct 32 ms 5828 KB Output is correct
82 Correct 22 ms 5796 KB Output is correct
83 Correct 19 ms 5780 KB Output is correct
84 Correct 28 ms 5624 KB Output is correct
85 Correct 21 ms 5604 KB Output is correct
86 Correct 17 ms 5580 KB Output is correct
87 Correct 25 ms 6732 KB Output is correct
88 Correct 106 ms 10088 KB Output is correct
89 Correct 346 ms 20832 KB Output is correct
90 Correct 371 ms 20632 KB Output is correct
91 Correct 345 ms 20716 KB Output is correct
92 Execution timed out 2086 ms 28928 KB Time limit exceeded
93 Halted 0 ms 0 KB -