Submission #1087400

# Submission time Handle Problem Language Result Execution time Memory
1087400 2024-09-12T15:32:52 Z coldbr3w Tourism (JOI23_tourism) C++17
100 / 100
924 ms 37940 KB
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define pll pair<long long, long long>
#define pb push_back
#define F first
#define S second  
#define all(x) (x).begin(), (x).end()
 
const ll N = 3e5 + 100;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll block = 350;
ll n,m,q,timer = 0;
vector<ll>adj[N];
ll tin[N], heavy[N], sz[N], p[N], head[N], res[N], a[N];
vector<pll>t[N];
set<array<ll,3>>s;
struct fenwick_tree{
	ll n;
	vector<ll>ft;
	void init(ll _n){
		n = _n;
		ft.resize(n + 10);
	}
	void update(ll idx, ll val){
		while(idx <= n) ft[idx] += val, idx += (idx & (-idx));
	}
	ll get(ll idx){
		ll res = 0;
		while(idx > 0) res += ft[idx], idx -= (idx & (-idx));
		return res;
	}
} ft;
void dfs(ll u, ll par){
    sz[u] = 1;
    for(auto v : adj[u]){
        if(v == par) continue;
        dfs(v, u);
        p[v] = u;
        sz[u] += sz[v];
        if(sz[v] > sz[heavy[u]]) heavy[u] = v;
    }
}
void HLD(ll u, ll h){
    head[u] = h; tin[u] = ++timer;
    if(heavy[u]) HLD(heavy[u], h);
    for(auto v : adj[u]){
        if(v != p[u] && v != heavy[u]) HLD(v, v);
    }
}
void change(ll l, ll r, ll val){
	auto it = s.lower_bound({l, l, 0});
	if(it != s.begin()) --it;
	vector<array<ll,3>>del, add;
	add.pb({l, r, val});
	while(it != s.end() && (*it)[0] <= r){
		ll l1 = (*it)[0], r1 = (*it)[1], v1 = (*it)[2];
		if(r1 < l){
			++it;
			continue;
		}
		del.pb(*it);
		if(l1 < l) add.pb({l1, l - 1, v1});
		if(r1 > r) add.pb({r + 1, r1, v1});
		++it;
	}
	for(auto x : del){
		ft.update(x[2], -(x[1] - x[0] + 1));
		s.erase(x);
	}
	for(auto x : add){
		ft.update(x[2], x[1] - x[0] + 1);
		s.insert(x);
	}
}
void upd(ll a, ll b, ll val){
    for(; head[a] != head[b]; b = p[head[b]]){
        if(tin[a] > tin[b]) swap(a, b);
        change(tin[head[b]], tin[b], val);
    }
    if(tin[a] > tin[b]) swap(a, b);
   	change(tin[a], tin[b], val);
}
void to_thic_cau(){
	cin >> n >> m >> q;
	for(int i = 1; i <= n - 1;i++){
		ll u,v; cin >> u >> v;
		adj[u].pb(v); adj[v].pb(u);
	}
	dfs(1, 0);	
	HLD(1, 1);
	for(int i = 1; i <= m;i++) cin >> a[i];
	for(int i = 1; i <= q;i++){
		ll l,r; cin >> l >> r;
		t[r].pb({l, i});
	}
	s.insert({1, n, 1});
	ft.init(m);
	ft.update(1, m);
	for(int i = 1; i <= m;i++){
		if(i > 1) upd(a[i-1], a[i], i);
		for(auto x : t[i]){
			ll l = x.F, idx = x.S;
			if(l == i){
				res[idx] = 1;
				continue;
			}
			res[idx] = ft.get(i) - ft.get(l);
		}
	}
	for(int i = 1; i <= q;i++) cout << res[i] << '\n';
	
}
 
