답안 #409592

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
409592 2021-05-21T06:09:01 Z urd05 Construction of Highway (JOI18_construction) C++14
16 / 100
2000 ms 25276 KB
#include <bits/stdc++.h>
using namespace std;

int n;
int arr[100000];
vector<int> adj[100000];
typedef pair<int,int> P;
P edge[100000];
int p[100000];
int ind[100000];
int siz[100000];
int f=0;
int depth[100000];
int par[100000][17];

void dfs(int v,int prev) {
    ind[v]=f++;
    siz[v]=1;
    p[v]=prev;
    for(int i=0;i<adj[v].size();i++) {
        int nt=adj[v][i];
        if (nt==prev) {
            continue;
        }
        depth[nt]=depth[v]+1;
        dfs(nt,v);
        siz[v]+=siz[nt];
    }
}

const int sz=131072;
P seg[sz*2];

P pmax(P a,P b) {
    return a<b?b:a;
}

P sum(int l,int r,int node=1,int nodel=0,int noder=sz-1) {
    if (r<nodel||l>noder) {
        return P(0,0);
    }
    if (l<=nodel&&noder<=r) {
        return seg[node];
    }
    int mid=(nodel+noder)/2;
    return pmax(sum(l,r,node*2,nodel,mid),sum(l,r,node*2+1,mid+1,noder));
}

void update(int i,P val) {
    i+=sz;
    seg[i]=val;
    while (i>1) {
        i/=2;
        seg[i]=pmax(seg[i*2],seg[i*2+1]);
    }
}

struct SegmentTree {
    vector<int> seg;
    int sz=1;
    void init(int x) {
        while (sz<x) {
            sz*=2;
        }
        seg.resize(sz*2);
    }
    int sum(int l,int r,int node=1,int nodel=0,int noder=0) {
        if (node==1) {
            noder=sz-1;
        }
        if (r<nodel||l>noder) {
            return 0;
        }
        if (l<=nodel&&noder<=r) {
            return seg[node];
        }
        int mid=(nodel+noder)/2;
        return sum(l,r,node*2,nodel,mid)+sum(l,r,node*2+1,mid+1,noder);
    }
    void update(int i,int val) {
        i+=sz;
        seg[i]+=val;
        while (i>1) {
            i/=2;
            seg[i]=seg[i*2]+seg[i*2+1];
        }
    }
};

long long cntinv(vector<P> v) {
    vector<int> press;
    for(int i=0;i<v.size();i++) {
        //printf("%d %d\n",v[i].first,v[i].second);
        press.push_back(v[i].first);
    }
    sort(press.begin(),press.end());
    press.erase(unique(press.begin(),press.end()),press.end());
    SegmentTree st;
    st.init(press.size());
    long long ret=0;
    for(int i=0;i<v.size();i++) {
        v[i].first=lower_bound(press.begin(),press.end(),v[i].first)-press.begin();
        if (v[i].first!=0)
            ret+=1LL*v[i].second*st.sum(0,v[i].first-1);
        st.update(v[i].first,v[i].second);
    }
    return ret;
}

