Submission #855196

# Submission time Handle Problem Language Result Execution time Memory
855196 2023-09-30T14:47:29 Z willychan Tourism (JOI23_tourism) C++14
100 / 100
818 ms 19536 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
int n,m,q;
const int N = 1e5+5;
int sz[N];
int hd[N];
int dep[N];
int p[N];
vector<int> side[N];
int t;
void dfs_sz(int cur){
	sz[cur]=1;
	for(int i=0;i<side[cur].size();i++){
		if(!sz[side[cur][i]]){
			dfs_sz(side[cur][i]);
			sz[cur]+=sz[side[cur][i]];
			if(sz[side[cur][i]]>sz[side[cur][0]]){
				swap(side[cur][i],side[cur][0]);
			}
		}
	}
}
void dfs_hd(int cur){
	dep[cur]=++t;
	for(int i=0;i<side[cur].size();i++){
		if(!hd[side[cur][i]]){
			p[side[cur][i]] = cur;
			if(i==0) hd[side[cur][i]] = hd[cur];
			else hd[side[cur][i]] = side[cur][i];
			dfs_hd(side[cur][i]);
		}
	}
}

map<int,int> s;
vector<int> bit;
void modify(int ind,int v){
	if(ind==0) return;
	while(ind<=m){
		bit[ind]+=v;
		ind+=(ind&-ind);
	}
}
int Query(int ind){
	int ans = 0;
	while(ind){
		ans+=bit[ind];
		ind-=(ind&-ind);
	}
	return ans;
}
void setx(int l,int r,int x){
	auto lit = s.upper_bound(l);
	lit = prev(lit);
	int fl = lit->first;
	int fv = lit->second;
	int pv = 0;
	while(lit->first<=r){
		pv = lit->second;
		modify(lit->second,-(next(lit)->first-lit->first));	
		auto k = lit;
		++lit;
		s.erase(k);
	}
	modify(x,r-l+1);
	modify(fv,l-fl);
	if(lit->first!=r+1){
		s[r+1] = pv;
		modify(pv,lit->first-(r+1));
	}
	s[fl] = fv;
	s[l] = x;
}

void cover(int a,int b,int x){
	while(a!=b){
		if(hd[a]==hd[b]){
			int l = dep[a];
			int r = dep[b];
			if(l>r) swap(l,r);
			setx(l,r,x);
			a=b;
		}else{
			if(dep[hd[a]]>dep[hd[b]]) swap(a,b);
			int l = dep[hd[b]];
			int r = dep[b];
			setx(l,r,x);
			b = p[hd[b]];
		}
	}
	setx(dep[a],dep[b],x);
}

int main(){
	ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m>>q;
	bit.resize(m+1);
	s[1]=0;
	s[n+1]=0;
	for(int i=0;i<n-1;i++){
		int a,b;cin>>a>>b;
		side[a].push_back(b);
		side[b].push_back(a);
	}
	p[1]=1;
	hd[1]=1;
	dfs_sz(1);
	dfs_hd(1);
	vector<int> arr(m+1);
	for(int i=1;i<=m;i++) cin>>arr[i];
	vector<pair<int,int>> query(q);
	vector<int> ord(q);
	vector<int> ans(q);
	for(int i=0;i<q;i++){
		cin>>query[i].first>>query[i].second;
		ord[i]=i;
	}
	sort(ord.begin(),ord.end(),[&](const int a,const int b){return query[a].second<query[b].second;});
	int qhead = 0;
	for(int r=1;r<=m;r++){
		if(r>1){
			int u = arr[r-1];
			int v = arr[r];
			cover(u,v,r);
		}else{
			setx(dep[arr[r]],dep[arr[r]],1)	;
		}
		while(qhead<q && query[ord[qhead]].second==r){
			ans[ord[qhead]]=Query(m)-Query(query[ord[qhead]].first);
			qhead++;
		}
	}
	for(int i=0;i<q;i++){
		cout<<max(1,ans[i])<<"\n";
	}
	return 0;
}

Compilation message

