답안 #741741

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
741741 2023-05-14T18:09:24 Z arnold518 Tourism (JOI23_tourism) C++17
100 / 100
728 ms 24420 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5;

int N, M, Q;
vector<int> adj[MAXN+10];
int A[MAXN+10], ans[MAXN+10];
vector<pii> B[MAXN+10];


int sz[MAXN+10], par[MAXN+10], dep[MAXN+10];
void dfs(int now, int bef, int d)
{
	sz[now]=1;
	par[now]=bef;
	dep[now]=d;
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		dfs(nxt, now, d+1);
		sz[now]+=sz[nxt];
	}
}

int dfn[MAXN+10], head[MAXN+10], cnt;
void hld(int now, int bef)
{
	dfn[now]=++cnt;
	int heavy=0;
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		if(sz[heavy]<sz[nxt]) heavy=nxt;
	}

	if(heavy==0) return;

	head[heavy]=head[now];
	hld(heavy, now);
	for(int nxt : adj[now])
	{
		if(nxt==bef || nxt==heavy) continue;
		head[nxt]=nxt;
		hld(nxt, now);
	}
}

struct BIT
{
	int tree[MAXN+10];
	void update(int i, int k) { for(; i<=M; i+=(i&-i)) tree[i]+=k; }
	int query(int i) { int ret=0; for(; i>0; i-=(i&-i)) ret+=tree[i]; return ret; }
}bit;

set<pair<pii, int>> S;

void update(int l, int r, int k)
{
	//printf("%d %d %d\n", l, r, k);
	auto it=S.lower_bound({{l, 0}, 0});
	if(it!=S.begin() && prev(it)->first.second>=l) it--;
	vector<pair<pii, int>> V;
	for(; it!=S.end() && it->first.first<=r;)
	{
		int l2=it->first.first, r2=it->first.second, v=it->second;
		if(l2<l) V.push_back({{l2, l-1}, v});
		if(r<r2) V.push_back({{r+1, r2}, v});
		it=S.erase(it);
		bit.update(v, -(r2-l2+1));
	}
	for(auto it : V) S.insert(it), bit.update(it.second, it.first.second-it.first.first+1);
	S.insert({{l, r}, k}); bit.update(k, r-l+1);
	
	//for(auto it : S) printf("(%d %d %d) ", it.first.first, it.first.second, it.second); printf("\n");
}

void query(int u, int v, int k)
{
	while(head[u]!=head[v])
	{
		if(dep[head[u]]>dep[head[v]]) swap(u, v);
		update(dfn[head[v]], dfn[v], k);
		v=par[head[v]];
	}
	if(dep[u]>dep[v]) swap(u, v);
	update(dfn[u], dfn[v], k);
}

