답안 #114002

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
114002 2019-05-29T14:15:39 Z shayan_p Construction of Highway (JOI18_construction) C++14
16 / 100
2000 ms 15616 KB
// High above the clouds there is a rainbow...

#include<bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const int maxn=1e5+10,SQ=350;

int a[maxn],fn[maxn],par[maxn],state[maxn],arr[maxn],A[maxn],B[maxn],C;
vector<int>v[maxn];

pii p[SQ+10];

void add(int pos,int x){
    for(pos=pos+3;pos<maxn;pos+=(pos & -pos))
	fn[pos]+=x;
}
int ask(int pos){
    int ans=0;
    for(pos=pos+3;pos>0;pos-=(pos & -pos))
	ans+=fn[pos];
    return ans;
}

int sp[20][maxn],h[maxn];

void dfs(int u){
    sp[0][u]=par[u];
    if(par[u]!=-1) h[u]=h[par[u]]+1;
    
    for(int i=1;i<20;i++){
	sp[i][u]=sp[i-1][u];
	if(sp[i][u]!=-1) sp[i][u]=sp[i-1][sp[i-1][u]];
    }
    for(int y:v[u]){
	dfs(y);
    }
}
int lca(int a,int b){
    if(h[a]<h[b]) swap(a,b);
    for(int i=19;i>=0;i--){
	if(sp[i][a]!=-1 && h[sp[i][a]]>=h[b])
	    a=sp[i][a];
    }
    if(a==b) return a;
    for(int i=19;i>=0;i--){
	if(sp[i][a]!=sp[i][b]) a=sp[i][a],b=sp[i][b];
    }
    return par[a];
}

