Submission #909704

# Submission time Handle Problem Language Result Execution time Memory
909704 2024-01-17T10:56:00 Z penguin133 Birthday gift (IZhO18_treearray) C++17
56 / 100
4000 ms 106756 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
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, m, q;
vector <int> adj[300005];
multiset <int> ms[300005];
int S[300005], E[300005], cnt = 1, par[20][200005], dep[200005];

void dfs(int x, int p, int d){
	S[x] = cnt++;
	par[0][x] = p;
	dep[x] = d;
	for(auto i : adj[x])if(i != p)dfs(i, x, d + 1);
	E[x] = cnt - 1;
}

int lca(int u, int v){
	if(dep[u] > dep[v])swap(u, v);
	int df = dep[v] - dep[u];
	for(int i=0;i<19;i++)if(df >> i & 1)v = par[i][v];
	if(u == v)return u;
	for(int i=19;i>=0;i--){
		if(par[i][u] != par[i][v])u = par[i][u], v = par[i][v];
	}
	return par[0][u];
}
int back[300005];
struct node{
	int s, e, m, mn, mx, mx2;
	node *l, *r;
	node(int _s, int _e){
		s = _s, e = _e, m = (s + e) >> 1;
		if(s != e)l = new node(s, m), r = new node(m+1, e);
		mn = 1e9, mx = mx2 = 0;
	}
	
	void upd(int p, int v, int v2){
		if(s == e)mn = mx2 = v, mx = v2;
		else{
			if(p <= m)l->upd(p, v, v2);
			else r->upd(p, v, v2);
			mn = min(l->mn, r->mn);
			mx = max(l->mx, r->mx);
			mx2 = max(l->mx2, r->mx2);
		}
	}
	
	int q1(int a, int b){
		if(a == s && b == e)return mn;
		if(b <= m)return l->q1(a, b);
		if(a > m)return r->q1(a, b);
		return min(l->q1(a, m), r->q1(m+1, b));
	}
	
	int q2(int a, int b){
		if(a == s && b == e)return mx;
		if(b <= m)return l->q2(a, b);
		if(a > m)return r->q2(a, b);
		return max(l->q2(a, m), r->q2(m+1, b));
	}
	
	int q3(int a, int b){
		if(a == s && b == e)return mx2;
		if(b <= m)return l->q3(a, b);
		if(a > m)return r->q3(a, b);
		return max(l->q3(a, m), r->q3(m+1, b));
	}
}*root;

int A[300005], f, v;

void dnc(int l, int r){
	if(f || l > r)return;
	int mid = (l + r) >> 1;
	if(mid != r)dnc(l, mid); dnc(mid + 1, r);
	if(f)return;
	int lo = mid + 1, hi = r, tmp = hi + 1;
	while(lo <= hi){
		int mm = (lo + hi) >> 1;
		if(root->q1(mid + 1, mm) < S[v] || root->q2(mid + 1, mm) > E[v])tmp = mm, hi = mm - 1;
		else lo = mm + 1;
	}
	tmp--;
	lo = l, hi = mid;
	int tmp2 = lo - 1;
	while(lo <= hi){
		int mm = (lo + hi) >> 1;
		if(root->q1(mm, mid) < S[v] || root->q2(mm, mid) > E[v])tmp2 = mm, lo = mm + 1;
		else hi = mm - 1;
	}
	tmp2++;
	if(tmp2 > tmp)return;
	hi = root->q1(tmp2, tmp); int hi2 = root->q3(tmp2, tmp);
	//cerr << l << ' ' << r << ' ' << hi << ' ' << hi2 << '\n'; 
	if(lca(back[hi], back[hi2]) == v){
		cout << tmp2 << ' ' << tmp << '\n';
		f = 1;
		return;
	}
}

void solve(){
	cin >> n >> m >> q;
	for(int i=1;i<n;i++){
		int a, b; cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(1, -1, 1);
	root = new node(1, m);
	for(int i=1;i<=m;i++)cin >> A[i], root->upd(i, S[A[i]], E[A[i]]);
	/*
	root = new node(1, n);
	for(int i=1;i<=n;i++){
		if(!ms[i].empty()){
			auto it = ms[i].begin(), it2 = --ms[i].end();
			root->upd(S[i], *it, *it2);
		}
	}
	*/
	for(int i=1;i<=n;i++)back[S[i]] = i;
	for(int i=1;i<=19;i++)for(int j=1;j<=n;j++)par[i][j] = par[i-1][par[i-1][j]];
	cerr << "ok\n";
	while(q--){
		int t; cin >> t;
		if(t == 1){
			int x, y; cin >> x >> y;
			root->upd(x, S[y], E[y]);
		}
		else{
			int l, r; cin >> l >> r >> v;
			f = 0;
			dnc(l, r);
			if(!f)cout << -1 << ' ' << -1 << '\n';
		}
	}
}

main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int tc = 1;
	//cin >> tc;
	for(int tc1=1;tc1<=tc;tc1++){
		// cout << "Case #" << tc1 << ": ";
		solve();
	}
}

