답안 #999242

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
999242 2024-06-15T08:37:25 Z Dzadzo Tourism (JOI23_tourism) C++17
100 / 100
701 ms 54212 KB
#include <bits/stdc++.h>
#define ll long long
#define int ll
#define pb push_back
#define S second
#define F first
#define pii pair<int,int>
#define vi vector <int>
#define vvi vector <vi>
#define vvvi vector <vvi>
#define vp vector <pii>
#define vvp vector <vp>
#define vb vector <bool>
#define vvb vector <vb>;
#define INF LLONG_MAX
#define MOD 1000000007
#define MAXN 100005
using namespace std;
set <pair<int,pii>> myset;
int bit[MAXN+1];
vvi P(MAXN+1,vi(20));
void add(int idx,int delta){
	for (;idx<=MAXN;idx+=idx&-idx)bit[idx]+=delta;
}
int get(int idx){
	int res=0;
	for (;idx>0;idx-=idx&-idx)res+=bit[idx];
	return res;
}
int n;
void update(int l,int r,int val){
	if(l>r)return;
	if (r>n)return;
	if (l<=0)return;
	auto it=myset.lower_bound({l,{0,0}});
	add(val,r-l+1);
	if ((*it).F!=l){
		it--;
		if ((*it).S.F<r){
			add((*it).S.S,-((*it).S.F-l+1));
			myset.insert({(*it).F,{l-1,(*it).S.S}});
			myset.erase(*it);
		}else if ((*it).S.F>=r){
			add((*it).S.S,-(r-l+1));
			myset.insert({(*it).F,{l-1,(*it).S.S}});
			if ((*it).S.F!=r)
			myset.insert({r+1,{(*it).S.F,(*it).S.S}});
			myset.erase(*it);
			myset.insert({l,{r,val}});
			return;
		}		
	}
	it=myset.lower_bound({l,{0,0}});
	vector <pair <int,pii>> del;
	for (;;it++){
		if (it==myset.end())break;
		if ((*it).F>r)break;

		del.pb(*it);
		if ((*it).S.F>r){
			add((*it).S.S,-(r-((*it).F)+1));
			myset.insert({r+1,{(*it).S.F,(*it).S.S}});
			break;
		}else add((*it).S.S,-((*it).S.F-(*it).F+1));		
	}
	myset.insert({l,{r,val}});
	for (auto x:del)myset.erase(x);
}
vvi adj(MAXN+1);
vi s(MAXN+1),tin(MAXN+1),tout(MAXN+1),dep(MAXN+1),par(MAXN+1);
int timer=0;
void dfs(int v,int p){
	tin[v]=++timer;
	par[v]=p;
	s[v]=1;
	P[v][0]=p;
	for (int i=1;i<20;i++)P[v][i]=P[P[v][i-1]][i-1];
	dep[v]=dep[par[v]]+1;
	for (int to:adj[v])if(to!=p)dfs(to,v),s[v]+=s[to];
	tout[v]=++timer;
}
vp T(4*MAXN+1);
vi c(MAXN+1);
void build(int v,int tl,int tr){
	if (tl==tr)T[v]={tin[c[tl]],tout[c[tl]]};else{
		int tm=(tl+tr)/2;
		build(v*2,tl,tm);
		build(v*2+1,tm+1,tr);
		T[v]={min(T[2*v].F,T[2*v+1].F),max(T[2*v].S,T[2*v+1].S)};
	}
}
pii gettime(int v,int tl,int tr,int l,int r){
	if (l>r)return {INF,0};
	if (tl==l && tr==r)return T[v];
	int tm=(tl+tr)/2;
	pii L=gettime(v*2,tl,tm,l,min(r,tm));
	pii R=gettime(v*2+1,tm+1,tr,max(l,tm+1),r);
	return {min(L.F,R.F),max(L.S,R.S)};
}

