Submission #923095

# Submission time Handle Problem Language Result Execution time Memory
923095 2024-02-06T15:01:10 Z denniskim Tourism (JOI23_tourism) C++17
100 / 100
449 ms 51924 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef __int128 lll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
#define MAX 9223372036854775807LL
#define MIN -9223372036854775807LL
#define INF 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed; cout.precision(10);
#define sp << " "
#define en << "\n"
#define compress(v) sort(v.begin(), v.end()), v.erase(unique(v.begin(), v.end()), v.end())

struct gujo
{
	ll L, R, num;
	
	bool operator < (const gujo &xx) const
	{
		return L > xx.L;
	}
};

ll n, m, q;
ll all, bll;
vector<ll> vec[100010];
gujo qry[100010];
ll a[100010];
ll IN[100010], OUT[100010], cc;
ll spa[100010][21], dep[100010], siz[100010];
ll tp[100010];
set<pll> st[100010];
ll maxx[100010];
ll ans[100010];

struct fenwicktree
{
	ll bit[100010];
	
	void update(ll w, ll v)
	{
		for(ll i = w ; i <= m + 1 ; i += (i & (-i)))
			bit[i] += v;
	}
	
	ll query(ll w)
	{
		ll ret = 0;
		
		for(ll i = w ; i > 0 ; i -= (i & (-i)))
			ret += bit[i];
		
		return ret;
	}
}BIT;

void dfs0(ll here, ll pa)
{
	siz[here] = 1;
	dep[here] = dep[pa] + 1;
	spa[here][0] = pa;
	
	for(auto &i : vec[here])
	{
		if(i == pa)
			continue;
		
		dfs0(i, here);
		siz[here] += siz[i];
	}
}

void dfs(ll here)
{
	IN[here] = ++cc;
	
	for(auto &i : vec[here])
	{
		if(i == vec[here][0])
			tp[i] = tp[here];
		else
			tp[i] = i;
		
		dfs(i);
	}
	
	OUT[here] = cc;
}

ll lift(ll X, ll Y)
{
	for(ll i = 20 ; i >= 0 ; i--)
	{
		if((1LL << i) <= Y)
		{
			Y -= (1LL << i);
			X = spa[X][i];
		}
	}
	
	return X;
}

void map_update(ll num, ll X, ll Y, ll Z)
{
	auto p = st[num].upper_bound({X, INF});
	auto p2 = p;
	vector< pair<pll, ll> > vv;
	ll END = maxx[num];
	
	p2--;
	
	while(1)
	{
		vv.push_back({{(*p2).fi, INF}, (*p2).se});
		p2++;
		
		if(p2 == st[num].end())
			break;
		
		if((*p2).fi > Y)
		{
			END = (*p2).fi - 1;
			break;
		}
	}
	
	for(ll i = 0 ; i < (ll)vv.size() ; i++)
	{
		if(i < (ll)vv.size() - 1)
			vv[i].fi.se = vv[i + 1].fi.fi - 1;
		else
			vv[i].fi.se = END;
	}
	
	vector< pair<pll, ll> > vv2;
	
	for(auto &i : vv)
	{
		if(X <= i.fi.fi && i.fi.se <= Y)
			continue;
		
		if(i.fi.fi < X)
		{
			if(X <= i.fi.se)
			{
				vv2.push_back({{i.fi.fi, X - 1}, i.se});
				
				if(Y < i.fi.se)
					vv2.push_back({{Y + 1, i.fi.se}, i.se});
			}
			
			else
				vv2.push_back(i);
		}
		
		else if(Y < i.fi.se)
		{
			if(i.fi.fi <= Y)
				vv2.push_back({{Y + 1, i.fi.se}, i.se});
			else
				vv2.push_back(i);
		}
	}
	
	vv2.push_back({{X, Y}, Z});
	
	for(auto &i : vv)
	{
		BIT.update(i.se, -i.fi.se + i.fi.fi - 1);
		st[num].erase({i.fi.fi, i.se});
	}
	
	for(auto &i : vv2)
	{
		BIT.update(i.se, i.fi.se - i.fi.fi + 1);
		st[num].insert({i.fi.fi, i.se});
	}
}

void update(ll X, ll Y, ll Z)
{
	while(tp[X] != tp[Y])
	{
		if(dep[tp[X]] < dep[tp[Y]])
			swap(X, Y);
		
		map_update(tp[X], IN[tp[X]], IN[X], Z);
		X = spa[tp[X]][0];
	}
	
	if(dep[X] < dep[Y])
		swap(X, Y);
	
	if(X == Y)
		return;
	
	ll tp2 = lift(X, dep[X] - dep[Y] - 1);
	
	map_update(tp[X], IN[tp2], IN[X], Z);
}

