답안 #164057

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
164057 2019-11-17T08:35:25 Z georgerapeanu Construction of Highway (JOI18_construction) C++11
16 / 100
202 ms 29020 KB
#include <cstdio>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

const int NMAX = 1e5;

int n;
int ferikire[NMAX + 5];

pair<int,int> edges[NMAX + 5];

vector<int> graph[NMAX + 5];
int fst[NMAX + 5];
int snd[NMAX + 5];
int euler_len;
int lant_father[NMAX + 5];
int lvl[NMAX + 5];
int when[NMAX + 5];

void predfs(int nod,int tata){
    lvl[nod] = 1 + lvl[tata];
    fst[nod] = ++euler_len;
    for(auto it:graph[nod]){
        if(it == tata){
            continue;
        }
        predfs(it,nod);
    }
    snd[nod] = euler_len;
}

pair<int,int> aint[NMAX + 5];

void update(int nod,int st,int dr,const int &pos,const pair<int,int> &val){
    if(dr < pos || st > pos){
        return;
    }

    if(st == dr){
        aint[nod] = val;
        return ;
    }

    int mid = (st + dr) / 2;

    update(nod * 2,st,mid,pos,val);
    update(nod * 2 + 1,mid + 1,dr,pos,val);

    if(when[aint[2 * nod].second] > when[aint[2 * nod + 1].second]){
        aint[nod] = aint[2 * nod];
    }
    else{
        aint[nod] = aint[2 * nod + 1];
    }
}

pair<int,int> query(int nod,int st,int dr,int l,int r){
    if(l > dr || r < st){
        return {0,-1};
    }

    if(l <= st && dr <= r){
        return aint[nod];
    }

    int mid = (st + dr) / 2;

    pair<int,int> a = query(nod * 2,st,mid,l,r);
    pair<int,int> b = query(nod * 2 + 1,mid + 1,dr,l,r);

    if(when[a.second] > when[b.second]){
        return a;
    }
    else{
        return b;
    }

}

int aib[NMAX + 5];
map<int,int> to_norm;

void aib_update(int pos,int val){
    for(;pos <= NMAX;pos += (-pos) & pos){
        aib[pos] += val;
    }
}

int aib_query(int pos){
    int ans = 0;

    for(;pos;pos -= (-pos) & pos){
        ans += aib[pos];
    }

    return ans;
}

int main(){

    scanf("%d",&n);

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

    int last = 0;

    for(auto &it:to_norm){
        it.second = ++last;
    }

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

    predfs(1,0);

    update(1,1,n,fst[1],{ferikire[1],1});
    lant_father[1] = 0;

    for(int i = 1;i < n;i++){
        int nod = edges[i].first;
        long long ans = 0;
        vector<pair<int,int> > to_clear;
        while(nod != 0){
            pair<int,int> _lant = query(1,1,n,fst[nod],snd[nod]);
            ans += 1LL * (lvl[nod] - lvl[lant_father[_lant.second]]) * aib_query(to_norm[_lant.first] - 1);
            aib_update(to_norm[_lant.first],lvl[nod] - lvl[lant_father[_lant.second]]);
            to_clear.push_back({to_norm[_lant.first],lvl[lant_father[_lant.second]] - lvl[nod]});
            int lst_nod = nod;
            nod = lant_father[_lant.second];
            lant_father[_lant.second] = lst_nod;
        }
        update(1,1,n,fst[edges[i].second],{ferikire[edges[i].second],edges[i].second});
        lant_father[edges[i].second] = 0;
        for(auto it:to_clear){
            aib_update(it.first,it.second);
        }
        printf("%lld\n",ans);
    }



    return 0;
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:104:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&n);
     ~~~~~^~~~~~~~~
construction.cpp:107:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&ferikire[i]);
         ~~~~~^~~~~~~~~~~~~~~~~~~
construction.cpp:120:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&x,&y);
         ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 5 ms 2868 KB Output is correct
