답안 #173133

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
173133 2020-01-03T12:28:05 Z Akashi Construction of Highway (JOI18_construction) C++14
100 / 100
391 ms 107016 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;

int n;
int a[N], H[N], TT[N], sz[N], Next[N], x[N], y[N];
bool viz[N];
vector <int> v[N];
deque <pair <int, int> > L[N];
vector <pair <int, int> > aux, tmp;
vector <int> val;

void adfs(int nod = 1){
    sz[nod] = 1;
    for(auto &it : v[nod]){
        TT[it] = nod; H[it] = H[nod] + 1;
        adfs(it);
        if(sz[it] > sz[v[nod][0]]) swap(it, v[nod][0]);
    }
}

void bdfs(int nod = 1){
    if(v[nod].empty()) return ;
    Next[v[nod][0]] = Next[nod];
    bdfs(v[nod][0]);
    for(int i = 1; i < v[nod].size() ; ++i){
        Next[v[nod][i]] = v[nod][i];
        bdfs(v[nod][i]);
    }
}

int AS;
long long bit[N];

inline void C(){
    for(int i = 1; i <= AS ; ++i)
        bit[i] = 0;
}

inline void U(int x, int val){
    for(int i = x; i <= AS ; i = i + (i & (-i)))
        bit[i] += val;
}

inline long long Q(int x){
    long long Sol = 0;
    for(int i = x; i > 0 ; i = i - (i & (-i)))
        Sol += bit[i];
    return Sol;
}

long long calc_path(int nod){
    aux.clear(); val.clear();

    while(nod != 0){
        int p = Next[nod];
        int S = H[nod] - H[p] + 1;

        int NR = 0;
        tmp.clear();
        for(auto it : L[p]){
            if(NR + it.second < S) NR = NR + it.second, tmp.push_back({it.first, it.second});
            else {tmp.push_back({it.first, S - NR}), NR = S; break ;}
        }

        for(vector <pair <int, int> > :: reverse_iterator it = tmp.rbegin() ; it != tmp.rend() ; ++it)
            aux.push_back({it->first, it->second});

        nod = TT[p];
    }

    for(auto it : aux) val.push_back(it.first);
    sort(val.begin(), val.end());
    val.erase(unique(val.begin(), val.end()), val.end());

    AS = val.size();
    C();
    long long Sum = 0, Sol = 0;
    for(auto it : aux){
        int pos = lower_bound(val.begin(), val.end(), it.first) - val.begin() + 1;
        Sol = Sol + 1LL * it.second * Q(pos - 1);

        Sum += it.second;
        U(pos, it.second);
    }

    return Sol;
}

void update(int nod, int val){
    while(nod != 0){
        int p = Next[nod];
        int S = H[nod] - H[p] + 1;

        int NR = 0;
        while(NR < S && !L[p].empty()){
            if(NR + L[p].front().second <= S) NR = NR + L[p].front().second, L[p].pop_front();
            else{
                L[p].front().second -= (S - NR);
                NR = S;
            }
        }
        L[p].push_front({val, S});

        nod = TT[p];
    }
}

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n ; ++i) scanf("%d", &a[i]);

    for(int i = 1; i < n ; ++i){
        scanf("%d%d", &x[i], &y[i]);
        v[x[i]].push_back(y[i]);
    }

    adfs();
    Next[1] = 1;
    bdfs();

    for(int i = 1; i < n ; ++i){
        printf("%d\n", calc_path(x[i]));
        update(y[i], a[y[i]]);
    }

    return 0;
}



















Compilation message

construction.cpp: In function 'void bdfs(int)':
construction.cpp:27:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 1; i < v[nod].size() ; ++i){
                    ~~^~~~~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:125:39: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
         printf("%d\n", calc_path(x[i]));
                        ~~~~~~~~~~~~~~~^
construction.cpp:112:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
construction.cpp:113:39: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i = 1; i <= n ; ++i) scanf("%d", &a[i]);
                                  ~~~~~^~~~~~~~~~~~~
