답안 #789114

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

using namespace std;
typedef long long ll;
constexpr int B = 800, 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) 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:111:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |  scanf("%d %d %d", &n, &m, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:115:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  115 |   scanf("%d %d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~
tourism.cpp:120:30: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |  for (int i=1;i<=m;i++) scanf("%d", a+i);
      |                         ~~~~~^~~~~~~~~~~
tourism.cpp:123:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  123 |   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 3 ms 5716 KB Output is correct
4 Correct 4 ms 5716 KB Output is correct
5 Correct 4 ms 5716 KB Output is correct
6 Correct 3 ms 5716 KB Output is correct
7 Correct 5 ms 5716 KB Output is correct
8 Correct 3 ms 5844 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 5848 KB Output is correct
12 Correct 2 ms 5844 KB Output is correct
13 Correct 2 ms 5844 KB Output is correct
14 Correct 3 ms 5844 KB Output is correct
15 Correct 5 ms 5844 KB Output is correct
16 Correct 5 ms 5844 KB Output is correct
17 Correct 5 ms 5844 KB Output is correct
18 Correct 5 ms 5844 KB Output is correct
19 Correct 5 ms 5852 KB Output is correct
20 Correct 5 ms 5844 KB Output is correct
21 Correct 5 ms 5852 KB Output is correct
22 Correct 5 ms 5844 KB Output is correct
23 Correct 5 ms 5796 KB Output is correct
24 Correct 6 ms 5844 KB Output is correct
25 Correct 6 ms 5780 KB Output is correct
26 Correct 6 ms 5844 KB Output is correct
27 Correct 2 ms 5716 KB Output is correct
28 Correct 3 ms 5716 KB Output is correct
29 Correct 3 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 3 ms 5716 KB Output is correct
4 Correct 4 ms 5716 KB Output is correct
5 Correct 4 ms 5716 KB Output is correct
6 Correct 3 ms 5716 KB Output is correct
7 Correct 5 ms 5716 KB Output is correct
8 Correct 3 ms 5844 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 5848 KB Output is correct
12 Correct 2 ms 5844 KB Output is correct
13 Correct 2 ms 5844 KB Output is correct
14 Correct 3 ms 5844 KB Output is correct
15 Correct 5 ms 5844 KB Output is correct
16 Correct 5 ms 5844 KB Output is correct
17 Correct 5 ms 5844 KB Output is correct
18 Correct 5 ms 5844 KB Output is correct
19 Correct 5 ms 5852 KB Output is correct
20 Correct 5 ms 5844 KB Output is correct
21 Correct 5 ms 5852 KB Output is correct
22 Correct 5 ms 5844 KB Output is correct
23 Correct 5 ms 5796 KB Output is correct
24 Correct 6 ms 5844 KB Output is correct
25 Correct 6 ms 5780 KB Output is correct
26 Correct 6 ms 5844 KB Output is correct
27 Correct 2 ms 5716 KB Output is correct
28 Correct 3 ms 5716 KB Output is correct
29 Correct 3 ms 5716 KB Output is correct
30 Correct 49 ms 5928 KB Output is correct
31 Correct 59 ms 5936 KB Output is correct
32 Correct 78 ms 5984 KB Output is correct
33 Correct 78 ms 6112 KB Output is correct
34 Correct 74 ms 5972 KB Output is correct
35 Correct 5 ms 5972 KB Output is correct
36 Correct 10 ms 5972 KB Output is correct
37 Correct 5 ms 5972 KB Output is correct
38 Correct 62 ms 6076 KB Output is correct
39 Correct 60 ms 6092 KB Output is correct
40 Correct 64 ms 6092 KB Output is correct
41 Correct 4 ms 6100 KB Output is correct
42 Correct 5 ms 6100 KB Output is correct
43 Correct 5 ms 6100 KB Output is correct
44 Correct 64 ms 6036 KB Output is correct
45 Correct 64 ms 6008 KB Output is correct
46 Correct 68 ms 6036 KB Output is correct
47 Correct 4 ms 5972 KB Output is correct
48 Correct 4 ms 5972 KB Output is correct
49 Correct 5 ms 5972 KB Output is correct
50 Correct 69 ms 5996 KB Output is correct
51 Correct 61 ms 5988 KB Output is correct
52 Correct 64 ms 6100 KB Output is correct
53 Correct 63 ms 5992 KB Output is correct
54 Correct 62 ms 5992 KB Output is correct
55 Correct 62 ms 5988 KB Output is correct
56 Correct 5 ms 5844 KB Output is correct
57 Correct 3 ms 5844 KB Output is correct
58 Correct 5 ms 5844 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 Execution timed out 5080 ms 16072 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5716 KB Output is correct
2 Correct 78 ms 8388 KB Output is correct
3 Correct 121 ms 8800 KB Output is correct
4 Correct 99 ms 9044 KB Output is correct
5 Correct 67 ms 13496 KB Output is correct
6 Correct 85 ms 12788 KB Output is correct
7 Correct 107 ms 11444 KB Output is correct
8 Correct 124 ms 10892 KB Output is correct
9 Correct 130 ms 10684 KB Output is correct
10 Correct 136 ms 10624 KB Output is correct
11 Correct 146 ms 10636 KB Output is correct
12 Correct 149 ms 10648 KB Output is correct
13 Correct 158 ms 10720 KB Output is correct
14 Correct 170 ms 11148 KB Output is correct
15 Correct 137 ms 12276 KB Output is correct
16 Correct 149 ms 12360 KB Output is correct
17 Correct 142 ms 12404 KB Output is correct
18 Correct 142 ms 12428 KB Output is correct
19 Correct 64 ms 13592 KB Output is correct
20 Correct 75 ms 13552 KB Output is correct
21 Correct 86 ms 12072 KB Output is correct
22 Correct 94 ms 11376 KB Output is correct
23 Correct 101 ms 11072 KB Output is correct
24 Correct 108 ms 10804 KB Output is correct
25 Correct 113 ms 10692 KB Output is correct
26 Correct 113 ms 10712 KB Output is correct
27 Correct 121 ms 10568 KB Output is correct
28 Correct 122 ms 10600 KB Output is correct
29 Correct 129 ms 10684 KB Output is correct
30 Correct 164 ms 10660 KB Output is correct
31 Correct 143 ms 10732 KB Output is correct
32 Correct 155 ms 11148 KB Output is correct
33 Correct 158 ms 11500 KB Output is correct
34 Correct 127 ms 12360 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 Execution timed out 5087 ms 12140 KB Time limit exceeded
5 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 3 ms 5716 KB Output is correct
4 Correct 4 ms 5716 KB Output is correct
5 Correct 4 ms 5716 KB Output is correct
6 Correct 3 ms 5716 KB Output is correct
7 Correct 5 ms 5716 KB Output is correct
8 Correct 3 ms 5844 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 5848 KB Output is correct
12 Correct 2 ms 5844 KB Output is correct
13 Correct 2 ms 5844 KB Output is correct
14 Correct 3 ms 5844 KB Output is correct
15 Correct 5 ms 5844 KB Output is correct
16 Correct 5 ms 5844 KB Output is correct
17 Correct 5 ms 5844 KB Output is correct
18 Correct 5 ms 5844 KB Output is correct
19 Correct 5 ms 5852 KB Output is correct
20 Correct 5 ms 5844 KB Output is correct
21 Correct 5 ms 5852 KB Output is correct
22 Correct 5 ms 5844 KB Output is correct
23 Correct 5 ms 5796 KB Output is correct
24 Correct 6 ms 5844 KB Output is correct
25 Correct 6 ms 5780 KB Output is correct
26 Correct 6 ms 5844 KB Output is correct
27 Correct 2 ms 5716 KB Output is correct
28 Correct 3 ms 5716 KB Output is correct
29 Correct 3 ms 5716 KB Output is correct
30 Correct 49 ms 5928 KB Output is correct
31 Correct 59 ms 5936 KB Output is correct
32 Correct 78 ms 5984 KB Output is correct
33 Correct 78 ms 6112 KB Output is correct
34 Correct 74 ms 5972 KB Output is correct
35 Correct 5 ms 5972 KB Output is correct
36 Correct 10 ms 5972 KB Output is correct
37 Correct 5 ms 5972 KB Output is correct
38 Correct 62 ms 6076 KB Output is correct
39 Correct 60 ms 6092 KB Output is correct
40 Correct 64 ms 6092 KB Output is correct
41 Correct 4 ms 6100 KB Output is correct
42 Correct 5 ms 6100 KB Output is correct
43 Correct 5 ms 6100 KB Output is correct
44 Correct 64 ms 6036 KB Output is correct
45 Correct 64 ms 6008 KB Output is correct
46 Correct 68 ms 6036 KB Output is correct
47 Correct 4 ms 5972 KB Output is correct
48 Correct 4 ms 5972 KB Output is correct
49 Correct 5 ms 5972 KB Output is correct
50 Correct 69 ms 5996 KB Output is correct
51 Correct 61 ms 5988 KB Output is correct
52 Correct 64 ms 6100 KB Output is correct
53 Correct 63 ms 5992 KB Output is correct
54 Correct 62 ms 5992 KB Output is correct
55 Correct 62 ms 5988 KB Output is correct
56 Correct 5 ms 5844 KB Output is correct
57 Correct 3 ms 5844 KB Output is correct
58 Correct 5 ms 5844 KB Output is correct
59 Correct 2 ms 5716 KB Output is correct
60 Correct 2 ms 5716 KB Output is correct
61 Correct 4 ms 5844 KB Output is correct
62 Execution timed out 5080 ms 16072 KB Time limit exceeded
63 Halted 0 ms 0 KB -