Submission #728268

# Submission time Handle Problem Language Result Execution time Memory
728268 2023-04-22T06:32:36 Z penguin133 Tourism (JOI23_tourism) C++17
100 / 100
1169 ms 65976 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second

int sz[1000005], head[1000005], par[20][100005], dep[1000005], S[1000005], E[1000005];
int up[1000005];
vector<int>v[1000005];
int n, m, q;
vector<pii>adj;
int hello = 1;
void dfs(int x, int p, int d){
	dep[x] = d;
	up[x] = p;
	sz[x] = 1;
	for(auto i : v[x]){
		if(i == p)continue;
		dfs(i, x, d + 1);
		sz[x] += sz[i];
	}
}


struct node{
	int s,e,m,val, lazy = 0, mc = 0;
	node *l, *r;
	node(int _s, int _e){
		s = _s, e = _e, m = (s + e)>>1;
		val = 0, mc = e - s + 1;
		if(s != e)l = new node(s, m), r = new node(m+1, e);
	}
	void propo(){
		if(lazy){
			val += lazy;
			if(s != e)l->lazy += lazy, r->lazy += lazy;
			lazy = 0;
		}
	}
	
	pi comb(pi a, pi b){
		pi ans;
		ans.fi = min(a.fi, b.fi);
		if(a.fi == b.fi)ans.se = a.se + b.se;
		else ans.se = (a.fi < b.fi ? a.se : b.se);
		return ans;
	}
	
	void upd(int a, int b, int c){
		//cerr << "UPD " << a << " " << b << " " << c << '\n';
		if(a == s && b == e)lazy += c;
		else{
			if(b <= m)l->upd(a, b, c);
			else if(a > m)r->upd(a, b, c);
			else l->upd(a, m, c), r->upd(m+1, b, c);
			l->propo(), r->propo();
			//val = l->val + r->val;
			val = min(l->val, r->val);
			if(l->val == r->val)mc = l->mc + r->mc;
			else mc = (l->val < r->val ? l->mc : r->mc);
		}
	}
	pi query(int a, int b){
		propo();
		if(a == s && b == e)return {val, mc};
		else if( b <= m)return l->query(a, b);
		else if(a > m)return r->query(a, b);
		else return comb(l->query(a, m), r->query(m+1, b));
	}
}*root;
int cnt = 1;
void dfs2(int x, int h, int par){
	S[x] = cnt++;
	head[x] = h;
	int maxi = 0, in = -1;
	for(auto i : v[x]){
		if(i == par)continue;
		if(maxi < sz[i])maxi = sz[i], in = i;
	}
	for(auto i : v[x]){
		if(i == par)continue;
		if(i == in)dfs2(i, h, x);
	}
	for(auto i : v[x]){
		if(i != par && i != in)dfs2(i, i, x);
	}
	E[x] = cnt-  1;
}


/*
int query(int a, int b) {
    int res = 0;
    for (; head[a] != head[b]; b = up[head[b]]) {
        if (dep[head[a]] > dep[head[b]])
            swap(a, b);
        int cur_heavy_path_max = root->query(S[head[b]], S[b]);
        res += cur_heavy_path_max;
    }
    if (dep[a] > dep[b])
        swap(a, b);
    res += root->query(S[a], S[b]);
    return res;
}
void st(int x, int a){
	if(S[x] <= S[hello] && E[hello] <= E[x]){
		int in = -1;
		for(auto i : v[x]){
			if(S[i] <= S[hello] && E[hello] <= E[i] && S[i] > S[x])in = i;
		}
		root->upd(1, n, a);
		if(in != -1)root->upd(S[in], E[in], -a);
	}
	else root->upd(S[x], E[x], a);
}
int qu(int x){
	int res=  0;
	if(S[x] <= S[hello] && E[hello] <= E[x]){
		int in = -1;
		for(auto i : v[x]){
			if(S[i] <= S[hello] && E[hello] <= E[i] && S[i] > S[x])in = i;
		}
		root->propo();
		res = root->val;
		if(in != -1)res -= root->query(S[in], E[in]);
		return res;
	}
	else return root->query(S[x], E[x]);
}
*/
int C[100005], ans[100005];
vector <pi> qu[100005];
set <pii> s;

