Submission #523358

# Submission time Handle Problem Language Result Execution time Memory
523358 2022-02-07T14:31:16 Z CPSC Construction of Highway (JOI18_construction) C++14
16 / 100
2000 ms 38124 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() {
	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 '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:153:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  153 |     for (int i = 1; i < tocomp.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14412 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 7 ms 14540 KB Output is correct
4 Correct 8 ms 14540 KB Output is correct
5 Correct 8 ms 14476 KB Output is correct
6 Correct 8 ms 14488 KB Output is correct
7 Correct 11 ms 14572 KB Output is correct
8 Correct 10 ms 14524 KB Output is correct
9 Correct 9 ms 14540 KB Output is correct
10 Correct 12 ms 14636 KB Output is correct
11 Correct 9 ms 14540 KB Output is correct
12 Correct 9 ms 14540 KB Output is correct
13 Correct 9 ms 14540 KB Output is correct
14 Correct 10 ms 14540 KB Output is correct
15 Correct 10 ms 14540 KB Output is correct
16 Correct 9 ms 14540 KB Output is correct
17 Correct 11 ms 14540 KB Output is correct
18 Correct 9 ms 14540 KB Output is correct
19 Correct 8 ms 14540 KB Output is correct
20 Correct 8 ms 14540 KB Output is correct
21 Correct 8 ms 14540 KB Output is correct
22 Correct 9 ms 14540 KB Output is correct
23 Correct 9 ms 14540 KB Output is correct
24 Correct 8 ms 14540 KB Output is correct
25 Correct 8 ms 14540 KB Output is correct
26 Correct 9 ms 14540 KB Output is correct
27 Correct 9 ms 14540 KB Output is correct
28 Correct 9 ms 14464 KB Output is correct
29 Correct 8 ms 14540 KB Output is correct
30 Correct 9 ms 14588 KB Output is correct
31 Correct 9 ms 14528 KB Output is correct
32 Correct 9 ms 14540 KB Output is correct
33 Correct 9 ms 14540 KB Output is correct
34 Correct 9 ms 14596 KB Output is correct
35 Correct 8 ms 14572 KB Output is correct
36 Correct 9 ms 14540 KB Output is correct
37 Correct 8 ms 14540 KB Output is correct
38 Correct 8 ms 14540 KB Output is correct
39 Correct 8 ms 14556 KB Output is correct
40 Correct 9 ms 14668 KB Output is correct
41 Correct 8 ms 14588 KB Output is correct
42 Correct 11 ms 14468 KB Output is correct
43 Correct 9 ms 14476 KB Output is correct
44 Correct 8 ms 14540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14412 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 7 ms 14540 KB Output is correct
4 Correct 8 ms 14540 KB Output is correct
5 Correct 8 ms 14476 KB Output is correct
6 Correct 8 ms 14488 KB Output is correct
7 Correct 11 ms 14572 KB Output is correct
8 Correct 10 ms 14524 KB Output is correct
9 Correct 9 ms 14540 KB Output is correct
10 Correct 12 ms 14636 KB Output is correct
11 Correct 9 ms 14540 KB Output is correct
12 Correct 9 ms 14540 KB Output is correct
13 Correct 9 ms 14540 KB Output is correct
14 Correct 10 ms 14540 KB Output is correct
15 Correct 10 ms 14540 KB Output is correct
16 Correct 9 ms 14540 KB Output is correct
17 Correct 11 ms 14540 KB Output is correct
18 Correct 9 ms 14540 KB Output is correct
19 Correct 8 ms 14540 KB Output is correct
20 Correct 8 ms 14540 KB Output is correct
21 Correct 8 ms 14540 KB Output is correct
22 Correct 9 ms 14540 KB Output is correct
23 Correct 9 ms 14540 KB Output is correct
24 Correct 8 ms 14540 KB Output is correct
25 Correct 8 ms 14540 KB Output is correct
26 Correct 9 ms 14540 KB Output is correct
27 Correct 9 ms 14540 KB Output is correct
28 Correct 9 ms 14464 KB Output is correct
29 Correct 8 ms 14540 KB Output is correct
30 Correct 9 ms 14588 KB Output is correct
31 Correct 9 ms 14528 KB Output is correct
32 Correct 9 ms 14540 KB Output is correct
33 Correct 9 ms 14540 KB Output is correct
34 Correct 9 ms 14596 KB Output is correct
35 Correct 8 ms 14572 KB Output is correct
36 Correct 9 ms 14540 KB Output is correct
37 Correct 8 ms 14540 KB Output is correct
38 Correct 8 ms 14540 KB Output is correct
39 Correct 8 ms 14556 KB Output is correct
40 Correct 9 ms 14668 KB Output is correct
41 Correct 8 ms 14588 KB Output is correct
42 Correct 11 ms 14468 KB Output is correct
43 Correct 9 ms 14476 KB Output is correct
44 Correct 8 ms 14540 KB Output is correct
45 Correct 13 ms 14660 KB Output is correct
46 Correct 17 ms 15052 KB Output is correct
47 Correct 17 ms 15124 KB Output is correct
48 Correct 15 ms 15052 KB Output is correct
49 Correct 93 ms 15432 KB Output is correct
50 Correct 97 ms 15436 KB Output is correct
51 Correct 95 ms 15436 KB Output is correct
52 Correct 59 ms 15268 KB Output is correct
53 Correct 54 ms 15284 KB Output is correct
54 Correct 53 ms 15280 KB Output is correct
55 Correct 52 ms 15284 KB Output is correct
56 Correct 32 ms 15180 KB Output is correct
57 Correct 17 ms 15052 KB Output is correct
58 Correct 19 ms 15052 KB Output is correct
59 Correct 18 ms 15052 KB Output is correct
60 Correct 17 ms 15112 KB Output is correct
61 Correct 32 ms 15276 KB Output is correct
62 Correct 32 ms 15292 KB Output is correct
63 Correct 34 ms 15180 KB Output is correct
64 Correct 15 ms 14800 KB Output is correct
65 Correct 14 ms 14916 KB Output is correct
66 Correct 15 ms 14940 KB Output is correct
67 Correct 18 ms 14940 KB Output is correct
68 Correct 96 ms 15256 KB Output is correct
69 Correct 54 ms 15284 KB Output is correct
70 Correct 54 ms 15092 KB Output is correct
71 Correct 34 ms 15084 KB Output is correct
72 Correct 21 ms 15100 KB Output is correct
73 Correct 17 ms 14924 KB Output is correct
74 Correct 30 ms 15096 KB Output is correct
75 Correct 23 ms 15108 KB Output is correct
76 Correct 21 ms 15180 KB Output is correct
77 Correct 18 ms 15152 KB Output is correct
78 Correct 26 ms 14924 KB Output is correct
79 Correct 19 ms 14984 KB Output is correct
80 Correct 17 ms 14952 KB Output is correct
81 Correct 23 ms 15144 KB Output is correct
82 Correct 20 ms 15180 KB Output is correct
83 Correct 18 ms 15052 KB Output is correct
84 Correct 22 ms 15008 KB Output is correct
85 Correct 18 ms 14984 KB Output is correct
86 Correct 17 ms 14924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14412 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 7 ms 14540 KB Output is correct
4 Correct 8 ms 14540 KB Output is correct
5 Correct 8 ms 14476 KB Output is correct
6 Correct 8 ms 14488 KB Output is correct
7 Correct 11 ms 14572 KB Output is correct
8 Correct 10 ms 14524 KB Output is correct
9 Correct 9 ms 14540 KB Output is correct
10 Correct 12 ms 14636 KB Output is correct
11 Correct 9 ms 14540 KB Output is correct
12 Correct 9 ms 14540 KB Output is correct
13 Correct 9 ms 14540 KB Output is correct
14 Correct 10 ms 14540 KB Output is correct
15 Correct 10 ms 14540 KB Output is correct
16 Correct 9 ms 14540 KB Output is correct
17 Correct 11 ms 14540 KB Output is correct
18 Correct 9 ms 14540 KB Output is correct
19 Correct 8 ms 14540 KB Output is correct
20 Correct 8 ms 14540 KB Output is correct
21 Correct 8 ms 14540 KB Output is correct
22 Correct 9 ms 14540 KB Output is correct
23 Correct 9 ms 14540 KB Output is correct
24 Correct 8 ms 14540 KB Output is correct
25 Correct 8 ms 14540 KB Output is correct
26 Correct 9 ms 14540 KB Output is correct
27 Correct 9 ms 14540 KB Output is correct
28 Correct 9 ms 14464 KB Output is correct
29 Correct 8 ms 14540 KB Output is correct
30 Correct 9 ms 14588 KB Output is correct
31 Correct 9 ms 14528 KB Output is correct
32 Correct 9 ms 14540 KB Output is correct
33 Correct 9 ms 14540 KB Output is correct
34 Correct 9 ms 14596 KB Output is correct
35 Correct 8 ms 14572 KB Output is correct
36 Correct 9 ms 14540 KB Output is correct
37 Correct 8 ms 14540 KB Output is correct
38 Correct 8 ms 14540 KB Output is correct
39 Correct 8 ms 14556 KB Output is correct
40 Correct 9 ms 14668 KB Output is correct
41 Correct 8 ms 14588 KB Output is correct
42 Correct 11 ms 14468 KB Output is correct
43 Correct 9 ms 14476 KB Output is correct
44 Correct 8 ms 14540 KB Output is correct
45 Correct 13 ms 14660 KB Output is correct
46 Correct 17 ms 15052 KB Output is correct
47 Correct 17 ms 15124 KB Output is correct
48 Correct 15 ms 15052 KB Output is correct
49 Correct 93 ms 15432 KB Output is correct
50 Correct 97 ms 15436 KB Output is correct
51 Correct 95 ms 15436 KB Output is correct
52 Correct 59 ms 15268 KB Output is correct
53 Correct 54 ms 15284 KB Output is correct
54 Correct 53 ms 15280 KB Output is correct
55 Correct 52 ms 15284 KB Output is correct
56 Correct 32 ms 15180 KB Output is correct
57 Correct 17 ms 15052 KB Output is correct
58 Correct 19 ms 15052 KB Output is correct
59 Correct 18 ms 15052 KB Output is correct
60 Correct 17 ms 15112 KB Output is correct
61 Correct 32 ms 15276 KB Output is correct
62 Correct 32 ms 15292 KB Output is correct
63 Correct 34 ms 15180 KB Output is correct
64 Correct 15 ms 14800 KB Output is correct
65 Correct 14 ms 14916 KB Output is correct
66 Correct 15 ms 14940 KB Output is correct
67 Correct 18 ms 14940 KB Output is correct
68 Correct 96 ms 15256 KB Output is correct
69 Correct 54 ms 15284 KB Output is correct
70 Correct 54 ms 15092 KB Output is correct
71 Correct 34 ms 15084 KB Output is correct
72 Correct 21 ms 15100 KB Output is correct
73 Correct 17 ms 14924 KB Output is correct
74 Correct 30 ms 15096 KB Output is correct
75 Correct 23 ms 15108 KB Output is correct
76 Correct 21 ms 15180 KB Output is correct
77 Correct 18 ms 15152 KB Output is correct
78 Correct 26 ms 14924 KB Output is correct
79 Correct 19 ms 14984 KB Output is correct
80 Correct 17 ms 14952 KB Output is correct
81 Correct 23 ms 15144 KB Output is correct
82 Correct 20 ms 15180 KB Output is correct
83 Correct 18 ms 15052 KB Output is correct
84 Correct 22 ms 15008 KB Output is correct
85 Correct 18 ms 14984 KB Output is correct
86 Correct 17 ms 14924 KB Output is correct
87 Correct 35 ms 16016 KB Output is correct
88 Correct 86 ms 19040 KB Output is correct
89 Correct 357 ms 29612 KB Output is correct
90 Correct 361 ms 29676 KB Output is correct
91 Correct 305 ms 29712 KB Output is correct
92 Execution timed out 2066 ms 38124 KB Time limit exceeded
93 Halted 0 ms 0 KB -