답안 #45414

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
45414 2018-04-13T08:51:12 Z Extazy Construction of Highway (JOI18_construction) C++17
16 / 100
1000 ms 90232 KB
#include <bits/stdc++.h>
#define endl '\n'

using namespace std;

const int N = 100007;

int n,a[N];
vector < int > v[N];
pair < int, int > e[N];
int size_of_subtree[N];
deque < pair < int, int > > d[N];
int chains,chain_of_node[N],head_of_chain[N],index_in_chain[N],parent[N];
int it[N];
int p[N];
map < int, int > code;
int all;
char buff[1<<24],*buffp=buff;

void read(int &res) {
    res=0;
    while(*buffp<48) ++buffp;
    while(*buffp>47) res=(res<<3)+(res<<1)+*buffp++-48;
}

void update(int pos, int value) {
    for(;pos<=all;pos+=pos&(-pos)) it[pos]+=value;
}

int query(int pos) {
    int ans=0;
    for(;pos>=1;pos-=pos&(-pos)) ans+=it[pos];
    return ans;
}

void dfs(int node) {
    size_of_subtree[node]=1;
    if(v[node].empty()) return;

    int i,max_size=-1,which=-1;

    for(i=0;i<(int)(v[node].size());i++) {
        dfs(v[node][i]);
        size_of_subtree[node]+=size_of_subtree[v[node][i]];
        
        if(size_of_subtree[v[node][i]]>max_size) {
            max_size=size_of_subtree[v[node][i]];
            which=i;
        }
    }

    swap(v[node][0],v[node][which]);
}

void build_hld(int node, int chain, int idx) {
    chain_of_node[node]=chain;
    index_in_chain[node]=idx;

    if(v[node].empty()) return;

    int i;

    build_hld(v[node][0],chain,idx+1);
    for(i=1;i<(int)(v[node].size());i++) {
        head_of_chain[++chains]=v[node][i];
        build_hld(v[node][i],chains,1);
    }
}

void append(int chain, int value) {
    if(!d[chain].empty() && d[chain].back().first==value) {
        ++d[chain][d[chain].size()-1].second;
    }
    else {
        d[chain].push_back(make_pair(value,1));
    }
}

void extract(deque < pair < int, int > > d, int cnt, vector < pair < int, int > > &f) {
    int i;
    vector < pair < int, int > > v;

    for(i=0;i<(int)(d.size()) && cnt>0;i++) {
        int curr=min(cnt,d[i].second);
        cnt-=curr;
        v.push_back(make_pair(d[i].first,curr));
    }

    reverse(v.begin(),v.end());
    for(i=0;i<(int)(v.size());i++) f.push_back(v[i]);
}

long long count_inversions(int node) {
    vector < pair < int, int > > v;
    long long ans=0;
    int i;

    while(node) {
        extract(d[chain_of_node[node]],index_in_chain[node],v);
        node=parent[head_of_chain[chain_of_node[node]]];
    }

    for(i=0;i<(int)(v.size());i++) {
        ans+=query(v[i].first-1)*1ll*v[i].second;
        update(v[i].first,v[i].second);
    }

    for(i=0;i<(int)(v.size());i++) {
        update(v[i].first,-v[i].second);
    }

    return ans;
}

void eliminate(deque < pair < int, int > > &d, int cnt) {
    while(!d.empty() && cnt>=d.front().second) {
        cnt-=d.front().second;
        d.pop_front();
    }
    if(cnt>0) d[0].second-=cnt;
}

void assign(int node, int value) {
    while(node) {
        eliminate(d[chain_of_node[node]],index_in_chain[node]);
        if(!d[chain_of_node[node]].empty() && d[chain_of_node[node]].front().first==value) {
            d[chain_of_node[node]][0].second+=index_in_chain[node];
        }
        else {
            d[chain_of_node[node]].push_front(make_pair(value,index_in_chain[node]));
        }

        node=parent[head_of_chain[chain_of_node[node]]];
    }
}

