답안 #45416

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
45416 2018-04-13T08:54:10 Z Extazy Construction of Highway (JOI18_construction) C++17
16 / 100
1000 ms 90300 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,from=(int)(f.size()),to=from-1;
    //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));
        f.push_back(make_pair(d[i].first,curr));
        ++to;
    }

    //reverse(v.begin(),v.end());
    //for(i=0;i<(int)(v.size());i++) f.push_back(v[i]);
    while(from<to) {
        swap(f[from++],f[to--]);
    }
}

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:143: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 74 ms 70008 KB Output is correct
2 Correct 59 ms 70116 KB Output is correct
3 Correct 64 ms 70168 KB Output is correct
4 Correct 64 ms 70168 KB Output is correct
5 Correct 65 ms 70240 KB Output is correct
6 Correct 59 ms 70240 KB Output is correct
7 Correct 61 ms 70244 KB Output is correct
8 Correct 61 ms 70244 KB Output is correct
9 Correct 62 ms 70296 KB Output is correct
10 Correct 62 ms 70424 KB Output is correct
11 Correct 69 ms 70428 KB Output is correct
12 Correct 67 ms 70428 KB Output is correct
13 Correct 78 ms 70428 KB Output is correct
14 Correct 60 ms 70428 KB Output is correct
15 Correct 66 ms 70428 KB Output is correct
16 Correct 67 ms 70428 KB Output is correct
17 Correct 72 ms 70428 KB Output is correct
18 Correct 66 ms 70428 KB Output is correct
19 Correct 62 ms 70428 KB Output is correct
20 Correct 81 ms 70428 KB Output is correct
21 Correct 62 ms 70428 KB Output is correct
22 Correct 80 ms 70428 KB Output is correct
23 Correct 63 ms 70428 KB Output is correct
24 Correct 59 ms 70428 KB Output is correct
25 Correct 60 ms 70428 KB Output is correct
26 Correct 60 ms 70428 KB Output is correct
27 Correct 66 ms 70428 KB Output is correct
28 Correct 69 ms 70428 KB Output is correct
29 Correct 70 ms 70428 KB Output is correct
30 Correct 66 ms 70428 KB Output is correct
31 Correct 79 ms 70428 KB Output is correct
32 Correct 60 ms 70428 KB Output is correct
33 Correct 60 ms 70428 KB Output is correct
34 Correct 78 ms 70428 KB Output is correct
35 Correct 60 ms 70524 KB Output is correct
36 Correct 74 ms 70524 KB Output is correct
37 Correct 62 ms 70524 KB Output is correct
38 Correct 63 ms 70524 KB Output is correct
39 Correct 61 ms 70524 KB Output is correct
40 Correct 62 ms 70524 KB Output is correct
41 Correct 62 ms 70528 KB Output is correct
42 Correct 81 ms 70528 KB Output is correct
43 Correct 61 ms 70528 KB Output is correct
44 Correct 61 ms 70528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 70008 KB Output is correct
2 Correct 59 ms 70116 KB Output is correct
3 Correct 64 ms 70168 KB Output is correct
4 Correct 64 ms 70168 KB Output is correct
5 Correct 65 ms 70240 KB Output is correct
6 Correct 59 ms 70240 KB Output is correct
7 Correct 61 ms 70244 KB Output is correct
8 Correct 61 ms 70244 KB Output is correct
9 Correct 62 ms 70296 KB Output is correct
10 Correct 62 ms 70424 KB Output is correct
11 Correct 69 ms 70428 KB Output is correct
12 Correct 67 ms 70428 KB Output is correct
13 Correct 78 ms 70428 KB Output is correct
14 Correct 60 ms 70428 KB Output is correct
15 Correct 66 ms 70428 KB Output is correct
16 Correct 67 ms 70428 KB Output is correct
17 Correct 72 ms 70428 KB Output is correct
18 Correct 66 ms 70428 KB Output is correct
19 Correct 62 ms 70428 KB Output is correct
20 Correct 81 ms 70428 KB Output is correct
21 Correct 62 ms 70428 KB Output is correct
22 Correct 80 ms 70428 KB Output is correct
23 Correct 63 ms 70428 KB Output is correct
24 Correct 59 ms 70428 KB Output is correct
25 Correct 60 ms 70428 KB Output is correct
26 Correct 60 ms 70428 KB Output is correct
27 Correct 66 ms 70428 KB Output is correct
28 Correct 69 ms 70428 KB Output is correct
29 Correct 70 ms 70428 KB Output is correct
30 Correct 66 ms 70428 KB Output is correct
31 Correct 79 ms 70428 KB Output is correct
32 Correct 60 ms 70428 KB Output is correct
33 Correct 60 ms 70428 KB Output is correct
34 Correct 78 ms 70428 KB Output is correct
35 Correct 60 ms 70524 KB Output is correct
36 Correct 74 ms 70524 KB Output is correct
37 Correct 62 ms 70524 KB Output is correct
38 Correct 63 ms 70524 KB Output is correct
39 Correct 61 ms 70524 KB Output is correct
40 Correct 62 ms 70524 KB Output is correct
41 Correct 62 ms 70528 KB Output is correct
42 Correct 81 ms 70528 KB Output is correct
43 Correct 61 ms 70528 KB Output is correct
44 Correct 61 ms 70528 KB Output is correct
45 Correct 68 ms 70528 KB Output is correct
46 Correct 69 ms 70780 KB Output is correct
47 Correct 69 ms 70780 KB Output is correct
48 Correct 68 ms 70848 KB Output is correct
49 Correct 63 ms 71068 KB Output is correct
50 Correct 65 ms 71068 KB Output is correct
51 Correct 65 ms 71300 KB Output is correct
52 Correct 76 ms 71300 KB Output is correct
53 Correct 76 ms 71300 KB Output is correct
54 Correct 76 ms 71300 KB Output is correct
55 Correct 74 ms 71300 KB Output is correct
56 Correct 65 ms 71300 KB Output is correct
57 Correct 68 ms 71300 KB Output is correct
58 Correct 73 ms 71300 KB Output is correct
59 Correct 71 ms 71300 KB Output is correct
60 Correct 70 ms 71300 KB Output is correct
61 Correct 67 ms 71300 KB Output is correct
62 Correct 65 ms 71300 KB Output is correct
63 Correct 64 ms 71300 KB Output is correct
64 Correct 65 ms 71300 KB Output is correct
65 Correct 66 ms 71300 KB Output is correct
66 Correct 70 ms 71300 KB Output is correct
67 Correct 69 ms 71300 KB Output is correct
68 Correct 65 ms 71300 KB Output is correct
69 Correct 78 ms 71300 KB Output is correct
70 Correct 72 ms 71300 KB Output is correct
71 Correct 64 ms 71300 KB Output is correct
72 Correct 70 ms 71300 KB Output is correct
73 Correct 69 ms 71300 KB Output is correct
74 Correct 64 ms 71300 KB Output is correct
75 Correct 76 ms 71300 KB Output is correct
76 Correct 65 ms 71300 KB Output is correct
77 Correct 67 ms 71300 KB Output is correct
78 Correct 68 ms 71300 KB Output is correct
79 Correct 65 ms 71300 KB Output is correct
80 Correct 64 ms 71300 KB Output is correct
81 Correct 65 ms 71300 KB Output is correct
82 Correct 69 ms 71300 KB Output is correct
83 Correct 73 ms 71300 KB Output is correct
84 Correct 65 ms 71300 KB Output is correct
85 Correct 67 ms 71300 KB Output is correct
86 Correct 64 ms 71300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 70008 KB Output is correct
2 Correct 59 ms 70116 KB Output is correct
3 Correct 64 ms 70168 KB Output is correct
4 Correct 64 ms 70168 KB Output is correct
5 Correct 65 ms 70240 KB Output is correct
6 Correct 59 ms 70240 KB Output is correct
7 Correct 61 ms 70244 KB Output is correct
8 Correct 61 ms 70244 KB Output is correct
9 Correct 62 ms 70296 KB Output is correct
10 Correct 62 ms 70424 KB Output is correct
11 Correct 69 ms 70428 KB Output is correct
12 Correct 67 ms 70428 KB Output is correct
13 Correct 78 ms 70428 KB Output is correct
14 Correct 60 ms 70428 KB Output is correct
15 Correct 66 ms 70428 KB Output is correct
16 Correct 67 ms 70428 KB Output is correct
17 Correct 72 ms 70428 KB Output is correct
18 Correct 66 ms 70428 KB Output is correct
19 Correct 62 ms 70428 KB Output is correct
20 Correct 81 ms 70428 KB Output is correct
21 Correct 62 ms 70428 KB Output is correct
22 Correct 80 ms 70428 KB Output is correct
23 Correct 63 ms 70428 KB Output is correct
24 Correct 59 ms 70428 KB Output is correct
25 Correct 60 ms 70428 KB Output is correct
26 Correct 60 ms 70428 KB Output is correct
27 Correct 66 ms 70428 KB Output is correct
28 Correct 69 ms 70428 KB Output is correct
29 Correct 70 ms 70428 KB Output is correct
30 Correct 66 ms 70428 KB Output is correct
31 Correct 79 ms 70428 KB Output is correct
32 Correct 60 ms 70428 KB Output is correct
33 Correct 60 ms 70428 KB Output is correct
34 Correct 78 ms 70428 KB Output is correct
35 Correct 60 ms 70524 KB Output is correct
36 Correct 74 ms 70524 KB Output is correct
37 Correct 62 ms 70524 KB Output is correct
38 Correct 63 ms 70524 KB Output is correct
39 Correct 61 ms 70524 KB Output is correct
40 Correct 62 ms 70524 KB Output is correct
41 Correct 62 ms 70528 KB Output is correct
42 Correct 81 ms 70528 KB Output is correct
43 Correct 61 ms 70528 KB Output is correct
44 Correct 61 ms 70528 KB Output is correct
45 Correct 68 ms 70528 KB Output is correct
46 Correct 69 ms 70780 KB Output is correct
47 Correct 69 ms 70780 KB Output is correct
48 Correct 68 ms 70848 KB Output is correct
49 Correct 63 ms 71068 KB Output is correct
50 Correct 65 ms 71068 KB Output is correct
51 Correct 65 ms 71300 KB Output is correct
52 Correct 76 ms 71300 KB Output is correct
53 Correct 76 ms 71300 KB Output is correct
54 Correct 76 ms 71300 KB Output is correct
55 Correct 74 ms 71300 KB Output is correct
56 Correct 65 ms 71300 KB Output is correct
57 Correct 68 ms 71300 KB Output is correct
58 Correct 73 ms 71300 KB Output is correct
59 Correct 71 ms 71300 KB Output is correct
60 Correct 70 ms 71300 KB Output is correct
61 Correct 67 ms 71300 KB Output is correct
62 Correct 65 ms 71300 KB Output is correct
63 Correct 64 ms 71300 KB Output is correct
64 Correct 65 ms 71300 KB Output is correct
65 Correct 66 ms 71300 KB Output is correct
66 Correct 70 ms 71300 KB Output is correct
67 Correct 69 ms 71300 KB Output is correct
68 Correct 65 ms 71300 KB Output is correct
69 Correct 78 ms 71300 KB Output is correct
70 Correct 72 ms 71300 KB Output is correct
71 Correct 64 ms 71300 KB Output is correct
72 Correct 70 ms 71300 KB Output is correct
73 Correct 69 ms 71300 KB Output is correct
74 Correct 64 ms 71300 KB Output is correct
75 Correct 76 ms 71300 KB Output is correct
76 Correct 65 ms 71300 KB Output is correct
77 Correct 67 ms 71300 KB Output is correct
78 Correct 68 ms 71300 KB Output is correct
79 Correct 65 ms 71300 KB Output is correct
80 Correct 64 ms 71300 KB Output is correct
81 Correct 65 ms 71300 KB Output is correct
82 Correct 69 ms 71300 KB Output is correct
83 Correct 73 ms 71300 KB Output is correct
84 Correct 65 ms 71300 KB Output is correct
85 Correct 67 ms 71300 KB Output is correct
86 Correct 64 ms 71300 KB Output is correct
87 Correct 85 ms 71672 KB Output is correct
88 Correct 147 ms 74080 KB Output is correct
89 Correct 450 ms 82796 KB Output is correct
90 Correct 475 ms 82844 KB Output is correct
91 Correct 433 ms 82876 KB Output is correct
92 Correct 255 ms 90300 KB Output is correct
93 Correct 247 ms 90300 KB Output is correct
94 Correct 243 ms 90300 KB Output is correct
95 Execution timed out 1088 ms 90300 KB Time limit exceeded
96 Halted 0 ms 0 KB -