답안 #789115

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
789115 2023-07-21T05:13:06 Z 박상훈(#10042) Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 25688 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
constexpr int B = 400, INF = 1e9 + 100;

vector<int> adj[100100];
int a[100100], ans[100100], cnt[100100], val;
multiset<pair<int, int>> st;

int in[100100], out[100100], dep[100100], cnti;

struct Query{
	int s, e, i;
	Query(){}
	Query(int _s, int _e, int _i): s(_s), e(_e), i(_i) {}

	bool operator < (const Query &Q) const{
		if (s / B == Q.s / B){
			if ((s / B) % 2 == 0) return e < Q.e;
			return e > Q.e;
		} 
		return s / B < Q.s / B;
	}
}Q[100100];

struct Seg{
	pair<int, int> tree[400400];
	int sz;

	void init(int n){
		sz = n;
		// for (int i=sz;i<sz*2;i++) printf("(%d, %d) ", tree[i].first, tree[i].second);
		// printf("\n");
		for (int i=sz-1;i;i--) tree[i] = min(tree[i<<1], tree[i<<1|1]);
	}

	int query(int l, int r){
		// printf("  query %d to %d\n", l, r);
		++r;
		pair<int, int> ret = {INF, 0};
		for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
			if (l&1) ret = min(ret, tree[l++]);
			if (r&1) ret = min(ret, tree[--r]);
		}

		return ret.second;
	}
}rmq;

void dfs(int s, int pa = 0){
	in[s] = ++cnti;
	dep[s] = dep[pa] + 1;
	rmq.tree[cnti + rmq.sz] = {dep[s], s};
	
	for (auto &v:adj[s]) if (v!=pa){
		dfs(v, s);

		++cnti;
		rmq.tree[cnti + rmq.sz] = {dep[s], s};
	}

	// out[s] = ++cnti;
	// rmq.tree[cnti + rmq.sz] = {dep[s], s};
}

int dist(int x, int y){
	int lca = rmq.query(min(in[x], in[y]), max(in[x], in[y]));
	// printf(" calc %d %d -> %d (lca = %d)\n", x, y, dep[x] + dep[y] - dep[lca] * 2, lca);
	return dep[x] + dep[y] - dep[lca] * 2;
}

void push(int x){
	x = a[x];
	// printf("push %d\n", x);
	cnt[x]++;
	if (cnt[x]==1){
		auto iter = st.emplace(in[x], x);

		auto prv = (iter==st.begin())?(prev(st.end())):(prev(iter));
		auto nxt = (next(iter)==st.end())?(st.begin()):(next(iter));

		val -= dist(prv->second, nxt->second);
		val += dist(prv->second, iter->second);
		val += dist(iter->second, nxt->second);
	}

	// printf(" ok val = %d\n", val);
}

void pop(int x){
	x = a[x];
	// printf("pop %d\n", x);
	cnt[x]--;
	if (cnt[x]==0){
		auto iter = st.find(pair<int, int>(in[x], x));
		assert(iter!=st.end());

		auto prv = (iter==st.begin())?(prev(st.end())):(prev(iter));
		auto nxt = (next(iter)==st.end())?(st.begin()):(next(iter));

		val += dist(prv->second, nxt->second);
		val -= dist(prv->second, iter->second);
		val -= dist(iter->second, nxt->second);

		st.erase(iter);
	}

	// printf(" ok val = %d\n", val);
}

int main(){
	int n, m, q;
	scanf("%d %d %d", &n, &m, &q);

	for (int i=1;i<=n-1;i++){
		int x, y;
		scanf("%d %d", &x, &y);
		adj[x].push_back(y);
		adj[y].push_back(x);
	}

	for (int i=1;i<=m;i++) scanf("%d", a+i);

	for (int i=1;i<=q;i++){
		scanf("%d %d", &Q[i].s, &Q[i].e);
		Q[i].i = i;
	}

	rmq.sz = n*2+10;
	dfs(1);
	rmq.init(n*2+10);

	sort(Q+1, Q+q+1);
	int l = 1, r = 0;
	for (int i=1;i<=q;i++){
		while(r<Q[i].e){
			++r;
			push(r);
		}

		while(l>Q[i].s){
			--l;
			push(l);
		}

		while(r>Q[i].e){
			pop(r);
			r--;
		}

		while(l<Q[i].s){
			pop(l);
			l++;
		}

		ans[Q[i].i] = val/2 + 1;
	}

	for (int i=1;i<=q;i++) printf("%d\n", ans[i]);

}

