Submission #411794

# Submission time Handle Problem Language Result Execution time Memory
411794 2021-05-26T01:40:47 Z jamezzz Construction of Highway (JOI18_construction) C++14
100 / 100
1802 ms 85644 KB
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#include <ext/rope>
using namespace __gnu_cxx;

typedef tree<long long, null_type, less<long long>,
rb_tree_tag, tree_order_statistics_node_update> pbds;
//less_equal for identical elements

//#define DEBUG

#ifdef DEBUG
#define debug(...) printf(__VA_ARGS__);
#else
#define debug(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb emplace_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;

#define maxn 100005

int n,c[maxn],a[maxn],b[maxn];
int heavy[maxn],head[maxn],par[maxn],pos[maxn],cur=1;
int ft[maxn];
vi AL[maxn];
vii disc;


struct thing{ int pos,len,val; };

deque<thing> dq[maxn];


int dfs(int u){
	int mx=0,tsz=1;
	for(int v:AL[u]){
		if(v==par[u])continue;
		par[v]=u;
		int sz=dfs(v);
		tsz+=sz;
		if(sz>mx){
			mx=sz;heavy[u]=v;
		}
	}
	return tsz;
}

void decompose(int u,int h){
	head[u]=h;
	pos[u]=cur++;
	if(heavy[u]!=0)decompose(heavy[u],h);
	for(int v:AL[u]){
		if(v==par[u])continue;
		if(v==heavy[u])continue;
		decompose(v,v);
	}
}

void up(int x,int nv){
	while(x<=n)ft[x]+=nv,x+=x&-x;
}

int qry(int x){
	int res=0;
	while(x)res+=ft[x],x-=x&-x;
	return res;
}

ll path(int x){
	memset(ft,0,sizeof ft);
	ll res=0;
	while(x){
		int last=0,hd=head[x];
		for(int i=0;i<sz(dq[hd]);++i){
			thing t=dq[hd][i];
			if(t.pos+t.len-1>=pos[x]){
				last=i;break;
			}
		}
		for(int i=last;i>=0;--i){
			thing t=dq[hd][i];
			int ss=t.pos,ee=min(t.pos+t.len-1,pos[x]);
			int len=ee-ss+1;
			res+=qry(t.val)*len;
			up(t.val+1,len);
		}
		x=par[hd];
	}
	return res;
}

void add_edge(int x,int y){
	dq[head[y]].clear();
	dq[head[y]].push_back({pos[head[y]],pos[y]-pos[head[y]]+1,c[y]});
	int cur=par[head[y]];
	while(cur){
		int last=0,hd=head[cur];
		for(int i=0;i<sz(dq[hd]);++i){
			thing t=dq[hd][i];
			if(t.pos+t.len-1>=pos[cur]){
				last=i;break;
			}
		}
		for(int i=0;i<last;++i)dq[hd].pop_front();
		thing t=dq[hd].front();
		dq[hd].pop_front();
		int ss=pos[cur]+1,ee=t.pos+t.len-1;
		if(ss<=ee)dq[hd].push_front({ss,ee-ss+1,t.val});
		dq[hd].push_front({pos[hd],pos[cur]-pos[hd]+1,c[y]});
		cur=par[hd];
	}
}