5 Correct 6 ms 2808 KB Output is correct
6 Correct 5 ms 2808 KB Output is correct
7 Correct 6 ms 2808 KB Output is correct
8 Correct 5 ms 2808 KB Output is correct
9 Correct 5 ms 2808 KB Output is correct
10 Correct 5 ms 2808 KB Output is correct
11 Correct 5 ms 2808 KB Output is correct
12 Correct 5 ms 2812 KB Output is correct
13 Correct 5 ms 2808 KB Output is correct
14 Correct 5 ms 2808 KB Output is correct
15 Correct 6 ms 2808 KB Output is correct
16 Correct 24 ms 2808 KB Output is correct
17 Correct 6 ms 2808 KB Output is correct
18 Correct 11 ms 2808 KB Output is correct
19 Correct 5 ms 2808 KB Output is correct
20 Correct 5 ms 2808 KB Output is correct
21 Correct 5 ms 2808 KB Output is correct
22 Correct 5 ms 2808 KB Output is correct
23 Correct 5 ms 2808 KB Output is correct
24 Correct 5 ms 2808 KB Output is correct
25 Correct 5 ms 2808 KB Output is correct
26 Correct 5 ms 2808 KB Output is correct
27 Correct 5 ms 2780 KB Output is correct
28 Correct 5 ms 2808 KB Output is correct
29 Correct 5 ms 2808 KB Output is correct
30 Correct 6 ms 2808 KB Output is correct
31 Correct 6 ms 2808 KB Output is correct
32 Correct 5 ms 2808 KB Output is correct
33 Correct 5 ms 2808 KB Output is correct
34 Correct 5 ms 2808 KB Output is correct
35 Correct 5 ms 2808 KB Output is correct
36 Correct 6 ms 2808 KB Output is correct
37 Correct 5 ms 2808 KB Output is correct
38 Correct 5 ms 2808 KB Output is correct
39 Correct 5 ms 2808 KB Output is correct
40 Correct 5 ms 2808 KB Output is correct
41 Correct 5 ms 2808 KB Output is correct
42 Correct 5 ms 2808 KB Output is correct
43 Correct 5 ms 2808 KB Output is correct
44 Correct 5 ms 2808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 5 ms 2868 KB Output is correct
5 Correct 6 ms 2808 KB Output is correct
6 Correct 5 ms 2808 KB Output is correct
7 Correct 6 ms 2808 KB Output is correct
8 Correct 5 ms 2808 KB Output is correct
9 Correct 5 ms 2808 KB Output is correct
10 Correct 5 ms 2808 KB Output is correct
11 Correct 5 ms 2808 KB Output is correct
12 Correct 5 ms 2812 KB Output is correct
13 Correct 5 ms 2808 KB Output is correct
14 Correct 5 ms 2808 KB Output is correct
15 Correct 6 ms 2808 KB Output is correct
16 Correct 24 ms 2808 KB Output is correct
17 Correct 6 ms 2808 KB Output is correct
18 Correct 11 ms 2808 KB Output is correct
19 Correct 5 ms 2808 KB Output is correct
20 Correct 5 ms 2808 KB Output is correct
21 Correct 5 ms 2808 KB Output is correct
22 Correct 5 ms 2808 KB Output is correct
23 Correct 5 ms 2808 KB Output is correct
24 Correct 5 ms 2808 KB Output is correct
25 Correct 5 ms 2808 KB Output is correct
26 Correct 5 ms 2808 KB Output is correct
27 Correct 5 ms 2780 KB Output is correct
28 Correct 5 ms 2808 KB Output is correct
29 Correct 5 ms 2808 KB Output is correct
30 Correct 6 ms 2808 KB Output is correct
31 Correct 6 ms 2808 KB Output is correct
32 Correct 5 ms 2808 KB Output is correct
33 Correct 5 ms 2808 KB Output is correct
34 Correct 5 ms 2808 KB Output is correct
35 Correct 5 ms 2808 KB Output is correct
36 Correct 6 ms 2808 KB Output is correct
37 Correct 5 ms 2808 KB Output is correct
38 Correct 5 ms 2808 KB Output is correct
39 Correct 5 ms 2808 KB Output is correct
40 Correct 5 ms 2808 KB Output is correct
41 Correct 5 ms 2808 KB Output is correct
42 Correct 5 ms 2808 KB Output is correct
43 Correct 5 ms 2808 KB Output is correct
44 Correct 5 ms 2808 KB Output is correct
45 Correct 7 ms 2936 KB Output is correct
46 Correct 20 ms 3448 KB Output is correct
47 Correct 20 ms 3320 KB Output is correct
48 Correct 20 ms 3320 KB Output is correct
49 Correct 12 ms 3480 KB Output is correct
50 Correct 11 ms 3448 KB Output is correct
51 Correct 11 ms 3448 KB Output is correct
52 Correct 12 ms 3448 KB Output is correct
53 Correct 13 ms 3580 KB Output is correct
54 Correct 13 ms 3448 KB Output is correct
55 Correct 13 ms 3448 KB Output is correct
56 Correct 13 ms 3320 KB Output is correct
57 Correct 29 ms 3320 KB Output is correct
58 Correct 34 ms 3320 KB Output is correct
59 Correct 33 ms 3320 KB Output is correct
60 Correct 33 ms 3448 KB Output is correct
61 Correct 13 ms 3452 KB Output is correct
62 Correct 13 ms 3532 KB Output is correct
63 Correct 13 ms 3448 KB Output is correct
64 Correct 13 ms 3064 KB Output is correct
65 Correct 14 ms 3064 KB Output is correct
66 Correct 18 ms 3192 KB Output is correct
67 Correct 18 ms 3192 KB Output is correct
68 Correct 9 ms 3320 KB Output is correct
69 Correct 12 ms 3448 KB Output is correct
70 Correct 21 ms 3192 KB Output is correct
71 Correct 10 ms 3192 KB Output is correct
72 Correct 33 ms 3320 KB Output is correct
73 Correct 22 ms 3064 KB Output is correct
74 Correct 10 ms 3192 KB Output is correct
75 Correct 12 ms 3320 KB Output is correct
76 Correct 12 ms 3340 KB Output is correct
77 Correct 14 ms 3324 KB Output is correct
78 Correct 11 ms 3192 KB Output is correct
79 Correct 10 ms 3192 KB Output is correct
80 Correct 10 ms 3068 KB Output is correct
81 Correct 14 ms 3320 KB Output is correct
82 Correct 14 ms 3320 KB Output is correct
83 Correct 14 ms 3320 KB Output is correct
84 Correct 11 ms 3192 KB Output is correct
85 Correct 12 ms 3064 KB Output is correct
86 Correct 11 ms 3064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 5 ms 2868 KB Output is correct
5 Correct 6 ms 2808 KB Output is correct
6 Correct 5 ms 2808 KB Output is correct
7 Correct 6 ms 2808 KB Output is correct
8 Correct 5 ms 2808 KB Output is correct
9 Correct 5 ms 2808 KB Output is correct
10 Correct 5 ms 2808 KB Output is correct
11 Correct 5 ms 2808 KB Output is correct
12 Correct 5 ms 2812 KB Output is correct
13 Correct 5 ms 2808 KB Output is correct
14 Correct 5 ms 2808 KB Output is correct
15 Correct 6 ms 2808 KB Output is correct
16 Correct 24 ms 2808 KB Output is correct
17 Correct 6 ms 2808 KB Output is correct
18 Correct 11 ms 2808 KB Output is correct
19 Correct 5 ms 2808 KB Output is correct
20 Correct 5 ms 2808 KB Output is correct
21 Correct 5 ms 2808 KB Output is correct
22 Correct 5 ms 2808 KB Output is correct
23 Correct 5 ms 2808 KB Output is correct
24 Correct 5 ms 2808 KB Output is correct
25 Correct 5 ms 2808 KB Output is correct
26 Correct 5 ms 2808 KB Output is correct
27 Correct 5 ms 2780 KB Output is correct
28 Correct 5 ms 2808 KB Output is correct
29 Correct 5 ms 2808 KB Output is correct
30 Correct 6 ms 2808 KB Output is correct
31 Correct 6 ms 2808 KB Output is correct
32 Correct 5 ms 2808 KB Output is correct
33 Correct 5 ms 2808 KB Output is correct
34 Correct 5 ms 2808 KB Output is correct
35 Correct 5 ms 2808 KB Output is correct
36 Correct 6 ms 2808 KB Output is correct
37 Correct 5 ms 2808 KB Output is correct
38 Correct 5 ms 2808 KB Output is correct
39 Correct 5 ms 2808 KB Output is correct
40 Correct 5 ms 2808 KB Output is correct
41 Correct 5 ms 2808 KB Output is correct
42 Correct 5 ms 2808 KB Output is correct
43 Correct 5 ms 2808 KB Output is correct
44 Correct 5 ms 2808 KB Output is correct
45 Correct 7 ms 2936 KB Output is correct
46 Correct 20 ms 3448 KB Output is correct
47 Correct 20 ms 3320 KB Output is correct
48 Correct 20 ms 3320 KB Output is correct
49 Correct 12 ms 3480 KB Output is correct
50 Correct 11 ms 3448 KB Output is correct
51 Correct 11 ms 3448 KB Output is correct
52 Correct 12 ms 3448 KB Output is correct
53 Correct 13 ms 3580 KB Output is correct
54 Correct 13 ms 3448 KB Output is correct
55 Correct 13 ms 3448 KB Output is correct
56 Correct 13 ms 3320 KB Output is correct
57 Correct 29 ms 3320 KB Output is correct
58 Correct 34 ms 3320 KB Output is correct
59 Correct 33 ms 3320 KB Output is correct
60 Correct 33 ms 3448 KB Output is correct
61 Correct 13 ms 3452 KB Output is correct
62 Correct 13 ms 3532 KB Output is correct
63 Correct 13 ms 3448 KB Output is correct
64 Correct 13 ms 3064 KB Output is correct
65 Correct 14 ms 3064 KB Output is correct
66 Correct 18 ms 3192 KB Output is correct
67 Correct 18 ms 3192 KB Output is correct
68 Correct 9 ms 3320 KB Output is correct
69 Correct 12 ms 3448 KB Output is correct
70 Correct 21 ms 3192 KB Output is correct
71 Correct 10 ms 3192 KB Output is correct
72 Correct 33 ms 3320 KB Output is correct
73 Correct 22 ms 3064 KB Output is correct
74 Correct 10 ms 3192 KB Output is correct
75 Correct 12 ms 3320 KB Output is correct
76 Correct 12 ms 3340 KB Output is correct
77 Correct 14 ms 3324 KB Output is correct
78 Correct 11 ms 3192 KB Output is correct
79 Correct 10 ms 3192 KB Output is correct
80 Correct 10 ms 3068 KB Output is correct
81 Correct 14 ms 3320 KB Output is correct
82 Correct 14 ms 3320 KB Output is correct
83 Correct 14 ms 3320 KB Output is correct
84 Correct 11 ms 3192 KB Output is correct
85 Correct 12 ms 3064 KB Output is correct
86 Correct 11 ms 3064 KB Output is correct
87 Correct 74 ms 4444 KB Output is correct
88 Correct 202 ms 7372 KB Output is correct
89 Runtime error 168 ms 29020 KB Execution killed with signal 11 (could be triggered by violating memory limits)
90 Halted 0 ms 0 KB -