construction.cpp:116:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &x[i], &y[i]);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 78 ms 70008 KB Output is correct
2 Correct 77 ms 70008 KB Output is correct
3 Correct 76 ms 70136 KB Output is correct
4 Correct 77 ms 70136 KB Output is correct
5 Correct 78 ms 70356 KB Output is correct
6 Correct 78 ms 70264 KB Output is correct
7 Correct 77 ms 70256 KB Output is correct
8 Correct 73 ms 70136 KB Output is correct
9 Correct 71 ms 70136 KB Output is correct
10 Correct 72 ms 70136 KB Output is correct
11 Correct 77 ms 70136 KB Output is correct
12 Correct 78 ms 70236 KB Output is correct
13 Correct 78 ms 70216 KB Output is correct
14 Correct 80 ms 70360 KB Output is correct
15 Correct 78 ms 70136 KB Output is correct
16 Correct 78 ms 70164 KB Output is correct
17 Correct 78 ms 70264 KB Output is correct
18 Correct 78 ms 70136 KB Output is correct
19 Correct 79 ms 70264 KB Output is correct
20 Correct 78 ms 70196 KB Output is correct
21 Correct 77 ms 70136 KB Output is correct
22 Correct 77 ms 70264 KB Output is correct
23 Correct 77 ms 70264 KB Output is correct
24 Correct 77 ms 70136 KB Output is correct
25 Correct 78 ms 70264 KB Output is correct
26 Correct 72 ms 70136 KB Output is correct
27 Correct 78 ms 70364 KB Output is correct
28 Correct 77 ms 70236 KB Output is correct
29 Correct 79 ms 70328 KB Output is correct
30 Correct 79 ms 70136 KB Output is correct
31 Correct 77 ms 70136 KB Output is correct
32 Correct 77 ms 70180 KB Output is correct
33 Correct 77 ms 70260 KB Output is correct
34 Correct 76 ms 70136 KB Output is correct
35 Correct 76 ms 70136 KB Output is correct
36 Correct 77 ms 70116 KB Output is correct
37 Correct 77 ms 70136 KB Output is correct
38 Correct 77 ms 70260 KB Output is correct
39 Correct 95 ms 70260 KB Output is correct
40 Correct 78 ms 70136 KB Output is correct
41 Correct 77 ms 70260 KB Output is correct
42 Correct 76 ms 70136 KB Output is correct
43 Correct 78 ms 70260 KB Output is correct
44 Correct 76 ms 70232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 78 ms 70008 KB Output is correct
2 Correct 77 ms 70008 KB Output is correct
3 Correct 76 ms 70136 KB Output is correct
4 Correct 77 ms 70136 KB Output is correct
5 Correct 78 ms 70356 KB Output is correct
6 Correct 78 ms 70264 KB Output is correct
7 Correct 77 ms 70256 KB Output is correct
8 Correct 73 ms 70136 KB Output is correct
9 Correct 71 ms 70136 KB Output is correct
10 Correct 72 ms 70136 KB Output is correct
11 Correct 77 ms 70136 KB Output is correct
12 Correct 78 ms 70236 KB Output is correct
13 Correct 78 ms 70216 KB Output is correct
14 Correct 80 ms 70360 KB Output is correct
15 Correct 78 ms 70136 KB Output is correct
16 Correct 78 ms 70164 KB Output is correct
17 Correct 78 ms 70264 KB Output is correct
18 Correct 78 ms 70136 KB Output is correct
19 Correct 79 ms 70264 KB Output is correct
20 Correct 78 ms 70196 KB Output is correct
21 Correct 77 ms 70136 KB Output is correct
22 Correct 77 ms 70264 KB Output is correct
23 Correct 77 ms 70264 KB Output is correct
24 Correct 77 ms 70136 KB Output is correct
25 Correct 78 ms 70264 KB Output is correct
26 Correct 72 ms 70136 KB Output is correct
27 Correct 78 ms 70364 KB Output is correct
28 Correct 77 ms 70236 KB Output is correct
29 Correct 79 ms 70328 KB Output is correct
30 Correct 79 ms 70136 KB Output is correct
31 Correct 77 ms 70136 KB Output is correct
32 Correct 77 ms 70180 KB Output is correct
33 Correct 77 ms 70260 KB Output is correct
34 Correct 76 ms 70136 KB Output is correct
35 Correct 76 ms 70136 KB Output is correct
36 Correct 77 ms 70116 KB Output is correct
37 Correct 77 ms 70136 KB Output is correct
38 Correct 77 ms 70260 KB Output is correct
39 Correct 95 ms 70260 KB Output is correct
40 Correct 78 ms 70136 KB Output is correct
41 Correct 77 ms 70260 KB Output is correct
42 Correct 76 ms 70136 KB Output is correct
43 Correct 78 ms 70260 KB Output is correct
44 Correct 76 ms 70232 KB Output is correct
45 Correct 80 ms 70396 KB Output is correct
46 Correct 84 ms 71356 KB Output is correct
47 Correct 85 ms 71416 KB Output is correct
48 Correct 85 ms 71288 KB Output is correct
49 Correct 75 ms 70520 KB Output is correct
50 Correct 76 ms 70648 KB Output is correct
51 Correct 76 ms 70520 KB Output is correct
52 Correct 82 ms 71464 KB Output is correct
53 Correct 82 ms 71544 KB Output is correct
54 Correct 81 ms 71416 KB Output is correct
55 Correct 82 ms 71544 KB Output is correct
56 Correct 82 ms 71580 KB Output is correct
57 Correct 83 ms 71288 KB Output is correct
58 Correct 86 ms 71288 KB Output is correct
59 Correct 86 ms 71356 KB Output is correct
60 Correct 86 ms 71508 KB Output is correct
61 Correct 83 ms 71476 KB Output is correct
62 Correct 82 ms 71416 KB Output is correct
63 Correct 83 ms 71416 KB Output is correct
64 Correct 84 ms 71464 KB Output is correct
65 Correct 86 ms 71352 KB Output is correct
66 Correct 86 ms 71288 KB Output is correct
67 Correct 83 ms 71288 KB Output is correct
68 Correct 75 ms 70648 KB Output is correct
69 Correct 82 ms 71464 KB Output is correct
70 Correct 81 ms 71420 KB Output is correct
71 Correct 86 ms 71448 KB Output is correct
72 Correct 89 ms 71416 KB Output is correct
73 Correct 88 ms 71252 KB Output is correct
74 Correct 82 ms 71420 KB Output is correct
75 Correct 81 ms 71312 KB Output is correct
76 Correct 83 ms 71288 KB Output is correct
77 Correct 83 ms 71416 KB Output is correct
78 Correct 81 ms 71416 KB Output is correct
79 Correct 82 ms 71288 KB Output is correct
80 Correct 83 ms 71288 KB Output is correct
81 Correct 83 ms 71288 KB Output is correct
82 Correct 83 ms 71416 KB Output is correct
83 Correct 86 ms 71388 KB Output is correct
84 Correct 80 ms 71288 KB Output is correct
85 Correct 83 ms 71344 KB Output is correct
86 Correct 82 ms 71288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 78 ms 70008 KB Output is correct
2 Correct 77 ms 70008 KB Output is correct
3 Correct 76 ms 70136 KB Output is correct
4 Correct 77 ms 70136 KB Output is correct
5 Correct 78 ms 70356 KB Output is correct
6 Correct 78 ms 70264 KB Output is correct
7 Correct 77 ms 70256 KB Output is correct
8 Correct 73 ms 70136 KB Output is correct
9 Correct 71 ms 70136 KB Output is correct
10 Correct 72 ms 70136 KB Output is correct
11 Correct 77 ms 70136 KB Output is correct
12 Correct 78 ms 70236 KB Output is correct
13 Correct 78 ms 70216 KB Output is correct
14 Correct 80 ms 70360 KB Output is correct
15 Correct 78 ms 70136 KB Output is correct
16 Correct 78 ms 70164 KB Output is correct
17 Correct 78 ms 70264 KB Output is correct
18 Correct 78 ms 70136 KB Output is correct
19 Correct 79 ms 70264 KB Output is correct
20 Correct 78 ms 70196 KB Output is correct
21 Correct 77 ms 70136 KB Output is correct
22 Correct 77 ms 70264 KB Output is correct
23 Correct 77 ms 70264 KB Output is correct
24 Correct 77 ms 70136 KB Output is correct
25 Correct 78 ms 70264 KB Output is correct
26 Correct 72 ms 70136 KB Output is correct
27 Correct 78 ms 70364 KB Output is correct
28 Correct 77 ms 70236 KB Output is correct
29 Correct 79 ms 70328 KB Output is correct
30 Correct 79 ms 70136 KB Output is correct
31 Correct 77 ms 70136 KB Output is correct
32 Correct 77 ms 70180 KB Output is correct
33 Correct 77 ms 70260 KB Output is correct
34 Correct 76 ms 70136 KB Output is correct
35 Correct 76 ms 70136 KB Output is correct
36 Correct 77 ms 70116 KB Output is correct
37 Correct 77 ms 70136 KB Output is correct
38 Correct 77 ms 70260 KB Output is correct
39 Correct 95 ms 70260 KB Output is correct
40 Correct 78 ms 70136 KB Output is correct
41 Correct 77 ms 70260 KB Output is correct
42 Correct 76 ms 70136 KB Output is correct
43 Correct 78 ms 70260 KB Output is correct
44 Correct 76 ms 70232 KB Output is correct
45 Correct 80 ms 70396 KB Output is correct
46 Correct 84 ms 71356 KB Output is correct
47 Correct 85 ms 71416 KB Output is correct
48 Correct 85 ms 71288 KB Output is correct
49 Correct 75 ms 70520 KB Output is correct
50 Correct 76 ms 70648 KB Output is correct
51 Correct 76 ms 70520 KB Output is correct
52 Correct 82 ms 71464 KB Output is correct
53 Correct 82 ms 71544 KB Output is correct
54 Correct 81 ms 71416 KB Output is correct
55 Correct 82 ms 71544 KB Output is correct
56 Correct 82 ms 71580 KB Output is correct
57 Correct 83 ms 71288 KB Output is correct
58 Correct 86 ms 71288 KB Output is correct
59 Correct 86 ms 71356 KB Output is correct
60 Correct 86 ms 71508 KB Output is correct
61 Correct 83 ms 71476 KB Output is correct
62 Correct 82 ms 71416 KB Output is correct
63 Correct 83 ms 71416 KB Output is correct
64 Correct 84 ms 71464 KB Output is correct
65 Correct 86 ms 71352 KB Output is correct
66 Correct 86 ms 71288 KB Output is correct
67 Correct 83 ms 71288 KB Output is correct
68 Correct 75 ms 70648 KB Output is correct
69 Correct 82 ms 71464 KB Output is correct
70 Correct 81 ms 71420 KB Output is correct
71 Correct 86 ms 71448 KB Output is correct
72 Correct 89 ms 71416 KB Output is correct
73 Correct 88 ms 71252 KB Output is correct
74 Correct 82 ms 71420 KB Output is correct
75 Correct 81 ms 71312 KB Output is correct
76 Correct 83 ms 71288 KB Output is correct
77 Correct 83 ms 71416 KB Output is correct
78 Correct 81 ms 71416 KB Output is correct
79 Correct 82 ms 71288 KB Output is correct
80 Correct 83 ms 71288 KB Output is correct
81 Correct 83 ms 71288 KB Output is correct
82 Correct 83 ms 71416 KB Output is correct
83 Correct 86 ms 71388 KB Output is correct
84 Correct 80 ms 71288 KB Output is correct
85 Correct 83 ms 71344 KB Output is correct
86 Correct 82 ms 71288 KB Output is correct
87 Correct 96 ms 73464 KB Output is correct
88 Correct 138 ms 79864 KB Output is correct
89 Correct 318 ms 102728 KB Output is correct
90 Correct 321 ms 102648 KB Output is correct
91 Correct 305 ms 102648 KB Output is correct
92 Correct 171 ms 82936 KB Output is correct
93 Correct 172 ms 82880 KB Output is correct
94 Correct 170 ms 82936 KB Output is correct
95 Correct 228 ms 106608 KB Output is correct
96 Correct 243 ms 106872 KB Output is correct
97 Correct 242 ms 107016 KB Output is correct
98 Correct 240 ms 106892 KB Output is correct
99 Correct 242 ms 105004 KB Output is correct
100 Correct 311 ms 94712 KB Output is correct
101 Correct 391 ms 95032 KB Output is correct
102 Correct 373 ms 95068 KB Output is correct
103 Correct 372 ms 95252 KB Output is correct
104 Correct 241 ms 104964 KB Output is correct
105 Correct 248 ms 105080 KB Output is correct
106 Correct 245 ms 104856 KB Output is correct
107 Correct 293 ms 101880 KB Output is correct
108 Correct 320 ms 102008 KB Output is correct
109 Correct 331 ms 102264 KB Output is correct
110 Correct 158 ms 82368 KB Output is correct
111 Correct 233 ms 106580 KB Output is correct
112 Correct 227 ms 105972 KB Output is correct
113 Correct 215 ms 104408 KB Output is correct
114 Correct 310 ms 94756 KB Output is correct
115 Correct 362 ms 94412 KB Output is correct
116 Correct 236 ms 104468 KB Output is correct
117 Correct 214 ms 103380 KB Output is correct
118 Correct 222 ms 103032 KB Output is correct
119 Correct 230 ms 102776 KB Output is correct
120 Correct 220 ms 103032 KB Output is correct
121 Correct 220 ms 102776 KB Output is correct
122 Correct 236 ms 102580 KB Output is correct
123 Correct 236 ms 103644 KB Output is correct
124 Correct 234 ms 103160 KB Output is correct
125 Correct 237 ms 102904 KB Output is correct
126 Correct 230 ms 103160 KB Output is correct
127 Correct 235 ms 102776 KB Output is correct
128 Correct 233 ms 102648 KB Output is correct