답안 #961962

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
961962 2024-04-12T22:20:46 Z amirhoseinfar1385 Tourism (JOI23_tourism) C++17
100 / 100
199 ms 37356 KB
#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+10,lg=18;
int n,m,q,sz[maxn],par[maxn],parh[maxn],timea,all[maxn],res[maxn],high[maxn];
vector<int>adj[maxn];
vector<pair<int,int>>allq[maxn];
pair<int,int>stf[maxn],nag[maxn];
vector<pair<int,int>>allr[maxn];
int lca[lg][maxn],kaf=(1<<18);

bool zird(int u,int v){
	return stf[v].first<=stf[u].first&&stf[v].second>=stf[u].second;
}

int getlca(int u,int v){
	if(u==0){
		return v;
	}
	if(v==0){
		return u;
	}
	if(zird(u,v)){
		return v;
	}
	if(zird(v,u)){
		return u;
	}
	for(int i=lg-1;i>=0;i--){
		if(lca[i][u]!=0&&zird(v,lca[i][u])==0){
			u=lca[i][u];
		}
	}
	return lca[0][u];
}

struct segment{
	int seg[(1<<19)];
	void create(){
		for(int i=(1<<19)-1;i>=1;i--){
			if(i>=kaf){
				if(i-kaf<=m){
					seg[i]=all[i-kaf];
				}
			}else{
				seg[i]=getlca(seg[(i<<1)],seg[(i<<1)^1]);
			}
		}
	}
	int pors(int i,int l,int r,int tl,int tr){
		if(l>r||l>tr||r<tl||tl>tr){
			return 0;
		}
		if(l>=tl&&r<=tr){
			return seg[i];
		}
		int m=(l+r)>>1;
		return getlca(pors((i<<1),l,m,tl,tr),pors((i<<1)^1,m+1,r,tl,tr));
	}
}seg;

struct fenwick{
	int fn[maxn];
	void upd(int i,int w){
		//cout<<"upd: "<<i<<" "<<w<<endl;
		i++;
		while(i<maxn){
			fn[i]+=w;
			i+=((-i)&i);
		}
	}
	int pors(int i){
		i++;
		int ret=0;
		while(i>0){
			ret+=fn[i];
			i-=((-i)&i);
		}
		return ret;
	}
}fen;