int ft[100005];

void upd(int p, int v){
	for(;p<=m;p+=(p & -p))ft[p] += v;
}

int qry(int p){
	int res = 0;
	for(;p;p-=(p & -p))res += ft[p];
	return res;
}

void ins(int l, int r, int i){
	auto it = s.upper_bound({l-1, {1e9, 1e9}});
	vector <pii> sad, nw;
	if(it != s.begin()){
		it--;
		pii tmp = *it;
		if(tmp.se.fi < l)it++;
		else{
			nw.push_back({tmp.fi, {l - 1, tmp.se.se}});
			int x = tmp.se.se;
			if(tmp.se.fi > r){
				nw.push_back({r+1, {tmp.se.fi, tmp.se.se}});
			}
			upd(x, - (min(r, tmp.se.fi) - l + 1));
			sad.push_back(tmp);
			it++;
		}
	}
	while(it != s.end()){
		pii tmp = *it;
		if(tmp.fi > r)break;
		if(tmp.se.fi > r){
			nw.push_back({r+1, {tmp.se.fi, tmp.se.se}});
			int x = tmp.se.se;
			upd(x, -(r - tmp.fi + 1));
			sad.push_back(tmp);
			break;
		}
		sad.push_back(tmp);
		int x = tmp.se.se;
		upd(x, - (tmp.se.fi - tmp.fi + 1));
		it++;
	}
	s.insert({l, {r, i}});
	upd(i, r - l + 1);
	for(auto j : sad)s.erase(j);
	for(auto j : nw)s.insert(j);
}

void chng(int a, int b, int c) {
    for (; head[a] != head[b]; b = up[head[b]]) {
		//cerr << a << " " << b << " " << head[a] << " " << head[b] << '\n';
        if (dep[head[a]] > dep[head[b]])
            swap(a, b);
       // root->upd(S[head[b]], S[b], c);
       ins(S[head[b]], S[b], c);
    }
    if (dep[a] > dep[b])
        swap(a, b);
    //root->upd(S[a], S[b], c);
    ins(S[a], S[b], c);
}