int main() {
    fread(buff,1,sizeof(buff),stdin);
    int i;

    read(n);
    for(i=1;i<=n;i++) {
        read(a[i]);
        p[i]=a[i];
    }

    sort(p+1,p+1+n);
    code[p[1]]=1;
    for(i=2;i<=n;i++) if(p[i]!=p[i-1]) {
        code[p[i]]=code[p[i-1]]+1;
    }
    all=code[p[n]];
    
    for(i=1;i<=n;i++) {
        a[i]=code[a[i]];
    }

    for(i=1;i<n;i++) {
        read(e[i].first);
        read(e[i].second);
        parent[e[i].second]=e[i].first;
        v[e[i].first].push_back(e[i].second);
    }
    
    dfs(1);

    chains=1;
    head_of_chain[1]=1;
    build_hld(1,1,1);

    append(1,a[1]);

    for(i=1;i<n;i++) {
        printf("%lld\n", count_inversions(e[i].first));
        append(chain_of_node[e[i].second],a[e[i].second]);
        assign(e[i].second,a[e[i].second]);
    }

    return 0;
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:138:10: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     fread(buff,1,sizeof(buff),stdin);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 70008 KB Output is correct
2 Correct 62 ms 70268 KB Output is correct
3 Correct 60 ms 70268 KB Output is correct
4 Correct 60 ms 70268 KB Output is correct
5 Correct 60 ms 70268 KB Output is correct
6 Correct 62 ms 70268 KB Output is correct
7 Correct 61 ms 70304 KB Output is correct
8 Correct 60 ms 70416 KB Output is correct
9 Correct 60 ms 70416 KB Output is correct
10 Correct 59 ms 70416 KB Output is correct
11 Correct 67 ms 70416 KB Output is correct
12 Correct 68 ms 70416 KB Output is correct
13 Correct 66 ms 70508 KB Output is correct
14 Correct 60 ms 70508 KB Output is correct
15 Correct 62 ms 70508 KB Output is correct
16 Correct 65 ms 70508 KB Output is correct
17 Correct 62 ms 70508 KB Output is correct
18 Correct 84 ms 70508 KB Output is correct
19 Correct 60 ms 70508 KB Output is correct
20 Correct 62 ms 70508 KB Output is correct
21 Correct 61 ms 70508 KB Output is correct
22 Correct 67 ms 70508 KB Output is correct
23 Correct 61 ms 70508 KB Output is correct
24 Correct 62 ms 70508 KB Output is correct
25 Correct 63 ms 70508 KB Output is correct
26 Correct 59 ms 70508 KB Output is correct
27 Correct 70 ms 70508 KB Output is correct
28 Correct 70 ms 70508 KB Output is correct
29 Correct 60 ms 70508 KB Output is correct
30 Correct 64 ms 70508 KB Output is correct
31 Correct 70 ms 70508 KB Output is correct
32 Correct 90 ms 70508 KB Output is correct
33 Correct 61 ms 70508 KB Output is correct
34 Correct 63 ms 70508 KB Output is correct
35 Correct 62 ms 70508 KB Output is correct
36 Correct 62 ms 70508 KB Output is correct
37 Correct 62 ms 70508 KB Output is correct
38 Correct 62 ms 70508 KB Output is correct
39 Correct 61 ms 70508 KB Output is correct
40 Correct 69 ms 70508 KB Output is correct
41 Correct 60 ms 70508 KB Output is correct
42 Correct 62 ms 70508 KB Output is correct
43 Correct 62 ms 70508 KB Output is correct
44 Correct 60 ms 70508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 70008 KB Output is correct
2 Correct 62 ms 70268 KB Output is correct
3 Correct 60 ms 70268 KB Output is correct
4 Correct 60 ms 70268 KB Output is correct
5 Correct 60 ms 70268 KB Output is correct
6 Correct 62 ms 70268 KB Output is correct
7 Correct 61 ms 70304 KB Output is correct
8 Correct 60 ms 70416 KB Output is correct
9 Correct 60 ms 70416 KB Output is correct
10 Correct 59 ms 70416 KB Output is correct
11 Correct 67 ms 70416 KB Output is correct
12 Correct 68 ms 70416 KB Output is correct
13 Correct 66 ms 70508 KB Output is correct
14 Correct 60 ms 70508 KB Output is correct
15 Correct 62 ms 70508 KB Output is correct
16 Correct 65 ms 70508 KB Output is correct
17 Correct 62 ms 70508 KB Output is correct
18 Correct 84 ms 70508 KB Output is correct
19 Correct 60 ms 70508 KB Output is correct
20 Correct 62 ms 70508 KB Output is correct
21 Correct 61 ms 70508 KB Output is correct
22 Correct 67 ms 70508 KB Output is correct
23 Correct 61 ms 70508 KB Output is correct
24 Correct 62 ms 70508 KB Output is correct
25 Correct 63 ms 70508 KB Output is correct
26 Correct 59 ms 70508 KB Output is correct
27 Correct 70 ms 70508 KB Output is correct
28 Correct 70 ms 70508 KB Output is correct
29 Correct 60 ms 70508 KB Output is correct
30 Correct 64 ms 70508 KB Output is correct
31 Correct 70 ms 70508 KB Output is correct
32 Correct 90 ms 70508 KB Output is correct
33 Correct 61 ms 70508 KB Output is correct
34 Correct 63 ms 70508 KB Output is correct
35 Correct 62 ms 70508 KB Output is correct
36 Correct 62 ms 70508 KB Output is correct
37 Correct 62 ms 70508 KB Output is correct
38 Correct 62 ms 70508 KB Output is correct
39 Correct 61 ms 70508 KB Output is correct
40 Correct 69 ms 70508 KB Output is correct
41 Correct 60 ms 70508 KB Output is correct
42 Correct 62 ms 70508 KB Output is correct
43 Correct 62 ms 70508 KB Output is correct
44 Correct 60 ms 70508 KB Output is correct
45 Correct 84 ms 70508 KB Output is correct
46 Correct 69 ms 70832 KB Output is correct
47 Correct 70 ms 70832 KB Output is correct
48 Correct 81 ms 70832 KB Output is correct
49 Correct 67 ms 71196 KB Output is correct
50 Correct 65 ms 71196 KB Output is correct
51 Correct 64 ms 71196 KB Output is correct
52 Correct 81 ms 71196 KB Output is correct
53 Correct 76 ms 71196 KB Output is correct
54 Correct 84 ms 71196 KB Output is correct
55 Correct 94 ms 71196 KB Output is correct
56 Correct 88 ms 71196 KB Output is correct
57 Correct 73 ms 71196 KB Output is correct
58 Correct 73 ms 71196 KB Output is correct
59 Correct 70 ms 71196 KB Output is correct
60 Correct 73 ms 71196 KB Output is correct
61 Correct 67 ms 71196 KB Output is correct
62 Correct 67 ms 71196 KB Output is correct
63 Correct 71 ms 71196 KB Output is correct
64 Correct 65 ms 71196 KB Output is correct
65 Correct 65 ms 71196 KB Output is correct
66 Correct 68 ms 71196 KB Output is correct
67 Correct 71 ms 71196 KB Output is correct
68 Correct 64 ms 71196 KB Output is correct
69 Correct 75 ms 71196 KB Output is correct
70 Correct 105 ms 71196 KB Output is correct
71 Correct 60 ms 71196 KB Output is correct
72 Correct 77 ms 71196 KB Output is correct
73 Correct 71 ms 71196 KB Output is correct
74 Correct 64 ms 71196 KB Output is correct
75 Correct 63 ms 71196 KB Output is correct
76 Correct 64 ms 71196 KB Output is correct
77 Correct 65 ms 71196 KB Output is correct
78 Correct 84 ms 71196 KB Output is correct
79 Correct 63 ms 71196 KB Output is correct
80 Correct 67 ms 71196 KB Output is correct
81 Correct 66 ms 71196 KB Output is correct
82 Correct 65 ms 71196 KB Output is correct
83 Correct 67 ms 71196 KB Output is correct
84 Correct 63 ms 71196 KB Output is correct
85 Correct 67 ms 71196 KB Output is correct
86 Correct 67 ms 71196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 70008 KB Output is correct
2 Correct 62 ms 70268 KB Output is correct
3 Correct 60 ms 70268 KB Output is correct
4 Correct 60 ms 70268 KB Output is correct
5 Correct 60 ms 70268 KB Output is correct
6 Correct 62 ms 70268 KB Output is correct
7 Correct 61 ms 70304 KB Output is correct
8 Correct 60 ms 70416 KB Output is correct
9 Correct 60 ms 70416 KB Output is correct
10 Correct 59 ms 70416 KB Output is correct
11 Correct 67 ms 70416 KB Output is correct
12 Correct 68 ms 70416 KB Output is correct
13 Correct 66 ms 70508 KB Output is correct
14 Correct 60 ms 70508 KB Output is correct
15 Correct 62 ms 70508 KB Output is correct
16 Correct 65 ms 70508 KB Output is correct
17 Correct 62 ms 70508 KB Output is correct
18 Correct 84 ms 70508 KB Output is correct
19 Correct 60 ms 70508 KB Output is correct
20 Correct 62 ms 70508 KB Output is correct
21 Correct 61 ms 70508 KB Output is correct
22 Correct 67 ms 70508 KB Output is correct
23 Correct 61 ms 70508 KB Output is correct
24 Correct 62 ms 70508 KB Output is correct
25 Correct 63 ms 70508 KB Output is correct
26 Correct 59 ms 70508 KB Output is correct
27 Correct 70 ms 70508 KB Output is correct
28 Correct 70 ms 70508 KB Output is correct
29 Correct 60 ms 70508 KB Output is correct
30 Correct 64 ms 70508 KB Output is correct
31 Correct 70 ms 70508 KB Output is correct
32 Correct 90 ms 70508 KB Output is correct
33 Correct 61 ms 70508 KB Output is correct
34 Correct 63 ms 70508 KB Output is correct
35 Correct 62 ms 70508 KB Output is correct
36 Correct 62 ms 70508 KB Output is correct
37 Correct 62 ms 70508 KB Output is correct
38 Correct 62 ms 70508 KB Output is correct
39 Correct 61 ms 70508 KB Output is correct
40 Correct 69 ms 70508 KB Output is correct
41 Correct 60 ms 70508 KB Output is correct
42 Correct 62 ms 70508 KB Output is correct
43 Correct 62 ms 70508 KB Output is correct
44 Correct 60 ms 70508 KB Output is correct
45 Correct 84 ms 70508 KB Output is correct
46 Correct 69 ms 70832 KB Output is correct
47 Correct 70 ms 70832 KB Output is correct
48 Correct 81 ms 70832 KB Output is correct
49 Correct 67 ms 71196 KB Output is correct
50 Correct 65 ms 71196 KB Output is correct
51 Correct 64 ms 71196 KB Output is correct
52 Correct 81 ms 71196 KB Output is correct
53 Correct 76 ms 71196 KB Output is correct
54 Correct 84 ms 71196 KB Output is correct
55 Correct 94 ms 71196 KB Output is correct
56 Correct 88 ms 71196 KB Output is correct
57 Correct 73 ms 71196 KB Output is correct
58 Correct 73 ms 71196 KB Output is correct
59 Correct 70 ms 71196 KB Output is correct
60 Correct 73 ms 71196 KB Output is correct
61 Correct 67 ms 71196 KB Output is correct
62 Correct 67 ms 71196 KB Output is correct
63 Correct 71 ms 71196 KB Output is correct
64 Correct 65 ms 71196 KB Output is correct
65 Correct 65 ms 71196 KB Output is correct
66 Correct 68 ms 71196 KB Output is correct
67 Correct 71 ms 71196 KB Output is correct
68 Correct 64 ms 71196 KB Output is correct
69 Correct 75 ms 71196 KB Output is correct
70 Correct 105 ms 71196 KB Output is correct
71 Correct 60 ms 71196 KB Output is correct
72 Correct 77 ms 71196 KB Output is correct
73 Correct 71 ms 71196 KB Output is correct
74 Correct 64 ms 71196 KB Output is correct
75 Correct 63 ms 71196 KB Output is correct
76 Correct 64 ms 71196 KB Output is correct
77 Correct 65 ms 71196 KB Output is correct
78 Correct 84 ms 71196 KB Output is correct
79 Correct 63 ms 71196 KB Output is correct
80 Correct 67 ms 71196 KB Output is correct
81 Correct 66 ms 71196 KB Output is correct
82 Correct 65 ms 71196 KB Output is correct
83 Correct 67 ms 71196 KB Output is correct
84 Correct 63 ms 71196 KB Output is correct
85 Correct 67 ms 71196 KB Output is correct
86 Correct 67 ms 71196 KB Output is correct
87 Correct 85 ms 71616 KB Output is correct
88 Correct 150 ms 74172 KB Output is correct
89 Correct 470 ms 82744 KB Output is correct
90 Correct 462 ms 82876 KB Output is correct
91 Correct 465 ms 82876 KB Output is correct
92 Correct 251 ms 90172 KB Output is correct
93 Correct 245 ms 90232 KB Output is correct
94 Correct 262 ms 90232 KB Output is correct
95 Execution timed out 1067 ms 90232 KB Time limit exceeded
96 Halted 0 ms 0 KB -