void vorod(){
	cin>>n>>m>>q;
	for(int i=0;i<n-1;i++){
		int u,v;
		cin>>u>>v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	for(int i=1;i<=m;i++){
		cin>>all[i];
	}
	for(int i=0;i<q;i++){
		int l,r;
		cin>>l>>r;
		nag[i]=make_pair(l,r);
		allq[r].push_back(make_pair(l,i));
	}
}

void dfs1(int u=1){
	if(u==1){
		parh[u]=1;
	}
	//cout<<u<<" ";
	timea++;
	stf[u].first=timea;
	if((int)adj[u].size()==0){
		stf[u].second=timea;
		return ;
	}
	parh[adj[u][0]]=parh[u];
	dfs1(adj[u][0]);
	for(int i=1;i<(int)adj[u].size();i++){
		parh[adj[u][i]]=adj[u][i];
		dfs1(adj[u][i]);
	}
	stf[u].second=timea;
}

bool cmp(int a,int b){
	return sz[a]>sz[b];
}

void dfs(int u=1,int p=-1){
	if(p!=-1){
		sort(adj[u].begin(),adj[u].end());
		adj[u].erase(lower_bound(adj[u].begin(),adj[u].end(),p));
	}
	sz[u]=1;
	for(auto x:adj[u]){
		par[x]=u;
		high[x]=high[u]+1;
		lca[0][x]=u;
		dfs(x,u);
		sz[u]+=sz[x];
	}
	sort(adj[u].begin(),adj[u].end(),cmp);
}

void callca(){
	for(int i=1;i<lg;i++){
		for(int j=1;j<=n;j++){
			lca[i][j]=lca[i-1][lca[i-1][j]];
		}
	}
}

void pre(){
	dfs();
	dfs1();
	//cout<<endl;
	callca();
	seg.create();
}

void upd(int u,int ind){
	//cout<<u<<" "<<ind<<" "<<parh[u]<<endl;
	int ph=parh[u];
	int last=stf[ph].first-1;
	while((int)allr[ph].size()>0){
		int v=allr[ph].back().first;
		if(stf[v].first<=stf[u].first){
			fen.upd(allr[ph].back().second,-(stf[v].first-last));
			last=stf[v].first;
			allr[ph].pop_back();
		}else{
			fen.upd(allr[ph].back().second,-(stf[v].first-last));
			fen.upd(allr[ph].back().second,stf[v].first-stf[u].first);
			break;
		}
	}
	fen.upd(ind,stf[u].first-stf[ph].first+1);
	allr[ph].push_back(make_pair(u,ind));
	if(ph!=1){
		upd(par[ph],ind);
	}
}

void solve(){
	for(int i=1;i<=m;i++){
		//cout<<"hey: "<<i<<" "<<all[i]<<endl;
		upd(all[i],i);
		for(auto x:allq[i]){
			res[x.second]=fen.pors(i)-fen.pors(x.first-1);
		}
	}
}

void khor(){
	for(int i=0;i<q;i++){
		res[i]-=high[seg.pors(1,0,kaf-1,nag[i].first,nag[i].second)];
		//cout<<"injy: "<<seg.pors(1,0,kaf-1,nag[i].first,nag[i].second)<<" "<<high[seg.pors(1,0,kaf-1,nag[i].first,nag[i].second)]<<"\n";
		cout<<res[i]<<"\n";
	}
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	//cout.tie(0);
	//freopen("inp.txt","r",stdin);
	vorod();
	pre();
	solve();
	khor();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18780 KB Output is correct
2 Correct 4 ms 18952 KB Output is correct
3 Correct 4 ms 18780 KB Output is correct
4 Correct 5 ms 18984 KB Output is correct
5 Correct 4 ms 18780 KB Output is correct
6 Correct 4 ms 18916 KB Output is correct
7 Correct 4 ms 18776 KB Output is correct
8 Correct 4 ms 18780 KB Output is correct
9 Correct 5 ms 18964 KB Output is correct
10 Correct 4 ms 18780 KB Output is correct
11 Correct 4 ms 18988 KB Output is correct
12 Correct 4 ms 18780 KB Output is correct
13 Correct 5 ms 18780 KB Output is correct
14 Correct 4 ms 18780 KB Output is correct
15 Correct 4 ms 18780 KB Output is correct
16 Correct 4 ms 18776 KB Output is correct
17 Correct 4 ms 18780 KB Output is correct
18 Correct 4 ms 18780 KB Output is correct
19 Correct 4 ms 18780 KB Output is correct
20 Correct 4 ms 18780 KB Output is correct
21 Correct 4 ms 18780 KB Output is correct
22 Correct 4 ms 18780 KB Output is correct
23 Correct 4 ms 18780 KB Output is correct
24 Correct 5 ms 18780 KB Output is correct
25 Correct 4 ms 18780 KB Output is correct
26 Correct 4 ms 18920 KB Output is correct
27 Correct 4 ms 16732 KB Output is correct
28 Correct 5 ms 18780 KB Output is correct
29 Correct 6 ms 18980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18780 KB Output is correct
2 Correct 4 ms 18952 KB Output is correct
3 Correct 4 ms 18780 KB Output is correct
4 Correct 5 ms 18984 KB Output is correct
5 Correct 4 ms 18780 KB Output is correct
6 Correct 4 ms 18916 KB Output is correct
7 Correct 4 ms 18776 KB Output is correct
8 Correct 4 ms 18780 KB Output is correct
9 Correct 5 ms 18964 KB Output is correct
10 Correct 4 ms 18780 KB Output is correct
11 Correct 4 ms 18988 KB Output is correct
12 Correct 4 ms 18780 KB Output is correct
13 Correct 5 ms 18780 KB Output is correct
14 Correct 4 ms 18780 KB Output is correct
15 Correct 4 ms 18780 KB Output is correct
16 Correct 4 ms 18776 KB Output is correct
17 Correct 4 ms 18780 KB Output is correct
18 Correct 4 ms 18780 KB Output is correct
19 Correct 4 ms 18780 KB Output is correct
20 Correct 4 ms 18780 KB Output is correct
21 Correct 4 ms 18780 KB Output is correct
22 Correct 4 ms 18780 KB Output is correct
23 Correct 4 ms 18780 KB Output is correct
24 Correct 5 ms 18780 KB Output is correct
25 Correct 4 ms 18780 KB Output is correct
26 Correct 4 ms 18920 KB Output is correct
27 Correct 4 ms 16732 KB Output is correct
28 Correct 5 ms 18780 KB Output is correct
29 Correct 6 ms 18980 KB Output is correct
30 Correct 6 ms 19032 KB Output is correct
31 Correct 6 ms 19036 KB Output is correct
32 Correct 6 ms 19116 KB Output is correct
33 Correct 6 ms 19036 KB Output is correct
34 Correct 7 ms 19176 KB Output is correct
35 Correct 9 ms 19116 KB Output is correct
36 Correct 7 ms 19036 KB Output is correct
37 Correct 6 ms 19036 KB Output is correct
38 Correct 6 ms 19036 KB Output is correct
39 Correct 8 ms 19036 KB Output is correct
40 Correct 6 ms 19036 KB Output is correct
41 Correct 6 ms 19288 KB Output is correct
42 Correct 6 ms 19288 KB Output is correct
43 Correct 6 ms 19200 KB Output is correct
44 Correct 6 ms 19036 KB Output is correct
45 Correct 6 ms 19184 KB Output is correct
46 Correct 6 ms 19032 KB Output is correct
47 Correct 6 ms 19184 KB Output is correct
48 Correct 6 ms 19036 KB Output is correct
49 Correct 6 ms 19188 KB Output is correct
50 Correct 6 ms 19036 KB Output is correct
51 Correct 6 ms 19140 KB Output is correct
52 Correct 7 ms 19036 KB Output is correct
53 Correct 6 ms 19136 KB Output is correct
54 Correct 6 ms 19036 KB Output is correct
55 Correct 6 ms 19032 KB Output is correct
56 Correct 5 ms 16988 KB Output is correct
57 Correct 5 ms 19036 KB Output is correct
58 Correct 6 ms 19036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18780 KB Output is correct
2 Correct 4 ms 16732 KB Output is correct
3 Correct 6 ms 16932 KB Output is correct
4 Correct 88 ms 30120 KB Output is correct
5 Correct 83 ms 32852 KB Output is correct
6 Correct 77 ms 34208 KB Output is correct
7 Correct 121 ms 37228 KB Output is correct
8 Correct 114 ms 37204 KB Output is correct
9 Correct 114 ms 37216 KB Output is correct
10 Correct 117 ms 37200 KB Output is correct
11 Correct 119 ms 37272 KB Output is correct
12 Correct 92 ms 36436 KB Output is correct
13 Correct 89 ms 36260 KB Output is correct
14 Correct 90 ms 36432 KB Output is correct
15 Correct 56 ms 33932 KB Output is correct
16 Correct 108 ms 36948 KB Output is correct
17 Correct 71 ms 21196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18780 KB Output is correct
2 Correct 63 ms 23428 KB Output is correct
3 Correct 75 ms 24228 KB Output is correct
4 Correct 67 ms 24468 KB Output is correct
5 Correct 97 ms 26720 KB Output is correct
6 Correct 110 ms 26872 KB Output is correct
7 Correct 102 ms 26796 KB Output is correct
8 Correct 107 ms 26860 KB Output is correct
9 Correct 97 ms 26712 KB Output is correct
10 Correct 97 ms 26704 KB Output is correct
11 Correct 113 ms 26960 KB Output is correct
12 Correct 104 ms 27012 KB Output is correct
13 Correct 112 ms 27328 KB Output is correct
14 Correct 115 ms 28240 KB Output is correct
15 Correct 134 ms 31420 KB Output is correct
16 Correct 109 ms 27220 KB Output is correct
17 Correct 107 ms 27152 KB Output is correct
18 Correct 106 ms 27316 KB Output is correct
19 Correct 87 ms 25752 KB Output is correct
20 Correct 81 ms 25736 KB Output is correct
21 Correct 79 ms 25632 KB Output is correct
22 Correct 82 ms 25744 KB Output is correct
23 Correct 82 ms 25588 KB Output is correct
24 Correct 99 ms 25748 KB Output is correct
25 Correct 84 ms 25580 KB Output is correct
26 Correct 80 ms 25780 KB Output is correct
27 Correct 78 ms 25716 KB Output is correct
28 Correct 88 ms 26196 KB Output is correct
29 Correct 84 ms 25752 KB Output is correct
30 Correct 83 ms 25980 KB Output is correct
31 Correct 87 ms 26160 KB Output is correct
32 Correct 102 ms 26680 KB Output is correct
33 Correct 107 ms 27984 KB Output is correct
34 Correct 118 ms 30512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18780 KB Output is correct
2 Correct 4 ms 16824 KB Output is correct
3 Correct 6 ms 16988 KB Output is correct
4 Correct 158 ms 27544 KB Output is correct
5 Correct 146 ms 27524 KB Output is correct
6 Correct 172 ms 29768 KB Output is correct
7 Correct 180 ms 30744 KB Output is correct
8 Correct 179 ms 30784 KB Output is correct
9 Correct 199 ms 30700 KB Output is correct
10 Correct 194 ms 30836 KB Output is correct
11 Correct 180 ms 30852 KB Output is correct
12 Correct 178 ms 30800 KB Output is correct
13 Correct 184 ms 31032 KB Output is correct
14 Correct 70 ms 21084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18780 KB Output is correct
2 Correct 4 ms 18952 KB Output is correct
3 Correct 4 ms 18780 KB Output is correct
4 Correct 5 ms 18984 KB Output is correct
5 Correct 4 ms 18780 KB Output is correct
6 Correct 4 ms 18916 KB Output is correct
7 Correct 4 ms 18776 KB Output is correct
8 Correct 4 ms 18780 KB Output is correct
9 Correct 5 ms 18964 KB Output is correct
10 Correct 4 ms 18780 KB Output is correct
11 Correct 4 ms 18988 KB Output is correct
12 Correct 4 ms 18780 KB Output is correct
13 Correct 5 ms 18780 KB Output is correct
14 Correct 4 ms 18780 KB Output is correct
15 Correct 4 ms 18780 KB Output is correct
16 Correct 4 ms 18776 KB Output is correct
17 Correct 4 ms 18780 KB Output is correct
18 Correct 4 ms 18780 KB Output is correct
19 Correct 4 ms 18780 KB Output is correct
20 Correct 4 ms 18780 KB Output is correct
21 Correct 4 ms 18780 KB Output is correct
22 Correct 4 ms 18780 KB Output is correct
23 Correct 4 ms 18780 KB Output is correct
24 Correct 5 ms 18780 KB Output is correct
25 Correct 4 ms 18780 KB Output is correct
26 Correct 4 ms 18920 KB Output is correct
27 Correct 4 ms 16732 KB Output is correct
28 Correct 5 ms 18780 KB Output is correct
29 Correct 6 ms 18980 KB Output is correct
30 Correct 6 ms 19032 KB Output is correct
31 Correct 6 ms 19036 KB Output is correct
32 Correct 6 ms 19116 KB Output is correct
33 Correct 6 ms 19036 KB Output is correct
34 Correct 7 ms 19176 KB Output is correct
35 Correct 9 ms 19116 KB Output is correct
36 Correct 7 ms 19036 KB Output is correct
37 Correct 6 ms 19036 KB Output is correct
38 Correct 6 ms 19036 KB Output is correct
39 Correct 8 ms 19036 KB Output is correct
40 Correct 6 ms 19036 KB Output is correct
41 Correct 6 ms 19288 KB Output is correct
42 Correct 6 ms 19288 KB Output is correct
43 Correct 6 ms 19200 KB Output is correct
44 Correct 6 ms 19036 KB Output is correct
45 Correct 6 ms 19184 KB Output is correct
46 Correct 6 ms 19032 KB Output is correct
47 Correct 6 ms 19184 KB Output is correct
48 Correct 6 ms 19036 KB Output is correct
49 Correct 6 ms 19188 KB Output is correct
50 Correct 6 ms 19036 KB Output is correct
51 Correct 6 ms 19140 KB Output is correct
52 Correct 7 ms 19036 KB Output is correct
53 Correct 6 ms 19136 KB Output is correct
54 Correct 6 ms 19036 KB Output is correct
55 Correct 6 ms 19032 KB Output is correct
56 Correct 5 ms 16988 KB Output is correct
57 Correct 5 ms 19036 KB Output is correct
58 Correct 6 ms 19036 KB Output is correct
59 Correct 4 ms 18780 KB Output is correct
60 Correct 4 ms 16732 KB Output is correct
61 Correct 6 ms 16932 KB Output is correct
62 Correct 88 ms 30120 KB Output is correct
63 Correct 83 ms 32852 KB Output is correct
64 Correct 77 ms 34208 KB Output is correct
65 Correct 121 ms 37228 KB Output is correct
66 Correct 114 ms 37204 KB Output is correct
67 Correct 114 ms 37216 KB Output is correct
68 Correct 117 ms 37200 KB Output is correct
69 Correct 119 ms 37272 KB Output is correct
70 Correct 92 ms 36436 KB Output is correct
71 Correct 89 ms 36260 KB Output is correct
72 Correct 90 ms 36432 KB Output is correct
73 Correct 56 ms 33932 KB Output is correct
74 Correct 108 ms 36948 KB Output is correct
75 Correct 71 ms 21196 KB Output is correct
76 Correct 4 ms 18780 KB Output is correct
77 Correct 63 ms 23428 KB Output is correct
78 Correct 75 ms 24228 KB Output is correct
79 Correct 67 ms 24468 KB Output is correct
80 Correct 97 ms 26720 KB Output is correct
81 Correct 110 ms 26872 KB Output is correct
82 Correct 102 ms 26796 KB Output is correct
83 Correct 107 ms 26860 KB Output is correct
84 Correct 97 ms 26712 KB Output is correct
85 Correct 97 ms 26704 KB Output is correct
86 Correct 113 ms 26960 KB Output is correct
87 Correct 104 ms 27012 KB Output is correct
88 Correct 112 ms 27328 KB Output is correct
89 Correct 115 ms 28240 KB Output is correct
90 Correct 134 ms 31420 KB Output is correct
91 Correct 109 ms 27220 KB Output is correct
92 Correct 107 ms 27152 KB Output is correct
93 Correct 106 ms 27316 KB Output is correct
94 Correct 87 ms 25752 KB Output is correct
95 Correct 81 ms 25736 KB Output is correct
96 Correct 79 ms 25632 KB Output is correct
97 Correct 82 ms 25744 KB Output is correct
98 Correct 82 ms 25588 KB Output is correct
99 Correct 99 ms 25748 KB Output is correct
100 Correct 84 ms 25580 KB Output is correct
101 Correct 80 ms 25780 KB Output is correct
102 Correct 78 ms 25716 KB Output is correct
103 Correct 88 ms 26196 KB Output is correct
104 Correct 84 ms 25752 KB Output is correct
105 Correct 83 ms 25980 KB Output is correct
106 Correct 87 ms 26160 KB Output is correct
107 Correct 102 ms 26680 KB Output is correct
108 Correct 107 ms 27984 KB Output is correct
109 Correct 118 ms 30512 KB Output is correct
110 Correct 4 ms 18780 KB Output is correct
111 Correct 4 ms 16824 KB Output is correct
112 Correct 6 ms 16988 KB Output is correct
113 Correct 158 ms 27544 KB Output is correct
114 Correct 146 ms 27524 KB Output is correct
115 Correct 172 ms 29768 KB Output is correct
116 Correct 180 ms 30744 KB Output is correct
117 Correct 179 ms 30784 KB Output is correct
118 Correct 199 ms 30700 KB Output is correct
119 Correct 194 ms 30836 KB Output is correct
120 Correct 180 ms 30852 KB Output is correct
121 Correct 178 ms 30800 KB Output is correct
122 Correct 184 ms 31032 KB Output is correct
123 Correct 70 ms 21084 KB Output is correct
124 Correct 158 ms 30288 KB Output is correct
125 Correct 119 ms 28280 KB Output is correct
126 Correct 182 ms 30696 KB Output is correct
127 Correct 172 ms 30804 KB Output is correct
128 Correct 186 ms 30804 KB Output is correct
129 Correct 168 ms 30796 KB Output is correct
130 Correct 170 ms 30768 KB Output is correct
131 Correct 147 ms 35904 KB Output is correct
132 Correct 136 ms 37356 KB Output is correct
133 Correct 130 ms 33364 KB Output is correct
134 Correct 155 ms 29588 KB Output is correct
135 Correct 156 ms 29756 KB Output is correct
136 Correct 160 ms 29648 KB Output is correct
137 Correct 127 ms 31692 KB Output is correct
138 Correct 134 ms 31960 KB Output is correct
139 Correct 138 ms 31692 KB Output is correct
140 Correct 126 ms 31612 KB Output is correct
141 Correct 128 ms 31688 KB Output is correct
142 Correct 128 ms 31716 KB Output is correct
143 Correct 77 ms 26172 KB Output is correct
144 Correct 173 ms 30412 KB Output is correct