int main()
{
	scanf("%d%d%d", &N, &M, &Q);
	for(int i=1; i<N; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	for(int i=1; i<=M; i++) scanf("%d", &A[i]);
	for(int i=1; i<=Q; i++)
	{
		int l, r;
		scanf("%d%d", &l, &r);
		B[r].push_back({l, i});
	}

	dfs(1, 1, 1);
	head[1]=1;
	hld(1, 1);

	for(int i=1; i<=M; i++)
	{
		for(auto it : B[i])
		{
			ans[it.second]=bit.query(M)-bit.query(it.first-1);
		}
		if(i+1<=M) query(A[i], A[i+1], i);
	}
	for(int i=1; i<=Q; i++) printf("%d\n", max(1, ans[i]));
}

Compilation message

tourism.cpp: In function 'int main()':
tourism.cpp:96:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |  scanf("%d%d%d", &N, &M, &Q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:100:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |   scanf("%d%d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~
tourism.cpp:104:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |  for(int i=1; i<=M; i++) scanf("%d", &A[i]);
      |                          ~~~~~^~~~~~~~~~~~~
tourism.cpp:108:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |   scanf("%d%d", &l, &r);
      |   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 5008 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 4 ms 4976 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 4 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 5 ms 5016 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 4 ms 5076 KB Output is correct
18 Correct 4 ms 5016 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5016 KB Output is correct
24 Correct 3 ms 5008 KB Output is correct
25 Correct 4 ms 5076 KB Output is correct
26 Correct 4 ms 5020 KB Output is correct
27 Correct 3 ms 5008 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 4 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 5008 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 4 ms 4976 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 4 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 5 ms 5016 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 4 ms 5076 KB Output is correct
18 Correct 4 ms 5016 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5016 KB Output is correct
24 Correct 3 ms 5008 KB Output is correct
25 Correct 4 ms 5076 KB Output is correct
26 Correct 4 ms 5020 KB Output is correct
27 Correct 3 ms 5008 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 4 ms 5076 KB Output is correct
30 Correct 7 ms 5204 KB Output is correct
31 Correct 7 ms 5204 KB Output is correct
32 Correct 8 ms 5320 KB Output is correct
33 Correct 7 ms 5204 KB Output is correct
34 Correct 7 ms 5288 KB Output is correct
35 Correct 8 ms 5204 KB Output is correct
36 Correct 7 ms 5204 KB Output is correct
37 Correct 7 ms 5268 KB Output is correct
38 Correct 5 ms 5332 KB Output is correct
39 Correct 5 ms 5272 KB Output is correct
40 Correct 6 ms 5280 KB Output is correct
41 Correct 5 ms 5344 KB Output is correct
42 Correct 5 ms 5332 KB Output is correct
43 Correct 6 ms 5232 KB Output is correct
44 Correct 6 ms 5276 KB Output is correct
45 Correct 6 ms 5204 KB Output is correct
46 Correct 6 ms 5204 KB Output is correct
47 Correct 6 ms 5248 KB Output is correct
48 Correct 6 ms 5224 KB Output is correct
49 Correct 5 ms 5204 KB Output is correct
50 Correct 6 ms 5332 KB Output is correct
51 Correct 5 ms 5332 KB Output is correct
52 Correct 7 ms 5332 KB Output is correct
53 Correct 5 ms 5272 KB Output is correct
54 Correct 5 ms 5332 KB Output is correct
55 Correct 5 ms 5276 KB Output is correct
56 Correct 4 ms 5076 KB Output is correct
57 Correct 4 ms 5076 KB Output is correct
58 Correct 9 ms 5204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 98 ms 15132 KB Output is correct
5 Correct 87 ms 19524 KB Output is correct
6 Correct 107 ms 20820 KB Output is correct
7 Correct 139 ms 24024 KB Output is correct
8 Correct 148 ms 24016 KB Output is correct
9 Correct 133 ms 24136 KB Output is correct
10 Correct 152 ms 24140 KB Output is correct
11 Correct 151 ms 23588 KB Output is correct
12 Correct 128 ms 23052 KB Output is correct
13 Correct 121 ms 23484 KB Output is correct
14 Correct 136 ms 23208 KB Output is correct
15 Correct 63 ms 20528 KB Output is correct
16 Correct 129 ms 23948 KB Output is correct
17 Correct 61 ms 9480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5000 KB Output is correct
2 Correct 226 ms 10092 KB Output is correct
3 Correct 340 ms 11144 KB Output is correct
4 Correct 306 ms 12280 KB Output is correct
5 Correct 486 ms 15416 KB Output is correct
6 Correct 491 ms 15492 KB Output is correct
7 Correct 432 ms 15616 KB Output is correct
8 Correct 442 ms 15732 KB Output is correct
9 Correct 441 ms 15544 KB Output is correct
10 Correct 444 ms 15512 KB Output is correct
11 Correct 442 ms 15440 KB Output is correct
12 Correct 447 ms 15524 KB Output is correct
13 Correct 423 ms 15900 KB Output is correct
14 Correct 478 ms 16844 KB Output is correct
15 Correct 481 ms 20096 KB Output is correct
16 Correct 423 ms 15928 KB Output is correct
17 Correct 441 ms 16048 KB Output is correct
18 Correct 501 ms 15988 KB Output is correct
19 Correct 283 ms 12440 KB Output is correct
20 Correct 269 ms 12780 KB Output is correct
21 Correct 292 ms 12592 KB Output is correct
22 Correct 314 ms 12632 KB Output is correct
23 Correct 283 ms 12684 KB Output is correct
24 Correct 306 ms 12496 KB Output is correct
25 Correct 265 ms 12684 KB Output is correct
26 Correct 272 ms 12440 KB Output is correct
27 Correct 293 ms 12604 KB Output is correct
28 Correct 274 ms 12868 KB Output is correct
29 Correct 293 ms 13028 KB Output is correct
30 Correct 279 ms 13008 KB Output is correct
31 Correct 285 ms 13036 KB Output is correct
32 Correct 281 ms 13632 KB Output is correct
33 Correct 312 ms 15180 KB Output is correct
34 Correct 288 ms 17076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 581 ms 13780 KB Output is correct
5 Correct 581 ms 15820 KB Output is correct
6 Correct 700 ms 18628 KB Output is correct
7 Correct 683 ms 20104 KB Output is correct
8 Correct 695 ms 20132 KB Output is correct
9 Correct 726 ms 20064 KB Output is correct
10 Correct 712 ms 19944 KB Output is correct
11 Correct 728 ms 20544 KB Output is correct
12 Correct 700 ms 19796 KB Output is correct
13 Correct 704 ms 20420 KB Output is correct
14 Correct 64 ms 9652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 5008 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 4 ms 4976 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 4 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 5 ms 5016 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 4 ms 5076 KB Output is correct
18 Correct 4 ms 5016 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5016 KB Output is correct
24 Correct 3 ms 5008 KB Output is correct
25 Correct 4 ms 5076 KB Output is correct
26 Correct 4 ms 5020 KB Output is correct
27 Correct 3 ms 5008 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 4 ms 5076 KB Output is correct
30 Correct 7 ms 5204 KB Output is correct
31 Correct 7 ms 5204 KB Output is correct
32 Correct 8 ms 5320 KB Output is correct
33 Correct 7 ms 5204 KB Output is correct
34 Correct 7 ms 5288 KB Output is correct
35 Correct 8 ms 5204 KB Output is correct
36 Correct 7 ms 5204 KB Output is correct
37 Correct 7 ms 5268 KB Output is correct
38 Correct 5 ms 5332 KB Output is correct
39 Correct 5 ms 5272 KB Output is correct
40 Correct 6 ms 5280 KB Output is correct
41 Correct 5 ms 5344 KB Output is correct
42 Correct 5 ms 5332 KB Output is correct
43 Correct 6 ms 5232 KB Output is correct
44 Correct 6 ms 5276 KB Output is correct
45 Correct 6 ms 5204 KB Output is correct
46 Correct 6 ms 5204 KB Output is correct
47 Correct 6 ms 5248 KB Output is correct
48 Correct 6 ms 5224 KB Output is correct
49 Correct 5 ms 5204 KB Output is correct
50 Correct 6 ms 5332 KB Output is correct
51 Correct 5 ms 5332 KB Output is correct
52 Correct 7 ms 5332 KB Output is correct
53 Correct 5 ms 5272 KB Output is correct
54 Correct 5 ms 5332 KB Output is correct
55 Correct 5 ms 5276 KB Output is correct
56 Correct 4 ms 5076 KB Output is correct
57 Correct 4 ms 5076 KB Output is correct
58 Correct 9 ms 5204 KB Output is correct
59 Correct 3 ms 4948 KB Output is correct
60 Correct 4 ms 4948 KB Output is correct
61 Correct 3 ms 5076 KB Output is correct
62 Correct 98 ms 15132 KB Output is correct
63 Correct 87 ms 19524 KB Output is correct
64 Correct 107 ms 20820 KB Output is correct
65 Correct 139 ms 24024 KB Output is correct
66 Correct 148 ms 24016 KB Output is correct
67 Correct 133 ms 24136 KB Output is correct
68 Correct 152 ms 24140 KB Output is correct
69 Correct 151 ms 23588 KB Output is correct
70 Correct 128 ms 23052 KB Output is correct
71 Correct 121 ms 23484 KB Output is correct
72 Correct 136 ms 23208 KB Output is correct
73 Correct 63 ms 20528 KB Output is correct
74 Correct 129 ms 23948 KB Output is correct
75 Correct 61 ms 9480 KB Output is correct
76 Correct 3 ms 5000 KB Output is correct
77 Correct 226 ms 10092 KB Output is correct
78 Correct 340 ms 11144 KB Output is correct
79 Correct 306 ms 12280 KB Output is correct
80 Correct 486 ms 15416 KB Output is correct
81 Correct 491 ms 15492 KB Output is correct
82 Correct 432 ms 15616 KB Output is correct
83 Correct 442 ms 15732 KB Output is correct
84 Correct 441 ms 15544 KB Output is correct
85 Correct 444 ms 15512 KB Output is correct
86 Correct 442 ms 15440 KB Output is correct
87 Correct 447 ms 15524 KB Output is correct
88 Correct 423 ms 15900 KB Output is correct
89 Correct 478 ms 16844 KB Output is correct
90 Correct 481 ms 20096 KB Output is correct
91 Correct 423 ms 15928 KB Output is correct
92 Correct 441 ms 16048 KB Output is correct
93 Correct 501 ms 15988 KB Output is correct
94 Correct 283 ms 12440 KB Output is correct
95 Correct 269 ms 12780 KB Output is correct
96 Correct 292 ms 12592 KB Output is correct
97 Correct 314 ms 12632 KB Output is correct
98 Correct 283 ms 12684 KB Output is correct
99 Correct 306 ms 12496 KB Output is correct
100 Correct 265 ms 12684 KB Output is correct
101 Correct 272 ms 12440 KB Output is correct
102 Correct 293 ms 12604 KB Output is correct
103 Correct 274 ms 12868 KB Output is correct
104 Correct 293 ms 13028 KB Output is correct
105 Correct 279 ms 13008 KB Output is correct
106 Correct 285 ms 13036 KB Output is correct
107 Correct 281 ms 13632 KB Output is correct
108 Correct 312 ms 15180 KB Output is correct
109 Correct 288 ms 17076 KB Output is correct
110 Correct 3 ms 4948 KB Output is correct
111 Correct 3 ms 4948 KB Output is correct
112 Correct 4 ms 5076 KB Output is correct
113 Correct 581 ms 13780 KB Output is correct
114 Correct 581 ms 15820 KB Output is correct
115 Correct 700 ms 18628 KB Output is correct
116 Correct 683 ms 20104 KB Output is correct
117 Correct 695 ms 20132 KB Output is correct
118 Correct 726 ms 20064 KB Output is correct
119 Correct 712 ms 19944 KB Output is correct
120 Correct 728 ms 20544 KB Output is correct
121 Correct 700 ms 19796 KB Output is correct
122 Correct 704 ms 20420 KB Output is correct
123 Correct 64 ms 9652 KB Output is correct
124 Correct 426 ms 19436 KB Output is correct
125 Correct 313 ms 17160 KB Output is correct
126 Correct 482 ms 20064 KB Output is correct
127 Correct 504 ms 19744 KB Output is correct
128 Correct 493 ms 19980 KB Output is correct
129 Correct 527 ms 19824 KB Output is correct
130 Correct 497 ms 20144 KB Output is correct
131 Correct 161 ms 23308 KB Output is correct
132 Correct 142 ms 24420 KB Output is correct
133 Correct 164 ms 20004 KB Output is correct
134 Correct 322 ms 17052 KB Output is correct
135 Correct 324 ms 17064 KB Output is correct
136 Correct 313 ms 17060 KB Output is correct
137 Correct 202 ms 21048 KB Output is correct
138 Correct 211 ms 21036 KB Output is correct
139 Correct 221 ms 20560 KB Output is correct
140 Correct 193 ms 20972 KB Output is correct
141 Correct 193 ms 20760 KB Output is correct
142 Correct 196 ms 21036 KB Output is correct
143 Correct 76 ms 13200 KB Output is correct
144 Correct 543 ms 19320 KB Output is correct