int main(){
	sf("%d",&n);
	for(int i=1;i<=n;++i){
		sf("%d",&c[i]);
		disc.pb(c[i],i);
	}
	sort(all(disc));
	int cnt=0,pv=-1;
	for(int i=0;i<n;++i){
		if(disc[i].fi!=pv)++cnt;
		pv=disc[i].fi;
		c[disc[i].se]=cnt;
	}
	for(int i=0;i<n-1;++i){
		sf("%d%d",&a[i],&b[i]);
		AL[a[i]].pb(b[i]);
		AL[b[i]].pb(a[i]);
	}
	
	dfs(1);decompose(1,1);
	dq[1].push_back({1,1,c[1]});

	for(int i=0;i<n-1;++i){
		pf("%lld\n",path(a[i]));
		add_edge(a[i],b[i]);
	}
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:136:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |  sf("%d",&n);
      |    ^
construction.cpp:138:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  138 |   sf("%d",&c[i]);
      |     ^
construction.cpp:149:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  149 |   sf("%d%d",&a[i],&b[i]);
      |     ^
# Verdict Execution time Memory Grader output
1 Correct 46 ms 68804 KB Output is correct
2 Correct 45 ms 68756 KB Output is correct
3 Correct 49 ms 68756 KB Output is correct
4 Correct 52 ms 68848 KB Output is correct
5 Correct 56 ms 68812 KB Output is correct
6 Correct 56 ms 68824 KB Output is correct
7 Correct 58 ms 68804 KB Output is correct
8 Correct 52 ms 68780 KB Output is correct
9 Correct 59 ms 68880 KB Output is correct
10 Correct 52 ms 68892 KB Output is correct
11 Correct 52 ms 68868 KB Output is correct
12 Correct 59 ms 68808 KB Output is correct
13 Correct 52 ms 68872 KB Output is correct
14 Correct 54 ms 68928 KB Output is correct
15 Correct 57 ms 68804 KB Output is correct
16 Correct 55 ms 68804 KB Output is correct
17 Correct 65 ms 68904 KB Output is correct
18 Correct 56 ms 68820 KB Output is correct
19 Correct 52 ms 68804 KB Output is correct
20 Correct 55 ms 68796 KB Output is correct
21 Correct 55 ms 68808 KB Output is correct
22 Correct 57 ms 68864 KB Output is correct
23 Correct 56 ms 68804 KB Output is correct
24 Correct 54 ms 68860 KB Output is correct
25 Correct 56 ms 68876 KB Output is correct
26 Correct 52 ms 68844 KB Output is correct
27 Correct 52 ms 68904 KB Output is correct
28 Correct 51 ms 68876 KB Output is correct
29 Correct 54 ms 68804 KB Output is correct
30 Correct 55 ms 68776 KB Output is correct
31 Correct 55 ms 68776 KB Output is correct
32 Correct 55 ms 68864 KB Output is correct
33 Correct 54 ms 68808 KB Output is correct
34 Correct 60 ms 68932 KB Output is correct
35 Correct 63 ms 68804 KB Output is correct
36 Correct 59 ms 68852 KB Output is correct
37 Correct 54 ms 68864 KB Output is correct
38 Correct 55 ms 68752 KB Output is correct
39 Correct 54 ms 68804 KB Output is correct
40 Correct 54 ms 68772 KB Output is correct
41 Correct 57 ms 68760 KB Output is correct
42 Correct 54 ms 68864 KB Output is correct
43 Correct 55 ms 68812 KB Output is correct
44 Correct 55 ms 68804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 68804 KB Output is correct
2 Correct 45 ms 68756 KB Output is correct
3 Correct 49 ms 68756 KB Output is correct
4 Correct 52 ms 68848 KB Output is correct
5 Correct 56 ms 68812 KB Output is correct
6 Correct 56 ms 68824 KB Output is correct
7 Correct 58 ms 68804 KB Output is correct
8 Correct 52 ms 68780 KB Output is correct
9 Correct 59 ms 68880 KB Output is correct
10 Correct 52 ms 68892 KB Output is correct
11 Correct 52 ms 68868 KB Output is correct
12 Correct 59 ms 68808 KB Output is correct
13 Correct 52 ms 68872 KB Output is correct
14 Correct 54 ms 68928 KB Output is correct
15 Correct 57 ms 68804 KB Output is correct
16 Correct 55 ms 68804 KB Output is correct
17 Correct 65 ms 68904 KB Output is correct
18 Correct 56 ms 68820 KB Output is correct
19 Correct 52 ms 68804 KB Output is correct
20 Correct 55 ms 68796 KB Output is correct
21 Correct 55 ms 68808 KB Output is correct
22 Correct 57 ms 68864 KB Output is correct
23 Correct 56 ms 68804 KB Output is correct
24 Correct 54 ms 68860 KB Output is correct
25 Correct 56 ms 68876 KB Output is correct
26 Correct 52 ms 68844 KB Output is correct
27 Correct 52 ms 68904 KB Output is correct
28 Correct 51 ms 68876 KB Output is correct
29 Correct 54 ms 68804 KB Output is correct
30 Correct 55 ms 68776 KB Output is correct
31 Correct 55 ms 68776 KB Output is correct
32 Correct 55 ms 68864 KB Output is correct
33 Correct 54 ms 68808 KB Output is correct
34 Correct 60 ms 68932 KB Output is correct
35 Correct 63 ms 68804 KB Output is correct
36 Correct 59 ms 68852 KB Output is correct
37 Correct 54 ms 68864 KB Output is correct
38 Correct 55 ms 68752 KB Output is correct
39 Correct 54 ms 68804 KB Output is correct
40 Correct 54 ms 68772 KB Output is correct
41 Correct 57 ms 68760 KB Output is correct
42 Correct 54 ms 68864 KB Output is correct
43 Correct 55 ms 68812 KB Output is correct
44 Correct 55 ms 68804 KB Output is correct
45 Correct 68 ms 68844 KB Output is correct
46 Correct 117 ms 69260 KB Output is correct
47 Correct 110 ms 69244 KB Output is correct
48 Correct 108 ms 69292 KB Output is correct
49 Correct 101 ms 69444 KB Output is correct
50 Correct 105 ms 69424 KB Output is correct
51 Correct 103 ms 69436 KB Output is correct
52 Correct 107 ms 69320 KB Output is correct
53 Correct 117 ms 69336 KB Output is correct
54 Correct 114 ms 69320 KB Output is correct
55 Correct 114 ms 69316 KB Output is correct
56 Correct 107 ms 69308 KB Output is correct
57 Correct 117 ms 69440 KB Output is correct
58 Correct 122 ms 69480 KB Output is correct
59 Correct 115 ms 69456 KB Output is correct
60 Correct 116 ms 69420 KB Output is correct
61 Correct 104 ms 69208 KB Output is correct
62 Correct 112 ms 69212 KB Output is correct
63 Correct 106 ms 69328 KB Output is correct
64 Correct 112 ms 69188 KB Output is correct
65 Correct 108 ms 69240 KB Output is correct
66 Correct 109 ms 69268 KB Output is correct
67 Correct 108 ms 69188 KB Output is correct
68 Correct 106 ms 69444 KB Output is correct
69 Correct 105 ms 69316 KB Output is correct
70 Correct 102 ms 69316 KB Output is correct
71 Correct 111 ms 69296 KB Output is correct
72 Correct 116 ms 69496 KB Output is correct
73 Correct 112 ms 69560 KB Output is correct
74 Correct 110 ms 69192 KB Output is correct
75 Correct 109 ms 69220 KB Output is correct
76 Correct 110 ms 69196 KB Output is correct
77 Correct 111 ms 69168 KB Output is correct
78 Correct 107 ms 69204 KB Output is correct
79 Correct 105 ms 69188 KB Output is correct
80 Correct 108 ms 69124 KB Output is correct
81 Correct 111 ms 69204 KB Output is correct
82 Correct 107 ms 69176 KB Output is correct
83 Correct 115 ms 69192 KB Output is correct
84 Correct 107 ms 69204 KB Output is correct
85 Correct 106 ms 69184 KB Output is correct
86 Correct 114 ms 69060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 68804 KB Output is correct
2 Correct 45 ms 68756 KB Output is correct
3 Correct 49 ms 68756 KB Output is correct
4 Correct 52 ms 68848 KB Output is correct
5 Correct 56 ms 68812 KB Output is correct
6 Correct 56 ms 68824 KB Output is correct
7 Correct 58 ms 68804 KB Output is correct
8 Correct 52 ms 68780 KB Output is correct
9 Correct 59 ms 68880 KB Output is correct
10 Correct 52 ms 68892 KB Output is correct
11 Correct 52 ms 68868 KB Output is correct
12 Correct 59 ms 68808 KB Output is correct
13 Correct 52 ms 68872 KB Output is correct
14 Correct 54 ms 68928 KB Output is correct
15 Correct 57 ms 68804 KB Output is correct
16 Correct 55 ms 68804 KB Output is correct
17 Correct 65 ms 68904 KB Output is correct
18 Correct 56 ms 68820 KB Output is correct
19 Correct 52 ms 68804 KB Output is correct
20 Correct 55 ms 68796 KB Output is correct
21 Correct 55 ms 68808 KB Output is correct
22 Correct 57 ms 68864 KB Output is correct
23 Correct 56 ms 68804 KB Output is correct
24 Correct 54 ms 68860 KB Output is correct
25 Correct 56 ms 68876 KB Output is correct
26 Correct 52 ms 68844 KB Output is correct
27 Correct 52 ms 68904 KB Output is correct
28 Correct 51 ms 68876 KB Output is correct
29 Correct 54 ms 68804 KB Output is correct
30 Correct 55 ms 68776 KB Output is correct
31 Correct 55 ms 68776 KB Output is correct
32 Correct 55 ms 68864 KB Output is correct
33 Correct 54 ms 68808 KB Output is correct
34 Correct 60 ms 68932 KB Output is correct
35 Correct 63 ms 68804 KB Output is correct
36 Correct 59 ms 68852 KB Output is correct
37 Correct 54 ms 68864 KB Output is correct
38 Correct 55 ms 68752 KB Output is correct
39 Correct 54 ms 68804 KB Output is correct
40 Correct 54 ms 68772 KB Output is correct
41 Correct 57 ms 68760 KB Output is correct
42 Correct 54 ms 68864 KB Output is correct
43 Correct 55 ms 68812 KB Output is correct
44 Correct 55 ms 68804 KB Output is correct
45 Correct 68 ms 68844 KB Output is correct
46 Correct 117 ms 69260 KB Output is correct
47 Correct 110 ms 69244 KB Output is correct
48 Correct 108 ms 69292 KB Output is correct
49 Correct 101 ms 69444 KB Output is correct
50 Correct 105 ms 69424 KB Output is correct
51 Correct 103 ms 69436 KB Output is correct
52 Correct 107 ms 69320 KB Output is correct
53 Correct 117 ms 69336 KB Output is correct
54 Correct 114 ms 69320 KB Output is correct
55 Correct 114 ms 69316 KB Output is correct
56 Correct 107 ms 69308 KB Output is correct
57 Correct 117 ms 69440 KB Output is correct
58 Correct 122 ms 69480 KB Output is correct
59 Correct 115 ms 69456 KB Output is correct
60 Correct 116 ms 69420 KB Output is correct
61 Correct 104 ms 69208 KB Output is correct
62 Correct 112 ms 69212 KB Output is correct
63 Correct 106 ms 69328 KB Output is correct
64 Correct 112 ms 69188 KB Output is correct
65 Correct 108 ms 69240 KB Output is correct
66 Correct 109 ms 69268 KB Output is correct
67 Correct 108 ms 69188 KB Output is correct
68 Correct 106 ms 69444 KB Output is correct
69 Correct 105 ms 69316 KB Output is correct
70 Correct 102 ms 69316 KB Output is correct
71 Correct 111 ms 69296 KB Output is correct
72 Correct 116 ms 69496 KB Output is correct
73 Correct 112 ms 69560 KB Output is correct
74 Correct 110 ms 69192 KB Output is correct
75 Correct 109 ms 69220 KB Output is correct
76 Correct 110 ms 69196 KB Output is correct
77 Correct 111 ms 69168 KB Output is correct
78 Correct 107 ms 69204 KB Output is correct
79 Correct 105 ms 69188 KB Output is correct
80 Correct 108 ms 69124 KB Output is correct
81 Correct 111 ms 69204 KB Output is correct
82 Correct 107 ms 69176 KB Output is correct
83 Correct 115 ms 69192 KB Output is correct
84 Correct 107 ms 69204 KB Output is correct
85 Correct 106 ms 69184 KB Output is correct
86 Correct 114 ms 69060 KB Output is correct
87 Correct 207 ms 70064 KB Output is correct
88 Correct 525 ms 72596 KB Output is correct
89 Correct 1680 ms 81696 KB Output is correct
90 Correct 1718 ms 81688 KB Output is correct
91 Correct 1686 ms 81692 KB Output is correct
92 Correct 1499 ms 85552 KB Output is correct
93 Correct 1528 ms 85496 KB Output is correct
94 Correct 1526 ms 85644 KB Output is correct
95 Correct 1509 ms 81896 KB Output is correct
96 Correct 1515 ms 82412 KB Output is correct
97 Correct 1552 ms 82260 KB Output is correct
98 Correct 1502 ms 82192 KB Output is correct
99 Correct 1568 ms 81772 KB Output is correct
100 Correct 1758 ms 83996 KB Output is correct
101 Correct 1781 ms 83968 KB Output is correct
102 Correct 1761 ms 84024 KB Output is correct
103 Correct 1802 ms 84040 KB Output is correct
104 Correct 1555 ms 81684 KB Output is correct
105 Correct 1549 ms 81768 KB Output is correct
106 Correct 1573 ms 81744 KB Output is correct
107 Correct 1654 ms 80952 KB Output is correct
108 Correct 1660 ms 81100 KB Output is correct
109 Correct 1718 ms 81232 KB Output is correct
110 Correct 1501 ms 84976 KB Output is correct
111 Correct 1502 ms 81788 KB Output is correct
112 Correct 1534 ms 81544 KB Output is correct
113 Correct 1546 ms 81084 KB Output is correct
114 Correct 1755 ms 83832 KB Output is correct
115 Correct 1743 ms 83464 KB Output is correct
116 Correct 1568 ms 81312 KB Output is correct
117 Correct 1521 ms 79556 KB Output is correct
118 Correct 1535 ms 78908 KB Output is correct
119 Correct 1533 ms 78528 KB Output is correct
120 Correct 1511 ms 79428 KB Output is correct
121 Correct 1545 ms 78536 KB Output is correct
122 Correct 1522 ms 78208 KB Output is correct
123 Correct 1527 ms 79588 KB Output is correct
124 Correct 1554 ms 79084 KB Output is correct
125 Correct 1524 ms 78756 KB Output is correct
126 Correct 1505 ms 79352 KB Output is correct
127 Correct 1597 ms 78500 KB Output is correct
128 Correct 1537 ms 78404 KB Output is correct