Compilation message

tourism.cpp: In function 'int main()':
tourism.cpp:114:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  114 |  scanf("%d %d %d", &n, &m, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:118:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |   scanf("%d %d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~
tourism.cpp:123:30: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  123 |  for (int i=1;i<=m;i++) scanf("%d", a+i);
      |                         ~~~~~^~~~~~~~~~~
tourism.cpp:126:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 |   scanf("%d %d", &Q[i].s, &Q[i].e);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5716 KB Output is correct
2 Correct 3 ms 5716 KB Output is correct
3 Correct 2 ms 5716 KB Output is correct
4 Correct 4 ms 5716 KB Output is correct
5 Correct 3 ms 5716 KB Output is correct
6 Correct 3 ms 5716 KB Output is correct
7 Correct 4 ms 5716 KB Output is correct
8 Correct 4 ms 5716 KB Output is correct
9 Correct 5 ms 5844 KB Output is correct
10 Correct 5 ms 5844 KB Output is correct
11 Correct 5 ms 5840 KB Output is correct
12 Correct 3 ms 5844 KB Output is correct
13 Correct 3 ms 5844 KB Output is correct
14 Correct 2 ms 5844 KB Output is correct
15 Correct 5 ms 5844 KB Output is correct
16 Correct 7 ms 5844 KB Output is correct
17 Correct 5 ms 5844 KB Output is correct
18 Correct 5 ms 5848 KB Output is correct
19 Correct 5 ms 5844 KB Output is correct
20 Correct 5 ms 5772 KB Output is correct
21 Correct 5 ms 5844 KB Output is correct
22 Correct 5 ms 5844 KB Output is correct
23 Correct 5 ms 5844 KB Output is correct
24 Correct 5 ms 5844 KB Output is correct
25 Correct 5 ms 5844 KB Output is correct
26 Correct 6 ms 5844 KB Output is correct
27 Correct 2 ms 5716 KB Output is correct
28 Correct 2 ms 5716 KB Output is correct
29 Correct 2 ms 5716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5716 KB Output is correct
2 Correct 3 ms 5716 KB Output is correct
3 Correct 2 ms 5716 KB Output is correct
4 Correct 4 ms 5716 KB Output is correct
5 Correct 3 ms 5716 KB Output is correct
6 Correct 3 ms 5716 KB Output is correct
7 Correct 4 ms 5716 KB Output is correct
8 Correct 4 ms 5716 KB Output is correct
9 Correct 5 ms 5844 KB Output is correct
10 Correct 5 ms 5844 KB Output is correct
11 Correct 5 ms 5840 KB Output is correct
12 Correct 3 ms 5844 KB Output is correct
13 Correct 3 ms 5844 KB Output is correct
14 Correct 2 ms 5844 KB Output is correct
15 Correct 5 ms 5844 KB Output is correct
16 Correct 7 ms 5844 KB Output is correct
17 Correct 5 ms 5844 KB Output is correct
18 Correct 5 ms 5848 KB Output is correct
19 Correct 5 ms 5844 KB Output is correct
20 Correct 5 ms 5772 KB Output is correct
21 Correct 5 ms 5844 KB Output is correct
22 Correct 5 ms 5844 KB Output is correct
23 Correct 5 ms 5844 KB Output is correct
24 Correct 5 ms 5844 KB Output is correct
25 Correct 5 ms 5844 KB Output is correct
26 Correct 6 ms 5844 KB Output is correct
27 Correct 2 ms 5716 KB Output is correct
28 Correct 2 ms 5716 KB Output is correct
29 Correct 2 ms 5716 KB Output is correct
30 Correct 27 ms 5960 KB Output is correct
31 Correct 34 ms 5940 KB Output is correct
32 Correct 40 ms 5988 KB Output is correct
33 Correct 41 ms 6004 KB Output is correct
34 Correct 40 ms 5964 KB Output is correct
35 Correct 4 ms 5972 KB Output is correct
36 Correct 6 ms 6012 KB Output is correct
37 Correct 5 ms 6000 KB Output is correct
38 Correct 33 ms 6100 KB Output is correct
39 Correct 33 ms 6100 KB Output is correct
40 Correct 34 ms 6108 KB Output is correct
41 Correct 4 ms 6056 KB Output is correct
42 Correct 5 ms 6100 KB Output is correct
43 Correct 4 ms 6100 KB Output is correct
44 Correct 34 ms 6044 KB Output is correct
45 Correct 34 ms 6020 KB Output is correct
46 Correct 34 ms 6016 KB Output is correct
47 Correct 4 ms 5972 KB Output is correct
48 Correct 5 ms 5972 KB Output is correct
49 Correct 5 ms 5972 KB Output is correct
50 Correct 35 ms 5992 KB Output is correct
51 Correct 35 ms 5988 KB Output is correct
52 Correct 33 ms 5992 KB Output is correct
53 Correct 34 ms 5972 KB Output is correct
54 Correct 33 ms 5972 KB Output is correct
55 Correct 34 ms 6008 KB Output is correct
56 Correct 4 ms 5844 KB Output is correct
57 Correct 3 ms 5828 KB Output is correct
58 Correct 6 ms 5844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5716 KB Output is correct
2 Correct 3 ms 5716 KB Output is correct
3 Correct 4 ms 5844 KB Output is correct
4 Correct 3439 ms 16492 KB Output is correct
5 Correct 2809 ms 21128 KB Output is correct
6 Correct 3580 ms 23076 KB Output is correct
7 Execution timed out 5052 ms 25688 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5716 KB Output is correct
2 Correct 133 ms 8600 KB Output is correct
3 Correct 195 ms 8784 KB Output is correct
4 Correct 152 ms 9040 KB Output is correct
5 Correct 66 ms 13544 KB Output is correct
6 Correct 84 ms 12876 KB Output is correct
7 Correct 110 ms 11452 KB Output is correct
8 Correct 119 ms 10896 KB Output is correct
9 Correct 128 ms 10684 KB Output is correct
10 Correct 154 ms 10644 KB Output is correct
11 Correct 215 ms 10704 KB Output is correct
12 Correct 240 ms 10648 KB Output is correct
13 Correct 266 ms 10692 KB Output is correct
14 Correct 270 ms 11060 KB Output is correct
15 Correct 242 ms 12236 KB Output is correct
16 Correct 199 ms 12432 KB Output is correct
17 Correct 197 ms 12412 KB Output is correct
18 Correct 192 ms 12364 KB Output is correct
19 Correct 68 ms 13516 KB Output is correct
20 Correct 68 ms 13556 KB Output is correct
21 Correct 87 ms 12236 KB Output is correct
22 Correct 95 ms 11464 KB Output is correct
23 Correct 108 ms 11080 KB Output is correct
24 Correct 105 ms 10800 KB Output is correct
25 Correct 108 ms 10688 KB Output is correct
26 Correct 121 ms 10728 KB Output is correct
27 Correct 157 ms 10684 KB Output is correct
28 Correct 191 ms 10636 KB Output is correct
29 Correct 205 ms 10672 KB Output is correct
30 Correct 211 ms 10780 KB Output is correct
31 Correct 221 ms 10796 KB Output is correct
32 Correct 229 ms 11048 KB Output is correct
33 Correct 230 ms 11508 KB Output is correct
34 Correct 216 ms 12344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5716 KB Output is correct
2 Correct 2 ms 5716 KB Output is correct
3 Correct 4 ms 5844 KB Output is correct
4 Correct 4639 ms 12692 KB Output is correct
5 Correct 4705 ms 14944 KB Output is correct
6 Execution timed out 5102 ms 16716 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5716 KB Output is correct
2 Correct 3 ms 5716 KB Output is correct
3 Correct 2 ms 5716 KB Output is correct
4 Correct 4 ms 5716 KB Output is correct
5 Correct 3 ms 5716 KB Output is correct
6 Correct 3 ms 5716 KB Output is correct
7 Correct 4 ms 5716 KB Output is correct
8 Correct 4 ms 5716 KB Output is correct
9 Correct 5 ms 5844 KB Output is correct
10 Correct 5 ms 5844 KB Output is correct
11 Correct 5 ms 5840 KB Output is correct
12 Correct 3 ms 5844 KB Output is correct
13 Correct 3 ms 5844 KB Output is correct
14 Correct 2 ms 5844 KB Output is correct
15 Correct 5 ms 5844 KB Output is correct
16 Correct 7 ms 5844 KB Output is correct
17 Correct 5 ms 5844 KB Output is correct
18 Correct 5 ms 5848 KB Output is correct
19 Correct 5 ms 5844 KB Output is correct
20 Correct 5 ms 5772 KB Output is correct
21 Correct 5 ms 5844 KB Output is correct
22 Correct 5 ms 5844 KB Output is correct
23 Correct 5 ms 5844 KB Output is correct
24 Correct 5 ms 5844 KB Output is correct
25 Correct 5 ms 5844 KB Output is correct
26 Correct 6 ms 5844 KB Output is correct
27 Correct 2 ms 5716 KB Output is correct
28 Correct 2 ms 5716 KB Output is correct
29 Correct 2 ms 5716 KB Output is correct
30 Correct 27 ms 5960 KB Output is correct
31 Correct 34 ms 5940 KB Output is correct
32 Correct 40 ms 5988 KB Output is correct
33 Correct 41 ms 6004 KB Output is correct
34 Correct 40 ms 5964 KB Output is correct
35 Correct 4 ms 5972 KB Output is correct
36 Correct 6 ms 6012 KB Output is correct
37 Correct 5 ms 6000 KB Output is correct
38 Correct 33 ms 6100 KB Output is correct
39 Correct 33 ms 6100 KB Output is correct
40 Correct 34 ms 6108 KB Output is correct
41 Correct 4 ms 6056 KB Output is correct
42 Correct 5 ms 6100 KB Output is correct
43 Correct 4 ms 6100 KB Output is correct
44 Correct 34 ms 6044 KB Output is correct
45 Correct 34 ms 6020 KB Output is correct
46 Correct 34 ms 6016 KB Output is correct
47 Correct 4 ms 5972 KB Output is correct
48 Correct 5 ms 5972 KB Output is correct
49 Correct 5 ms 5972 KB Output is correct
50 Correct 35 ms 5992 KB Output is correct
51 Correct 35 ms 5988 KB Output is correct
52 Correct 33 ms 5992 KB Output is correct
53 Correct 34 ms 5972 KB Output is correct
54 Correct 33 ms 5972 KB Output is correct
55 Correct 34 ms 6008 KB Output is correct
56 Correct 4 ms 5844 KB Output is correct
57 Correct 3 ms 5828 KB Output is correct
58 Correct 6 ms 5844 KB Output is correct
59 Correct 2 ms 5716 KB Output is correct
60 Correct 3 ms 5716 KB Output is correct
61 Correct 4 ms 5844 KB Output is correct
62 Correct 3439 ms 16492 KB Output is correct
63 Correct 2809 ms 21128 KB Output is correct
64 Correct 3580 ms 23076 KB Output is correct
65 Execution timed out 5052 ms 25688 KB Time limit exceeded
66 Halted 0 ms 0 KB -