Submission #1037623

# Submission time Handle Problem Language Result Execution time Memory
1037623 2024-07-29T06:05:54 Z ㅇㅇ(223.39)(#11011) Tourism (JOI23_tourism) C++17
100 / 100
641 ms 147764 KB
#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
using ll=long long;
using pii=array<int,2>;
using tii=array<int,3>;
const int N=100005,inf=1000000000;
int n,m,q,c[N];
vector<int> g[N],L[N];
set<tii> S[N],T[N];
int add[N];
vector<pii> rect[N],qry[N];
int ans[N];
void ins(tii t,int add){
	t[2]+=add;
	if(!t[2]) return;
	rect[t[0]].push_back({t[1],t[2]});
}
void upd(int u,int l,int r){
	auto iter=S[u].lower_bound({l,l});
	if(iter!=S[u].begin()&&iter!=S[u].end()){
		auto [Ll,Lr,Lv]=*prev(iter);
		auto [Rl,Rr,Rv]=*iter;
		auto iter2=T[u].lower_bound({Ll,-inf,-inf});
		ins(*iter2,add[u]);
		T[u].erase(iter2);
		int s=l,e=r;
		if(Lr+1==l){
			ins({Ll,Lr,Lv},add[u]);
			S[u].erase({Ll,Lr,Lv});
			s=Ll;
		} else{
			T[u].insert({Lr+1,l-1,-add[u]});
		}
		if(r+1==Rl){
			ins({Rl,Rr,Rv},add[u]);
			S[u].erase({Rl,Rr,Rv});
			e=Rr;
		} else{
			T[u].insert({r+1,Rl-1,-add[u]});
		}
		S[u].insert({s,e,-add[u]});
	} else if(iter!=S[u].begin()){
		auto [Ll,Lr,Lv]=*prev(iter);
		auto iter2=T[u].lower_bound({Ll,-inf,-inf});
		ins(*iter2,add[u]);
		T[u].erase(iter2);
		int s=l,e=r;
		if(Lr+1==l){
			ins({Ll,Lr,Lv},add[u]);
			S[u].erase({Ll,Lr,Lv});
			s=Ll;
		} else{
			T[u].insert({Lr+1,l-1,-add[u]});
		}
		if(r+1<=m) T[u].insert({r+1,m,-add[u]});
		S[u].insert({s,e,-add[u]});
	} else if(iter!=S[u].end()){
		auto [Rl,Rr,Rv]=*iter;
		auto iter2=T[u].lower_bound({1,-inf,-inf});
		ins(*iter2,add[u]);
		T[u].erase(iter2);
		int s=l,e=r;
		if(1<=l-1) T[u].insert({1,l-1,-add[u]});
		if(r+1==Rl){
			ins({Rl,Rr,Rv},add[u]);
			S[u].erase({Rl,Rr,Rv});
			e=Rr;
		} else{
			T[u].insert({r+1,Rl-1,-add[u]});
		}
		S[u].insert({s,e,-add[u]});
	} else{
		auto iter2=T[u].lower_bound({1,-inf,-inf});
		ins(*iter2,add[u]);
		T[u].erase(iter2);
		int s=l,e=r;
		if(1<=l-1) T[u].insert({1,l-1,-add[u]});
		if(r+1<=m) T[u].insert({r+1,m,-add[u]});
		S[u].insert({s,e,-add[u]});
	}
}
void dfs(int u,int p){
	T[u].insert({1,m,0});
	for(int i: L[u]) upd(u,i,i);
	for(int v: g[u]) if(p!=v){
		dfs(v,u);
		if(S[u].size()<S[v].size()){
			swap(S[u],S[v]);
			swap(T[u],T[v]);
			swap(add[u],add[v]);
		}
		for(tii t: S[v]){
			ins(t,add[v]);
			upd(u,t[0],t[1]);
		}
		for(tii t: T[v]){
			ins(t,add[v]);
		}
	}
	if(p) add[u]++;
}
struct FEN{
	ll F[N]={0};
	void upd(int i,int v){
		i=m+1-i;
		for(;i<=m;i+=i&-i) F[i]+=v;
	}
	ll qry(int i){
		i=m+1-i;
		ll r=0;
		for(;i;i-=i&-i) r+=F[i];
		return r;
	}
}F;
int main(){
	ios::sync_with_stdio(false); cin.tie(0);
	cin>>n>>m>>q;
	for(int u,v,i=1;i<n;i++){
		cin>>u>>v;
		g[u].emplace_back(v);
		g[v].emplace_back(u);
	}
	for(int i=1;i<=m;i++){
		cin>>c[i];
		L[c[i]].push_back(i);
	}
	for(int l,r,i=1;i<=q;i++){
		cin>>l>>r;
		qry[l].push_back({i,r});
	}
	dfs(1,0);
	for(tii t: S[1]){
		ins(t,add[1]);
	}
	for(tii t: T[1]){
		ins(t,add[1]);
	}
	for(int i=1;i<=m;i++){
		for(auto [j,v]: rect[i]) F.upd(j,v);
		for(auto [j,k]: qry[i]) ans[j]=n-1-F.qry(k)+1;
	}
	for(int i=1;i<=q;i++) cout<<ans[i]<<"\n";
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 20824 KB Output is correct
2 Correct 5 ms 20828 KB Output is correct
3 Correct 6 ms 20912 KB Output is correct
4 Correct 5 ms 21088 KB Output is correct
5 Correct 5 ms 20828 KB Output is correct
6 Correct 6 ms 20828 KB Output is correct
7 Correct 5 ms 20828 KB Output is correct
8 Correct 6 ms 20944 KB Output is correct
9 Correct 5 ms 21084 KB Output is correct
10 Correct 6 ms 21080 KB Output is correct
11 Correct 5 ms 21084 KB Output is correct
12 Correct 6 ms 21084 KB Output is correct
13 Correct 5 ms 21132 KB Output is correct
14 Correct 5 ms 20940 KB Output is correct
15 Correct 5 ms 21084 KB Output is correct
16 Correct 5 ms 21084 KB Output is correct
17 Correct 5 ms 20884 KB Output is correct
18 Correct 6 ms 21084 KB Output is correct
19 Correct 6 ms 21084 KB Output is correct
20 Correct 5 ms 21084 KB Output is correct
21 Correct 5 ms 21084 KB Output is correct
22 Correct 5 ms 21084 KB Output is correct
23 Correct 5 ms 21084 KB Output is correct
24 Correct 5 ms 21084 KB Output is correct
25 Correct 5 ms 21084 KB Output is correct
26 Correct 5 ms 21084 KB Output is correct
27 Correct 4 ms 20984 KB Output is correct
28 Correct 5 ms 20828 KB Output is correct
29 Correct 5 ms 21084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 20824 KB Output is correct
2 Correct 5 ms 20828 KB Output is correct
3 Correct 6 ms 20912 KB Output is correct
4 Correct 5 ms 21088 KB Output is correct
5 Correct 5 ms 20828 KB Output is correct
6 Correct 6 ms 20828 KB Output is correct
7 Correct 5 ms 20828 KB Output is correct
8 Correct 6 ms 20944 KB Output is correct
9 Correct 5 ms 21084 KB Output is correct
10 Correct 6 ms 21080 KB Output is correct
11 Correct 5 ms 21084 KB Output is correct
12 Correct 6 ms 21084 KB Output is correct
13 Correct 5 ms 21132 KB Output is correct
14 Correct 5 ms 20940 KB Output is correct
15 Correct 5 ms 21084 KB Output is correct
16 Correct 5 ms 21084 KB Output is correct
17 Correct 5 ms 20884 KB Output is correct
18 Correct 6 ms 21084 KB Output is correct
19 Correct 6 ms 21084 KB Output is correct
20 Correct 5 ms 21084 KB Output is correct
21 Correct 5 ms 21084 KB Output is correct
22 Correct 5 ms 21084 KB Output is correct
23 Correct 5 ms 21084 KB Output is correct
24 Correct 5 ms 21084 KB Output is correct
25 Correct 5 ms 21084 KB Output is correct
26 Correct 5 ms 21084 KB Output is correct
27 Correct 4 ms 20984 KB Output is correct
28 Correct 5 ms 20828 KB Output is correct
29 Correct 5 ms 21084 KB Output is correct
30 Correct 10 ms 21848 KB Output is correct
31 Correct 8 ms 21852 KB Output is correct
32 Correct 9 ms 22108 KB Output is correct
33 Correct 10 ms 22108 KB Output is correct
34 Correct 9 ms 22108 KB Output is correct
35 Correct 9 ms 22108 KB Output is correct
36 Correct 9 ms 22184 KB Output is correct
37 Correct 10 ms 22108 KB Output is correct
38 Correct 8 ms 21968 KB Output is correct
39 Correct 7 ms 21852 KB Output is correct
40 Correct 7 ms 21852 KB Output is correct
41 Correct 7 ms 21852 KB Output is correct
42 Correct 7 ms 21848 KB Output is correct
43 Correct 7 ms 21724 KB Output is correct
44 Correct 8 ms 21884 KB Output is correct
45 Correct 8 ms 21852 KB Output is correct
46 Correct 8 ms 21768 KB Output is correct
47 Correct 9 ms 21848 KB Output is correct
48 Correct 9 ms 21848 KB Output is correct
49 Correct 8 ms 21852 KB Output is correct
50 Correct 7 ms 21596 KB Output is correct
51 Correct 7 ms 21592 KB Output is correct
52 Correct 8 ms 21848 KB Output is correct
53 Correct 7 ms 21596 KB Output is correct
54 Correct 7 ms 21592 KB Output is correct
55 Correct 7 ms 21596 KB Output is correct
56 Correct 5 ms 20828 KB Output is correct
57 Correct 5 ms 21080 KB Output is correct
58 Correct 12 ms 22360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 20824 KB Output is correct
2 Correct 4 ms 20824 KB Output is correct
3 Correct 5 ms 20828 KB Output is correct
4 Correct 171 ms 56172 KB Output is correct
5 Correct 128 ms 56300 KB Output is correct
6 Correct 163 ms 64080 KB Output is correct
7 Correct 241 ms 71508 KB Output is correct
8 Correct 201 ms 71504 KB Output is correct
9 Correct 200 ms 71540 KB Output is correct
10 Correct 208 ms 71504 KB Output is correct
11 Correct 207 ms 71632 KB Output is correct
12 Correct 133 ms 68688 KB Output is correct
13 Correct 151 ms 68528 KB Output is correct
14 Correct 128 ms 68724 KB Output is correct
15 Correct 48 ms 49352 KB Output is correct
16 Correct 199 ms 71112 KB Output is correct
17 Correct 36 ms 24784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 20824 KB Output is correct
2 Correct 215 ms 63948 KB Output is correct
3 Correct 375 ms 87664 KB Output is correct
4 Correct 272 ms 73416 KB Output is correct
5 Correct 458 ms 103624 KB Output is correct
6 Correct 465 ms 102088 KB Output is correct
7 Correct 524 ms 101484 KB Output is correct
8 Correct 492 ms 100792 KB Output is correct
9 Correct 509 ms 102532 KB Output is correct
10 Correct 494 ms 102692 KB Output is correct
11 Correct 467 ms 101408 KB Output is correct
12 Correct 479 ms 102524 KB Output is correct
13 Correct 429 ms 102092 KB Output is correct
14 Correct 474 ms 103672 KB Output is correct
15 Correct 461 ms 108228 KB Output is correct
16 Correct 453 ms 99784 KB Output is correct
17 Correct 440 ms 102100 KB Output is correct
18 Correct 438 ms 100148 KB Output is correct
19 Correct 361 ms 86692 KB Output is correct
20 Correct 373 ms 86096 KB Output is correct
21 Correct 363 ms 88292 KB Output is correct
22 Correct 368 ms 86800 KB Output is correct
23 Correct 356 ms 85332 KB Output is correct
24 Correct 401 ms 86352 KB Output is correct
25 Correct 370 ms 86608 KB Output is correct
26 Correct 406 ms 86864 KB Output is correct
27 Correct 384 ms 86196 KB Output is correct
28 Correct 347 ms 87376 KB Output is correct
29 Correct 375 ms 87124 KB Output is correct
30 Correct 368 ms 88656 KB Output is correct
31 Correct 371 ms 87328 KB Output is correct
32 Correct 368 ms 89428 KB Output is correct
33 Correct 460 ms 93376 KB Output is correct
34 Correct 374 ms 89428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 20824 KB Output is correct
2 Correct 4 ms 20948 KB Output is correct
3 Correct 5 ms 20828 KB Output is correct
4 Correct 481 ms 126412 KB Output is correct
5 Correct 511 ms 129992 KB Output is correct
6 Correct 569 ms 143256 KB Output is correct
7 Correct 621 ms 147392 KB Output is correct
8 Correct 574 ms 147764 KB Output is correct
9 Correct 602 ms 147572 KB Output is correct
10 Correct 569 ms 147648 KB Output is correct
11 Correct 564 ms 147604 KB Output is correct
12 Correct 584 ms 147648 KB Output is correct
13 Correct 641 ms 147652 KB Output is correct
14 Correct 35 ms 24784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 20824 KB Output is correct
2 Correct 5 ms 20828 KB Output is correct
3 Correct 6 ms 20912 KB Output is correct
4 Correct 5 ms 21088 KB Output is correct
5 Correct 5 ms 20828 KB Output is correct
6 Correct 6 ms 20828 KB Output is correct
7 Correct 5 ms 20828 KB Output is correct
8 Correct 6 ms 20944 KB Output is correct
9 Correct 5 ms 21084 KB Output is correct
10 Correct 6 ms 21080 KB Output is correct
11 Correct 5 ms 21084 KB Output is correct
12 Correct 6 ms 21084 KB Output is correct
13 Correct 5 ms 21132 KB Output is correct
14 Correct 5 ms 20940 KB Output is correct
15 Correct 5 ms 21084 KB Output is correct
16 Correct 5 ms 21084 KB Output is correct
17 Correct 5 ms 20884 KB Output is correct
18 Correct 6 ms 21084 KB Output is correct
19 Correct 6 ms 21084 KB Output is correct
20 Correct 5 ms 21084 KB Output is correct
21 Correct 5 ms 21084 KB Output is correct
22 Correct 5 ms 21084 KB Output is correct
23 Correct 5 ms 21084 KB Output is correct
24 Correct 5 ms 21084 KB Output is correct
25 Correct 5 ms 21084 KB Output is correct
26 Correct 5 ms 21084 KB Output is correct
27 Correct 4 ms 20984 KB Output is correct
28 Correct 5 ms 20828 KB Output is correct
29 Correct 5 ms 21084 KB Output is correct
30 Correct 10 ms 21848 KB Output is correct
31 Correct 8 ms 21852 KB Output is correct
32 Correct 9 ms 22108 KB Output is correct
33 Correct 10 ms 22108 KB Output is correct
34 Correct 9 ms 22108 KB Output is correct
35 Correct 9 ms 22108 KB Output is correct
36 Correct 9 ms 22184 KB Output is correct
37 Correct 10 ms 22108 KB Output is correct
38 Correct 8 ms 21968 KB Output is correct
39 Correct 7 ms 21852 KB Output is correct
40 Correct 7 ms 21852 KB Output is correct
41 Correct 7 ms 21852 KB Output is correct
42 Correct 7 ms 21848 KB Output is correct
43 Correct 7 ms 21724 KB Output is correct
44 Correct 8 ms 21884 KB Output is correct
45 Correct 8 ms 21852 KB Output is correct
46 Correct 8 ms 21768 KB Output is correct
47 Correct 9 ms 21848 KB Output is correct
48 Correct 9 ms 21848 KB Output is correct
49 Correct 8 ms 21852 KB Output is correct
50 Correct 7 ms 21596 KB Output is correct
51 Correct 7 ms 21592 KB Output is correct
52 Correct 8 ms 21848 KB Output is correct
53 Correct 7 ms 21596 KB Output is correct
54 Correct 7 ms 21592 KB Output is correct
55 Correct 7 ms 21596 KB Output is correct
56 Correct 5 ms 20828 KB Output is correct
57 Correct 5 ms 21080 KB Output is correct
58 Correct 12 ms 22360 KB Output is correct
59 Correct 4 ms 20824 KB Output is correct
60 Correct 4 ms 20824 KB Output is correct
61 Correct 5 ms 20828 KB Output is correct
62 Correct 171 ms 56172 KB Output is correct
63 Correct 128 ms 56300 KB Output is correct
64 Correct 163 ms 64080 KB Output is correct
65 Correct 241 ms 71508 KB Output is correct
66 Correct 201 ms 71504 KB Output is correct
67 Correct 200 ms 71540 KB Output is correct
68 Correct 208 ms 71504 KB Output is correct
69 Correct 207 ms 71632 KB Output is correct
70 Correct 133 ms 68688 KB Output is correct
71 Correct 151 ms 68528 KB Output is correct
72 Correct 128 ms 68724 KB Output is correct
73 Correct 48 ms 49352 KB Output is correct
74 Correct 199 ms 71112 KB Output is correct
75 Correct 36 ms 24784 KB Output is correct
76 Correct 5 ms 20824 KB Output is correct
77 Correct 215 ms 63948 KB Output is correct
78 Correct 375 ms 87664 KB Output is correct
79 Correct 272 ms 73416 KB Output is correct
80 Correct 458 ms 103624 KB Output is correct
81 Correct 465 ms 102088 KB Output is correct
82 Correct 524 ms 101484 KB Output is correct
83 Correct 492 ms 100792 KB Output is correct
84 Correct 509 ms 102532 KB Output is correct
85 Correct 494 ms 102692 KB Output is correct
86 Correct 467 ms 101408 KB Output is correct
87 Correct 479 ms 102524 KB Output is correct
88 Correct 429 ms 102092 KB Output is correct
89 Correct 474 ms 103672 KB Output is correct
90 Correct 461 ms 108228 KB Output is correct
91 Correct 453 ms 99784 KB Output is correct
92 Correct 440 ms 102100 KB Output is correct
93 Correct 438 ms 100148 KB Output is correct
94 Correct 361 ms 86692 KB Output is correct
95 Correct 373 ms 86096 KB Output is correct
96 Correct 363 ms 88292 KB Output is correct
97 Correct 368 ms 86800 KB Output is correct
98 Correct 356 ms 85332 KB Output is correct
99 Correct 401 ms 86352 KB Output is correct
100 Correct 370 ms 86608 KB Output is correct
101 Correct 406 ms 86864 KB Output is correct
102 Correct 384 ms 86196 KB Output is correct
103 Correct 347 ms 87376 KB Output is correct
104 Correct 375 ms 87124 KB Output is correct
105 Correct 368 ms 88656 KB Output is correct
106 Correct 371 ms 87328 KB Output is correct
107 Correct 368 ms 89428 KB Output is correct
108 Correct 460 ms 93376 KB Output is correct
109 Correct 374 ms 89428 KB Output is correct
110 Correct 6 ms 20824 KB Output is correct
111 Correct 4 ms 20948 KB Output is correct
112 Correct 5 ms 20828 KB Output is correct
113 Correct 481 ms 126412 KB Output is correct
114 Correct 511 ms 129992 KB Output is correct
115 Correct 569 ms 143256 KB Output is correct
116 Correct 621 ms 147392 KB Output is correct
117 Correct 574 ms 147764 KB Output is correct
118 Correct 602 ms 147572 KB Output is correct
119 Correct 569 ms 147648 KB Output is correct
120 Correct 564 ms 147604 KB Output is correct
121 Correct 584 ms 147648 KB Output is correct
122 Correct 641 ms 147652 KB Output is correct
123 Correct 35 ms 24784 KB Output is correct
124 Correct 401 ms 94004 KB Output is correct
125 Correct 313 ms 77564 KB Output is correct
126 Correct 496 ms 107700 KB Output is correct
127 Correct 545 ms 108480 KB Output is correct
128 Correct 470 ms 107204 KB Output is correct
129 Correct 473 ms 106976 KB Output is correct
130 Correct 469 ms 104492 KB Output is correct
131 Correct 240 ms 70532 KB Output is correct
132 Correct 232 ms 70804 KB Output is correct
133 Correct 262 ms 67664 KB Output is correct
134 Correct 375 ms 89684 KB Output is correct
135 Correct 410 ms 91984 KB Output is correct
136 Correct 443 ms 91988 KB Output is correct
137 Correct 232 ms 54652 KB Output is correct
138 Correct 235 ms 54724 KB Output is correct
139 Correct 224 ms 54728 KB Output is correct
140 Correct 251 ms 54916 KB Output is correct
141 Correct 267 ms 54728 KB Output is correct
142 Correct 240 ms 54728 KB Output is correct
143 Correct 54 ms 33660 KB Output is correct
144 Correct 455 ms 106432 KB Output is correct