int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);
    int n;cin>>n;
    for(int i=0;i<n;i++){
	cin>>a[i];
	arr[i]=a[i];
    }
    sort(arr,arr+n);
    C=unique(arr,arr+n)-arr;
    for(int i=0;i<n;i++){
	a[i]=lower_bound(arr,arr+C,a[i])-arr;
    }

    par[0]=-1;
    for(int i=0;i<n-1;i++){
	cin>>A[i]>>B[i];
	par[--B[i]]=--A[i];
	v[A[i]].PB(B[i]);
    }
    
    dfs(0);
    
    for(int l=0;l<n-1;l+=SQ){
	int r=min(n-1,l+SQ);
	++C;
	for(int i=l;i<r;i++){
	    ll ans=0;
	    int x=A[i],lst=x;
	    state[B[i]]=C;
	    while(x>=0 && state[x]!=C){
		state[x]=C;
		ans+=ask(a[x]-1);
		add(a[x],1);
		lst=x=par[x];
	    }
	    int bh=-1,SZ=0;
	    for(int j=i-1;j>=l;j--){
		int hh=h[lca(A[i],B[j])];
		if(bh<hh) p[SZ++]={hh-bh,a[B[j]]}, bh=hh;
	    }
	    for(int j=SZ-1;j>=0;j--){
		ans+= 1ll*p[j].F * ask(p[j].S-1);
		add(p[j].S,p[j].F);
	    }
	    x=A[i];
	    while(x!=lst){
		add(a[x],-1);
		x=par[x];
	    }
	    for(int j=0;j<SZ;j++){
		add(p[j].S,-p[j].F);
	    }
	    cout<<ans<<"\n";
	}
	++C;
	for(int i=r-1;i>=l;i--){
	    int x=B[i];
	    while(x>=0 && state[x]!=C){
		state[x]=C;
		a[x]=a[B[i]];
		x=par[x];
	    }
	}
    }
    return 0;
}
// Deathly mistakes:
//  * Read the problem curfully.
//  * Check maxn.
//  * Overflows.
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2816 KB Output is correct
2 Correct 4 ms 2816 KB Output is correct
3 Correct 4 ms 2816 KB Output is correct
4 Correct 8 ms 2936 KB Output is correct
5 Correct 11 ms 2944 KB Output is correct
6 Correct 10 ms 2944 KB Output is correct
7 Correct 10 ms 2944 KB Output is correct
8 Correct 8 ms 2944 KB Output is correct
9 Correct 4 ms 2944 KB Output is correct
10 Correct 8 ms 2944 KB Output is correct
11 Correct 8 ms 2944 KB Output is correct
12 Correct 7 ms 2944 KB Output is correct
13 Correct 8 ms 2944 KB Output is correct
14 Correct 8 ms 2944 KB Output is correct
15 Correct 10 ms 2944 KB Output is correct
16 Correct 10 ms 2944 KB Output is correct
17 Correct 9 ms 2944 KB Output is correct
18 Correct 10 ms 2944 KB Output is correct
19 Correct 8 ms 2944 KB Output is correct
20 Correct 8 ms 2944 KB Output is correct
21 Correct 7 ms 2944 KB Output is correct
22 Correct 11 ms 2916 KB Output is correct
23 Correct 11 ms 2944 KB Output is correct
24 Correct 10 ms 2944 KB Output is correct
25 Correct 11 ms 2944 KB Output is correct
26 Correct 8 ms 2944 KB Output is correct
27 Correct 8 ms 2944 KB Output is correct
28 Correct 7 ms 2944 KB Output is correct
29 Correct 9 ms 2944 KB Output is correct
30 Correct 11 ms 2944 KB Output is correct
31 Correct 10 ms 2944 KB Output is correct
32 Correct 8 ms 2944 KB Output is correct
33 Correct 10 ms 2944 KB Output is correct
34 Correct 11 ms 2944 KB Output is correct
35 Correct 12 ms 2944 KB Output is correct
36 Correct 10 ms 2944 KB Output is correct
37 Correct 10 ms 2944 KB Output is correct
38 Correct 11 ms 2944 KB Output is correct
39 Correct 10 ms 2944 KB Output is correct
40 Correct 10 ms 2944 KB Output is correct
41 Correct 11 ms 2944 KB Output is correct
42 Correct 10 ms 2944 KB Output is correct
43 Correct 10 ms 2944 KB Output is correct
44 Correct 10 ms 2944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2816 KB Output is correct
2 Correct 4 ms 2816 KB Output is correct
3 Correct 4 ms 2816 KB Output is correct
4 Correct 8 ms 2936 KB Output is correct
5 Correct 11 ms 2944 KB Output is correct
6 Correct 10 ms 2944 KB Output is correct
7 Correct 10 ms 2944 KB Output is correct
8 Correct 8 ms 2944 KB Output is correct
9 Correct 4 ms 2944 KB Output is correct
10 Correct 8 ms 2944 KB Output is correct
11 Correct 8 ms 2944 KB Output is correct
12 Correct 7 ms 2944 KB Output is correct
13 Correct 8 ms 2944 KB Output is correct
14 Correct 8 ms 2944 KB Output is correct
15 Correct 10 ms 2944 KB Output is correct
16 Correct 10 ms 2944 KB Output is correct
17 Correct 9 ms 2944 KB Output is correct
18 Correct 10 ms 2944 KB Output is correct
19 Correct 8 ms 2944 KB Output is correct
20 Correct 8 ms 2944 KB Output is correct
21 Correct 7 ms 2944 KB Output is correct
22 Correct 11 ms 2916 KB Output is correct
23 Correct 11 ms 2944 KB Output is correct
24 Correct 10 ms 2944 KB Output is correct
25 Correct 11 ms 2944 KB Output is correct
26 Correct 8 ms 2944 KB Output is correct
27 Correct 8 ms 2944 KB Output is correct
28 Correct 7 ms 2944 KB Output is correct
29 Correct 9 ms 2944 KB Output is correct
30 Correct 11 ms 2944 KB Output is correct
31 Correct 10 ms 2944 KB Output is correct
32 Correct 8 ms 2944 KB Output is correct
33 Correct 10 ms 2944 KB Output is correct
34 Correct 11 ms 2944 KB Output is correct
35 Correct 12 ms 2944 KB Output is correct
36 Correct 10 ms 2944 KB Output is correct
37 Correct 10 ms 2944 KB Output is correct
38 Correct 11 ms 2944 KB Output is correct
39 Correct 10 ms 2944 KB Output is correct
40 Correct 10 ms 2944 KB Output is correct
41 Correct 11 ms 2944 KB Output is correct
42 Correct 10 ms 2944 KB Output is correct
43 Correct 10 ms 2944 KB Output is correct
44 Correct 10 ms 2944 KB Output is correct
45 Correct 20 ms 2944 KB Output is correct
46 Correct 94 ms 3408 KB Output is correct
47 Correct 95 ms 3328 KB Output is correct
48 Correct 95 ms 3428 KB Output is correct
49 Correct 47 ms 3576 KB Output is correct
50 Correct 49 ms 3584 KB Output is correct
51 Correct 44 ms 3584 KB Output is correct
52 Correct 54 ms 3488 KB Output is correct
53 Correct 54 ms 3576 KB Output is correct
54 Correct 61 ms 3456 KB Output is correct
55 Correct 59 ms 3456 KB Output is correct
56 Correct 81 ms 3576 KB Output is correct
57 Correct 114 ms 3328 KB Output is correct
58 Correct 103 ms 3328 KB Output is correct
59 Correct 103 ms 3440 KB Output is correct
60 Correct 99 ms 3400 KB Output is correct
61 Correct 79 ms 3460 KB Output is correct
62 Correct 80 ms 3456 KB Output is correct
63 Correct 73 ms 3456 KB Output is correct
64 Correct 90 ms 3388 KB Output is correct
65 Correct 92 ms 3328 KB Output is correct
66 Correct 89 ms 3328 KB Output is correct
67 Correct 95 ms 3396 KB Output is correct
68 Correct 43 ms 3576 KB Output is correct
69 Correct 54 ms 3576 KB Output is correct
70 Correct 58 ms 3456 KB Output is correct
71 Correct 69 ms 3456 KB Output is correct
72 Correct 102 ms 3412 KB Output is correct
73 Correct 98 ms 3328 KB Output is correct
74 Correct 73 ms 3456 KB Output is correct
75 Correct 76 ms 3328 KB Output is correct
76 Correct 75 ms 3456 KB Output is correct
77 Correct 78 ms 3448 KB Output is correct
78 Correct 72 ms 3328 KB Output is correct
79 Correct 79 ms 3448 KB Output is correct
80 Correct 74 ms 3456 KB Output is correct
81 Correct 88 ms 3456 KB Output is correct
82 Correct 88 ms 3328 KB Output is correct
83 Correct 100 ms 3420 KB Output is correct
84 Correct 85 ms 3448 KB Output is correct
85 Correct 88 ms 3424 KB Output is correct
86 Correct 90 ms 3328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2816 KB Output is correct
2 Correct 4 ms 2816 KB Output is correct
3 Correct 4 ms 2816 KB Output is correct
4 Correct 8 ms 2936 KB Output is correct
5 Correct 11 ms 2944 KB Output is correct
6 Correct 10 ms 2944 KB Output is correct
7 Correct 10 ms 2944 KB Output is correct
8 Correct 8 ms 2944 KB Output is correct
9 Correct 4 ms 2944 KB Output is correct
10 Correct 8 ms 2944 KB Output is correct
11 Correct 8 ms 2944 KB Output is correct
12 Correct 7 ms 2944 KB Output is correct
13 Correct 8 ms 2944 KB Output is correct
14 Correct 8 ms 2944 KB Output is correct
15 Correct 10 ms 2944 KB Output is correct
16 Correct 10 ms 2944 KB Output is correct
17 Correct 9 ms 2944 KB Output is correct
18 Correct 10 ms 2944 KB Output is correct
19 Correct 8 ms 2944 KB Output is correct
20 Correct 8 ms 2944 KB Output is correct
21 Correct 7 ms 2944 KB Output is correct
22 Correct 11 ms 2916 KB Output is correct
23 Correct 11 ms 2944 KB Output is correct
24 Correct 10 ms 2944 KB Output is correct
25 Correct 11 ms 2944 KB Output is correct
26 Correct 8 ms 2944 KB Output is correct
27 Correct 8 ms 2944 KB Output is correct
28 Correct 7 ms 2944 KB Output is correct
29 Correct 9 ms 2944 KB Output is correct
30 Correct 11 ms 2944 KB Output is correct
31 Correct 10 ms 2944 KB Output is correct
32 Correct 8 ms 2944 KB Output is correct
33 Correct 10 ms 2944 KB Output is correct
34 Correct 11 ms 2944 KB Output is correct
35 Correct 12 ms 2944 KB Output is correct
36 Correct 10 ms 2944 KB Output is correct
37 Correct 10 ms 2944 KB Output is correct
38 Correct 11 ms 2944 KB Output is correct
39 Correct 10 ms 2944 KB Output is correct
40 Correct 10 ms 2944 KB Output is correct
41 Correct 11 ms 2944 KB Output is correct
42 Correct 10 ms 2944 KB Output is correct
43 Correct 10 ms 2944 KB Output is correct
44 Correct 10 ms 2944 KB Output is correct
45 Correct 20 ms 2944 KB Output is correct
46 Correct 94 ms 3408 KB Output is correct
47 Correct 95 ms 3328 KB Output is correct
48 Correct 95 ms 3428 KB Output is correct
49 Correct 47 ms 3576 KB Output is correct
50 Correct 49 ms 3584 KB Output is correct
51 Correct 44 ms 3584 KB Output is correct
52 Correct 54 ms 3488 KB Output is correct
53 Correct 54 ms 3576 KB Output is correct
54 Correct 61 ms 3456 KB Output is correct
55 Correct 59 ms 3456 KB Output is correct
56 Correct 81 ms 3576 KB Output is correct
57 Correct 114 ms 3328 KB Output is correct
58 Correct 103 ms 3328 KB Output is correct
59 Correct 103 ms 3440 KB Output is correct
60 Correct 99 ms 3400 KB Output is correct
61 Correct 79 ms 3460 KB Output is correct
62 Correct 80 ms 3456 KB Output is correct
63 Correct 73 ms 3456 KB Output is correct
64 Correct 90 ms 3388 KB Output is correct
65 Correct 92 ms 3328 KB Output is correct
66 Correct 89 ms 3328 KB Output is correct
67 Correct 95 ms 3396 KB Output is correct
68 Correct 43 ms 3576 KB Output is correct
69 Correct 54 ms 3576 KB Output is correct
70 Correct 58 ms 3456 KB Output is correct
71 Correct 69 ms 3456 KB Output is correct
72 Correct 102 ms 3412 KB Output is correct
73 Correct 98 ms 3328 KB Output is correct
74 Correct 73 ms 3456 KB Output is correct
75 Correct 76 ms 3328 KB Output is correct
76 Correct 75 ms 3456 KB Output is correct
77 Correct 78 ms 3448 KB Output is correct
78 Correct 72 ms 3328 KB Output is correct
79 Correct 79 ms 3448 KB Output is correct
80 Correct 74 ms 3456 KB Output is correct
81 Correct 88 ms 3456 KB Output is correct
82 Correct 88 ms 3328 KB Output is correct
83 Correct 100 ms 3420 KB Output is correct
84 Correct 85 ms 3448 KB Output is correct
85 Correct 88 ms 3424 KB Output is correct
86 Correct 90 ms 3328 KB Output is correct
87 Correct 292 ms 4172 KB Output is correct
88 Correct 1091 ms 6932 KB Output is correct
89 Execution timed out 2069 ms 15616 KB Time limit exceeded
90 Halted 0 ms 0 KB -