Compilation message

treearray.cpp: In function 'void dnc(long long int, long long int)':
treearray.cpp:85:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   85 |  if(mid != r)dnc(l, mid); dnc(mid + 1, r);
      |  ^~
treearray.cpp:85:27: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   85 |  if(mid != r)dnc(l, mid); dnc(mid + 1, r);
      |                           ^~~
treearray.cpp: At global scope:
treearray.cpp:149:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  149 | main(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 61532 KB n=5
2 Correct 13 ms 61528 KB n=100
3 Correct 16 ms 61532 KB n=100
4 Correct 14 ms 61528 KB n=100
5 Correct 13 ms 61532 KB n=100
6 Correct 15 ms 61528 KB n=100
7 Correct 14 ms 61528 KB n=100
8 Correct 14 ms 61532 KB n=100
9 Correct 14 ms 61532 KB n=100
10 Correct 14 ms 61532 KB n=100
11 Correct 14 ms 61532 KB n=100
12 Correct 13 ms 61532 KB n=100
13 Correct 14 ms 61532 KB n=100
14 Correct 14 ms 61532 KB n=100
15 Correct 13 ms 61632 KB n=100
16 Correct 14 ms 61528 KB n=100
17 Correct 15 ms 61532 KB n=100
18 Correct 14 ms 61528 KB n=100
19 Correct 13 ms 61632 KB n=100
20 Correct 13 ms 61532 KB n=100
21 Correct 14 ms 61532 KB n=100
22 Correct 14 ms 61528 KB n=100
23 Correct 16 ms 61532 KB n=100
24 Correct 16 ms 61532 KB n=100
25 Correct 14 ms 61664 KB n=100
26 Correct 13 ms 61528 KB n=12
27 Correct 13 ms 61532 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 13 ms 61532 KB n=5
2 Correct 13 ms 61528 KB n=100
3 Correct 16 ms 61532 KB n=100
4 Correct 14 ms 61528 KB n=100
5 Correct 13 ms 61532 KB n=100
6 Correct 15 ms 61528 KB n=100
7 Correct 14 ms 61528 KB n=100
8 Correct 14 ms 61532 KB n=100
9 Correct 14 ms 61532 KB n=100
10 Correct 14 ms 61532 KB n=100
11 Correct 14 ms 61532 KB n=100
12 Correct 13 ms 61532 KB n=100
13 Correct 14 ms 61532 KB n=100
14 Correct 14 ms 61532 KB n=100
15 Correct 13 ms 61632 KB n=100
16 Correct 14 ms 61528 KB n=100
17 Correct 15 ms 61532 KB n=100
18 Correct 14 ms 61528 KB n=100
19 Correct 13 ms 61632 KB n=100
20 Correct 13 ms 61532 KB n=100
21 Correct 14 ms 61532 KB n=100
22 Correct 14 ms 61528 KB n=100
23 Correct 16 ms 61532 KB n=100
24 Correct 16 ms 61532 KB n=100
25 Correct 14 ms 61664 KB n=100
26 Correct 13 ms 61528 KB n=12
27 Correct 13 ms 61532 KB n=100
28 Correct 14 ms 61532 KB n=500
29 Correct 26 ms 61532 KB n=500
30 Correct 24 ms 61528 KB n=500
31 Correct 28 ms 61752 KB n=500
32 Correct 15 ms 61532 KB n=500
33 Correct 23 ms 61528 KB n=500
34 Correct 14 ms 61532 KB n=500
35 Correct 24 ms 61768 KB n=500
36 Correct 61 ms 61764 KB n=500
37 Correct 75 ms 61744 KB n=500
38 Correct 76 ms 61528 KB n=500
39 Correct 38 ms 61784 KB n=500
40 Correct 47 ms 61528 KB n=500
41 Correct 47 ms 61528 KB n=500
42 Correct 39 ms 61780 KB n=500
43 Correct 44 ms 61740 KB n=500
44 Correct 43 ms 61528 KB n=500
45 Correct 14 ms 61532 KB n=500
46 Correct 27 ms 61532 KB n=500
47 Correct 23 ms 61532 KB n=500
48 Correct 15 ms 61744 KB n=500
49 Correct 22 ms 61788 KB n=500
50 Correct 25 ms 61528 KB n=500
51 Correct 27 ms 61528 KB n=500
52 Correct 32 ms 61708 KB n=500
53 Correct 26 ms 61528 KB n=500
54 Correct 49 ms 61788 KB n=500
55 Correct 16 ms 61752 KB n=278
56 Correct 90 ms 61756 KB n=500
57 Correct 90 ms 61532 KB n=500
58 Correct 33 ms 61752 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 13 ms 61532 KB n=5
2 Correct 13 ms 61528 KB n=100
3 Correct 16 ms 61532 KB n=100
4 Correct 14 ms 61528 KB n=100
5 Correct 13 ms 61532 KB n=100
6 Correct 15 ms 61528 KB n=100
7 Correct 14 ms 61528 KB n=100
8 Correct 14 ms 61532 KB n=100
9 Correct 14 ms 61532 KB n=100
10 Correct 14 ms 61532 KB n=100
11 Correct 14 ms 61532 KB n=100
12 Correct 13 ms 61532 KB n=100
13 Correct 14 ms 61532 KB n=100
14 Correct 14 ms 61532 KB n=100
15 Correct 13 ms 61632 KB n=100
16 Correct 14 ms 61528 KB n=100
17 Correct 15 ms 61532 KB n=100
18 Correct 14 ms 61528 KB n=100
19 Correct 13 ms 61632 KB n=100
20 Correct 13 ms 61532 KB n=100
21 Correct 14 ms 61532 KB n=100
22 Correct 14 ms 61528 KB n=100
23 Correct 16 ms 61532 KB n=100
24 Correct 16 ms 61532 KB n=100
25 Correct 14 ms 61664 KB n=100
26 Correct 13 ms 61528 KB n=12
27 Correct 13 ms 61532 KB n=100
28 Correct 14 ms 61532 KB n=500
29 Correct 26 ms 61532 KB n=500
30 Correct 24 ms 61528 KB n=500
31 Correct 28 ms 61752 KB n=500
32 Correct 15 ms 61532 KB n=500
33 Correct 23 ms 61528 KB n=500
34 Correct 14 ms 61532 KB n=500
35 Correct 24 ms 61768 KB n=500
36 Correct 61 ms 61764 KB n=500
37 Correct 75 ms 61744 KB n=500
38 Correct 76 ms 61528 KB n=500
39 Correct 38 ms 61784 KB n=500
40 Correct 47 ms 61528 KB n=500
41 Correct 47 ms 61528 KB n=500
42 Correct 39 ms 61780 KB n=500
43 Correct 44 ms 61740 KB n=500
44 Correct 43 ms 61528 KB n=500
45 Correct 14 ms 61532 KB n=500
46 Correct 27 ms 61532 KB n=500
47 Correct 23 ms 61532 KB n=500
48 Correct 15 ms 61744 KB n=500
49 Correct 22 ms 61788 KB n=500
50 Correct 25 ms 61528 KB n=500
51 Correct 27 ms 61528 KB n=500
52 Correct 32 ms 61708 KB n=500
53 Correct 26 ms 61528 KB n=500
54 Correct 49 ms 61788 KB n=500
55 Correct 16 ms 61752 KB n=278
56 Correct 90 ms 61756 KB n=500
57 Correct 90 ms 61532 KB n=500
58 Correct 33 ms 61752 KB n=500
59 Correct 18 ms 62044 KB n=2000
60 Correct 213 ms 62124 KB n=2000
61 Correct 208 ms 62100 KB n=2000
62 Correct 142 ms 62040 KB n=2000
63 Correct 20 ms 62040 KB n=2000
64 Correct 189 ms 62080 KB n=2000
65 Correct 18 ms 62296 KB n=2000
66 Correct 225 ms 62044 KB n=2000
67 Correct 25 ms 62040 KB n=2000
68 Correct 192 ms 62088 KB n=2000
69 Correct 900 ms 62072 KB n=2000
70 Correct 902 ms 62316 KB n=2000
71 Correct 910 ms 62060 KB n=2000
72 Correct 503 ms 62056 KB n=2000
73 Correct 498 ms 62076 KB n=2000
74 Correct 16 ms 62044 KB n=1844
75 Correct 519 ms 62044 KB n=2000
76 Correct 474 ms 62040 KB n=2000
77 Correct 465 ms 62052 KB n=2000
78 Correct 544 ms 62052 KB n=2000
79 Correct 17 ms 62044 KB n=2000
80 Correct 186 ms 62112 KB n=2000
81 Correct 178 ms 62076 KB n=2000
82 Correct 17 ms 62044 KB n=2000
83 Correct 192 ms 62096 KB n=2000
84 Correct 219 ms 62044 KB n=2000
85 Correct 273 ms 62056 KB n=2000
86 Correct 240 ms 62064 KB n=2000
87 Correct 223 ms 62060 KB n=2000
88 Correct 1545 ms 62124 KB n=2000
89 Correct 1528 ms 62124 KB n=2000
90 Correct 645 ms 62132 KB n=2000
91 Correct 526 ms 62064 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 13 ms 61532 KB n=5
2 Correct 13 ms 61528 KB n=100
3 Correct 16 ms 61532 KB n=100
4 Correct 14 ms 61528 KB n=100
5 Correct 13 ms 61532 KB n=100
6 Correct 15 ms 61528 KB n=100
7 Correct 14 ms 61528 KB n=100
8 Correct 14 ms 61532 KB n=100
9 Correct 14 ms 61532 KB n=100
10 Correct 14 ms 61532 KB n=100
11 Correct 14 ms 61532 KB n=100
12 Correct 13 ms 61532 KB n=100
13 Correct 14 ms 61532 KB n=100
14 Correct 14 ms 61532 KB n=100
15 Correct 13 ms 61632 KB n=100
16 Correct 14 ms 61528 KB n=100
17 Correct 15 ms 61532 KB n=100
18 Correct 14 ms 61528 KB n=100
19 Correct 13 ms 61632 KB n=100
20 Correct 13 ms 61532 KB n=100
21 Correct 14 ms 61532 KB n=100
22 Correct 14 ms 61528 KB n=100
23 Correct 16 ms 61532 KB n=100
24 Correct 16 ms 61532 KB n=100
25 Correct 14 ms 61664 KB n=100
26 Correct 13 ms 61528 KB n=12
27 Correct 13 ms 61532 KB n=100
28 Correct 14 ms 61532 KB n=500
29 Correct 26 ms 61532 KB n=500
30 Correct 24 ms 61528 KB n=500
31 Correct 28 ms 61752 KB n=500
32 Correct 15 ms 61532 KB n=500
33 Correct 23 ms 61528 KB n=500
34 Correct 14 ms 61532 KB n=500
35 Correct 24 ms 61768 KB n=500
36 Correct 61 ms 61764 KB n=500
37 Correct 75 ms 61744 KB n=500
38 Correct 76 ms 61528 KB n=500
39 Correct 38 ms 61784 KB n=500
40 Correct 47 ms 61528 KB n=500
41 Correct 47 ms 61528 KB n=500
42 Correct 39 ms 61780 KB n=500
43 Correct 44 ms 61740 KB n=500
44 Correct 43 ms 61528 KB n=500
45 Correct 14 ms 61532 KB n=500
46 Correct 27 ms 61532 KB n=500
47 Correct 23 ms 61532 KB n=500
48 Correct 15 ms 61744 KB n=500
49 Correct 22 ms 61788 KB n=500
50 Correct 25 ms 61528 KB n=500
51 Correct 27 ms 61528 KB n=500
52 Correct 32 ms 61708 KB n=500
53 Correct 26 ms 61528 KB n=500
54 Correct 49 ms 61788 KB n=500
55 Correct 16 ms 61752 KB n=278
56 Correct 90 ms 61756 KB n=500
57 Correct 90 ms 61532 KB n=500
58 Correct 33 ms 61752 KB n=500
59 Correct 18 ms 62044 KB n=2000
60 Correct 213 ms 62124 KB n=2000
61 Correct 208 ms 62100 KB n=2000
62 Correct 142 ms 62040 KB n=2000
63 Correct 20 ms 62040 KB n=2000
64 Correct 189 ms 62080 KB n=2000
65 Correct 18 ms 62296 KB n=2000
66 Correct 225 ms 62044 KB n=2000
67 Correct 25 ms 62040 KB n=2000
68 Correct 192 ms 62088 KB n=2000
69 Correct 900 ms 62072 KB n=2000
70 Correct 902 ms 62316 KB n=2000
71 Correct 910 ms 62060 KB n=2000
72 Correct 503 ms 62056 KB n=2000
73 Correct 498 ms 62076 KB n=2000
74 Correct 16 ms 62044 KB n=1844
75 Correct 519 ms 62044 KB n=2000
76 Correct 474 ms 62040 KB n=2000
77 Correct 465 ms 62052 KB n=2000
78 Correct 544 ms 62052 KB n=2000
79 Correct 17 ms 62044 KB n=2000
80 Correct 186 ms 62112 KB n=2000
81 Correct 178 ms 62076 KB n=2000
82 Correct 17 ms 62044 KB n=2000
83 Correct 192 ms 62096 KB n=2000
84 Correct 219 ms 62044 KB n=2000
85 Correct 273 ms 62056 KB n=2000
86 Correct 240 ms 62064 KB n=2000
87 Correct 223 ms 62060 KB n=2000
88 Correct 1545 ms 62124 KB n=2000
89 Correct 1528 ms 62124 KB n=2000
90 Correct 645 ms 62132 KB n=2000
91 Correct 526 ms 62064 KB n=2000
92 Correct 1988 ms 104268 KB n=200000
93 Execution timed out 4054 ms 106756 KB Time limit exceeded
94 Halted 0 ms 0 KB -