Submission #4353584


Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;

template<typename T>
struct BIT {
    int n;
    vector<T> dat;

    BIT(int n=0):n(n){
        initialize(n);
    }

    void initialize(int n){
        dat.resize(n);
        for(int i = 0; i<n; i++) dat[i] = 0;
    }

    T sum(int i){
        T s = 0;
        while(i >= 0){
            s += dat[i];
            i = (i & (i+1)) - 1;
        }
        return s;
    }

    T sum_between(int i, int j){
        return sum(j) - (i>0 ? sum(i-1) : 0);
    }

    void add(int i, T x){
        while(i < n){
            dat[i] += x;
            i |= i+1;
        }
    }

    // a[0]+...+a[ret] >= x
    int lower_bound(T x){
        int ret = -1;
        int k = 1;
        while(2*k <= n) k <<= 1;
        for( ;k>0; k>>=1){
            if(ret+k < n && dat[ret+k] < x){
                x -= dat[ret+k];
                ret += k;
            }
        }
        return ret + 1;
    }
};

int N[2];
vector<int> edges[100000];
int mx[100000][2];
int mx2[100000][2];
int64_t num[2][100000];
int MX[2];

void dfs(int i, int p){
    vector<int> cand = {0, 0};
    for(int j : edges[i]){
        if(j == p) continue;
        dfs(j, i);
        cand.push_back(mx[j][0]+1);
    }
    sort(cand.rbegin(), cand.rend());
    mx[i][0] = cand[0];
    mx[i][1] = cand[1];
}


void dfs2(int i, int p){
    vector<int> cand = {mx[i][0], mx[i][1]};
    if(p >= 0){
        if(mx[i][0]+1 == mx2[p][0]){
            cand.push_back(mx2[p][1]+1);
        }else{
            cand.push_back(mx2[p][0]+1);
        }
    }
    sort(cand.rbegin(), cand.rend());
    mx2[i][0] = cand[0];
    mx2[i][1] = cand[1];
    for(int j : edges[i]){
        if(j == p) continue;
        dfs2(j, i);
    }
}

int main(){
    for(int k=0; k<2; k++){

        for(int i=0; i<100000; i++){
            edges[i].clear();
        }

        cin >> N[k];
        for(int i=0; i<N[k]-1; i++){
            int a, b;
            cin >> a >> b;
            edges[a-1].push_back(b-1);
            edges[b-1].push_back(a-1);
        }

        dfs(0, -1);
        dfs2(0, -1);

        for(int i=0; i<N[k]; i++){
            num[k][mx2[i][0]]++;
            MX[k] = max(MX[k], mx2[i][0]);
        }
    }

    BIT<int64_t> bit(100001), bit_num(100001);
    for(int i=1; i<=MX[1]; i++){
        bit.add(i, num[1][i] * i);
        bit_num.add(i, num[1][i]);
    }

    int64_t ans = 0;
    int MAX = max(MX[0], MX[1]);
    for(int i=1; i<=MX[0]; i++){
        int64_t less = bit_num.sum(MAX-i-1);
        int64_t res1 = MAX * less;
        int64_t res2 = bit.sum_between(MAX-i, 100000) + (i+1) * (N[1] - less);
        ans += num[0][i] * (res1 + res2);
    }
    cout << ans << endl;

    return 0;
}

Submission Info

Submission Time
Task B - Bonsai Grafting
User betrue12
Language C++14 (GCC 5.4.1)
Score 700
Code Size 2825 Byte
Status AC
Exec Time 245 ms
Memory 21376 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 2
AC × 42
Set Name Test Cases
Sample 01.txt, 02.txt
All 01.txt, 02.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, 49.txt, 50.txt
Case Name Status Exec Time Memory
01.txt AC 4 ms 5248 KB
02.txt AC 3 ms 5248 KB
11.txt AC 3 ms 5248 KB
12.txt AC 3 ms 5248 KB
13.txt AC 3 ms 5248 KB
14.txt AC 3 ms 5248 KB
15.txt AC 3 ms 5248 KB
16.txt AC 3 ms 5248 KB
17.txt AC 3 ms 5248 KB
18.txt AC 3 ms 5248 KB
19.txt AC 3 ms 5248 KB
20.txt AC 3 ms 5248 KB
21.txt AC 201 ms 9216 KB
22.txt AC 189 ms 9472 KB
23.txt AC 215 ms 9088 KB
24.txt AC 245 ms 17408 KB
25.txt AC 191 ms 9728 KB
26.txt AC 190 ms 9316 KB
27.txt AC 210 ms 17408 KB
28.txt AC 201 ms 8960 KB
29.txt AC 220 ms 17408 KB
30.txt AC 235 ms 21376 KB
31.txt AC 198 ms 9216 KB
32.txt AC 188 ms 9472 KB
33.txt AC 200 ms 9088 KB
34.txt AC 222 ms 19840 KB
35.txt AC 189 ms 9600 KB
36.txt AC 195 ms 9336 KB
37.txt AC 208 ms 15356 KB
38.txt AC 200 ms 8960 KB
39.txt AC 205 ms 16128 KB
40.txt AC 211 ms 18816 KB
41.txt AC 106 ms 8960 KB
42.txt AC 105 ms 8960 KB
43.txt AC 108 ms 8960 KB
44.txt AC 98 ms 8960 KB
45.txt AC 100 ms 9344 KB
46.txt AC 99 ms 9344 KB
47.txt AC 133 ms 19328 KB
48.txt AC 108 ms 8960 KB
49.txt AC 128 ms 14976 KB
50.txt AC 118 ms 14976 KB