Submission #523356

# Submission time Handle Problem Language Result Execution time Memory
523356 2022-02-07T14:29:42 Z CPSC Construction of Highway (JOI18_construction) C++14
16 / 100
2000 ms 40020 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 = 3e5 + 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);
    }
}
void prnt(vector <pii> vv) {
    for (int i = 0; i < vv.size(); i++) {
        cout<<vv[i].f<<"--->"<<vv[i].s<<endl;
    }cout<<"\n";
}
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] = vv;
        cur = par[lst];
        if (cur == 0) break;
    }
}
main() {
    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 'void prnt(std::vector<std::pair<int, int> >)':
construction.cpp:69: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]
   69 |     for (int i = 0; i < vv.size(); i++) {
      |                     ~~^~~~~~~~~~~
construction.cpp: In function 'int getans(int, int)':
construction.cpp:85: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]
   85 |         for (int i = 0; i < vec[lst].size(); i++) {
      |                         ~~^~~~~~~~~~~~~~~~~
construction.cpp:97: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]
   97 |         for (int i = 0; i < vv.size(); i++) {
      |                         ~~^~~~~~~~~~~
construction.cpp:107: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]
  107 |     for (int i = 0; i < pas.size(); i++) {
      |                     ~~^~~~~~~~~~~~
construction.cpp:111: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]
  111 |     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:123: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]
  123 |         for (int i = 0; i < vec[lst].size(); i++) {
      |                         ~~^~~~~~~~~~~~~~~~~
construction.cpp:134: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]
  134 |         for (int i = idx; i < vec[lst].size(); i++) {
      |                           ~~^~~~~~~~~~~~~~~~~
construction.cpp: At global scope:
construction.cpp:142:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  142 | main() {
      | ^~~~
construction.cpp: In function 'int main()':
construction.cpp:151:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  151 |     for (int i = 1; i < tocomp.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14404 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 8 ms 14528 KB Output is correct
4 Correct 11 ms 14488 KB Output is correct
5 Correct 12 ms 14580 KB Output is correct
6 Correct 11 ms 14532 KB Output is correct
7 Correct 9 ms 14540 KB Output is correct
8 Correct 11 ms 14536 KB Output is correct
9 Correct 10 ms 14540 KB Output is correct
10 Correct 10 ms 14532 KB Output is correct
11 Correct 10 ms 14500 KB Output is correct
12 Correct 10 ms 14600 KB Output is correct
13 Correct 10 ms 14544 KB Output is correct
14 Correct 12 ms 14524 KB Output is correct
15 Correct 9 ms 14540 KB Output is correct
16 Correct 10 ms 14532 KB Output is correct
17 Correct 10 ms 14552 KB Output is correct
18 Correct 10 ms 14536 KB Output is correct
19 Correct 10 ms 14540 KB Output is correct
20 Correct 10 ms 14536 KB Output is correct
21 Correct 9 ms 14540 KB Output is correct
22 Correct 9 ms 14540 KB Output is correct
23 Correct 9 ms 14436 KB Output is correct
24 Correct 9 ms 14532 KB Output is correct
25 Correct 9 ms 14540 KB Output is correct
26 Correct 10 ms 14540 KB Output is correct
27 Correct 10 ms 14540 KB Output is correct
28 Correct 10 ms 14540 KB Output is correct
29 Correct 9 ms 14540 KB Output is correct
30 Correct 13 ms 14548 KB Output is correct
31 Correct 12 ms 14540 KB Output is correct
32 Correct 12 ms 14548 KB Output is correct
33 Correct 9 ms 14576 KB Output is correct
34 Correct 9 ms 14540 KB Output is correct
35 Correct 10 ms 14532 KB Output is correct
36 Correct 9 ms 14540 KB Output is correct
37 Correct 9 ms 14540 KB Output is correct
38 Correct 9 ms 14540 KB Output is correct
39 Correct 10 ms 14528 KB Output is correct
40 Correct 11 ms 14532 KB Output is correct
41 Correct 12 ms 14528 KB Output is correct
42 Correct 9 ms 14532 KB Output is correct
43 Correct 13 ms 14540 KB Output is correct
44 Correct 9 ms 14540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14404 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 8 ms 14528 KB Output is correct
4 Correct 11 ms 14488 KB Output is correct
5 Correct 12 ms 14580 KB Output is correct
6 Correct 11 ms 14532 KB Output is correct
7 Correct 9 ms 14540 KB Output is correct
8 Correct 11 ms 14536 KB Output is correct
9 Correct 10 ms 14540 KB Output is correct
10 Correct 10 ms 14532 KB Output is correct
11 Correct 10 ms 14500 KB Output is correct
12 Correct 10 ms 14600 KB Output is correct
13 Correct 10 ms 14544 KB Output is correct
14 Correct 12 ms 14524 KB Output is correct
15 Correct 9 ms 14540 KB Output is correct
16 Correct 10 ms 14532 KB Output is correct
17 Correct 10 ms 14552 KB Output is correct
18 Correct 10 ms 14536 KB Output is correct
19 Correct 10 ms 14540 KB Output is correct
20 Correct 10 ms 14536 KB Output is correct
21 Correct 9 ms 14540 KB Output is correct
22 Correct 9 ms 14540 KB Output is correct
23 Correct 9 ms 14436 KB Output is correct
24 Correct 9 ms 14532 KB Output is correct
25 Correct 9 ms 14540 KB Output is correct
26 Correct 10 ms 14540 KB Output is correct
27 Correct 10 ms 14540 KB Output is correct
28 Correct 10 ms 14540 KB Output is correct
29 Correct 9 ms 14540 KB Output is correct
30 Correct 13 ms 14548 KB Output is correct
31 Correct 12 ms 14540 KB Output is correct
32 Correct 12 ms 14548 KB Output is correct
33 Correct 9 ms 14576 KB Output is correct
34 Correct 9 ms 14540 KB Output is correct
35 Correct 10 ms 14532 KB Output is correct
36 Correct 9 ms 14540 KB Output is correct
37 Correct 9 ms 14540 KB Output is correct
38 Correct 9 ms 14540 KB Output is correct
39 Correct 10 ms 14528 KB Output is correct
40 Correct 11 ms 14532 KB Output is correct
41 Correct 12 ms 14528 KB Output is correct
42 Correct 9 ms 14532 KB Output is correct
43 Correct 13 ms 14540 KB Output is correct
44 Correct 9 ms 14540 KB Output is correct
45 Correct 11 ms 14540 KB Output is correct
46 Correct 24 ms 15180 KB Output is correct
47 Correct 22 ms 15116 KB Output is correct
48 Correct 24 ms 15044 KB Output is correct
49 Correct 103 ms 15480 KB Output is correct
50 Correct 109 ms 15488 KB Output is correct
51 Correct 108 ms 15432 KB Output is correct
52 Correct 64 ms 15428 KB Output is correct
53 Correct 61 ms 15336 KB Output is correct
54 Correct 61 ms 15288 KB Output is correct
55 Correct 61 ms 15348 KB Output is correct
56 Correct 40 ms 15308 KB Output is correct
57 Correct 24 ms 15044 KB Output is correct
58 Correct 24 ms 15144 KB Output is correct
59 Correct 24 ms 15180 KB Output is correct
60 Correct 25 ms 15048 KB Output is correct
61 Correct 41 ms 15308 KB Output is correct
62 Correct 40 ms 15308 KB Output is correct
63 Correct 40 ms 15300 KB Output is correct
64 Correct 22 ms 14924 KB Output is correct
65 Correct 23 ms 14924 KB Output is correct
66 Correct 23 ms 14924 KB Output is correct
67 Correct 21 ms 14920 KB Output is correct
68 Correct 100 ms 15272 KB Output is correct
69 Correct 64 ms 15308 KB Output is correct
70 Correct 60 ms 15108 KB Output is correct
71 Correct 38 ms 15052 KB Output is correct
72 Correct 25 ms 15152 KB Output is correct
73 Correct 24 ms 14924 KB Output is correct
74 Correct 39 ms 15084 KB Output is correct
75 Correct 30 ms 15244 KB Output is correct
76 Correct 27 ms 15180 KB Output is correct
77 Correct 26 ms 15200 KB Output is correct
78 Correct 29 ms 15052 KB Output is correct
79 Correct 25 ms 14924 KB Output is correct
80 Correct 24 ms 14924 KB Output is correct
81 Correct 30 ms 15200 KB Output is correct
82 Correct 27 ms 15216 KB Output is correct
83 Correct 26 ms 15180 KB Output is correct
84 Correct 28 ms 15036 KB Output is correct
85 Correct 25 ms 14924 KB Output is correct
86 Correct 24 ms 14924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14404 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 8 ms 14528 KB Output is correct
4 Correct 11 ms 14488 KB Output is correct
5 Correct 12 ms 14580 KB Output is correct
6 Correct 11 ms 14532 KB Output is correct
7 Correct 9 ms 14540 KB Output is correct
8 Correct 11 ms 14536 KB Output is correct
9 Correct 10 ms 14540 KB Output is correct
10 Correct 10 ms 14532 KB Output is correct
11 Correct 10 ms 14500 KB Output is correct
12 Correct 10 ms 14600 KB Output is correct
13 Correct 10 ms 14544 KB Output is correct
14 Correct 12 ms 14524 KB Output is correct
15 Correct 9 ms 14540 KB Output is correct
16 Correct 10 ms 14532 KB Output is correct
17 Correct 10 ms 14552 KB Output is correct
18 Correct 10 ms 14536 KB Output is correct
19 Correct 10 ms 14540 KB Output is correct
20 Correct 10 ms 14536 KB Output is correct
21 Correct 9 ms 14540 KB Output is correct
22 Correct 9 ms 14540 KB Output is correct
23 Correct 9 ms 14436 KB Output is correct
24 Correct 9 ms 14532 KB Output is correct
25 Correct 9 ms 14540 KB Output is correct
26 Correct 10 ms 14540 KB Output is correct
27 Correct 10 ms 14540 KB Output is correct
28 Correct 10 ms 14540 KB Output is correct
29 Correct 9 ms 14540 KB Output is correct
30 Correct 13 ms 14548 KB Output is correct
31 Correct 12 ms 14540 KB Output is correct
32 Correct 12 ms 14548 KB Output is correct
33 Correct 9 ms 14576 KB Output is correct
34 Correct 9 ms 14540 KB Output is correct
35 Correct 10 ms 14532 KB Output is correct
36 Correct 9 ms 14540 KB Output is correct
37 Correct 9 ms 14540 KB Output is correct
38 Correct 9 ms 14540 KB Output is correct
39 Correct 10 ms 14528 KB Output is correct
40 Correct 11 ms 14532 KB Output is correct
41 Correct 12 ms 14528 KB Output is correct
42 Correct 9 ms 14532 KB Output is correct
43 Correct 13 ms 14540 KB Output is correct
44 Correct 9 ms 14540 KB Output is correct
45 Correct 11 ms 14540 KB Output is correct
46 Correct 24 ms 15180 KB Output is correct
47 Correct 22 ms 15116 KB Output is correct
48 Correct 24 ms 15044 KB Output is correct
49 Correct 103 ms 15480 KB Output is correct
50 Correct 109 ms 15488 KB Output is correct
51 Correct 108 ms 15432 KB Output is correct
52 Correct 64 ms 15428 KB Output is correct
53 Correct 61 ms 15336 KB Output is correct
54 Correct 61 ms 15288 KB Output is correct
55 Correct 61 ms 15348 KB Output is correct
56 Correct 40 ms 15308 KB Output is correct
57 Correct 24 ms 15044 KB Output is correct
58 Correct 24 ms 15144 KB Output is correct
59 Correct 24 ms 15180 KB Output is correct
60 Correct 25 ms 15048 KB Output is correct
61 Correct 41 ms 15308 KB Output is correct
62 Correct 40 ms 15308 KB Output is correct
63 Correct 40 ms 15300 KB Output is correct
64 Correct 22 ms 14924 KB Output is correct
65 Correct 23 ms 14924 KB Output is correct
66 Correct 23 ms 14924 KB Output is correct
67 Correct 21 ms 14920 KB Output is correct
68 Correct 100 ms 15272 KB Output is correct
69 Correct 64 ms 15308 KB Output is correct
70 Correct 60 ms 15108 KB Output is correct
71 Correct 38 ms 15052 KB Output is correct
72 Correct 25 ms 15152 KB Output is correct
73 Correct 24 ms 14924 KB Output is correct
74 Correct 39 ms 15084 KB Output is correct
75 Correct 30 ms 15244 KB Output is correct
76 Correct 27 ms 15180 KB Output is correct
77 Correct 26 ms 15200 KB Output is correct
78 Correct 29 ms 15052 KB Output is correct
79 Correct 25 ms 14924 KB Output is correct
80 Correct 24 ms 14924 KB Output is correct
81 Correct 30 ms 15200 KB Output is correct
82 Correct 27 ms 15216 KB Output is correct
83 Correct 26 ms 15180 KB Output is correct
84 Correct 28 ms 15036 KB Output is correct
85 Correct 25 ms 14924 KB Output is correct
86 Correct 24 ms 14924 KB Output is correct
87 Correct 49 ms 16192 KB Output is correct
88 Correct 127 ms 19608 KB Output is correct
89 Correct 504 ms 31764 KB Output is correct
90 Correct 470 ms 31784 KB Output is correct
91 Correct 509 ms 31804 KB Output is correct
92 Execution timed out 2050 ms 40020 KB Time limit exceeded
93 Halted 0 ms 0 KB -