int32_t main(){
	ios::sync_with_stdio(0);cin.tie(0);
	cin >> n >> m >> q;
	for(int i=1;i<n;i++){
		int a, b; cin >> a >> b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	for(int i=1;i<=m;i++)cin >> C[i];
	root= new node(1, n);
	dfs(1, -1, 1);
	dfs2(1, 1, -1);
	for(int i=1;i<=q;i++){
		int a, b; cin >> a >> b;
		qu[b].push_back({a, i});
	}
	for(auto [i, j] : qu[1])ans[j] = 1;
	s.insert({1, {n, 1}});
	upd(1, n);
	for(int i=2;i<=m;i++){
		chng(C[i], C[i-1], i);
		for(auto [j, k] : qu[i]){
			ans[k] = n - qry(j);
		}
	}
	for(int i=1;i<=q;i++)cout << max(1ll, ans[i]) << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 18 ms 26196 KB Output is correct
2 Correct 17 ms 26152 KB Output is correct
3 Correct 14 ms 26208 KB Output is correct
4 Correct 16 ms 26196 KB Output is correct
5 Correct 15 ms 26300 KB Output is correct
6 Correct 15 ms 26196 KB Output is correct
7 Correct 14 ms 26220 KB Output is correct
8 Correct 14 ms 26236 KB Output is correct
9 Correct 15 ms 26196 KB Output is correct
10 Correct 14 ms 26196 KB Output is correct
11 Correct 14 ms 26248 KB Output is correct
12 Correct 15 ms 26304 KB Output is correct
13 Correct 16 ms 26196 KB Output is correct
14 Correct 15 ms 26200 KB Output is correct
15 Correct 15 ms 26196 KB Output is correct
16 Correct 13 ms 26324 KB Output is correct
17 Correct 16 ms 26196 KB Output is correct
18 Correct 14 ms 26196 KB Output is correct
19 Correct 14 ms 26196 KB Output is correct
20 Correct 16 ms 26268 KB Output is correct
21 Correct 15 ms 26324 KB Output is correct
22 Correct 14 ms 26204 KB Output is correct
23 Correct 14 ms 26236 KB Output is correct
24 Correct 15 ms 26308 KB Output is correct
25 Correct 14 ms 26196 KB Output is correct
26 Correct 14 ms 26192 KB Output is correct
27 Correct 15 ms 26196 KB Output is correct
28 Correct 15 ms 26216 KB Output is correct
29 Correct 17 ms 26192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 26196 KB Output is correct
2 Correct 17 ms 26152 KB Output is correct
3 Correct 14 ms 26208 KB Output is correct
4 Correct 16 ms 26196 KB Output is correct
5 Correct 15 ms 26300 KB Output is correct
6 Correct 15 ms 26196 KB Output is correct
7 Correct 14 ms 26220 KB Output is correct
8 Correct 14 ms 26236 KB Output is correct
9 Correct 15 ms 26196 KB Output is correct
10 Correct 14 ms 26196 KB Output is correct
11 Correct 14 ms 26248 KB Output is correct
12 Correct 15 ms 26304 KB Output is correct
13 Correct 16 ms 26196 KB Output is correct
14 Correct 15 ms 26200 KB Output is correct
15 Correct 15 ms 26196 KB Output is correct
16 Correct 13 ms 26324 KB Output is correct
17 Correct 16 ms 26196 KB Output is correct
18 Correct 14 ms 26196 KB Output is correct
19 Correct 14 ms 26196 KB Output is correct
20 Correct 16 ms 26268 KB Output is correct
21 Correct 15 ms 26324 KB Output is correct
22 Correct 14 ms 26204 KB Output is correct
23 Correct 14 ms 26236 KB Output is correct
24 Correct 15 ms 26308 KB Output is correct
25 Correct 14 ms 26196 KB Output is correct
26 Correct 14 ms 26192 KB Output is correct
27 Correct 15 ms 26196 KB Output is correct
28 Correct 15 ms 26216 KB Output is correct
29 Correct 17 ms 26192 KB Output is correct
30 Correct 22 ms 26708 KB Output is correct
31 Correct 19 ms 26724 KB Output is correct
32 Correct 21 ms 26792 KB Output is correct
33 Correct 20 ms 26868 KB Output is correct
34 Correct 24 ms 26856 KB Output is correct
35 Correct 21 ms 26924 KB Output is correct
36 Correct 21 ms 26836 KB Output is correct
37 Correct 22 ms 26852 KB Output is correct
38 Correct 17 ms 26836 KB Output is correct
39 Correct 17 ms 26836 KB Output is correct
40 Correct 20 ms 26856 KB Output is correct
41 Correct 18 ms 26944 KB Output is correct
42 Correct 20 ms 26884 KB Output is correct
43 Correct 17 ms 26892 KB Output is correct
44 Correct 18 ms 26828 KB Output is correct
45 Correct 17 ms 26860 KB Output is correct
46 Correct 17 ms 26836 KB Output is correct
47 Correct 18 ms 26764 KB Output is correct
48 Correct 18 ms 26836 KB Output is correct
49 Correct 18 ms 26844 KB Output is correct
50 Correct 17 ms 26860 KB Output is correct
51 Correct 18 ms 26836 KB Output is correct
52 Correct 18 ms 26868 KB Output is correct
53 Correct 19 ms 26904 KB Output is correct
54 Correct 22 ms 26836 KB Output is correct
55 Correct 18 ms 26928 KB Output is correct
56 Correct 15 ms 26252 KB Output is correct
57 Correct 16 ms 26740 KB Output is correct
58 Correct 21 ms 26884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 26196 KB Output is correct
2 Correct 16 ms 26108 KB Output is correct
3 Correct 15 ms 26280 KB Output is correct
4 Correct 154 ms 48604 KB Output is correct
5 Correct 116 ms 56916 KB Output is correct
6 Correct 144 ms 59404 KB Output is correct
7 Correct 180 ms 65656 KB Output is correct
8 Correct 163 ms 65276 KB Output is correct
9 Correct 171 ms 65976 KB Output is correct
10 Correct 199 ms 65868 KB Output is correct
11 Correct 173 ms 65492 KB Output is correct
12 Correct 160 ms 64204 KB Output is correct
13 Correct 150 ms 65804 KB Output is correct
14 Correct 158 ms 65856 KB Output is correct
15 Correct 92 ms 61832 KB Output is correct
16 Correct 199 ms 65496 KB Output is correct
17 Correct 85 ms 31660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 26252 KB Output is correct
2 Correct 342 ms 43440 KB Output is correct
3 Correct 514 ms 44928 KB Output is correct
4 Correct 439 ms 46804 KB Output is correct
5 Correct 708 ms 57204 KB Output is correct
6 Correct 715 ms 56656 KB Output is correct
7 Correct 716 ms 57136 KB Output is correct
8 Correct 696 ms 56588 KB Output is correct
9 Correct 684 ms 56408 KB Output is correct
10 Correct 662 ms 57000 KB Output is correct
11 Correct 677 ms 57284 KB Output is correct
12 Correct 747 ms 57032 KB Output is correct
13 Correct 683 ms 58252 KB Output is correct
14 Correct 717 ms 58004 KB Output is correct
15 Correct 773 ms 61468 KB Output is correct
16 Correct 726 ms 56516 KB Output is correct
17 Correct 698 ms 56696 KB Output is correct
18 Correct 680 ms 56520 KB Output is correct
19 Correct 407 ms 52812 KB Output is correct
20 Correct 415 ms 51884 KB Output is correct
21 Correct 415 ms 52052 KB Output is correct
22 Correct 436 ms 52772 KB Output is correct
23 Correct 431 ms 53356 KB Output is correct
24 Correct 498 ms 51656 KB Output is correct
25 Correct 408 ms 52000 KB Output is correct
26 Correct 404 ms 51984 KB Output is correct
27 Correct 432 ms 52512 KB Output is correct
28 Correct 410 ms 53408 KB Output is correct
29 Correct 458 ms 52164 KB Output is correct
30 Correct 493 ms 52520 KB Output is correct
31 Correct 502 ms 53324 KB Output is correct
32 Correct 471 ms 53156 KB Output is correct
33 Correct 514 ms 54864 KB Output is correct
34 Correct 480 ms 56360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 26196 KB Output is correct
2 Correct 18 ms 26196 KB Output is correct
3 Correct 19 ms 26264 KB Output is correct
4 Correct 916 ms 49448 KB Output is correct
5 Correct 956 ms 49432 KB Output is correct
6 Correct 1105 ms 58104 KB Output is correct
7 Correct 1125 ms 63548 KB Output is correct
8 Correct 1057 ms 63104 KB Output is correct
9 Correct 1161 ms 62772 KB Output is correct
10 Correct 1108 ms 63540 KB Output is correct
11 Correct 1110 ms 63548 KB Output is correct
12 Correct 1065 ms 63012 KB Output is correct
13 Correct 1169 ms 63352 KB Output is correct
14 Correct 76 ms 32488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 26196 KB Output is correct
2 Correct 17 ms 26152 KB Output is correct
3 Correct 14 ms 26208 KB Output is correct
4 Correct 16 ms 26196 KB Output is correct
5 Correct 15 ms 26300 KB Output is correct
6 Correct 15 ms 26196 KB Output is correct
7 Correct 14 ms 26220 KB Output is correct
8 Correct 14 ms 26236 KB Output is correct
9 Correct 15 ms 26196 KB Output is correct
10 Correct 14 ms 26196 KB Output is correct
11 Correct 14 ms 26248 KB Output is correct
12 Correct 15 ms 26304 KB Output is correct
13 Correct 16 ms 26196 KB Output is correct
14 Correct 15 ms 26200 KB Output is correct
15 Correct 15 ms 26196 KB Output is correct
16 Correct 13 ms 26324 KB Output is correct
17 Correct 16 ms 26196 KB Output is correct
18 Correct 14 ms 26196 KB Output is correct
19 Correct 14 ms 26196 KB Output is correct
20 Correct 16 ms 26268 KB Output is correct
21 Correct 15 ms 26324 KB Output is correct
22 Correct 14 ms 26204 KB Output is correct
23 Correct 14 ms 26236 KB Output is correct
24 Correct 15 ms 26308 KB Output is correct
25 Correct 14 ms 26196 KB Output is correct
26 Correct 14 ms 26192 KB Output is correct
27 Correct 15 ms 26196 KB Output is correct
28 Correct 15 ms 26216 KB Output is correct
29 Correct 17 ms 26192 KB Output is correct
30 Correct 22 ms 26708 KB Output is correct
31 Correct 19 ms 26724 KB Output is correct
32 Correct 21 ms 26792 KB Output is correct
33 Correct 20 ms 26868 KB Output is correct
34 Correct 24 ms 26856 KB Output is correct
35 Correct 21 ms 26924 KB Output is correct
36 Correct 21 ms 26836 KB Output is correct
37 Correct 22 ms 26852 KB Output is correct
38 Correct 17 ms 26836 KB Output is correct
39 Correct 17 ms 26836 KB Output is correct
40 Correct 20 ms 26856 KB Output is correct
41 Correct 18 ms 26944 KB Output is correct
42 Correct 20 ms 26884 KB Output is correct
43 Correct 17 ms 26892 KB Output is correct
44 Correct 18 ms 26828 KB Output is correct
45 Correct 17 ms 26860 KB Output is correct
46 Correct 17 ms 26836 KB Output is correct
47 Correct 18 ms 26764 KB Output is correct
48 Correct 18 ms 26836 KB Output is correct
49 Correct 18 ms 26844 KB Output is correct
50 Correct 17 ms 26860 KB Output is correct
51 Correct 18 ms 26836 KB Output is correct
52 Correct 18 ms 26868 KB Output is correct
53 Correct 19 ms 26904 KB Output is correct
54 Correct 22 ms 26836 KB Output is correct
55 Correct 18 ms 26928 KB Output is correct
56 Correct 15 ms 26252 KB Output is correct
57 Correct 16 ms 26740 KB Output is correct
58 Correct 21 ms 26884 KB Output is correct
59 Correct 14 ms 26196 KB Output is correct
60 Correct 16 ms 26108 KB Output is correct
61 Correct 15 ms 26280 KB Output is correct
62 Correct 154 ms 48604 KB Output is correct
63 Correct 116 ms 56916 KB Output is correct
64 Correct 144 ms 59404 KB Output is correct
65 Correct 180 ms 65656 KB Output is correct
66 Correct 163 ms 65276 KB Output is correct
67 Correct 171 ms 65976 KB Output is correct
68 Correct 199 ms 65868 KB Output is correct
69 Correct 173 ms 65492 KB Output is correct
70 Correct 160 ms 64204 KB Output is correct
71 Correct 150 ms 65804 KB Output is correct
72 Correct 158 ms 65856 KB Output is correct
73 Correct 92 ms 61832 KB Output is correct
74 Correct 199 ms 65496 KB Output is correct
75 Correct 85 ms 31660 KB Output is correct
76 Correct 17 ms 26252 KB Output is correct
77 Correct 342 ms 43440 KB Output is correct
78 Correct 514 ms 44928 KB Output is correct
79 Correct 439 ms 46804 KB Output is correct
80 Correct 708 ms 57204 KB Output is correct
81 Correct 715 ms 56656 KB Output is correct
82 Correct 716 ms 57136 KB Output is correct
83 Correct 696 ms 56588 KB Output is correct
84 Correct 684 ms 56408 KB Output is correct
85 Correct 662 ms 57000 KB Output is correct
86 Correct 677 ms 57284 KB Output is correct
87 Correct 747 ms 57032 KB Output is correct
88 Correct 683 ms 58252 KB Output is correct
89 Correct 717 ms 58004 KB Output is correct
90 Correct 773 ms 61468 KB Output is correct
91 Correct 726 ms 56516 KB Output is correct
92 Correct 698 ms 56696 KB Output is correct
93 Correct 680 ms 56520 KB Output is correct
94 Correct 407 ms 52812 KB Output is correct
95 Correct 415 ms 51884 KB Output is correct
96 Correct 415 ms 52052 KB Output is correct
97 Correct 436 ms 52772 KB Output is correct
98 Correct 431 ms 53356 KB Output is correct
99 Correct 498 ms 51656 KB Output is correct
100 Correct 408 ms 52000 KB Output is correct
101 Correct 404 ms 51984 KB Output is correct
102 Correct 432 ms 52512 KB Output is correct
103 Correct 410 ms 53408 KB Output is correct
104 Correct 458 ms 52164 KB Output is correct
105 Correct 493 ms 52520 KB Output is correct
106 Correct 502 ms 53324 KB Output is correct
107 Correct 471 ms 53156 KB Output is correct
108 Correct 514 ms 54864 KB Output is correct
109 Correct 480 ms 56360 KB Output is correct
110 Correct 16 ms 26196 KB Output is correct
111 Correct 18 ms 26196 KB Output is correct
112 Correct 19 ms 26264 KB Output is correct
113 Correct 916 ms 49448 KB Output is correct
114 Correct 956 ms 49432 KB Output is correct
115 Correct 1105 ms 58104 KB Output is correct
116 Correct 1125 ms 63548 KB Output is correct
117 Correct 1057 ms 63104 KB Output is correct
118 Correct 1161 ms 62772 KB Output is correct
119 Correct 1108 ms 63540 KB Output is correct
120 Correct 1110 ms 63548 KB Output is correct
121 Correct 1065 ms 63012 KB Output is correct
122 Correct 1169 ms 63352 KB Output is correct
123 Correct 76 ms 32488 KB Output is correct
124 Correct 640 ms 62172 KB Output is correct
125 Correct 470 ms 58752 KB Output is correct
126 Correct 764 ms 62944 KB Output is correct
127 Correct 822 ms 62800 KB Output is correct
128 Correct 763 ms 62464 KB Output is correct
129 Correct 764 ms 62804 KB Output is correct
130 Correct 712 ms 62940 KB Output is correct
131 Correct 181 ms 64708 KB Output is correct
132 Correct 177 ms 65844 KB Output is correct
133 Correct 202 ms 62024 KB Output is correct
134 Correct 474 ms 58544 KB Output is correct
135 Correct 539 ms 58340 KB Output is correct
136 Correct 453 ms 58640 KB Output is correct
137 Correct 297 ms 64296 KB Output is correct
138 Correct 314 ms 64304 KB Output is correct
139 Correct 294 ms 64212 KB Output is correct
140 Correct 306 ms 64308 KB Output is correct
141 Correct 317 ms 64220 KB Output is correct
142 Correct 284 ms 64192 KB Output is correct
143 Correct 99 ms 54596 KB Output is correct
144 Correct 793 ms 62560 KB Output is correct