답안 #961963

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
961963 2024-04-12T22:22:06 Z amirhoseinfar1385 Tourism (JOI23_tourism) C++17
100 / 100
196 ms 35964 KB
#include<bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
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 3 ms 18780 KB Output is correct
3 Correct 5 ms 18776 KB Output is correct
4 Correct 4 ms 18780 KB Output is correct
5 Correct 4 ms 18780 KB Output is correct
6 Correct 4 ms 18780 KB Output is correct
7 Correct 4 ms 18780 KB Output is correct
8 Correct 4 ms 18780 KB Output is correct
9 Correct 5 ms 18780 KB Output is correct
10 Correct 4 ms 19032 KB Output is correct
11 Correct 4 ms 18780 KB Output is correct
12 Correct 4 ms 18972 KB Output is correct
13 Correct 4 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 18988 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 18888 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 4 ms 18780 KB Output is correct
25 Correct 4 ms 19004 KB Output is correct
26 Correct 4 ms 18780 KB Output is correct
27 Correct 4 ms 16732 KB Output is correct
28 Correct 4 ms 18780 KB Output is correct
29 Correct 4 ms 18780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18780 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 5 ms 18776 KB Output is correct
4 Correct 4 ms 18780 KB Output is correct
5 Correct 4 ms 18780 KB Output is correct
6 Correct 4 ms 18780 KB Output is correct
7 Correct 4 ms 18780 KB Output is correct
8 Correct 4 ms 18780 KB Output is correct
9 Correct 5 ms 18780 KB Output is correct
10 Correct 4 ms 19032 KB Output is correct
11 Correct 4 ms 18780 KB Output is correct
12 Correct 4 ms 18972 KB Output is correct
13 Correct 4 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 18988 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 18888 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 4 ms 18780 KB Output is correct
25 Correct 4 ms 19004 KB Output is correct
26 Correct 4 ms 18780 KB Output is correct
27 Correct 4 ms 16732 KB Output is correct
28 Correct 4 ms 18780 KB Output is correct
29 Correct 4 ms 18780 KB Output is correct
30 Correct 5 ms 19032 KB Output is correct
31 Correct 6 ms 19036 KB Output is correct
32 Correct 6 ms 19032 KB Output is correct
33 Correct 6 ms 19036 KB Output is correct
34 Correct 6 ms 19036 KB Output is correct
35 Correct 6 ms 19036 KB Output is correct
36 Correct 7 ms 19032 KB Output is correct
37 Correct 6 ms 19036 KB Output is correct
38 Correct 6 ms 19036 KB Output is correct
39 Correct 6 ms 19212 KB Output is correct
40 Correct 6 ms 19036 KB Output is correct
41 Correct 6 ms 19192 KB Output is correct
42 Correct 6 ms 19244 KB Output is correct
43 Correct 5 ms 19036 KB Output is correct
44 Correct 6 ms 19168 KB Output is correct
45 Correct 6 ms 19036 KB Output is correct
46 Correct 7 ms 19188 KB Output is correct
47 Correct 5 ms 19036 KB Output is correct
48 Correct 6 ms 19036 KB Output is correct
49 Correct 7 ms 19036 KB Output is correct
50 Correct 5 ms 19036 KB Output is correct
51 Correct 6 ms 19036 KB Output is correct
52 Correct 5 ms 19036 KB Output is correct
53 Correct 6 ms 19132 KB Output is correct
54 Correct 6 ms 19036 KB Output is correct
55 Correct 5 ms 19036 KB Output is correct
56 Correct 4 ms 16988 KB Output is correct
57 Correct 5 ms 19036 KB Output is correct
58 Correct 5 ms 19036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18776 KB Output is correct
2 Correct 3 ms 16732 KB Output is correct
3 Correct 4 ms 16732 KB Output is correct
4 Correct 86 ms 29308 KB Output is correct
5 Correct 75 ms 31892 KB Output is correct
6 Correct 78 ms 33484 KB Output is correct
7 Correct 114 ms 35952 KB Output is correct
8 Correct 115 ms 35780 KB Output is correct
9 Correct 111 ms 35964 KB Output is correct
10 Correct 111 ms 35924 KB Output is correct
11 Correct 114 ms 35928 KB Output is correct
12 Correct 85 ms 35156 KB Output is correct
13 Correct 89 ms 35180 KB Output is correct
14 Correct 86 ms 35152 KB Output is correct
15 Correct 52 ms 34012 KB Output is correct
16 Correct 106 ms 35632 KB Output is correct
17 Correct 70 ms 19792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18780 KB Output is correct
2 Correct 54 ms 22380 KB Output is correct
3 Correct 77 ms 22936 KB Output is correct
4 Correct 66 ms 23120 KB Output is correct
5 Correct 100 ms 25164 KB Output is correct
6 Correct 102 ms 25068 KB Output is correct
7 Correct 100 ms 25156 KB Output is correct
8 Correct 95 ms 25168 KB Output is correct
9 Correct 103 ms 25156 KB Output is correct
10 Correct 97 ms 25172 KB Output is correct
11 Correct 111 ms 25140 KB Output is correct
12 Correct 99 ms 25144 KB Output is correct
13 Correct 116 ms 25344 KB Output is correct
14 Correct 121 ms 26024 KB Output is correct
15 Correct 129 ms 28752 KB Output is correct
16 Correct 100 ms 25360 KB Output is correct
17 Correct 109 ms 25344 KB Output is correct
18 Correct 97 ms 25308 KB Output is correct
19 Correct 87 ms 23840 KB Output is correct
20 Correct 81 ms 23780 KB Output is correct
21 Correct 81 ms 23996 KB Output is correct
22 Correct 85 ms 23892 KB Output is correct
23 Correct 74 ms 23888 KB Output is correct
24 Correct 77 ms 23912 KB Output is correct
25 Correct 90 ms 23808 KB Output is correct
26 Correct 84 ms 24044 KB Output is correct
27 Correct 79 ms 23916 KB Output is correct
28 Correct 77 ms 23884 KB Output is correct
29 Correct 79 ms 24044 KB Output is correct
30 Correct 81 ms 24152 KB Output is correct
31 Correct 84 ms 24148 KB Output is correct
32 Correct 89 ms 24660 KB Output is correct
33 Correct 102 ms 25676 KB Output is correct
34 Correct 118 ms 27476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18776 KB Output is correct
2 Correct 4 ms 16732 KB Output is correct
3 Correct 4 ms 16988 KB Output is correct
4 Correct 153 ms 25172 KB Output is correct
5 Correct 149 ms 25424 KB Output is correct
6 Correct 180 ms 26960 KB Output is correct
7 Correct 176 ms 27916 KB Output is correct
8 Correct 196 ms 27908 KB Output is correct
9 Correct 175 ms 27988 KB Output is correct
10 Correct 196 ms 28404 KB Output is correct
11 Correct 178 ms 27984 KB Output is correct
12 Correct 175 ms 27984 KB Output is correct
13 Correct 187 ms 28028 KB Output is correct
14 Correct 71 ms 19704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18780 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 5 ms 18776 KB Output is correct
4 Correct 4 ms 18780 KB Output is correct
5 Correct 4 ms 18780 KB Output is correct
6 Correct 4 ms 18780 KB Output is correct
7 Correct 4 ms 18780 KB Output is correct
8 Correct 4 ms 18780 KB Output is correct
9 Correct 5 ms 18780 KB Output is correct
10 Correct 4 ms 19032 KB Output is correct
11 Correct 4 ms 18780 KB Output is correct
12 Correct 4 ms 18972 KB Output is correct
13 Correct 4 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 18988 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 18888 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 4 ms 18780 KB Output is correct
25 Correct 4 ms 19004 KB Output is correct
26 Correct 4 ms 18780 KB Output is correct
27 Correct 4 ms 16732 KB Output is correct
28 Correct 4 ms 18780 KB Output is correct
29 Correct 4 ms 18780 KB Output is correct
30 Correct 5 ms 19032 KB Output is correct
31 Correct 6 ms 19036 KB Output is correct
32 Correct 6 ms 19032 KB Output is correct
33 Correct 6 ms 19036 KB Output is correct
34 Correct 6 ms 19036 KB Output is correct
35 Correct 6 ms 19036 KB Output is correct
36 Correct 7 ms 19032 KB Output is correct
37 Correct 6 ms 19036 KB Output is correct
38 Correct 6 ms 19036 KB Output is correct
39 Correct 6 ms 19212 KB Output is correct
40 Correct 6 ms 19036 KB Output is correct
41 Correct 6 ms 19192 KB Output is correct
42 Correct 6 ms 19244 KB Output is correct
43 Correct 5 ms 19036 KB Output is correct
44 Correct 6 ms 19168 KB Output is correct
45 Correct 6 ms 19036 KB Output is correct
46 Correct 7 ms 19188 KB Output is correct
47 Correct 5 ms 19036 KB Output is correct
48 Correct 6 ms 19036 KB Output is correct
49 Correct 7 ms 19036 KB Output is correct
50 Correct 5 ms 19036 KB Output is correct
51 Correct 6 ms 19036 KB Output is correct
52 Correct 5 ms 19036 KB Output is correct
53 Correct 6 ms 19132 KB Output is correct
54 Correct 6 ms 19036 KB Output is correct
55 Correct 5 ms 19036 KB Output is correct
56 Correct 4 ms 16988 KB Output is correct
57 Correct 5 ms 19036 KB Output is correct
58 Correct 5 ms 19036 KB Output is correct
59 Correct 4 ms 18776 KB Output is correct
60 Correct 3 ms 16732 KB Output is correct
61 Correct 4 ms 16732 KB Output is correct
62 Correct 86 ms 29308 KB Output is correct
63 Correct 75 ms 31892 KB Output is correct
64 Correct 78 ms 33484 KB Output is correct
65 Correct 114 ms 35952 KB Output is correct
66 Correct 115 ms 35780 KB Output is correct
67 Correct 111 ms 35964 KB Output is correct
68 Correct 111 ms 35924 KB Output is correct
69 Correct 114 ms 35928 KB Output is correct
70 Correct 85 ms 35156 KB Output is correct
71 Correct 89 ms 35180 KB Output is correct
72 Correct 86 ms 35152 KB Output is correct
73 Correct 52 ms 34012 KB Output is correct
74 Correct 106 ms 35632 KB Output is correct
75 Correct 70 ms 19792 KB Output is correct
76 Correct 4 ms 18780 KB Output is correct
77 Correct 54 ms 22380 KB Output is correct
78 Correct 77 ms 22936 KB Output is correct
79 Correct 66 ms 23120 KB Output is correct
80 Correct 100 ms 25164 KB Output is correct
81 Correct 102 ms 25068 KB Output is correct
82 Correct 100 ms 25156 KB Output is correct
83 Correct 95 ms 25168 KB Output is correct
84 Correct 103 ms 25156 KB Output is correct
85 Correct 97 ms 25172 KB Output is correct
86 Correct 111 ms 25140 KB Output is correct
87 Correct 99 ms 25144 KB Output is correct
88 Correct 116 ms 25344 KB Output is correct
89 Correct 121 ms 26024 KB Output is correct
90 Correct 129 ms 28752 KB Output is correct
91 Correct 100 ms 25360 KB Output is correct
92 Correct 109 ms 25344 KB Output is correct
93 Correct 97 ms 25308 KB Output is correct
94 Correct 87 ms 23840 KB Output is correct
95 Correct 81 ms 23780 KB Output is correct
96 Correct 81 ms 23996 KB Output is correct
97 Correct 85 ms 23892 KB Output is correct
98 Correct 74 ms 23888 KB Output is correct
99 Correct 77 ms 23912 KB Output is correct
100 Correct 90 ms 23808 KB Output is correct
101 Correct 84 ms 24044 KB Output is correct
102 Correct 79 ms 23916 KB Output is correct
103 Correct 77 ms 23884 KB Output is correct
104 Correct 79 ms 24044 KB Output is correct
105 Correct 81 ms 24152 KB Output is correct
106 Correct 84 ms 24148 KB Output is correct
107 Correct 89 ms 24660 KB Output is correct
108 Correct 102 ms 25676 KB Output is correct
109 Correct 118 ms 27476 KB Output is correct
110 Correct 4 ms 18776 KB Output is correct
111 Correct 4 ms 16732 KB Output is correct
112 Correct 4 ms 16988 KB Output is correct
113 Correct 153 ms 25172 KB Output is correct
114 Correct 149 ms 25424 KB Output is correct
115 Correct 180 ms 26960 KB Output is correct
116 Correct 176 ms 27916 KB Output is correct
117 Correct 196 ms 27908 KB Output is correct
118 Correct 175 ms 27988 KB Output is correct
119 Correct 196 ms 28404 KB Output is correct
120 Correct 178 ms 27984 KB Output is correct
121 Correct 175 ms 27984 KB Output is correct
122 Correct 187 ms 28028 KB Output is correct
123 Correct 71 ms 19704 KB Output is correct
124 Correct 154 ms 27556 KB Output is correct
125 Correct 112 ms 26148 KB Output is correct
126 Correct 165 ms 27988 KB Output is correct
127 Correct 177 ms 27988 KB Output is correct
128 Correct 175 ms 27896 KB Output is correct
129 Correct 174 ms 27984 KB Output is correct
130 Correct 174 ms 27876 KB Output is correct
131 Correct 136 ms 34488 KB Output is correct
132 Correct 136 ms 35812 KB Output is correct
133 Correct 135 ms 31568 KB Output is correct
134 Correct 150 ms 26704 KB Output is correct
135 Correct 150 ms 26960 KB Output is correct
136 Correct 178 ms 26688 KB Output is correct
137 Correct 123 ms 28868 KB Output is correct
138 Correct 124 ms 28800 KB Output is correct
139 Correct 121 ms 28736 KB Output is correct
140 Correct 122 ms 28876 KB Output is correct
141 Correct 137 ms 28732 KB Output is correct
142 Correct 139 ms 29232 KB Output is correct
143 Correct 64 ms 24712 KB Output is correct
144 Correct 141 ms 27768 KB Output is correct