vi head(MAXN+1),ind(MAXN+1);int cnt;
void decomp(int v,int h){
	head[v]=h;
	ind[v]=++cnt;
	pii best={0,0};
	for (int to:adj[v])if (to!=par[v])best=max(best,{s[to],to});
	if(!best.S)return;
	decomp(best.S,h);
	for (int to:adj[v])if(to!=best.S && to!=par[v])decomp(to,to);
}
void up(int a,int b,int val){
	for (;head[a]!=head[b];b=par[head[b]]){
		if (dep[head[a]]>dep[head[b]])swap(a,b);
		update(ind[head[b]],ind[b],val);
	}
	if (dep[a]>dep[b])swap(a,b);
	update(ind[a],ind[b],val);
}
int calc(int v,int t1,int t2){
	if (t1>=tin[v] && t2<=tout[v])return dep[v]-1;
	int res=v;
	for (int bit=19;bit>=0;bit--){
		if (!P[res][bit])continue;
		if (t1<tin[P[res][bit]] || t2>tout[P[res][bit]])res=P[res][bit];
	}
	return dep[res]-2;
}
signed main(){	
	ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
	int m,q;
	cin>>n>>m>>q;
	for (int i=1;i<n;i++){
		int u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for (int i=1;i<=m;i++)cin>>c[i];
	dfs(1,0);
	build(1,1,m);
	decomp(1,1);
	myset.insert({1,{n,100001}});add(100001,n);
	vvp Q(MAXN+1);
	for (int i=1;i<=q;i++){
		int l,r;
		cin>>l>>r;
		Q[l].pb({r,i});
	}
	int ans[q+1];
	for (int l=m;l>=1;l--){
		up(c[l],1,l);
		for (auto &[r,id]:Q[l]){
			pii x=gettime(1,1,m,l,r);
			ans[id]=get(r)-get(l-1)-calc(c[l],x.F,x.S);
		}
	}
	for (int i=1;i<=q;i++)cout<<ans[i]<<"\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 37212 KB Output is correct
2 Correct 16 ms 37260 KB Output is correct
3 Correct 16 ms 37212 KB Output is correct
4 Correct 19 ms 37464 KB Output is correct
5 Correct 20 ms 37244 KB Output is correct
6 Correct 16 ms 37212 KB Output is correct
7 Correct 17 ms 37308 KB Output is correct
8 Correct 17 ms 37212 KB Output is correct
9 Correct 18 ms 37208 KB Output is correct
10 Correct 21 ms 37360 KB Output is correct
11 Correct 17 ms 37212 KB Output is correct
12 Correct 17 ms 37212 KB Output is correct
13 Correct 18 ms 37212 KB Output is correct
14 Correct 17 ms 37212 KB Output is correct
15 Correct 16 ms 37212 KB Output is correct
16 Correct 16 ms 37212 KB Output is correct
17 Correct 17 ms 37264 KB Output is correct
18 Correct 15 ms 37184 KB Output is correct
19 Correct 16 ms 37332 KB Output is correct
20 Correct 18 ms 37212 KB Output is correct
21 Correct 17 ms 37212 KB Output is correct
22 Correct 16 ms 37208 KB Output is correct
23 Correct 18 ms 37340 KB Output is correct
24 Correct 17 ms 37212 KB Output is correct
25 Correct 18 ms 37212 KB Output is correct
26 Correct 17 ms 37212 KB Output is correct
27 Correct 18 ms 37208 KB Output is correct
28 Correct 16 ms 37252 KB Output is correct
29 Correct 17 ms 37208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 37212 KB Output is correct
2 Correct 16 ms 37260 KB Output is correct
3 Correct 16 ms 37212 KB Output is correct
4 Correct 19 ms 37464 KB Output is correct
5 Correct 20 ms 37244 KB Output is correct
6 Correct 16 ms 37212 KB Output is correct
7 Correct 17 ms 37308 KB Output is correct
8 Correct 17 ms 37212 KB Output is correct
9 Correct 18 ms 37208 KB Output is correct
10 Correct 21 ms 37360 KB Output is correct
11 Correct 17 ms 37212 KB Output is correct
12 Correct 17 ms 37212 KB Output is correct
13 Correct 18 ms 37212 KB Output is correct
14 Correct 17 ms 37212 KB Output is correct
15 Correct 16 ms 37212 KB Output is correct
16 Correct 16 ms 37212 KB Output is correct
17 Correct 17 ms 37264 KB Output is correct
18 Correct 15 ms 37184 KB Output is correct
19 Correct 16 ms 37332 KB Output is correct
20 Correct 18 ms 37212 KB Output is correct
21 Correct 17 ms 37212 KB Output is correct
22 Correct 16 ms 37208 KB Output is correct
23 Correct 18 ms 37340 KB Output is correct
24 Correct 17 ms 37212 KB Output is correct
25 Correct 18 ms 37212 KB Output is correct
26 Correct 17 ms 37212 KB Output is correct
27 Correct 18 ms 37208 KB Output is correct
28 Correct 16 ms 37252 KB Output is correct
29 Correct 17 ms 37208 KB Output is correct
30 Correct 24 ms 37456 KB Output is correct
31 Correct 22 ms 37468 KB Output is correct
32 Correct 20 ms 37468 KB Output is correct
33 Correct 20 ms 37468 KB Output is correct
34 Correct 21 ms 37468 KB Output is correct
35 Correct 20 ms 37468 KB Output is correct
36 Correct 22 ms 37352 KB Output is correct
37 Correct 20 ms 37464 KB Output is correct
38 Correct 19 ms 37468 KB Output is correct
39 Correct 19 ms 37464 KB Output is correct
40 Correct 18 ms 37464 KB Output is correct
41 Correct 24 ms 37468 KB Output is correct
42 Correct 19 ms 37380 KB Output is correct
43 Correct 19 ms 37468 KB Output is correct
44 Correct 19 ms 37468 KB Output is correct
45 Correct 19 ms 37464 KB Output is correct
46 Correct 19 ms 37468 KB Output is correct
47 Correct 19 ms 37464 KB Output is correct
48 Correct 19 ms 37468 KB Output is correct
49 Correct 19 ms 37468 KB Output is correct
50 Correct 19 ms 37468 KB Output is correct
51 Correct 18 ms 37468 KB Output is correct
52 Correct 19 ms 37612 KB Output is correct
53 Correct 20 ms 37468 KB Output is correct
54 Correct 18 ms 37552 KB Output is correct
55 Correct 19 ms 37468 KB Output is correct
56 Correct 15 ms 37212 KB Output is correct
57 Correct 18 ms 37468 KB Output is correct
58 Correct 19 ms 37368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 37464 KB Output is correct
2 Correct 18 ms 37212 KB Output is correct
3 Correct 17 ms 37376 KB Output is correct
4 Correct 117 ms 47572 KB Output is correct
5 Correct 108 ms 49236 KB Output is correct
6 Correct 127 ms 50436 KB Output is correct
7 Correct 169 ms 53328 KB Output is correct
8 Correct 171 ms 53424 KB Output is correct
9 Correct 173 ms 53332 KB Output is correct
10 Correct 177 ms 53328 KB Output is correct
11 Correct 169 ms 53332 KB Output is correct
12 Correct 119 ms 53412 KB Output is correct
13 Correct 127 ms 53436 KB Output is correct
14 Correct 120 ms 53344 KB Output is correct
15 Correct 58 ms 50184 KB Output is correct
16 Correct 113 ms 53072 KB Output is correct
17 Correct 82 ms 42772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 37208 KB Output is correct
2 Correct 200 ms 42836 KB Output is correct
3 Correct 307 ms 44112 KB Output is correct
4 Correct 240 ms 44268 KB Output is correct
5 Correct 385 ms 47652 KB Output is correct
6 Correct 397 ms 47728 KB Output is correct
7 Correct 396 ms 47560 KB Output is correct
8 Correct 397 ms 47696 KB Output is correct
9 Correct 394 ms 47700 KB Output is correct
10 Correct 418 ms 47632 KB Output is correct
11 Correct 377 ms 47952 KB Output is correct
12 Correct 398 ms 47628 KB Output is correct
13 Correct 412 ms 48160 KB Output is correct
14 Correct 423 ms 49236 KB Output is correct
15 Correct 451 ms 52816 KB Output is correct
16 Correct 398 ms 48208 KB Output is correct
17 Correct 387 ms 48212 KB Output is correct
18 Correct 389 ms 48208 KB Output is correct
19 Correct 233 ms 43088 KB Output is correct
20 Correct 230 ms 43088 KB Output is correct
21 Correct 252 ms 43192 KB Output is correct
22 Correct 240 ms 43184 KB Output is correct
23 Correct 227 ms 43092 KB Output is correct
24 Correct 226 ms 43092 KB Output is correct
25 Correct 241 ms 43092 KB Output is correct
26 Correct 237 ms 43300 KB Output is correct
27 Correct 231 ms 43236 KB Output is correct
28 Correct 252 ms 43128 KB Output is correct
29 Correct 262 ms 43348 KB Output is correct
30 Correct 256 ms 43348 KB Output is correct
31 Correct 245 ms 43600 KB Output is correct
32 Correct 298 ms 44176 KB Output is correct
33 Correct 298 ms 45652 KB Output is correct
34 Correct 288 ms 48204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 37208 KB Output is correct
2 Correct 23 ms 37132 KB Output is correct
3 Correct 27 ms 37200 KB Output is correct
4 Correct 567 ms 48812 KB Output is correct
5 Correct 599 ms 48756 KB Output is correct
6 Correct 661 ms 51792 KB Output is correct
7 Correct 697 ms 53584 KB Output is correct
8 Correct 678 ms 53384 KB Output is correct
9 Correct 659 ms 53332 KB Output is correct
10 Correct 650 ms 53588 KB Output is correct
11 Correct 653 ms 53336 KB Output is correct
12 Correct 701 ms 53496 KB Output is correct
13 Correct 671 ms 53368 KB Output is correct
14 Correct 98 ms 42820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 37212 KB Output is correct
2 Correct 16 ms 37260 KB Output is correct
3 Correct 16 ms 37212 KB Output is correct
4 Correct 19 ms 37464 KB Output is correct
5 Correct 20 ms 37244 KB Output is correct
6 Correct 16 ms 37212 KB Output is correct
7 Correct 17 ms 37308 KB Output is correct
8 Correct 17 ms 37212 KB Output is correct
9 Correct 18 ms 37208 KB Output is correct
10 Correct 21 ms 37360 KB Output is correct
11 Correct 17 ms 37212 KB Output is correct
12 Correct 17 ms 37212 KB Output is correct
13 Correct 18 ms 37212 KB Output is correct
14 Correct 17 ms 37212 KB Output is correct
15 Correct 16 ms 37212 KB Output is correct
16 Correct 16 ms 37212 KB Output is correct
17 Correct 17 ms 37264 KB Output is correct
18 Correct 15 ms 37184 KB Output is correct
19 Correct 16 ms 37332 KB Output is correct
20 Correct 18 ms 37212 KB Output is correct
21 Correct 17 ms 37212 KB Output is correct
22 Correct 16 ms 37208 KB Output is correct
23 Correct 18 ms 37340 KB Output is correct
24 Correct 17 ms 37212 KB Output is correct
25 Correct 18 ms 37212 KB Output is correct
26 Correct 17 ms 37212 KB Output is correct
27 Correct 18 ms 37208 KB Output is correct
28 Correct 16 ms 37252 KB Output is correct
29 Correct 17 ms 37208 KB Output is correct
30 Correct 24 ms 37456 KB Output is correct
31 Correct 22 ms 37468 KB Output is correct
32 Correct 20 ms 37468 KB Output is correct
33 Correct 20 ms 37468 KB Output is correct
34 Correct 21 ms 37468 KB Output is correct
35 Correct 20 ms 37468 KB Output is correct
36 Correct 22 ms 37352 KB Output is correct
37 Correct 20 ms 37464 KB Output is correct
38 Correct 19 ms 37468 KB Output is correct
39 Correct 19 ms 37464 KB Output is correct
40 Correct 18 ms 37464 KB Output is correct
41 Correct 24 ms 37468 KB Output is correct
42 Correct 19 ms 37380 KB Output is correct
43 Correct 19 ms 37468 KB Output is correct
44 Correct 19 ms 37468 KB Output is correct
45 Correct 19 ms 37464 KB Output is correct
46 Correct 19 ms 37468 KB Output is correct
47 Correct 19 ms 37464 KB Output is correct
48 Correct 19 ms 37468 KB Output is correct
49 Correct 19 ms 37468 KB Output is correct
50 Correct 19 ms 37468 KB Output is correct
51 Correct 18 ms 37468 KB Output is correct
52 Correct 19 ms 37612 KB Output is correct
53 Correct 20 ms 37468 KB Output is correct
54 Correct 18 ms 37552 KB Output is correct
55 Correct 19 ms 37468 KB Output is correct
56 Correct 15 ms 37212 KB Output is correct
57 Correct 18 ms 37468 KB Output is correct
58 Correct 19 ms 37368 KB Output is correct
59 Correct 16 ms 37464 KB Output is correct
60 Correct 18 ms 37212 KB Output is correct
61 Correct 17 ms 37376 KB Output is correct
62 Correct 117 ms 47572 KB Output is correct
63 Correct 108 ms 49236 KB Output is correct
64 Correct 127 ms 50436 KB Output is correct
65 Correct 169 ms 53328 KB Output is correct
66 Correct 171 ms 53424 KB Output is correct
67 Correct 173 ms 53332 KB Output is correct
68 Correct 177 ms 53328 KB Output is correct
69 Correct 169 ms 53332 KB Output is correct
70 Correct 119 ms 53412 KB Output is correct
71 Correct 127 ms 53436 KB Output is correct
72 Correct 120 ms 53344 KB Output is correct
73 Correct 58 ms 50184 KB Output is correct
74 Correct 113 ms 53072 KB Output is correct
75 Correct 82 ms 42772 KB Output is correct
76 Correct 16 ms 37208 KB Output is correct
77 Correct 200 ms 42836 KB Output is correct
78 Correct 307 ms 44112 KB Output is correct
79 Correct 240 ms 44268 KB Output is correct
80 Correct 385 ms 47652 KB Output is correct
81 Correct 397 ms 47728 KB Output is correct
82 Correct 396 ms 47560 KB Output is correct
83 Correct 397 ms 47696 KB Output is correct
84 Correct 394 ms 47700 KB Output is correct
85 Correct 418 ms 47632 KB Output is correct
86 Correct 377 ms 47952 KB Output is correct
87 Correct 398 ms 47628 KB Output is correct
88 Correct 412 ms 48160 KB Output is correct
89 Correct 423 ms 49236 KB Output is correct
90 Correct 451 ms 52816 KB Output is correct
91 Correct 398 ms 48208 KB Output is correct
92 Correct 387 ms 48212 KB Output is correct
93 Correct 389 ms 48208 KB Output is correct
94 Correct 233 ms 43088 KB Output is correct
95 Correct 230 ms 43088 KB Output is correct
96 Correct 252 ms 43192 KB Output is correct
97 Correct 240 ms 43184 KB Output is correct
98 Correct 227 ms 43092 KB Output is correct
99 Correct 226 ms 43092 KB Output is correct
100 Correct 241 ms 43092 KB Output is correct
101 Correct 237 ms 43300 KB Output is correct
102 Correct 231 ms 43236 KB Output is correct
103 Correct 252 ms 43128 KB Output is correct
104 Correct 262 ms 43348 KB Output is correct
105 Correct 256 ms 43348 KB Output is correct
106 Correct 245 ms 43600 KB Output is correct
107 Correct 298 ms 44176 KB Output is correct
108 Correct 298 ms 45652 KB Output is correct
109 Correct 288 ms 48204 KB Output is correct
110 Correct 15 ms 37208 KB Output is correct
111 Correct 23 ms 37132 KB Output is correct
112 Correct 27 ms 37200 KB Output is correct
113 Correct 567 ms 48812 KB Output is correct
114 Correct 599 ms 48756 KB Output is correct
115 Correct 661 ms 51792 KB Output is correct
116 Correct 697 ms 53584 KB Output is correct
117 Correct 678 ms 53384 KB Output is correct
118 Correct 659 ms 53332 KB Output is correct
119 Correct 650 ms 53588 KB Output is correct
120 Correct 653 ms 53336 KB Output is correct
121 Correct 701 ms 53496 KB Output is correct
122 Correct 671 ms 53368 KB Output is correct
123 Correct 98 ms 42820 KB Output is correct
124 Correct 428 ms 52312 KB Output is correct
125 Correct 317 ms 49488 KB Output is correct
126 Correct 544 ms 52860 KB Output is correct
127 Correct 585 ms 52820 KB Output is correct
128 Correct 516 ms 52820 KB Output is correct
129 Correct 576 ms 52820 KB Output is correct
130 Correct 532 ms 52820 KB Output is correct
131 Correct 244 ms 52560 KB Output is correct
132 Correct 375 ms 53392 KB Output is correct
133 Correct 296 ms 50260 KB Output is correct
134 Correct 387 ms 48248 KB Output is correct
135 Correct 394 ms 48264 KB Output is correct
136 Correct 409 ms 48256 KB Output is correct
137 Correct 294 ms 54060 KB Output is correct
138 Correct 303 ms 54212 KB Output is correct
139 Correct 341 ms 54056 KB Output is correct
140 Correct 347 ms 54112 KB Output is correct
141 Correct 360 ms 54144 KB Output is correct
142 Correct 322 ms 54112 KB Output is correct
143 Correct 129 ms 45084 KB Output is correct
144 Correct 575 ms 52416 KB Output is correct