signed main()   
{ 
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	ll tc = 1;
	//cin >> tc;
	while(tc--) to_thic_cau();
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14428 KB Output is correct
2 Correct 5 ms 14368 KB Output is correct
3 Correct 8 ms 14424 KB Output is correct
4 Correct 10 ms 14440 KB Output is correct
5 Correct 6 ms 14428 KB Output is correct
6 Correct 7 ms 14424 KB Output is correct
7 Correct 6 ms 14428 KB Output is correct
8 Correct 6 ms 14428 KB Output is correct
9 Correct 7 ms 14672 KB Output is correct
10 Correct 6 ms 14424 KB Output is correct
11 Correct 6 ms 14428 KB Output is correct
12 Correct 6 ms 14480 KB Output is correct
13 Correct 7 ms 14648 KB Output is correct
14 Correct 6 ms 14464 KB Output is correct
15 Correct 6 ms 14428 KB Output is correct
16 Correct 6 ms 14424 KB Output is correct
17 Correct 8 ms 14428 KB Output is correct
18 Correct 6 ms 14428 KB Output is correct
19 Correct 9 ms 14616 KB Output is correct
20 Correct 7 ms 14428 KB Output is correct
21 Correct 6 ms 14424 KB Output is correct
22 Correct 7 ms 14428 KB Output is correct
23 Correct 6 ms 14564 KB Output is correct
24 Correct 6 ms 14424 KB Output is correct
25 Correct 8 ms 14428 KB Output is correct
26 Correct 6 ms 14428 KB Output is correct
27 Correct 6 ms 14424 KB Output is correct
28 Correct 6 ms 14428 KB Output is correct
29 Correct 7 ms 14428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14428 KB Output is correct
2 Correct 5 ms 14368 KB Output is correct
3 Correct 8 ms 14424 KB Output is correct
4 Correct 10 ms 14440 KB Output is correct
5 Correct 6 ms 14428 KB Output is correct
6 Correct 7 ms 14424 KB Output is correct
7 Correct 6 ms 14428 KB Output is correct
8 Correct 6 ms 14428 KB Output is correct
9 Correct 7 ms 14672 KB Output is correct
10 Correct 6 ms 14424 KB Output is correct
11 Correct 6 ms 14428 KB Output is correct
12 Correct 6 ms 14480 KB Output is correct
13 Correct 7 ms 14648 KB Output is correct
14 Correct 6 ms 14464 KB Output is correct
15 Correct 6 ms 14428 KB Output is correct
16 Correct 6 ms 14424 KB Output is correct
17 Correct 8 ms 14428 KB Output is correct
18 Correct 6 ms 14428 KB Output is correct
19 Correct 9 ms 14616 KB Output is correct
20 Correct 7 ms 14428 KB Output is correct
21 Correct 6 ms 14424 KB Output is correct
22 Correct 7 ms 14428 KB Output is correct
23 Correct 6 ms 14564 KB Output is correct
24 Correct 6 ms 14424 KB Output is correct
25 Correct 8 ms 14428 KB Output is correct
26 Correct 6 ms 14428 KB Output is correct
27 Correct 6 ms 14424 KB Output is correct
28 Correct 6 ms 14428 KB Output is correct
29 Correct 7 ms 14428 KB Output is correct
30 Correct 12 ms 14680 KB Output is correct
31 Correct 9 ms 14684 KB Output is correct
32 Correct 11 ms 14940 KB Output is correct
33 Correct 10 ms 14824 KB Output is correct
34 Correct 11 ms 14940 KB Output is correct
35 Correct 16 ms 14940 KB Output is correct
36 Correct 11 ms 14940 KB Output is correct
37 Correct 11 ms 14936 KB Output is correct
38 Correct 7 ms 14840 KB Output is correct
39 Correct 8 ms 14940 KB Output is correct
40 Correct 9 ms 14780 KB Output is correct
41 Correct 8 ms 14936 KB Output is correct
42 Correct 8 ms 14936 KB Output is correct
43 Correct 10 ms 14968 KB Output is correct
44 Correct 9 ms 14948 KB Output is correct
45 Correct 9 ms 14940 KB Output is correct
46 Correct 9 ms 14940 KB Output is correct
47 Correct 9 ms 14864 KB Output is correct
48 Correct 13 ms 14940 KB Output is correct
49 Correct 9 ms 14940 KB Output is correct
50 Correct 13 ms 14976 KB Output is correct
51 Correct 9 ms 14936 KB Output is correct
52 Correct 9 ms 14940 KB Output is correct
53 Correct 13 ms 14984 KB Output is correct
54 Correct 8 ms 14936 KB Output is correct
55 Correct 10 ms 14940 KB Output is correct
56 Correct 6 ms 14572 KB Output is correct
57 Correct 6 ms 14684 KB Output is correct
58 Correct 10 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 14424 KB Output is correct
2 Correct 8 ms 14428 KB Output is correct
3 Correct 9 ms 14684 KB Output is correct
4 Correct 97 ms 27060 KB Output is correct
5 Correct 76 ms 31972 KB Output is correct
6 Correct 100 ms 33876 KB Output is correct
7 Correct 109 ms 37712 KB Output is correct
8 Correct 122 ms 37712 KB Output is correct
9 Correct 146 ms 37712 KB Output is correct
10 Correct 138 ms 37940 KB Output is correct
11 Correct 127 ms 37684 KB Output is correct
12 Correct 120 ms 37792 KB Output is correct
13 Correct 111 ms 37784 KB Output is correct
14 Correct 108 ms 37716 KB Output is correct
15 Correct 41 ms 33476 KB Output is correct
16 Correct 134 ms 37452 KB Output is correct
17 Correct 71 ms 21072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14424 KB Output is correct
2 Correct 266 ms 22004 KB Output is correct
3 Correct 430 ms 23048 KB Output is correct
4 Correct 339 ms 24680 KB Output is correct
5 Correct 517 ms 29736 KB Output is correct
6 Correct 539 ms 29532 KB Output is correct
7 Correct 516 ms 29624 KB Output is correct
8 Correct 565 ms 29520 KB Output is correct
9 Correct 580 ms 29780 KB Output is correct
10 Correct 520 ms 29520 KB Output is correct
11 Correct 507 ms 29776 KB Output is correct
12 Correct 534 ms 30048 KB Output is correct
13 Correct 519 ms 30292 KB Output is correct
14 Correct 543 ms 31212 KB Output is correct
15 Correct 549 ms 34896 KB Output is correct
16 Correct 541 ms 30108 KB Output is correct
17 Correct 522 ms 30268 KB Output is correct
18 Correct 509 ms 30036 KB Output is correct
19 Correct 346 ms 25272 KB Output is correct
20 Correct 317 ms 25172 KB Output is correct
21 Correct 327 ms 25176 KB Output is correct
22 Correct 327 ms 25316 KB Output is correct
23 Correct 319 ms 25272 KB Output is correct
24 Correct 321 ms 25172 KB Output is correct
25 Correct 309 ms 25424 KB Output is correct
26 Correct 306 ms 25168 KB Output is correct
27 Correct 313 ms 25424 KB Output is correct
28 Correct 306 ms 25428 KB Output is correct
29 Correct 327 ms 25324 KB Output is correct
30 Correct 337 ms 25684 KB Output is correct
31 Correct 314 ms 25688 KB Output is correct
32 Correct 323 ms 26372 KB Output is correct
33 Correct 384 ms 27820 KB Output is correct
34 Correct 332 ms 30548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 14564 KB Output is correct
2 Correct 6 ms 14428 KB Output is correct
3 Correct 10 ms 14700 KB Output is correct
4 Correct 695 ms 26728 KB Output is correct
5 Correct 719 ms 29032 KB Output is correct
6 Correct 802 ms 32940 KB Output is correct
7 Correct 851 ms 35176 KB Output is correct
8 Correct 924 ms 35152 KB Output is correct
9 Correct 847 ms 35160 KB Output is correct
10 Correct 866 ms 35044 KB Output is correct
11 Correct 874 ms 35140 KB Output is correct
12 Correct 863 ms 35020 KB Output is correct
13 Correct 889 ms 35032 KB Output is correct
14 Correct 46 ms 21076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14428 KB Output is correct
2 Correct 5 ms 14368 KB Output is correct
3 Correct 8 ms 14424 KB Output is correct
4 Correct 10 ms 14440 KB Output is correct
5 Correct 6 ms 14428 KB Output is correct
6 Correct 7 ms 14424 KB Output is correct
7 Correct 6 ms 14428 KB Output is correct
8 Correct 6 ms 14428 KB Output is correct
9 Correct 7 ms 14672 KB Output is correct
10 Correct 6 ms 14424 KB Output is correct
11 Correct 6 ms 14428 KB Output is correct
12 Correct 6 ms 14480 KB Output is correct
13 Correct 7 ms 14648 KB Output is correct
14 Correct 6 ms 14464 KB Output is correct
15 Correct 6 ms 14428 KB Output is correct
16 Correct 6 ms 14424 KB Output is correct
17 Correct 8 ms 14428 KB Output is correct
18 Correct 6 ms 14428 KB Output is correct
19 Correct 9 ms 14616 KB Output is correct
20 Correct 7 ms 14428 KB Output is correct
21 Correct 6 ms 14424 KB Output is correct
22 Correct 7 ms 14428 KB Output is correct
23 Correct 6 ms 14564 KB Output is correct
24 Correct 6 ms 14424 KB Output is correct
25 Correct 8 ms 14428 KB Output is correct
26 Correct 6 ms 14428 KB Output is correct
27 Correct 6 ms 14424 KB Output is correct
28 Correct 6 ms 14428 KB Output is correct
29 Correct 7 ms 14428 KB Output is correct
30 Correct 12 ms 14680 KB Output is correct
31 Correct 9 ms 14684 KB Output is correct
32 Correct 11 ms 14940 KB Output is correct
33 Correct 10 ms 14824 KB Output is correct
34 Correct 11 ms 14940 KB Output is correct
35 Correct 16 ms 14940 KB Output is correct
36 Correct 11 ms 14940 KB Output is correct
37 Correct 11 ms 14936 KB Output is correct
38 Correct 7 ms 14840 KB Output is correct
39 Correct 8 ms 14940 KB Output is correct
40 Correct 9 ms 14780 KB Output is correct
41 Correct 8 ms 14936 KB Output is correct
42 Correct 8 ms 14936 KB Output is correct
43 Correct 10 ms 14968 KB Output is correct
44 Correct 9 ms 14948 KB Output is correct
45 Correct 9 ms 14940 KB Output is correct
46 Correct 9 ms 14940 KB Output is correct
47 Correct 9 ms 14864 KB Output is correct
48 Correct 13 ms 14940 KB Output is correct
49 Correct 9 ms 14940 KB Output is correct
50 Correct 13 ms 14976 KB Output is correct
51 Correct 9 ms 14936 KB Output is correct
52 Correct 9 ms 14940 KB Output is correct
53 Correct 13 ms 14984 KB Output is correct
54 Correct 8 ms 14936 KB Output is correct
55 Correct 10 ms 14940 KB Output is correct
56 Correct 6 ms 14572 KB Output is correct
57 Correct 6 ms 14684 KB Output is correct
58 Correct 10 ms 14940 KB Output is correct
59 Correct 6 ms 14424 KB Output is correct
60 Correct 8 ms 14428 KB Output is correct
61 Correct 9 ms 14684 KB Output is correct
62 Correct 97 ms 27060 KB Output is correct
63 Correct 76 ms 31972 KB Output is correct
64 Correct 100 ms 33876 KB Output is correct
65 Correct 109 ms 37712 KB Output is correct
66 Correct 122 ms 37712 KB Output is correct
67 Correct 146 ms 37712 KB Output is correct
68 Correct 138 ms 37940 KB Output is correct
69 Correct 127 ms 37684 KB Output is correct
70 Correct 120 ms 37792 KB Output is correct
71 Correct 111 ms 37784 KB Output is correct
72 Correct 108 ms 37716 KB Output is correct
73 Correct 41 ms 33476 KB Output is correct
74 Correct 134 ms 37452 KB Output is correct
75 Correct 71 ms 21072 KB Output is correct
76 Correct 5 ms 14424 KB Output is correct
77 Correct 266 ms 22004 KB Output is correct
78 Correct 430 ms 23048 KB Output is correct
79 Correct 339 ms 24680 KB Output is correct
80 Correct 517 ms 29736 KB Output is correct
81 Correct 539 ms 29532 KB Output is correct
82 Correct 516 ms 29624 KB Output is correct
83 Correct 565 ms 29520 KB Output is correct
84 Correct 580 ms 29780 KB Output is correct
85 Correct 520 ms 29520 KB Output is correct
86 Correct 507 ms 29776 KB Output is correct
87 Correct 534 ms 30048 KB Output is correct
88 Correct 519 ms 30292 KB Output is correct
89 Correct 543 ms 31212 KB Output is correct
90 Correct 549 ms 34896 KB Output is correct
91 Correct 541 ms 30108 KB Output is correct
92 Correct 522 ms 30268 KB Output is correct
93 Correct 509 ms 30036 KB Output is correct
94 Correct 346 ms 25272 KB Output is correct
95 Correct 317 ms 25172 KB Output is correct
96 Correct 327 ms 25176 KB Output is correct
97 Correct 327 ms 25316 KB Output is correct
98 Correct 319 ms 25272 KB Output is correct
99 Correct 321 ms 25172 KB Output is correct
100 Correct 309 ms 25424 KB Output is correct
101 Correct 306 ms 25168 KB Output is correct
102 Correct 313 ms 25424 KB Output is correct
103 Correct 306 ms 25428 KB Output is correct
104 Correct 327 ms 25324 KB Output is correct
105 Correct 337 ms 25684 KB Output is correct
106 Correct 314 ms 25688 KB Output is correct
107 Correct 323 ms 26372 KB Output is correct
108 Correct 384 ms 27820 KB Output is correct
109 Correct 332 ms 30548 KB Output is correct
110 Correct 6 ms 14564 KB Output is correct
111 Correct 6 ms 14428 KB Output is correct
112 Correct 10 ms 14700 KB Output is correct
113 Correct 695 ms 26728 KB Output is correct
114 Correct 719 ms 29032 KB Output is correct
115 Correct 802 ms 32940 KB Output is correct
116 Correct 851 ms 35176 KB Output is correct
117 Correct 924 ms 35152 KB Output is correct
118 Correct 847 ms 35160 KB Output is correct
119 Correct 866 ms 35044 KB Output is correct
120 Correct 874 ms 35140 KB Output is correct
121 Correct 863 ms 35020 KB Output is correct
122 Correct 889 ms 35032 KB Output is correct
123 Correct 46 ms 21076 KB Output is correct
124 Correct 468 ms 34128 KB Output is correct
125 Correct 379 ms 30972 KB Output is correct
126 Correct 565 ms 34744 KB Output is correct
127 Correct 572 ms 34844 KB Output is correct
128 Correct 578 ms 34896 KB Output is correct
129 Correct 541 ms 34896 KB Output is correct
130 Correct 553 ms 34904 KB Output is correct
131 Correct 123 ms 36692 KB Output is correct
132 Correct 126 ms 37636 KB Output is correct
133 Correct 131 ms 33932 KB Output is correct
134 Correct 358 ms 30364 KB Output is correct
135 Correct 364 ms 30548 KB Output is correct
136 Correct 348 ms 30464 KB Output is correct
137 Correct 213 ms 35268 KB Output is correct
138 Correct 211 ms 35268 KB Output is correct
139 Correct 216 ms 35340 KB Output is correct
140 Correct 225 ms 36184 KB Output is correct
141 Correct 213 ms 36040 KB Output is correct
142 Correct 215 ms 36036 KB Output is correct
143 Correct 56 ms 26464 KB Output is correct
144 Correct 558 ms 34384 KB Output is correct