tourism.cpp: In function 'void dfs_sz(int)':
tourism.cpp:16:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |  for(int i=0;i<side[cur].size();i++){
      |              ~^~~~~~~~~~~~~~~~~
tourism.cpp: In function 'void dfs_hd(int)':
tourism.cpp:28:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |  for(int i=0;i<side[cur].size();i++){
      |              ~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2908 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2656 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 2 ms 2652 KB Output is correct
11 Correct 2 ms 2816 KB Output is correct
12 Correct 2 ms 2816 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 2 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2648 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2860 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2648 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2908 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2656 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 2 ms 2652 KB Output is correct
11 Correct 2 ms 2816 KB Output is correct
12 Correct 2 ms 2816 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 2 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2648 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2860 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2648 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 5 ms 2908 KB Output is correct
31 Correct 5 ms 2908 KB Output is correct
32 Correct 7 ms 2908 KB Output is correct
33 Correct 7 ms 2920 KB Output is correct
34 Correct 7 ms 2908 KB Output is correct
35 Correct 7 ms 2908 KB Output is correct
36 Correct 7 ms 2908 KB Output is correct
37 Correct 7 ms 2908 KB Output is correct
38 Correct 3 ms 2908 KB Output is correct
39 Correct 3 ms 3092 KB Output is correct
40 Correct 3 ms 3096 KB Output is correct
41 Correct 3 ms 2904 KB Output is correct
42 Correct 3 ms 2908 KB Output is correct
43 Correct 3 ms 2908 KB Output is correct
44 Correct 4 ms 2908 KB Output is correct
45 Correct 4 ms 2908 KB Output is correct
46 Correct 4 ms 2908 KB Output is correct
47 Correct 4 ms 2908 KB Output is correct
48 Correct 4 ms 2908 KB Output is correct
49 Correct 4 ms 3104 KB Output is correct
50 Correct 3 ms 3016 KB Output is correct
51 Correct 3 ms 2908 KB Output is correct
52 Correct 3 ms 3084 KB Output is correct
53 Correct 3 ms 2908 KB Output is correct
54 Correct 3 ms 2908 KB Output is correct
55 Correct 3 ms 2996 KB Output is correct
56 Correct 1 ms 2652 KB Output is correct
57 Correct 2 ms 2908 KB Output is correct
58 Correct 8 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 93 ms 12720 KB Output is correct
5 Correct 71 ms 13400 KB Output is correct
6 Correct 85 ms 14672 KB Output is correct
7 Correct 110 ms 16464 KB Output is correct
8 Correct 116 ms 16724 KB Output is correct
9 Correct 117 ms 16616 KB Output is correct
10 Correct 111 ms 16724 KB Output is correct
11 Correct 111 ms 16468 KB Output is correct
12 Correct 111 ms 16912 KB Output is correct
13 Correct 114 ms 16980 KB Output is correct
14 Correct 110 ms 16924 KB Output is correct
15 Correct 39 ms 15452 KB Output is correct
16 Correct 118 ms 16212 KB Output is correct
17 Correct 40 ms 5200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 272 ms 8592 KB Output is correct
3 Correct 430 ms 9404 KB Output is correct
4 Correct 333 ms 9044 KB Output is correct
5 Correct 521 ms 12148 KB Output is correct
6 Correct 545 ms 12352 KB Output is correct
7 Correct 521 ms 12476 KB Output is correct
8 Correct 517 ms 12076 KB Output is correct
9 Correct 566 ms 12624 KB Output is correct
10 Correct 536 ms 12116 KB Output is correct
11 Correct 521 ms 12244 KB Output is correct
12 Correct 527 ms 12148 KB Output is correct
13 Correct 539 ms 12488 KB Output is correct
14 Correct 556 ms 14772 KB Output is correct
15 Correct 556 ms 16868 KB Output is correct
16 Correct 522 ms 14192 KB Output is correct
17 Correct 529 ms 14264 KB Output is correct
18 Correct 546 ms 14512 KB Output is correct
19 Correct 336 ms 10516 KB Output is correct
20 Correct 329 ms 10368 KB Output is correct
21 Correct 340 ms 10324 KB Output is correct
22 Correct 346 ms 10840 KB Output is correct
23 Correct 372 ms 10356 KB Output is correct
24 Correct 342 ms 10368 KB Output is correct
25 Correct 326 ms 10324 KB Output is correct
26 Correct 346 ms 10576 KB Output is correct
27 Correct 334 ms 10320 KB Output is correct
28 Correct 338 ms 10620 KB Output is correct
29 Correct 334 ms 10452 KB Output is correct
30 Correct 352 ms 10692 KB Output is correct
31 Correct 333 ms 10640 KB Output is correct
32 Correct 333 ms 10952 KB Output is correct
33 Correct 357 ms 11856 KB Output is correct
34 Correct 368 ms 13140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2648 KB Output is correct
3 Correct 2 ms 2648 KB Output is correct
4 Correct 654 ms 11852 KB Output is correct
5 Correct 687 ms 13132 KB Output is correct
6 Correct 784 ms 15812 KB Output is correct
7 Correct 810 ms 17404 KB Output is correct
8 Correct 812 ms 17324 KB Output is correct
9 Correct 806 ms 17428 KB Output is correct
10 Correct 811 ms 17492 KB Output is correct
11 Correct 811 ms 17476 KB Output is correct
12 Correct 806 ms 17408 KB Output is correct
13 Correct 818 ms 17440 KB Output is correct
14 Correct 46 ms 6620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2908 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2656 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 2 ms 2652 KB Output is correct
11 Correct 2 ms 2816 KB Output is correct
12 Correct 2 ms 2816 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 2 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2648 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2860 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2648 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 5 ms 2908 KB Output is correct
31 Correct 5 ms 2908 KB Output is correct
32 Correct 7 ms 2908 KB Output is correct
33 Correct 7 ms 2920 KB Output is correct
34 Correct 7 ms 2908 KB Output is correct
35 Correct 7 ms 2908 KB Output is correct
36 Correct 7 ms 2908 KB Output is correct
37 Correct 7 ms 2908 KB Output is correct
38 Correct 3 ms 2908 KB Output is correct
39 Correct 3 ms 3092 KB Output is correct
40 Correct 3 ms 3096 KB Output is correct
41 Correct 3 ms 2904 KB Output is correct
42 Correct 3 ms 2908 KB Output is correct
43 Correct 3 ms 2908 KB Output is correct
44 Correct 4 ms 2908 KB Output is correct
45 Correct 4 ms 2908 KB Output is correct
46 Correct 4 ms 2908 KB Output is correct
47 Correct 4 ms 2908 KB Output is correct
48 Correct 4 ms 2908 KB Output is correct
49 Correct 4 ms 3104 KB Output is correct
50 Correct 3 ms 3016 KB Output is correct
51 Correct 3 ms 2908 KB Output is correct
52 Correct 3 ms 3084 KB Output is correct
53 Correct 3 ms 2908 KB Output is correct
54 Correct 3 ms 2908 KB Output is correct
55 Correct 3 ms 2996 KB Output is correct
56 Correct 1 ms 2652 KB Output is correct
57 Correct 2 ms 2908 KB Output is correct
58 Correct 8 ms 2908 KB Output is correct
59 Correct 1 ms 2652 KB Output is correct
60 Correct 1 ms 2652 KB Output is correct
61 Correct 2 ms 2652 KB Output is correct
62 Correct 93 ms 12720 KB Output is correct
63 Correct 71 ms 13400 KB Output is correct
64 Correct 85 ms 14672 KB Output is correct
65 Correct 110 ms 16464 KB Output is correct
66 Correct 116 ms 16724 KB Output is correct
67 Correct 117 ms 16616 KB Output is correct
68 Correct 111 ms 16724 KB Output is correct
69 Correct 111 ms 16468 KB Output is correct
70 Correct 111 ms 16912 KB Output is correct
71 Correct 114 ms 16980 KB Output is correct
72 Correct 110 ms 16924 KB Output is correct
73 Correct 39 ms 15452 KB Output is correct
74 Correct 118 ms 16212 KB Output is correct
75 Correct 40 ms 5200 KB Output is correct
76 Correct 1 ms 2652 KB Output is correct
77 Correct 272 ms 8592 KB Output is correct
78 Correct 430 ms 9404 KB Output is correct
79 Correct 333 ms 9044 KB Output is correct
80 Correct 521 ms 12148 KB Output is correct
81 Correct 545 ms 12352 KB Output is correct
82 Correct 521 ms 12476 KB Output is correct
83 Correct 517 ms 12076 KB Output is correct
84 Correct 566 ms 12624 KB Output is correct
85 Correct 536 ms 12116 KB Output is correct
86 Correct 521 ms 12244 KB Output is correct
87 Correct 527 ms 12148 KB Output is correct
88 Correct 539 ms 12488 KB Output is correct
89 Correct 556 ms 14772 KB Output is correct
90 Correct 556 ms 16868 KB Output is correct
91 Correct 522 ms 14192 KB Output is correct
92 Correct 529 ms 14264 KB Output is correct
93 Correct 546 ms 14512 KB Output is correct
94 Correct 336 ms 10516 KB Output is correct
95 Correct 329 ms 10368 KB Output is correct
96 Correct 340 ms 10324 KB Output is correct
97 Correct 346 ms 10840 KB Output is correct
98 Correct 372 ms 10356 KB Output is correct
99 Correct 342 ms 10368 KB Output is correct
100 Correct 326 ms 10324 KB Output is correct
101 Correct 346 ms 10576 KB Output is correct
102 Correct 334 ms 10320 KB Output is correct
103 Correct 338 ms 10620 KB Output is correct
104 Correct 334 ms 10452 KB Output is correct
105 Correct 352 ms 10692 KB Output is correct
106 Correct 333 ms 10640 KB Output is correct
107 Correct 333 ms 10952 KB Output is correct
108 Correct 357 ms 11856 KB Output is correct
109 Correct 368 ms 13140 KB Output is correct
110 Correct 1 ms 2648 KB Output is correct
111 Correct 1 ms 2648 KB Output is correct
112 Correct 2 ms 2648 KB Output is correct
113 Correct 654 ms 11852 KB Output is correct
114 Correct 687 ms 13132 KB Output is correct
115 Correct 784 ms 15812 KB Output is correct
116 Correct 810 ms 17404 KB Output is correct
117 Correct 812 ms 17324 KB Output is correct
118 Correct 806 ms 17428 KB Output is correct
119 Correct 811 ms 17492 KB Output is correct
120 Correct 811 ms 17476 KB Output is correct
121 Correct 806 ms 17408 KB Output is correct
122 Correct 818 ms 17440 KB Output is correct
123 Correct 46 ms 6620 KB Output is correct
124 Correct 471 ms 16720 KB Output is correct
125 Correct 406 ms 14616 KB Output is correct
126 Correct 559 ms 17236 KB Output is correct
127 Correct 561 ms 17192 KB Output is correct
128 Correct 583 ms 17168 KB Output is correct
129 Correct 562 ms 17252 KB Output is correct
130 Correct 557 ms 17236 KB Output is correct
131 Correct 131 ms 18620 KB Output is correct
132 Correct 127 ms 19536 KB Output is correct
133 Correct 129 ms 16468 KB Output is correct
134 Correct 348 ms 13880 KB Output is correct
135 Correct 355 ms 13492 KB Output is correct
136 Correct 364 ms 13988 KB Output is correct
137 Correct 170 ms 17756 KB Output is correct
138 Correct 170 ms 17692 KB Output is correct
139 Correct 171 ms 17752 KB Output is correct
140 Correct 171 ms 17568 KB Output is correct
141 Correct 167 ms 17608 KB Output is correct
142 Correct 175 ms 17692 KB Output is correct
143 Correct 41 ms 10892 KB Output is correct
144 Correct 580 ms 17196 KB Output is correct