답안 #45419

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
45419 2018-04-13T09:02:52 Z Extazy Construction of Highway (JOI18_construction) C++17
16 / 100
1000 ms 95036 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;

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

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

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

inline 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]);
}

inline 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);
    }
}

inline 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));
    }
}

inline void extract(deque < pair < int, int > > d, int cnt, vector < pair < int, int > > &f) {
    int i,from=(int)(f.size()),to=from-1;

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

    while(from<to) {
        swap(f[from++],f[to--]);
    }
}

inline 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;
}

inline 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;
}

inline 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:139: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 89 ms 70008 KB Output is correct
2 Correct 73 ms 70120 KB Output is correct
3 Correct 71 ms 70176 KB Output is correct
4 Correct 72 ms 70264 KB Output is correct
5 Correct 71 ms 70264 KB Output is correct
6 Correct 74 ms 70264 KB Output is correct
7 Correct 71 ms 70296 KB Output is correct
8 Correct 81 ms 70296 KB Output is correct
9 Correct 72 ms 70296 KB Output is correct
10 Correct 69 ms 70408 KB Output is correct
11 Correct 77 ms 70484 KB Output is correct
12 Correct 85 ms 70484 KB Output is correct
13 Correct 92 ms 70484 KB Output is correct
14 Correct 74 ms 70484 KB Output is correct
15 Correct 71 ms 70484 KB Output is correct
16 Correct 78 ms 70484 KB Output is correct
17 Correct 82 ms 70484 KB Output is correct
18 Correct 73 ms 70484 KB Output is correct
19 Correct 85 ms 70508 KB Output is correct
20 Correct 77 ms 70508 KB Output is correct
21 Correct 70 ms 70508 KB Output is correct
22 Correct 87 ms 70508 KB Output is correct
23 Correct 75 ms 70508 KB Output is correct
24 Correct 80 ms 70508 KB Output is correct
25 Correct 76 ms 70508 KB Output is correct
26 Correct 71 ms 70508 KB Output is correct
27 Correct 82 ms 70508 KB Output is correct
28 Correct 89 ms 70508 KB Output is correct
29 Correct 74 ms 70508 KB Output is correct
30 Correct 71 ms 70508 KB Output is correct
31 Correct 78 ms 70508 KB Output is correct
32 Correct 94 ms 70508 KB Output is correct
33 Correct 85 ms 70508 KB Output is correct
34 Correct 71 ms 70508 KB Output is correct
35 Correct 75 ms 70508 KB Output is correct
36 Correct 77 ms 70508 KB Output is correct
37 Correct 70 ms 70508 KB Output is correct
38 Correct 71 ms 70508 KB Output is correct
39 Correct 77 ms 70508 KB Output is correct
40 Correct 71 ms 70508 KB Output is correct
41 Correct 71 ms 70508 KB Output is correct
42 Correct 84 ms 70508 KB Output is correct
43 Correct 71 ms 70508 KB Output is correct
44 Correct 71 ms 70508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 70008 KB Output is correct
2 Correct 73 ms 70120 KB Output is correct
3 Correct 71 ms 70176 KB Output is correct
4 Correct 72 ms 70264 KB Output is correct
5 Correct 71 ms 70264 KB Output is correct
6 Correct 74 ms 70264 KB Output is correct
7 Correct 71 ms 70296 KB Output is correct
8 Correct 81 ms 70296 KB Output is correct
9 Correct 72 ms 70296 KB Output is correct
10 Correct 69 ms 70408 KB Output is correct
11 Correct 77 ms 70484 KB Output is correct
12 Correct 85 ms 70484 KB Output is correct
13 Correct 92 ms 70484 KB Output is correct
14 Correct 74 ms 70484 KB Output is correct
15 Correct 71 ms 70484 KB Output is correct
16 Correct 78 ms 70484 KB Output is correct
17 Correct 82 ms 70484 KB Output is correct
18 Correct 73 ms 70484 KB Output is correct
19 Correct 85 ms 70508 KB Output is correct
20 Correct 77 ms 70508 KB Output is correct
21 Correct 70 ms 70508 KB Output is correct
22 Correct 87 ms 70508 KB Output is correct
23 Correct 75 ms 70508 KB Output is correct
24 Correct 80 ms 70508 KB Output is correct
25 Correct 76 ms 70508 KB Output is correct
26 Correct 71 ms 70508 KB Output is correct
27 Correct 82 ms 70508 KB Output is correct
28 Correct 89 ms 70508 KB Output is correct
29 Correct 74 ms 70508 KB Output is correct
30 Correct 71 ms 70508 KB Output is correct
31 Correct 78 ms 70508 KB Output is correct
32 Correct 94 ms 70508 KB Output is correct
33 Correct 85 ms 70508 KB Output is correct
34 Correct 71 ms 70508 KB Output is correct
35 Correct 75 ms 70508 KB Output is correct
36 Correct 77 ms 70508 KB Output is correct
37 Correct 70 ms 70508 KB Output is correct
38 Correct 71 ms 70508 KB Output is correct
39 Correct 77 ms 70508 KB Output is correct
40 Correct 71 ms 70508 KB Output is correct
41 Correct 71 ms 70508 KB Output is correct
42 Correct 84 ms 70508 KB Output is correct
43 Correct 71 ms 70508 KB Output is correct
44 Correct 71 ms 70508 KB Output is correct
45 Correct 80 ms 70508 KB Output is correct
46 Correct 89 ms 70908 KB Output is correct
47 Correct 78 ms 70908 KB Output is correct
48 Correct 80 ms 70908 KB Output is correct
49 Correct 79 ms 71308 KB Output is correct
50 Correct 76 ms 71348 KB Output is correct
51 Correct 75 ms 71348 KB Output is correct
52 Correct 100 ms 71348 KB Output is correct
53 Correct 105 ms 71348 KB Output is correct
54 Correct 97 ms 71348 KB Output is correct
55 Correct 121 ms 71348 KB Output is correct
56 Correct 107 ms 71348 KB Output is correct
57 Correct 110 ms 71348 KB Output is correct
58 Correct 97 ms 71348 KB Output is correct
59 Correct 94 ms 71348 KB Output is correct
60 Correct 91 ms 71348 KB Output is correct
61 Correct 80 ms 71348 KB Output is correct
62 Correct 77 ms 71348 KB Output is correct
63 Correct 95 ms 71348 KB Output is correct
64 Correct 105 ms 71348 KB Output is correct
65 Correct 72 ms 71348 KB Output is correct
66 Correct 79 ms 71348 KB Output is correct
67 Correct 81 ms 71348 KB Output is correct
68 Correct 80 ms 71348 KB Output is correct
69 Correct 87 ms 71348 KB Output is correct
70 Correct 98 ms 71348 KB Output is correct
71 Correct 70 ms 71348 KB Output is correct
72 Correct 75 ms 71348 KB Output is correct
73 Correct 80 ms 71348 KB Output is correct
74 Correct 71 ms 71348 KB Output is correct
75 Correct 78 ms 71348 KB Output is correct
76 Correct 70 ms 71348 KB Output is correct
77 Correct 72 ms 71348 KB Output is correct
78 Correct 68 ms 71348 KB Output is correct
79 Correct 67 ms 71348 KB Output is correct
80 Correct 68 ms 71348 KB Output is correct
81 Correct 68 ms 71348 KB Output is correct
82 Correct 87 ms 71348 KB Output is correct
83 Correct 66 ms 71348 KB Output is correct
84 Correct 67 ms 71348 KB Output is correct
85 Correct 66 ms 71348 KB Output is correct
86 Correct 65 ms 71348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 70008 KB Output is correct
2 Correct 73 ms 70120 KB Output is correct
3 Correct 71 ms 70176 KB Output is correct
4 Correct 72 ms 70264 KB Output is correct
5 Correct 71 ms 70264 KB Output is correct
6 Correct 74 ms 70264 KB Output is correct
7 Correct 71 ms 70296 KB Output is correct
8 Correct 81 ms 70296 KB Output is correct
9 Correct 72 ms 70296 KB Output is correct
10 Correct 69 ms 70408 KB Output is correct
11 Correct 77 ms 70484 KB Output is correct
12 Correct 85 ms 70484 KB Output is correct
13 Correct 92 ms 70484 KB Output is correct
14 Correct 74 ms 70484 KB Output is correct
15 Correct 71 ms 70484 KB Output is correct
16 Correct 78 ms 70484 KB Output is correct
17 Correct 82 ms 70484 KB Output is correct
18 Correct 73 ms 70484 KB Output is correct
19 Correct 85 ms 70508 KB Output is correct
20 Correct 77 ms 70508 KB Output is correct
21 Correct 70 ms 70508 KB Output is correct
22 Correct 87 ms 70508 KB Output is correct
23 Correct 75 ms 70508 KB Output is correct
24 Correct 80 ms 70508 KB Output is correct
25 Correct 76 ms 70508 KB Output is correct
26 Correct 71 ms 70508 KB Output is correct
27 Correct 82 ms 70508 KB Output is correct
28 Correct 89 ms 70508 KB Output is correct
29 Correct 74 ms 70508 KB Output is correct
30 Correct 71 ms 70508 KB Output is correct
31 Correct 78 ms 70508 KB Output is correct
32 Correct 94 ms 70508 KB Output is correct
33 Correct 85 ms 70508 KB Output is correct
34 Correct 71 ms 70508 KB Output is correct
35 Correct 75 ms 70508 KB Output is correct
36 Correct 77 ms 70508 KB Output is correct
37 Correct 70 ms 70508 KB Output is correct
38 Correct 71 ms 70508 KB Output is correct
39 Correct 77 ms 70508 KB Output is correct
40 Correct 71 ms 70508 KB Output is correct
41 Correct 71 ms 70508 KB Output is correct
42 Correct 84 ms 70508 KB Output is correct
43 Correct 71 ms 70508 KB Output is correct
44 Correct 71 ms 70508 KB Output is correct
45 Correct 80 ms 70508 KB Output is correct
46 Correct 89 ms 70908 KB Output is correct
47 Correct 78 ms 70908 KB Output is correct
48 Correct 80 ms 70908 KB Output is correct
49 Correct 79 ms 71308 KB Output is correct
50 Correct 76 ms 71348 KB Output is correct
51 Correct 75 ms 71348 KB Output is correct
52 Correct 100 ms 71348 KB Output is correct
53 Correct 105 ms 71348 KB Output is correct
54 Correct 97 ms 71348 KB Output is correct
55 Correct 121 ms 71348 KB Output is correct
56 Correct 107 ms 71348 KB Output is correct
57 Correct 110 ms 71348 KB Output is correct
58 Correct 97 ms 71348 KB Output is correct
59 Correct 94 ms 71348 KB Output is correct
60 Correct 91 ms 71348 KB Output is correct
61 Correct 80 ms 71348 KB Output is correct
62 Correct 77 ms 71348 KB Output is correct
63 Correct 95 ms 71348 KB Output is correct
64 Correct 105 ms 71348 KB Output is correct
65 Correct 72 ms 71348 KB Output is correct
66 Correct 79 ms 71348 KB Output is correct
67 Correct 81 ms 71348 KB Output is correct
68 Correct 80 ms 71348 KB Output is correct
69 Correct 87 ms 71348 KB Output is correct
70 Correct 98 ms 71348 KB Output is correct
71 Correct 70 ms 71348 KB Output is correct
72 Correct 75 ms 71348 KB Output is correct
73 Correct 80 ms 71348 KB Output is correct
74 Correct 71 ms 71348 KB Output is correct
75 Correct 78 ms 71348 KB Output is correct
76 Correct 70 ms 71348 KB Output is correct
77 Correct 72 ms 71348 KB Output is correct
78 Correct 68 ms 71348 KB Output is correct
79 Correct 67 ms 71348 KB Output is correct
80 Correct 68 ms 71348 KB Output is correct
81 Correct 68 ms 71348 KB Output is correct
82 Correct 87 ms 71348 KB Output is correct
83 Correct 66 ms 71348 KB Output is correct
84 Correct 67 ms 71348 KB Output is correct
85 Correct 66 ms 71348 KB Output is correct
86 Correct 65 ms 71348 KB Output is correct
87 Correct 86 ms 71612 KB Output is correct
88 Correct 141 ms 74032 KB Output is correct
89 Correct 431 ms 82748 KB Output is correct
90 Correct 490 ms 82876 KB Output is correct
91 Correct 446 ms 82876 KB Output is correct
92 Correct 263 ms 95036 KB Output is correct
93 Correct 259 ms 95036 KB Output is correct
94 Correct 274 ms 95036 KB Output is correct
95 Execution timed out 1089 ms 95036 KB Time limit exceeded
96 Halted 0 ms 0 KB -