int main(void) {
    scanf("%d",&n);
    for(int i=0;i<n;i++) {
        scanf("%d",&arr[i]);
    }
    vector<int> press;
    for(int i=0;i<n;i++) {
        press.push_back(arr[i]);
    }
    sort(press.begin(),press.end());
    press.erase(unique(press.begin(),press.end()),press.end());
    for(int i=0;i<n;i++) {
        arr[i]=lower_bound(press.begin(),press.end(),arr[i])-press.begin();
    }
    for(int i=1;i<n;i++) {
        int u,v;
        scanf("%d %d",&u,&v);
        u--;
        v--;
        edge[i]=P(u,v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(0,-1);
    memset(par,-1,sizeof(par));
    for(int i=0;i<n;i++) {
        par[i][0]=p[i];
    }
    for(int j=1;j<17;j++) {
        for(int i=0;i<n;i++) {
            if (par[i][j-1]!=-1) {
                par[i][j]=par[par[i][j-1]][j-1];
            }
        }
    }
    for(int i=1;i<n;i++) {
        int now=edge[i].first;
        vector<P> vec;
        while (now!=-1) {
            int rec=now;
            int deep=sum(ind[now],ind[now]+siz[now]-1).second;
            for(int j=16;j>=0;j--) {
                int nt=par[rec][j];
                if (nt==-1) {
                    continue;
                }
                if (sum(ind[nt],ind[nt]+siz[nt]-1).second==deep) {
                    rec=nt;
                }
            }
            vec.push_back(P(arr[deep],depth[now]-depth[rec]+1));
            now=p[rec];
        }
        update(ind[edge[i].second],P(i,edge[i].second));
        printf("%lld\n",cntinv(vec));
    }
}

Compilation message

construction.cpp: In function 'void dfs(int, int)':
construction.cpp:20:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |     for(int i=0;i<adj[v].size();i++) {
      |                 ~^~~~~~~~~~~~~~
construction.cpp: In function 'long long int cntinv(std::vector<std::pair<int, int> >)':
construction.cpp:92:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |     for(int i=0;i<v.size();i++) {
      |                 ~^~~~~~~~~
construction.cpp:101:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |     for(int i=0;i<v.size();i++) {
      |                 ~^~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:111:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
construction.cpp:113:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |         scanf("%d",&arr[i]);
      |         ~~~~~^~~~~~~~~~~~~~
construction.cpp:126:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 |         scanf("%d %d",&u,&v);
      |         ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9292 KB Output is correct
2 Correct 5 ms 9292 KB Output is correct
3 Correct 6 ms 9324 KB Output is correct
4 Correct 6 ms 9292 KB Output is correct
5 Correct 7 ms 9456 KB Output is correct
6 Correct 7 ms 9292 KB Output is correct
7 Correct 7 ms 9292 KB Output is correct
8 Correct 6 ms 9420 KB Output is correct
9 Correct 6 ms 9420 KB Output is correct
10 Correct 6 ms 9420 KB Output is correct
11 Correct 7 ms 9420 KB Output is correct
12 Correct 7 ms 9420 KB Output is correct
13 Correct 7 ms 9420 KB Output is correct
14 Correct 6 ms 9308 KB Output is correct
15 Correct 8 ms 9404 KB Output is correct
16 Correct 8 ms 9292 KB Output is correct
17 Correct 8 ms 9292 KB Output is correct
18 Correct 8 ms 9332 KB Output is correct
19 Correct 7 ms 9292 KB Output is correct
20 Correct 7 ms 9400 KB Output is correct
21 Correct 7 ms 9420 KB Output is correct
22 Correct 6 ms 9292 KB Output is correct
23 Correct 6 ms 9292 KB Output is correct
24 Correct 7 ms 9292 KB Output is correct
25 Correct 7 ms 9292 KB Output is correct
26 Correct 6 ms 9348 KB Output is correct
27 Correct 6 ms 9416 KB Output is correct
28 Correct 7 ms 9420 KB Output is correct
29 Correct 7 ms 9416 KB Output is correct
30 Correct 8 ms 9372 KB Output is correct
31 Correct 8 ms 9292 KB Output is correct
32 Correct 8 ms 9292 KB Output is correct
33 Correct 6 ms 9292 KB Output is correct
34 Correct 6 ms 9408 KB Output is correct
35 Correct 7 ms 9292 KB Output is correct
36 Correct 6 ms 9292 KB Output is correct
37 Correct 7 ms 9292 KB Output is correct
38 Correct 7 ms 9292 KB Output is correct
39 Correct 7 ms 9412 KB Output is correct
40 Correct 7 ms 9292 KB Output is correct
41 Correct 7 ms 9292 KB Output is correct
42 Correct 7 ms 9292 KB Output is correct
43 Correct 7 ms 9292 KB Output is correct
44 Correct 6 ms 9292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9292 KB Output is correct
2 Correct 5 ms 9292 KB Output is correct
3 Correct 6 ms 9324 KB Output is correct
4 Correct 6 ms 9292 KB Output is correct
5 Correct 7 ms 9456 KB Output is correct
6 Correct 7 ms 9292 KB Output is correct
7 Correct 7 ms 9292 KB Output is correct
8 Correct 6 ms 9420 KB Output is correct
9 Correct 6 ms 9420 KB Output is correct
10 Correct 6 ms 9420 KB Output is correct
11 Correct 7 ms 9420 KB Output is correct
12 Correct 7 ms 9420 KB Output is correct
13 Correct 7 ms 9420 KB Output is correct
14 Correct 6 ms 9308 KB Output is correct
15 Correct 8 ms 9404 KB Output is correct
16 Correct 8 ms 9292 KB Output is correct
17 Correct 8 ms 9292 KB Output is correct
18 Correct 8 ms 9332 KB Output is correct
19 Correct 7 ms 9292 KB Output is correct
20 Correct 7 ms 9400 KB Output is correct
21 Correct 7 ms 9420 KB Output is correct
22 Correct 6 ms 9292 KB Output is correct
23 Correct 6 ms 9292 KB Output is correct
24 Correct 7 ms 9292 KB Output is correct
25 Correct 7 ms 9292 KB Output is correct
26 Correct 6 ms 9348 KB Output is correct
27 Correct 6 ms 9416 KB Output is correct
28 Correct 7 ms 9420 KB Output is correct
29 Correct 7 ms 9416 KB Output is correct
30 Correct 8 ms 9372 KB Output is correct
31 Correct 8 ms 9292 KB Output is correct
32 Correct 8 ms 9292 KB Output is correct
33 Correct 6 ms 9292 KB Output is correct
34 Correct 6 ms 9408 KB Output is correct
35 Correct 7 ms 9292 KB Output is correct
36 Correct 6 ms 9292 KB Output is correct
37 Correct 7 ms 9292 KB Output is correct
38 Correct 7 ms 9292 KB Output is correct
39 Correct 7 ms 9412 KB Output is correct
40 Correct 7 ms 9292 KB Output is correct
41 Correct 7 ms 9292 KB Output is correct
42 Correct 7 ms 9292 KB Output is correct
43 Correct 7 ms 9292 KB Output is correct
44 Correct 6 ms 9292 KB Output is correct
45 Correct 9 ms 9420 KB Output is correct
46 Correct 27 ms 9672 KB Output is correct
47 Correct 28 ms 9688 KB Output is correct
48 Correct 26 ms 9684 KB Output is correct
49 Correct 16 ms 9932 KB Output is correct
50 Correct 16 ms 9940 KB Output is correct
51 Correct 16 ms 9992 KB Output is correct
52 Correct 23 ms 9788 KB Output is correct
53 Correct 23 ms 9804 KB Output is correct
54 Correct 24 ms 9804 KB Output is correct
55 Correct 24 ms 9808 KB Output is correct
56 Correct 24 ms 9828 KB Output is correct
57 Correct 49 ms 9676 KB Output is correct
58 Correct 51 ms 9684 KB Output is correct
59 Correct 50 ms 9680 KB Output is correct
60 Correct 51 ms 9664 KB Output is correct
61 Correct 25 ms 9804 KB Output is correct
62 Correct 25 ms 9796 KB Output is correct
63 Correct 26 ms 9836 KB Output is correct
64 Correct 28 ms 9676 KB Output is correct
65 Correct 26 ms 9680 KB Output is correct
66 Correct 27 ms 9684 KB Output is correct
67 Correct 26 ms 9816 KB Output is correct
68 Correct 16 ms 9972 KB Output is correct
69 Correct 23 ms 9812 KB Output is correct
70 Correct 25 ms 9816 KB Output is correct
71 Correct 23 ms 9840 KB Output is correct
72 Correct 49 ms 9780 KB Output is correct
73 Correct 51 ms 9668 KB Output is correct
74 Correct 25 ms 9784 KB Output is correct
75 Correct 22 ms 9764 KB Output is correct
76 Correct 22 ms 9700 KB Output is correct
77 Correct 21 ms 9724 KB Output is correct
78 Correct 22 ms 9692 KB Output is correct
79 Correct 21 ms 9724 KB Output is correct
80 Correct 20 ms 9676 KB Output is correct
81 Correct 24 ms 9752 KB Output is correct
82 Correct 25 ms 9736 KB Output is correct
83 Correct 24 ms 9676 KB Output is correct
84 Correct 25 ms 9676 KB Output is correct
85 Correct 23 ms 9676 KB Output is correct
86 Correct 22 ms 9724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9292 KB Output is correct
2 Correct 5 ms 9292 KB Output is correct
3 Correct 6 ms 9324 KB Output is correct
4 Correct 6 ms 9292 KB Output is correct
5 Correct 7 ms 9456 KB Output is correct
6 Correct 7 ms 9292 KB Output is correct
7 Correct 7 ms 9292 KB Output is correct
8 Correct 6 ms 9420 KB Output is correct
9 Correct 6 ms 9420 KB Output is correct
10 Correct 6 ms 9420 KB Output is correct
11 Correct 7 ms 9420 KB Output is correct
12 Correct 7 ms 9420 KB Output is correct
13 Correct 7 ms 9420 KB Output is correct
14 Correct 6 ms 9308 KB Output is correct
15 Correct 8 ms 9404 KB Output is correct
16 Correct 8 ms 9292 KB Output is correct
17 Correct 8 ms 9292 KB Output is correct
18 Correct 8 ms 9332 KB Output is correct
19 Correct 7 ms 9292 KB Output is correct
20 Correct 7 ms 9400 KB Output is correct
21 Correct 7 ms 9420 KB Output is correct
22 Correct 6 ms 9292 KB Output is correct
23 Correct 6 ms 9292 KB Output is correct
24 Correct 7 ms 9292 KB Output is correct
25 Correct 7 ms 9292 KB Output is correct
26 Correct 6 ms 9348 KB Output is correct
27 Correct 6 ms 9416 KB Output is correct
28 Correct 7 ms 9420 KB Output is correct
29 Correct 7 ms 9416 KB Output is correct
30 Correct 8 ms 9372 KB Output is correct
31 Correct 8 ms 9292 KB Output is correct
32 Correct 8 ms 9292 KB Output is correct
33 Correct 6 ms 9292 KB Output is correct
34 Correct 6 ms 9408 KB Output is correct
35 Correct 7 ms 9292 KB Output is correct
36 Correct 6 ms 9292 KB Output is correct
37 Correct 7 ms 9292 KB Output is correct
38 Correct 7 ms 9292 KB Output is correct
39 Correct 7 ms 9412 KB Output is correct
40 Correct 7 ms 9292 KB Output is correct
41 Correct 7 ms 9292 KB Output is correct
42 Correct 7 ms 9292 KB Output is correct
43 Correct 7 ms 9292 KB Output is correct
44 Correct 6 ms 9292 KB Output is correct
45 Correct 9 ms 9420 KB Output is correct
46 Correct 27 ms 9672 KB Output is correct
47 Correct 28 ms 9688 KB Output is correct
48 Correct 26 ms 9684 KB Output is correct
49 Correct 16 ms 9932 KB Output is correct
50 Correct 16 ms 9940 KB Output is correct
51 Correct 16 ms 9992 KB Output is correct
52 Correct 23 ms 9788 KB Output is correct
53 Correct 23 ms 9804 KB Output is correct
54 Correct 24 ms 9804 KB Output is correct
55 Correct 24 ms 9808 KB Output is correct
56 Correct 24 ms 9828 KB Output is correct
57 Correct 49 ms 9676 KB Output is correct
58 Correct 51 ms 9684 KB Output is correct
59 Correct 50 ms 9680 KB Output is correct
60 Correct 51 ms 9664 KB Output is correct
61 Correct 25 ms 9804 KB Output is correct
62 Correct 25 ms 9796 KB Output is correct
63 Correct 26 ms 9836 KB Output is correct
64 Correct 28 ms 9676 KB Output is correct
65 Correct 26 ms 9680 KB Output is correct
66 Correct 27 ms 9684 KB Output is correct
67 Correct 26 ms 9816 KB Output is correct
68 Correct 16 ms 9972 KB Output is correct
69 Correct 23 ms 9812 KB Output is correct
70 Correct 25 ms 9816 KB Output is correct
71 Correct 23 ms 9840 KB Output is correct
72 Correct 49 ms 9780 KB Output is correct
73 Correct 51 ms 9668 KB Output is correct
74 Correct 25 ms 9784 KB Output is correct
75 Correct 22 ms 9764 KB Output is correct
76 Correct 22 ms 9700 KB Output is correct
77 Correct 21 ms 9724 KB Output is correct
78 Correct 22 ms 9692 KB Output is correct
79 Correct 21 ms 9724 KB Output is correct
80 Correct 20 ms 9676 KB Output is correct
81 Correct 24 ms 9752 KB Output is correct
82 Correct 25 ms 9736 KB Output is correct
83 Correct 24 ms 9676 KB Output is correct
84 Correct 25 ms 9676 KB Output is correct
85 Correct 23 ms 9676 KB Output is correct
86 Correct 22 ms 9724 KB Output is correct
87 Correct 69 ms 10160 KB Output is correct
88 Correct 221 ms 11784 KB Output is correct
89 Correct 1030 ms 17556 KB Output is correct
90 Correct 1027 ms 17536 KB Output is correct
91 Correct 1029 ms 17680 KB Output is correct
92 Correct 436 ms 25276 KB Output is correct
93 Correct 453 ms 25188 KB Output is correct
94 Correct 433 ms 25148 KB Output is correct
95 Correct 774 ms 23168 KB Output is correct
96 Correct 781 ms 23044 KB Output is correct
97 Correct 791 ms 22908 KB Output is correct
98 Correct 818 ms 22968 KB Output is correct
99 Correct 796 ms 21448 KB Output is correct
100 Execution timed out 2084 ms 17476 KB Time limit exceeded
101 Halted 0 ms 0 KB -