Submission #790684

# Submission time Handle Problem Language Result Execution time Memory
790684 2023-07-23T05:45:00 Z qwerasdfzxcl Tourism (JOI23_tourism) C++17
100 / 100
995 ms 20980 KB
#include <bits/stdc++.h>

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

vector<int> adj[100100];
int a[100100], ans[100100];

int dep[100100], par[100100], sz[100100], top[100100], in[100100], out[100100], cnt;

struct Seg{
	int tree[200200], sz;
	void init(int n){
		sz = n;
		fill(tree, tree+sz*2, 0);
	}

	void update(int p, int x){
		p += sz;
		tree[p] += x;

		for (p>>=1;p;p>>=1) tree[p] = tree[p<<1] + tree[p<<1|1];
	}

	int query(int l, int r){
		++r;
		int ret = 0;
		for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
			if (l&1) ret += tree[l++];
			if (r&1) ret += tree[--r];
		}

		return ret;
	}
};

struct DS{
	set<array<int, 3>> st;
	Seg tree;

	void init(int n, int m){
		st.insert({1, n, m+1});

		tree.init(m+2);
		tree.update(m+1, n);
	}

	set<array<int, 3>>::iterator find(int x){
		return prev(st.lower_bound(array<int, 3>{x, INF, INF}));
	}

	void cut(int x){ // cut between x and x+1
		if (x < 1) return;

		auto iter = find(x);
		auto [l, r, v] = *iter;
		if (r==x) return;

		st.erase(iter);
		st.insert({l, x, v});
		st.insert({x+1, r, v});
	}

	void update(int l, int r, int x){
		// printf("update %d %d %d\n", l, r, x);
		cut(l-1);
		cut(r);

		for (auto iter=find(l);iter!=st.end();iter=st.erase(iter)){
			auto [_l, _r, _v] = *iter;
			if (_r > r) break;
			
			tree.update(_v, -(_r-_l+1));
		}

		st.insert({l, r, x});
		tree.update(x, r-l+1);

		// for (auto &[x, y, z]:st) printf("(%d, %d, %d) ", x, y, z);
		// printf("\n");
	}

	int query(int x){
		return tree.query(0, x-1);
	}
}D;

void dfs1(int s, int pa = 0){
	dep[s] = dep[pa] + 1;
	sz[s] = 1;
	par[s] = pa;

	if (pa) adj[s].erase(find(adj[s].begin(), adj[s].end(), pa));
	else top[s] = s;

	for (auto &v:adj[s]){
		dfs1(v, s);
		sz[s] += sz[v];
	}
}

void dfs2(int s){
	in[s] = ++cnt;
	sort(adj[s].begin(), adj[s].end(), [&](int i, int j){return sz[i] > sz[j];});

	for (auto &v:adj[s]){
		if (v==adj[s][0]) top[v] = top[s];
		else top[v] = v;

		dfs2(v);
	}

	out[s] = cnt;
}

void update(int x, int y, int val){
	while(top[x] != top[y]){
		if (dep[top[x]] > dep[top[y]]) swap(x, y);
		D.update(in[top[y]], in[y], val);

		y = par[top[y]];
	}

	if (dep[x] > dep[y]) swap(x, y);
	if (x!=y) D.update(in[x]+1, in[y], 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);
	}

	dfs1(1);
	dfs2(1);

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

	vector<array<int, 3>> Q;
	for (int i=1;i<=q;i++){
		int x, y;
		scanf("%d %d", &x, &y);
		Q.push_back({x, y, i});
	}

	D.init(n, m);
	sort(Q.begin(), Q.end(), greater<array<int, 3>>());

	int pt = 0;
	for (int i=m;i;i--){
		if (i<m) update(a[i], a[i+1], i);
		
		while(pt<(int)Q.size() && Q[pt][0]==i){
			ans[Q[pt][2]] = D.query(Q[pt][1]) + 1;
			pt++;
		}
	}

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

Compilation message