int main(void)
{
	fastio
	
	cin >> n >> m >> q;
	
	for(ll i = 1 ; i < n ; i++)
	{
		cin >> all >> bll;
		
		vec[all].push_back(bll);
		vec[bll].push_back(all);
	}
	
	for(ll i = 1 ; i <= m ; i++)
		cin >> a[i];
	
	for(ll i = 1 ; i <= q ; i++)
	{
		cin >> qry[i].L >> qry[i].R;
		qry[i].num = i;
	}
	
	sort(qry + 1, qry + 1 + q);
	
	dfs0(1, 0);
	
	for(ll i = 1 ; i <= 20 ; i++)
	{
		for(ll j = 1 ; j <= n ; j++)
			spa[j][i] = spa[spa[j][i - 1]][i - 1];
	}
	
	for(ll i = 2 ; i <= n ; i++)
	{
		for(ll j = 0 ; j < (ll)vec[i].size() - 1 ; j++)
		{
			if(vec[i][j] == spa[i][0])
				swap(vec[i][j], vec[i][j + 1]);
		}
		
		vec[i].pop_back();
		
		for(ll j = 0 ; j < (ll)vec[i].size() ; j++)
		{
			if(siz[vec[i][j]] > siz[vec[i][0]])
				swap(vec[i][j], vec[i][0]);
		}
	}
	
	tp[1] = 1;
	dfs(1);
	
	for(ll i = 1 ; i <= n ; i++)
	{
		maxx[tp[i]] = max(maxx[tp[i]], IN[i]);
		st[i].insert({IN[i], m + 1});
		BIT.update(m + 1, 1);
	}
	
	ll p = m;
	
	for(ll i = 1 ; i <= q ; i++)
	{
		while(p > qry[i].L)
		{
			update(a[p - 1], a[p], p);
			p--;
		}
		
		ans[qry[i].num] = BIT.query(qry[i].R);
	}
	
	for(ll i = 1 ; i <= q ; i++)
		cout << ans[i] + 1 en;
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 3 ms 12636 KB Output is correct
4 Correct 3 ms 12636 KB Output is correct
5 Correct 3 ms 12636 KB Output is correct
6 Correct 3 ms 12636 KB Output is correct
7 Correct 4 ms 12644 KB Output is correct
8 Correct 3 ms 12632 KB Output is correct
9 Correct 3 ms 12636 KB Output is correct
10 Correct 4 ms 12652 KB Output is correct
11 Correct 4 ms 12636 KB Output is correct
12 Correct 3 ms 12636 KB Output is correct
13 Correct 3 ms 12636 KB Output is correct
14 Correct 3 ms 12636 KB Output is correct
15 Correct 3 ms 12632 KB Output is correct
16 Correct 3 ms 12636 KB Output is correct
17 Correct 3 ms 12636 KB Output is correct
18 Correct 3 ms 12636 KB Output is correct
19 Correct 4 ms 12652 KB Output is correct
20 Correct 3 ms 12632 KB Output is correct
21 Correct 3 ms 12636 KB Output is correct
22 Correct 3 ms 12636 KB Output is correct
23 Correct 4 ms 12636 KB Output is correct
24 Correct 3 ms 12636 KB Output is correct
25 Correct 3 ms 12644 KB Output is correct
26 Correct 3 ms 12632 KB Output is correct
27 Correct 3 ms 12632 KB Output is correct
28 Correct 3 ms 12644 KB Output is correct
29 Correct 3 ms 12644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 3 ms 12636 KB Output is correct
4 Correct 3 ms 12636 KB Output is correct
5 Correct 3 ms 12636 KB Output is correct
6 Correct 3 ms 12636 KB Output is correct
7 Correct 4 ms 12644 KB Output is correct
8 Correct 3 ms 12632 KB Output is correct
9 Correct 3 ms 12636 KB Output is correct
10 Correct 4 ms 12652 KB Output is correct
11 Correct 4 ms 12636 KB Output is correct
12 Correct 3 ms 12636 KB Output is correct
13 Correct 3 ms 12636 KB Output is correct
14 Correct 3 ms 12636 KB Output is correct
15 Correct 3 ms 12632 KB Output is correct
16 Correct 3 ms 12636 KB Output is correct
17 Correct 3 ms 12636 KB Output is correct
18 Correct 3 ms 12636 KB Output is correct
19 Correct 4 ms 12652 KB Output is correct
20 Correct 3 ms 12632 KB Output is correct
21 Correct 3 ms 12636 KB Output is correct
22 Correct 3 ms 12636 KB Output is correct
23 Correct 4 ms 12636 KB Output is correct
24 Correct 3 ms 12636 KB Output is correct
25 Correct 3 ms 12644 KB Output is correct
26 Correct 3 ms 12632 KB Output is correct
27 Correct 3 ms 12632 KB Output is correct
28 Correct 3 ms 12644 KB Output is correct
29 Correct 3 ms 12644 KB Output is correct
30 Correct 6 ms 13148 KB Output is correct
31 Correct 5 ms 13148 KB Output is correct
32 Correct 6 ms 13148 KB Output is correct
33 Correct 6 ms 13148 KB Output is correct
34 Correct 7 ms 13268 KB Output is correct
35 Correct 6 ms 13148 KB Output is correct
36 Correct 7 ms 13148 KB Output is correct
37 Correct 6 ms 13148 KB Output is correct
38 Correct 5 ms 13404 KB Output is correct
39 Correct 5 ms 13404 KB Output is correct
40 Correct 5 ms 13412 KB Output is correct
41 Correct 5 ms 13404 KB Output is correct
42 Correct 5 ms 13404 KB Output is correct
43 Correct 5 ms 13404 KB Output is correct
44 Correct 6 ms 13148 KB Output is correct
45 Correct 6 ms 13148 KB Output is correct
46 Correct 6 ms 13148 KB Output is correct
47 Correct 6 ms 13148 KB Output is correct
48 Correct 7 ms 13148 KB Output is correct
49 Correct 6 ms 13376 KB Output is correct
50 Correct 5 ms 13144 KB Output is correct
51 Correct 5 ms 13148 KB Output is correct
52 Correct 5 ms 13280 KB Output is correct
53 Correct 5 ms 13144 KB Output is correct
54 Correct 5 ms 13148 KB Output is correct
55 Correct 5 ms 13148 KB Output is correct
56 Correct 3 ms 12636 KB Output is correct
57 Correct 4 ms 13168 KB Output is correct
58 Correct 6 ms 13148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12632 KB Output is correct
2 Correct 3 ms 12632 KB Output is correct
3 Correct 4 ms 12636 KB Output is correct
4 Correct 107 ms 33300 KB Output is correct
5 Correct 104 ms 41556 KB Output is correct
6 Correct 133 ms 45648 KB Output is correct
7 Correct 173 ms 48780 KB Output is correct
8 Correct 163 ms 48808 KB Output is correct
9 Correct 168 ms 48724 KB Output is correct
10 Correct 189 ms 48908 KB Output is correct
11 Correct 179 ms 48696 KB Output is correct
12 Correct 131 ms 49240 KB Output is correct
13 Correct 134 ms 49316 KB Output is correct
14 Correct 131 ms 49364 KB Output is correct
15 Correct 57 ms 47700 KB Output is correct
16 Correct 167 ms 48380 KB Output is correct
17 Correct 38 ms 13388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 137 ms 29972 KB Output is correct
3 Correct 222 ms 34128 KB Output is correct
4 Correct 205 ms 35028 KB Output is correct
5 Correct 281 ms 45396 KB Output is correct
6 Correct 311 ms 45744 KB Output is correct
7 Correct 326 ms 45392 KB Output is correct
8 Correct 315 ms 45536 KB Output is correct
9 Correct 304 ms 45596 KB Output is correct
10 Correct 297 ms 45584 KB Output is correct
11 Correct 315 ms 45544 KB Output is correct
12 Correct 331 ms 45580 KB Output is correct
13 Correct 327 ms 45776 KB Output is correct
14 Correct 304 ms 46136 KB Output is correct
15 Correct 312 ms 47576 KB Output is correct
16 Correct 278 ms 45704 KB Output is correct
17 Correct 282 ms 45648 KB Output is correct
18 Correct 298 ms 45844 KB Output is correct
19 Correct 221 ms 43860 KB Output is correct
20 Correct 225 ms 43880 KB Output is correct
21 Correct 223 ms 43860 KB Output is correct
22 Correct 224 ms 43940 KB Output is correct
23 Correct 221 ms 43856 KB Output is correct
24 Correct 227 ms 43860 KB Output is correct
25 Correct 222 ms 43856 KB Output is correct
26 Correct 228 ms 44108 KB Output is correct
27 Correct 223 ms 43856 KB Output is correct
28 Correct 224 ms 43860 KB Output is correct
29 Correct 233 ms 44228 KB Output is correct
30 Correct 228 ms 44040 KB Output is correct
31 Correct 238 ms 44100 KB Output is correct
32 Correct 236 ms 44372 KB Output is correct
33 Correct 248 ms 44884 KB Output is correct
34 Correct 238 ms 46112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12648 KB Output is correct
3 Correct 3 ms 12636 KB Output is correct
4 Correct 349 ms 32100 KB Output is correct
5 Correct 356 ms 34520 KB Output is correct
6 Correct 406 ms 42576 KB Output is correct
7 Correct 442 ms 45904 KB Output is correct
8 Correct 435 ms 45776 KB Output is correct
9 Correct 434 ms 45980 KB Output is correct
10 Correct 445 ms 45916 KB Output is correct
11 Correct 431 ms 45904 KB Output is correct
12 Correct 449 ms 45736 KB Output is correct
13 Correct 424 ms 45756 KB Output is correct
14 Correct 32 ms 13392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 3 ms 12636 KB Output is correct
4 Correct 3 ms 12636 KB Output is correct
5 Correct 3 ms 12636 KB Output is correct
6 Correct 3 ms 12636 KB Output is correct
7 Correct 4 ms 12644 KB Output is correct
8 Correct 3 ms 12632 KB Output is correct
9 Correct 3 ms 12636 KB Output is correct
10 Correct 4 ms 12652 KB Output is correct
11 Correct 4 ms 12636 KB Output is correct
12 Correct 3 ms 12636 KB Output is correct
13 Correct 3 ms 12636 KB Output is correct
14 Correct 3 ms 12636 KB Output is correct
15 Correct 3 ms 12632 KB Output is correct
16 Correct 3 ms 12636 KB Output is correct
17 Correct 3 ms 12636 KB Output is correct
18 Correct 3 ms 12636 KB Output is correct
19 Correct 4 ms 12652 KB Output is correct
20 Correct 3 ms 12632 KB Output is correct
21 Correct 3 ms 12636 KB Output is correct
22 Correct 3 ms 12636 KB Output is correct
23 Correct 4 ms 12636 KB Output is correct
24 Correct 3 ms 12636 KB Output is correct
25 Correct 3 ms 12644 KB Output is correct
26 Correct 3 ms 12632 KB Output is correct
27 Correct 3 ms 12632 KB Output is correct
28 Correct 3 ms 12644 KB Output is correct
29 Correct 3 ms 12644 KB Output is correct
30 Correct 6 ms 13148 KB Output is correct
31 Correct 5 ms 13148 KB Output is correct
32 Correct 6 ms 13148 KB Output is correct
33 Correct 6 ms 13148 KB Output is correct
34 Correct 7 ms 13268 KB Output is correct
35 Correct 6 ms 13148 KB Output is correct
36 Correct 7 ms 13148 KB Output is correct
37 Correct 6 ms 13148 KB Output is correct
38 Correct 5 ms 13404 KB Output is correct
39 Correct 5 ms 13404 KB Output is correct
40 Correct 5 ms 13412 KB Output is correct
41 Correct 5 ms 13404 KB Output is correct
42 Correct 5 ms 13404 KB Output is correct
43 Correct 5 ms 13404 KB Output is correct
44 Correct 6 ms 13148 KB Output is correct
45 Correct 6 ms 13148 KB Output is correct
46 Correct 6 ms 13148 KB Output is correct
47 Correct 6 ms 13148 KB Output is correct
48 Correct 7 ms 13148 KB Output is correct
49 Correct 6 ms 13376 KB Output is correct
50 Correct 5 ms 13144 KB Output is correct
51 Correct 5 ms 13148 KB Output is correct
52 Correct 5 ms 13280 KB Output is correct
53 Correct 5 ms 13144 KB Output is correct
54 Correct 5 ms 13148 KB Output is correct
55 Correct 5 ms 13148 KB Output is correct
56 Correct 3 ms 12636 KB Output is correct
57 Correct 4 ms 13168 KB Output is correct
58 Correct 6 ms 13148 KB Output is correct
59 Correct 3 ms 12632 KB Output is correct
60 Correct 3 ms 12632 KB Output is correct
61 Correct 4 ms 12636 KB Output is correct
62 Correct 107 ms 33300 KB Output is correct
63 Correct 104 ms 41556 KB Output is correct
64 Correct 133 ms 45648 KB Output is correct
65 Correct 173 ms 48780 KB Output is correct
66 Correct 163 ms 48808 KB Output is correct
67 Correct 168 ms 48724 KB Output is correct
68 Correct 189 ms 48908 KB Output is correct
69 Correct 179 ms 48696 KB Output is correct
70 Correct 131 ms 49240 KB Output is correct
71 Correct 134 ms 49316 KB Output is correct
72 Correct 131 ms 49364 KB Output is correct
73 Correct 57 ms 47700 KB Output is correct
74 Correct 167 ms 48380 KB Output is correct
75 Correct 38 ms 13388 KB Output is correct
76 Correct 3 ms 12636 KB Output is correct
77 Correct 137 ms 29972 KB Output is correct
78 Correct 222 ms 34128 KB Output is correct
79 Correct 205 ms 35028 KB Output is correct
80 Correct 281 ms 45396 KB Output is correct
81 Correct 311 ms 45744 KB Output is correct
82 Correct 326 ms 45392 KB Output is correct
83 Correct 315 ms 45536 KB Output is correct
84 Correct 304 ms 45596 KB Output is correct
85 Correct 297 ms 45584 KB Output is correct
86 Correct 315 ms 45544 KB Output is correct
87 Correct 331 ms 45580 KB Output is correct
88 Correct 327 ms 45776 KB Output is correct
89 Correct 304 ms 46136 KB Output is correct
90 Correct 312 ms 47576 KB Output is correct
91 Correct 278 ms 45704 KB Output is correct
92 Correct 282 ms 45648 KB Output is correct
93 Correct 298 ms 45844 KB Output is correct
94 Correct 221 ms 43860 KB Output is correct
95 Correct 225 ms 43880 KB Output is correct
96 Correct 223 ms 43860 KB Output is correct
97 Correct 224 ms 43940 KB Output is correct
98 Correct 221 ms 43856 KB Output is correct
99 Correct 227 ms 43860 KB Output is correct
100 Correct 222 ms 43856 KB Output is correct
101 Correct 228 ms 44108 KB Output is correct
102 Correct 223 ms 43856 KB Output is correct
103 Correct 224 ms 43860 KB Output is correct
104 Correct 233 ms 44228 KB Output is correct
105 Correct 228 ms 44040 KB Output is correct
106 Correct 238 ms 44100 KB Output is correct
107 Correct 236 ms 44372 KB Output is correct
108 Correct 248 ms 44884 KB Output is correct
109 Correct 238 ms 46112 KB Output is correct
110 Correct 2 ms 12636 KB Output is correct
111 Correct 2 ms 12648 KB Output is correct
112 Correct 3 ms 12636 KB Output is correct
113 Correct 349 ms 32100 KB Output is correct
114 Correct 356 ms 34520 KB Output is correct
115 Correct 406 ms 42576 KB Output is correct
116 Correct 442 ms 45904 KB Output is correct
117 Correct 435 ms 45776 KB Output is correct
118 Correct 434 ms 45980 KB Output is correct
119 Correct 445 ms 45916 KB Output is correct
120 Correct 431 ms 45904 KB Output is correct
121 Correct 449 ms 45736 KB Output is correct
122 Correct 424 ms 45756 KB Output is correct
123 Correct 32 ms 13392 KB Output is correct
124 Correct 274 ms 47696 KB Output is correct
125 Correct 217 ms 45896 KB Output is correct
126 Correct 307 ms 47952 KB Output is correct
127 Correct 309 ms 47952 KB Output is correct
128 Correct 308 ms 48044 KB Output is correct
129 Correct 317 ms 48060 KB Output is correct
130 Correct 318 ms 48104 KB Output is correct
131 Correct 187 ms 50616 KB Output is correct
132 Correct 207 ms 51924 KB Output is correct
133 Correct 168 ms 48512 KB Output is correct
134 Correct 251 ms 46432 KB Output is correct
135 Correct 250 ms 46324 KB Output is correct
136 Correct 246 ms 46560 KB Output is correct
137 Correct 131 ms 47044 KB Output is correct
138 Correct 121 ms 46788 KB Output is correct
139 Correct 127 ms 46916 KB Output is correct
140 Correct 125 ms 46792 KB Output is correct
141 Correct 139 ms 47136 KB Output is correct
142 Correct 125 ms 47100 KB Output is correct
143 Correct 67 ms 44112 KB Output is correct
144 Correct 326 ms 48084 KB Output is correct