Submission #114014

# Submission time Handle Problem Language Result Execution time Memory
114014 2019-05-29T14:38:34 Z shayan_p Construction of Highway (JOI18_construction) C++14
100 / 100
1094 ms 31044 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=1200;

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][2*maxn],h[maxn],start[maxn];
vector<int>vec;

void dfs(int u){
    if(par[u]!=-1) h[u]=h[par[u]]+1;  
    start[u]=sz(vec), vec.PB(u);
    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);
    }
    if(par[u]!=-1) vec.PB(par[u]);
}
void prep(){
    for(int i=0;i<sz(vec);i++){
	sp[0][i]=h[vec[i]];
    }
    for(int i=1;i<20;i++){
	for(int j=0;j<sz(vec);j++){
	    sp[i][j]=sp[i-1][j];
	    if(j+(1<<(i-1))<sz(vec)) sp[i][j]=min(sp[i][j],sp[i-1][j+(1<<(i-1))]);
	}
    }
}
int lca(int a,int b){
    a=start[a], b=start[b];
    if(a>b) swap(a,b);
    int id=31-__builtin_clz(b-a+1);
    return min(sp[id][a],sp[id][b+1-(1<<id)]);
}

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);
    prep();
    
    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=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.
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2816 KB Output is correct
2 Correct 4 ms 2944 KB Output is correct
3 Correct 4 ms 2944 KB Output is correct
4 Correct 5 ms 2904 KB Output is correct
5 Correct 5 ms 2944 KB Output is correct
6 Correct 5 ms 2944 KB Output is correct
7 Correct 5 ms 2944 KB Output is correct
8 Correct 5 ms 2944 KB Output is correct
9 Correct 5 ms 2944 KB Output is correct
10 Correct 5 ms 3072 KB Output is correct
11 Correct 4 ms 2940 KB Output is correct
12 Correct 5 ms 3072 KB Output is correct
13 Correct 5 ms 2944 KB Output is correct
14 Correct 5 ms 2940 KB Output is correct
15 Correct 5 ms 2944 KB Output is correct
16 Correct 4 ms 2944 KB Output is correct
17 Correct 5 ms 3072 KB Output is correct
18 Correct 5 ms 2944 KB Output is correct
19 Correct 5 ms 3072 KB Output is correct
20 Correct 5 ms 2944 KB Output is correct
21 Correct 4 ms 2944 KB Output is correct
22 Correct 5 ms 3072 KB Output is correct
23 Correct 5 ms 2944 KB Output is correct
24 Correct 5 ms 3072 KB Output is correct
25 Correct 6 ms 2944 KB Output is correct
26 Correct 4 ms 2944 KB Output is correct
27 Correct 4 ms 2944 KB Output is correct
28 Correct 5 ms 3072 KB Output is correct
29 Correct 5 ms 2944 KB Output is correct
30 Correct 5 ms 2944 KB Output is correct
31 Correct 5 ms 2944 KB Output is correct
32 Correct 5 ms 2940 KB Output is correct
33 Correct 4 ms 2944 KB Output is correct
34 Correct 5 ms 2916 KB Output is correct
35 Correct 5 ms 3032 KB Output is correct
36 Correct 5 ms 3072 KB Output is correct
37 Correct 4 ms 2944 KB Output is correct
38 Correct 4 ms 2944 KB Output is correct
39 Correct 5 ms 3072 KB Output is correct
40 Correct 4 ms 2944 KB Output is correct
41 Correct 5 ms 3072 KB Output is correct
42 Correct 5 ms 3072 KB Output is correct
43 Correct 4 ms 2944 KB Output is correct
44 Correct 5 ms 3072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2816 KB Output is correct
2 Correct 4 ms 2944 KB Output is correct
3 Correct 4 ms 2944 KB Output is correct
4 Correct 5 ms 2904 KB Output is correct
5 Correct 5 ms 2944 KB Output is correct
6 Correct 5 ms 2944 KB Output is correct
7 Correct 5 ms 2944 KB Output is correct
8 Correct 5 ms 2944 KB Output is correct
9 Correct 5 ms 2944 KB Output is correct
10 Correct 5 ms 3072 KB Output is correct
11 Correct 4 ms 2940 KB Output is correct
12 Correct 5 ms 3072 KB Output is correct
13 Correct 5 ms 2944 KB Output is correct
14 Correct 5 ms 2940 KB Output is correct
15 Correct 5 ms 2944 KB Output is correct
16 Correct 4 ms 2944 KB Output is correct
17 Correct 5 ms 3072 KB Output is correct
18 Correct 5 ms 2944 KB Output is correct
19 Correct 5 ms 3072 KB Output is correct
20 Correct 5 ms 2944 KB Output is correct
21 Correct 4 ms 2944 KB Output is correct
22 Correct 5 ms 3072 KB Output is correct
23 Correct 5 ms 2944 KB Output is correct
24 Correct 5 ms 3072 KB Output is correct
25 Correct 6 ms 2944 KB Output is correct
26 Correct 4 ms 2944 KB Output is correct
27 Correct 4 ms 2944 KB Output is correct
28 Correct 5 ms 3072 KB Output is correct
29 Correct 5 ms 2944 KB Output is correct
30 Correct 5 ms 2944 KB Output is correct
31 Correct 5 ms 2944 KB Output is correct
32 Correct 5 ms 2940 KB Output is correct
33 Correct 4 ms 2944 KB Output is correct
34 Correct 5 ms 2916 KB Output is correct
35 Correct 5 ms 3032 KB Output is correct
36 Correct 5 ms 3072 KB Output is correct
37 Correct 4 ms 2944 KB Output is correct
38 Correct 4 ms 2944 KB Output is correct
39 Correct 5 ms 3072 KB Output is correct
40 Correct 4 ms 2944 KB Output is correct
41 Correct 5 ms 3072 KB Output is correct
42 Correct 5 ms 3072 KB Output is correct
43 Correct 4 ms 2944 KB Output is correct
44 Correct 5 ms 3072 KB Output is correct
45 Correct 7 ms 3072 KB Output is correct
46 Correct 22 ms 3804 KB Output is correct
47 Correct 21 ms 3804 KB Output is correct
48 Correct 22 ms 3712 KB Output is correct
49 Correct 16 ms 3968 KB Output is correct
50 Correct 16 ms 4016 KB Output is correct
51 Correct 16 ms 3968 KB Output is correct
52 Correct 17 ms 3840 KB Output is correct
53 Correct 16 ms 3840 KB Output is correct
54 Correct 16 ms 3840 KB Output is correct
55 Correct 17 ms 3888 KB Output is correct
56 Correct 17 ms 3840 KB Output is correct
57 Correct 21 ms 3712 KB Output is correct
58 Correct 22 ms 3756 KB Output is correct
59 Correct 21 ms 3712 KB Output is correct
60 Correct 28 ms 3832 KB Output is correct
61 Correct 18 ms 3892 KB Output is correct
62 Correct 18 ms 3868 KB Output is correct
63 Correct 18 ms 3840 KB Output is correct
64 Correct 22 ms 3712 KB Output is correct
65 Correct 23 ms 3788 KB Output is correct
66 Correct 23 ms 3832 KB Output is correct
67 Correct 23 ms 3712 KB Output is correct
68 Correct 16 ms 3968 KB Output is correct
69 Correct 16 ms 3840 KB Output is correct
70 Correct 16 ms 3840 KB Output is correct
71 Correct 16 ms 3840 KB Output is correct
72 Correct 20 ms 3840 KB Output is correct
73 Correct 20 ms 3712 KB Output is correct
74 Correct 17 ms 3840 KB Output is correct
75 Correct 17 ms 3840 KB Output is correct
76 Correct 16 ms 3712 KB Output is correct
77 Correct 15 ms 3712 KB Output is correct
78 Correct 15 ms 3840 KB Output is correct
79 Correct 16 ms 3712 KB Output is correct
80 Correct 16 ms 3840 KB Output is correct
81 Correct 17 ms 3840 KB Output is correct
82 Correct 17 ms 3840 KB Output is correct
83 Correct 17 ms 3840 KB Output is correct
84 Correct 17 ms 3840 KB Output is correct
85 Correct 18 ms 3840 KB Output is correct
86 Correct 17 ms 3840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2816 KB Output is correct
2 Correct 4 ms 2944 KB Output is correct
3 Correct 4 ms 2944 KB Output is correct
4 Correct 5 ms 2904 KB Output is correct
5 Correct 5 ms 2944 KB Output is correct
6 Correct 5 ms 2944 KB Output is correct
7 Correct 5 ms 2944 KB Output is correct
8 Correct 5 ms 2944 KB Output is correct
9 Correct 5 ms 2944 KB Output is correct
10 Correct 5 ms 3072 KB Output is correct
11 Correct 4 ms 2940 KB Output is correct
12 Correct 5 ms 3072 KB Output is correct
13 Correct 5 ms 2944 KB Output is correct
14 Correct 5 ms 2940 KB Output is correct
15 Correct 5 ms 2944 KB Output is correct
16 Correct 4 ms 2944 KB Output is correct
17 Correct 5 ms 3072 KB Output is correct
18 Correct 5 ms 2944 KB Output is correct
19 Correct 5 ms 3072 KB Output is correct
20 Correct 5 ms 2944 KB Output is correct
21 Correct 4 ms 2944 KB Output is correct
22 Correct 5 ms 3072 KB Output is correct
23 Correct 5 ms 2944 KB Output is correct
24 Correct 5 ms 3072 KB Output is correct
25 Correct 6 ms 2944 KB Output is correct
26 Correct 4 ms 2944 KB Output is correct
27 Correct 4 ms 2944 KB Output is correct
28 Correct 5 ms 3072 KB Output is correct
29 Correct 5 ms 2944 KB Output is correct
30 Correct 5 ms 2944 KB Output is correct
31 Correct 5 ms 2944 KB Output is correct
32 Correct 5 ms 2940 KB Output is correct
33 Correct 4 ms 2944 KB Output is correct
34 Correct 5 ms 2916 KB Output is correct
35 Correct 5 ms 3032 KB Output is correct
36 Correct 5 ms 3072 KB Output is correct
37 Correct 4 ms 2944 KB Output is correct
38 Correct 4 ms 2944 KB Output is correct
39 Correct 5 ms 3072 KB Output is correct
40 Correct 4 ms 2944 KB Output is correct
41 Correct 5 ms 3072 KB Output is correct
42 Correct 5 ms 3072 KB Output is correct
43 Correct 4 ms 2944 KB Output is correct
44 Correct 5 ms 3072 KB Output is correct
45 Correct 7 ms 3072 KB Output is correct
46 Correct 22 ms 3804 KB Output is correct
47 Correct 21 ms 3804 KB Output is correct
48 Correct 22 ms 3712 KB Output is correct
49 Correct 16 ms 3968 KB Output is correct
50 Correct 16 ms 4016 KB Output is correct
51 Correct 16 ms 3968 KB Output is correct
52 Correct 17 ms 3840 KB Output is correct
53 Correct 16 ms 3840 KB Output is correct
54 Correct 16 ms 3840 KB Output is correct
55 Correct 17 ms 3888 KB Output is correct
56 Correct 17 ms 3840 KB Output is correct
57 Correct 21 ms 3712 KB Output is correct
58 Correct 22 ms 3756 KB Output is correct
59 Correct 21 ms 3712 KB Output is correct
60 Correct 28 ms 3832 KB Output is correct
61 Correct 18 ms 3892 KB Output is correct
62 Correct 18 ms 3868 KB Output is correct
63 Correct 18 ms 3840 KB Output is correct
64 Correct 22 ms 3712 KB Output is correct
65 Correct 23 ms 3788 KB Output is correct
66 Correct 23 ms 3832 KB Output is correct
67 Correct 23 ms 3712 KB Output is correct
68 Correct 16 ms 3968 KB Output is correct
69 Correct 16 ms 3840 KB Output is correct
70 Correct 16 ms 3840 KB Output is correct
71 Correct 16 ms 3840 KB Output is correct
72 Correct 20 ms 3840 KB Output is correct
73 Correct 20 ms 3712 KB Output is correct
74 Correct 17 ms 3840 KB Output is correct
75 Correct 17 ms 3840 KB Output is correct
76 Correct 16 ms 3712 KB Output is correct
77 Correct 15 ms 3712 KB Output is correct
78 Correct 15 ms 3840 KB Output is correct
79 Correct 16 ms 3712 KB Output is correct
80 Correct 16 ms 3840 KB Output is correct
81 Correct 17 ms 3840 KB Output is correct
82 Correct 17 ms 3840 KB Output is correct
83 Correct 17 ms 3840 KB Output is correct
84 Correct 17 ms 3840 KB Output is correct
85 Correct 18 ms 3840 KB Output is correct
86 Correct 17 ms 3840 KB Output is correct
87 Correct 61 ms 5216 KB Output is correct
88 Correct 240 ms 9588 KB Output is correct
89 Correct 1031 ms 24824 KB Output is correct
90 Correct 1018 ms 24908 KB Output is correct
91 Correct 1000 ms 24760 KB Output is correct
92 Correct 959 ms 30868 KB Output is correct
93 Correct 931 ms 31044 KB Output is correct
94 Correct 940 ms 31040 KB Output is correct
95 Correct 730 ms 26984 KB Output is correct
96 Correct 751 ms 27116 KB Output is correct
97 Correct 766 ms 26976 KB Output is correct
98 Correct 739 ms 26992 KB Output is correct
99 Correct 743 ms 27216 KB Output is correct
100 Correct 972 ms 25336 KB Output is correct
101 Correct 1092 ms 25424 KB Output is correct
102 Correct 1062 ms 25192 KB Output is correct
103 Correct 1094 ms 25320 KB Output is correct
104 Correct 915 ms 27220 KB Output is correct
105 Correct 984 ms 27252 KB Output is correct
106 Correct 982 ms 27180 KB Output is correct
107 Correct 1008 ms 24604 KB Output is correct
108 Correct 1020 ms 24472 KB Output is correct
109 Correct 985 ms 24560 KB Output is correct
110 Correct 999 ms 30656 KB Output is correct
111 Correct 758 ms 27016 KB Output is correct
112 Correct 769 ms 26736 KB Output is correct
113 Correct 694 ms 26992 KB Output is correct
114 Correct 999 ms 25420 KB Output is correct
115 Correct 1004 ms 24916 KB Output is correct
116 Correct 975 ms 26764 KB Output is correct
117 Correct 651 ms 26008 KB Output is correct
118 Correct 609 ms 25516 KB Output is correct
119 Correct 576 ms 25336 KB Output is correct
120 Correct 617 ms 25492 KB Output is correct
121 Correct 555 ms 25328 KB Output is correct
122 Correct 535 ms 24932 KB Output is correct
123 Correct 861 ms 26008 KB Output is correct
124 Correct 878 ms 25664 KB Output is correct
125 Correct 884 ms 25312 KB Output is correct
126 Correct 863 ms 25636 KB Output is correct
127 Correct 884 ms 25212 KB Output is correct
128 Correct 944 ms 25108 KB Output is correct