tourism.cpp: In function 'int main()':
tourism.cpp:131:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |  scanf("%d %d %d", &n, &m, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:135:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 |   scanf("%d %d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~
tourism.cpp:143:30: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  143 |  for (int i=1;i<=m;i++) scanf("%d", a+i);
      |                         ~~~~~^~~~~~~~~~~
tourism.cpp:148:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  148 |   scanf("%d %d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2660 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 2 ms 2660 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 3 ms 2728 KB Output is correct
10 Correct 3 ms 2724 KB Output is correct
11 Correct 3 ms 2644 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2668 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2664 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2672 KB Output is correct
25 Correct 2 ms 2772 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2656 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 3 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2660 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 2 ms 2660 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 3 ms 2728 KB Output is correct
10 Correct 3 ms 2724 KB Output is correct
11 Correct 3 ms 2644 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2668 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2664 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2672 KB Output is correct
25 Correct 2 ms 2772 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2656 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 3 ms 2644 KB Output is correct
30 Correct 6 ms 2864 KB Output is correct
31 Correct 6 ms 2928 KB Output is correct
32 Correct 7 ms 3000 KB Output is correct
33 Correct 7 ms 2928 KB Output is correct
34 Correct 7 ms 2900 KB Output is correct
35 Correct 7 ms 2980 KB Output is correct
36 Correct 7 ms 2900 KB Output is correct
37 Correct 9 ms 2968 KB Output is correct
38 Correct 4 ms 2900 KB Output is correct
39 Correct 3 ms 3028 KB Output is correct
40 Correct 5 ms 3028 KB Output is correct
41 Correct 3 ms 3028 KB Output is correct
42 Correct 3 ms 3028 KB Output is correct
43 Correct 3 ms 2900 KB Output is correct
44 Correct 4 ms 2932 KB Output is correct
45 Correct 5 ms 2900 KB Output is correct
46 Correct 4 ms 2900 KB Output is correct
47 Correct 5 ms 2900 KB Output is correct
48 Correct 5 ms 2900 KB Output is correct
49 Correct 4 ms 2900 KB Output is correct
50 Correct 5 ms 3024 KB Output is correct
51 Correct 5 ms 2928 KB Output is correct
52 Correct 4 ms 3028 KB Output is correct
53 Correct 4 ms 2920 KB Output is correct
54 Correct 5 ms 3028 KB Output is correct
55 Correct 5 ms 2924 KB Output is correct
56 Correct 2 ms 2772 KB Output is correct
57 Correct 2 ms 2772 KB Output is correct
58 Correct 7 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 3 ms 2900 KB Output is correct
4 Correct 103 ms 13948 KB Output is correct
5 Correct 78 ms 16364 KB Output is correct
6 Correct 88 ms 17824 KB Output is correct
7 Correct 118 ms 20672 KB Output is correct
8 Correct 129 ms 20640 KB Output is correct
9 Correct 116 ms 20640 KB Output is correct
10 Correct 121 ms 20644 KB Output is correct
11 Correct 117 ms 20648 KB Output is correct
12 Correct 157 ms 20980 KB Output is correct
13 Correct 125 ms 20804 KB Output is correct
14 Correct 121 ms 20804 KB Output is correct
15 Correct 60 ms 17804 KB Output is correct
16 Correct 137 ms 20340 KB Output is correct
17 Correct 46 ms 7016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2660 KB Output is correct
2 Correct 277 ms 9636 KB Output is correct
3 Correct 474 ms 10992 KB Output is correct
4 Correct 333 ms 11236 KB Output is correct
5 Correct 544 ms 15256 KB Output is correct
6 Correct 546 ms 15356 KB Output is correct
7 Correct 549 ms 15348 KB Output is correct
8 Correct 542 ms 15296 KB Output is correct
9 Correct 557 ms 15304 KB Output is correct
10 Correct 562 ms 15268 KB Output is correct
11 Correct 536 ms 15296 KB Output is correct
12 Correct 576 ms 15356 KB Output is correct
13 Correct 580 ms 15576 KB Output is correct
14 Correct 564 ms 16144 KB Output is correct
15 Correct 589 ms 18156 KB Output is correct
16 Correct 537 ms 15668 KB Output is correct
17 Correct 547 ms 15656 KB Output is correct
18 Correct 541 ms 15620 KB Output is correct
19 Correct 319 ms 11432 KB Output is correct
20 Correct 309 ms 11336 KB Output is correct
21 Correct 323 ms 11360 KB Output is correct
22 Correct 330 ms 11424 KB Output is correct
23 Correct 301 ms 11340 KB Output is correct
24 Correct 314 ms 11396 KB Output is correct
25 Correct 340 ms 11328 KB Output is correct
26 Correct 311 ms 11416 KB Output is correct
27 Correct 330 ms 11400 KB Output is correct
28 Correct 314 ms 11396 KB Output is correct
29 Correct 317 ms 11468 KB Output is correct
30 Correct 323 ms 11492 KB Output is correct
31 Correct 341 ms 11740 KB Output is correct
32 Correct 329 ms 12000 KB Output is correct
33 Correct 361 ms 12832 KB Output is correct
34 Correct 339 ms 14416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 743 ms 13880 KB Output is correct
5 Correct 772 ms 13968 KB Output is correct
6 Correct 936 ms 17224 KB Output is correct
7 Correct 995 ms 19036 KB Output is correct
8 Correct 954 ms 19068 KB Output is correct
9 Correct 959 ms 19084 KB Output is correct
10 Correct 990 ms 19000 KB Output is correct
11 Correct 925 ms 18972 KB Output is correct
12 Correct 924 ms 19056 KB Output is correct
13 Correct 954 ms 18984 KB Output is correct
14 Correct 46 ms 6960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2660 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 2 ms 2660 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 3 ms 2728 KB Output is correct
10 Correct 3 ms 2724 KB Output is correct
11 Correct 3 ms 2644 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2668 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2664 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2672 KB Output is correct
25 Correct 2 ms 2772 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2656 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 3 ms 2644 KB Output is correct
30 Correct 6 ms 2864 KB Output is correct
31 Correct 6 ms 2928 KB Output is correct
32 Correct 7 ms 3000 KB Output is correct
33 Correct 7 ms 2928 KB Output is correct
34 Correct 7 ms 2900 KB Output is correct
35 Correct 7 ms 2980 KB Output is correct
36 Correct 7 ms 2900 KB Output is correct
37 Correct 9 ms 2968 KB Output is correct
38 Correct 4 ms 2900 KB Output is correct
39 Correct 3 ms 3028 KB Output is correct
40 Correct 5 ms 3028 KB Output is correct
41 Correct 3 ms 3028 KB Output is correct
42 Correct 3 ms 3028 KB Output is correct
43 Correct 3 ms 2900 KB Output is correct
44 Correct 4 ms 2932 KB Output is correct
45 Correct 5 ms 2900 KB Output is correct
46 Correct 4 ms 2900 KB Output is correct
47 Correct 5 ms 2900 KB Output is correct
48 Correct 5 ms 2900 KB Output is correct
49 Correct 4 ms 2900 KB Output is correct
50 Correct 5 ms 3024 KB Output is correct
51 Correct 5 ms 2928 KB Output is correct
52 Correct 4 ms 3028 KB Output is correct
53 Correct 4 ms 2920 KB Output is correct
54 Correct 5 ms 3028 KB Output is correct
55 Correct 5 ms 2924 KB Output is correct
56 Correct 2 ms 2772 KB Output is correct
57 Correct 2 ms 2772 KB Output is correct
58 Correct 7 ms 2900 KB Output is correct
59 Correct 2 ms 2644 KB Output is correct
60 Correct 1 ms 2644 KB Output is correct
61 Correct 3 ms 2900 KB Output is correct
62 Correct 103 ms 13948 KB Output is correct
63 Correct 78 ms 16364 KB Output is correct
64 Correct 88 ms 17824 KB Output is correct
65 Correct 118 ms 20672 KB Output is correct
66 Correct 129 ms 20640 KB Output is correct
67 Correct 116 ms 20640 KB Output is correct
68 Correct 121 ms 20644 KB Output is correct
69 Correct 117 ms 20648 KB Output is correct
70 Correct 157 ms 20980 KB Output is correct
71 Correct 125 ms 20804 KB Output is correct
72 Correct 121 ms 20804 KB Output is correct
73 Correct 60 ms 17804 KB Output is correct
74 Correct 137 ms 20340 KB Output is correct
75 Correct 46 ms 7016 KB Output is correct
76 Correct 1 ms 2660 KB Output is correct
77 Correct 277 ms 9636 KB Output is correct
78 Correct 474 ms 10992 KB Output is correct
79 Correct 333 ms 11236 KB Output is correct
80 Correct 544 ms 15256 KB Output is correct
81 Correct 546 ms 15356 KB Output is correct
82 Correct 549 ms 15348 KB Output is correct
83 Correct 542 ms 15296 KB Output is correct
84 Correct 557 ms 15304 KB Output is correct
85 Correct 562 ms 15268 KB Output is correct
86 Correct 536 ms 15296 KB Output is correct
87 Correct 576 ms 15356 KB Output is correct
88 Correct 580 ms 15576 KB Output is correct
89 Correct 564 ms 16144 KB Output is correct
90 Correct 589 ms 18156 KB Output is correct
91 Correct 537 ms 15668 KB Output is correct
92 Correct 547 ms 15656 KB Output is correct
93 Correct 541 ms 15620 KB Output is correct
94 Correct 319 ms 11432 KB Output is correct
95 Correct 309 ms 11336 KB Output is correct
96 Correct 323 ms 11360 KB Output is correct
97 Correct 330 ms 11424 KB Output is correct
98 Correct 301 ms 11340 KB Output is correct
99 Correct 314 ms 11396 KB Output is correct
100 Correct 340 ms 11328 KB Output is correct
101 Correct 311 ms 11416 KB Output is correct
102 Correct 330 ms 11400 KB Output is correct
103 Correct 314 ms 11396 KB Output is correct
104 Correct 317 ms 11468 KB Output is correct
105 Correct 323 ms 11492 KB Output is correct
106 Correct 341 ms 11740 KB Output is correct
107 Correct 329 ms 12000 KB Output is correct
108 Correct 361 ms 12832 KB Output is correct
109 Correct 339 ms 14416 KB Output is correct
110 Correct 2 ms 2644 KB Output is correct
111 Correct 2 ms 2644 KB Output is correct
112 Correct 2 ms 2772 KB Output is correct
113 Correct 743 ms 13880 KB Output is correct
114 Correct 772 ms 13968 KB Output is correct
115 Correct 936 ms 17224 KB Output is correct
116 Correct 995 ms 19036 KB Output is correct
117 Correct 954 ms 19068 KB Output is correct
118 Correct 959 ms 19084 KB Output is correct
119 Correct 990 ms 19000 KB Output is correct
120 Correct 925 ms 18972 KB Output is correct
121 Correct 924 ms 19056 KB Output is correct
122 Correct 954 ms 18984 KB Output is correct
123 Correct 46 ms 6960 KB Output is correct
124 Correct 501 ms 18172 KB Output is correct
125 Correct 402 ms 15832 KB Output is correct
126 Correct 592 ms 18696 KB Output is correct
127 Correct 603 ms 18652 KB Output is correct
128 Correct 616 ms 18548 KB Output is correct
129 Correct 622 ms 18616 KB Output is correct
130 Correct 577 ms 18648 KB Output is correct
131 Correct 134 ms 19700 KB Output is correct
132 Correct 125 ms 20656 KB Output is correct
133 Correct 129 ms 17512 KB Output is correct
134 Correct 352 ms 14660 KB Output is correct
135 Correct 357 ms 14660 KB Output is correct
136 Correct 353 ms 14652 KB Output is correct
137 Correct 219 ms 20132 KB Output is correct
138 Correct 231 ms 20136 KB Output is correct
139 Correct 217 ms 20064 KB Output is correct
140 Correct 214 ms 20092 KB Output is correct
141 Correct 246 ms 20096 KB Output is correct
142 Correct 228 ms 20004 KB Output is correct
143 Correct 59 ms 11636 KB Output is correct
144 Correct 618 ms 18228 KB Output is correct