Submission #697937

# Submission time Handle Problem Language Result Execution time Memory
697937 2023-02-11T13:58:16 Z vjudge1 Birthday gift (IZhO18_treearray) Java 11
56 / 100
2787 ms 262144 KB
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.IntStream;

import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.stream.IntStream.*;

/********************************************/
public class treearray {
	public static void main(String[] args) throws IOException {
		new Solution().run();
	}
}
/********************************************/

class Solution {
	final FastScanner scanner = new FastScanner();
	final PrintWriter out = new PrintWriter(System.out);
	final int mod = (int) 1e9+7;
	final int[] dx = {1, 0, -1, 0};
	final int[] dy = {0, 1, 0, -1};
	int maxn = (int) 3e5+10;
	int n, m, q;
	int[] a, d;
	int[][] up;
	TreeSet<Integer>[] s, t;

	void solve() throws IOException {
		n = scanner.nextInt(); m = scanner.nextInt(); q = scanner.nextInt();
		init(n); up = new int[20][n+1]; a = new int[m+1]; d = new int[n+1];
		s = new TreeSet[n+1]; t = new TreeSet[n+1];
		for (int i = 1; i<n; i++) {
			int x = scanner.nextInt(), y = scanner.nextInt();
			g[x].add(y); g[y].add(x);
		}
		for (int i = 1; i<=m; i++) {
			a[i] = scanner.nextInt();
		}
		for (int i = 1; i<=n; i++) {
			s[i] = new TreeSet<>();
			t[i] = new TreeSet<>();
		}
		dfs(1, 1);
		for (int i = 1; i<=m; i++) s[a[i]].add(i);
		for (int i = 2; i<=m; i++) t[lca(a[i], a[i-1])].add(i);
		for(int i = 1; i <= n; i++) {
			s[i].add(mod);
			t[i].add(mod);
		}
		for(int i = 0, type, l, r, x; i < q; i++){
			type = scanner.nextInt();
			if (type == 1){
				l = scanner.nextInt(); x = scanner.nextInt();
				s[a[l]].remove(l);
				if(l > 1) t[lca(a[l], a[l-1])].remove(l);
				if(l < m) t[lca(a[l], a[l+1])].remove(l+1);
				a[l] = x;
				s[a[l]].add(l);
				if(l > 1) t[lca(a[l], a[l-1])].add(l);
				if(l < m) t[lca(a[l], a[l+1])].add(l+1);
			} else { 
				l = scanner.nextInt(); r = scanner.nextInt(); x = scanner.nextInt();
				var d = s[x].ceiling(l);
				var e = t[x].ceiling(l+1);
				if (d!=null && d <= r){
					out.println(d+" "+d);
				} else if(e!=null && e <= r && e>1){
					out.println((e-1)+" "+e);
				} else{
					out.println("-1 -1");
				}
			}
		}
	}

	void dfs(int v, int pr) {
		up[0][v] = pr;
		for(int i = 1; i < 19; i++){
			up[i][v] = up[i-1][up[i-1][v]];
		}
		d[v] = d[pr] + 1;
		for(var to : g[v]) {
			if(to == pr) continue;
			dfs(to, v);
		}
	}
	
	int lca(int u, int v) {
		if(d[u] < d[v]) {
			int temp = u;
			u = v;
			v = temp;
		}
		for(int i = 18; i >= 0; i--){
			if(d[u] - (1<<i) >= d[v]) {
				u = up[i][u];
			}
		}
		if(u == v) return u;
		for(int i = 18; i >= 0; i--){
			if(up[i][u] != up[i][v]) {
				u = up[i][u];
				v = up[i][v];
			}
		}
		return up[0][v];
	}

	List<Integer>[] g;
	boolean[] was;

	void init(int n) {
		g = new ArrayList[n+1];
		was = new boolean[n+1];
		for (int i = 0; i<=n; i++) {
			g[i] = new ArrayList<>();
		}
	}

	void run() throws IOException {
		int t = 1;
		while (t-->0) {
			solve();
		}
		out.flush();
	}
}

class FastScanner {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	
	int nextInt() throws IOException {
		int x = 0, sign = 1;
		char c = (char) br.read();
		
		while ((c < '0' || c > '9') && c != '-') {
			c = (char) br.read();
		}
		if (c == '-') {
			sign = -1;
			c = (char) br.read();
		}
		while (c >= '0' && c <= '9') {
			x = (x << 3) + (x << 1) + (c & 15);
			c = (char) br.read();
		}
		return sign * x;
	}
	
	long nextLong() throws IOException {
		long x = 0, sign = 1;
		char c = (char) br.read();
		
		while ((c < '0' || c > '9') && c != '-') {
			c = (char) br.read();
		}
		if (c == '-') {
			sign = -1;
			c = (char) br.read();
		}
		while (c >= '0' && c <= '9') {
			x = (x << 3) + (x << 1) + (c & 15);
			c = (char) br.read();
		}
		return sign * x;
	}
	
	String next() throws IOException {
		StringBuilder sb = new StringBuilder();
		char c = (char) br.read();
		while (c == ' ' || c == '\n' || c == '\r') {
			c = (char) br.read();
		}
		while (c != ' ' && c != '\n' && c != '\r') {
			sb.append(c);
			c = (char) br.read();
		}
		return sb.toString();
	}
	
	int[] readArray(int n) throws IOException {
		int[] a = new int[n];
		for (int i = 0; i < n; i++) a[i] = nextInt();
		return a;
	}
	
	long[] readLong(int n) throws IOException {
		long[] a = new long[n];
		for (int i = 0; i < n; i++) a[i] = nextInt();
		return a;
	}
}

Compilation message

Note: treearray.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
# Verdict Execution time Memory Grader output
1 Correct 159 ms 12044 KB n=5
2 Correct 164 ms 11916 KB n=100
3 Correct 165 ms 11660 KB n=100
4 Correct 158 ms 11960 KB n=100
5 Correct 171 ms 11904 KB n=100
6 Correct 118 ms 10364 KB n=100
7 Correct 131 ms 10576 KB n=100
8 Correct 61 ms 8428 KB n=100
9 Correct 66 ms 8324 KB n=100
10 Correct 132 ms 10852 KB n=100
11 Correct 144 ms 10848 KB n=100
12 Correct 184 ms 12032 KB n=100
13 Correct 171 ms 12016 KB n=100
14 Correct 158 ms 11996 KB n=100
15 Correct 159 ms 11988 KB n=100
16 Correct 158 ms 12000 KB n=100
17 Correct 120 ms 10432 KB n=100
18 Correct 136 ms 10464 KB n=100
19 Correct 173 ms 12152 KB n=100
20 Correct 157 ms 11736 KB n=100
21 Correct 117 ms 10404 KB n=100
22 Correct 167 ms 11996 KB n=100
23 Correct 62 ms 8300 KB n=100
24 Correct 62 ms 8624 KB n=100
25 Correct 130 ms 10368 KB n=100
26 Correct 152 ms 11944 KB n=12
27 Correct 111 ms 10560 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 159 ms 12044 KB n=5
2 Correct 164 ms 11916 KB n=100
3 Correct 165 ms 11660 KB n=100
4 Correct 158 ms 11960 KB n=100
5 Correct 171 ms 11904 KB n=100
6 Correct 118 ms 10364 KB n=100
7 Correct 131 ms 10576 KB n=100
8 Correct 61 ms 8428 KB n=100
9 Correct 66 ms 8324 KB n=100
10 Correct 132 ms 10852 KB n=100
11 Correct 144 ms 10848 KB n=100
12 Correct 184 ms 12032 KB n=100
13 Correct 171 ms 12016 KB n=100
14 Correct 158 ms 11996 KB n=100
15 Correct 159 ms 11988 KB n=100
16 Correct 158 ms 12000 KB n=100
17 Correct 120 ms 10432 KB n=100
18 Correct 136 ms 10464 KB n=100
19 Correct 173 ms 12152 KB n=100
20 Correct 157 ms 11736 KB n=100
21 Correct 117 ms 10404 KB n=100
22 Correct 167 ms 11996 KB n=100
23 Correct 62 ms 8300 KB n=100
24 Correct 62 ms 8624 KB n=100
25 Correct 130 ms 10368 KB n=100
26 Correct 152 ms 11944 KB n=12
27 Correct 111 ms 10560 KB n=100
28 Correct 234 ms 13192 KB n=500
29 Correct 147 ms 11504 KB n=500
30 Correct 220 ms 13276 KB n=500
31 Correct 203 ms 13048 KB n=500
32 Correct 217 ms 13184 KB n=500
33 Correct 205 ms 13060 KB n=500
34 Correct 231 ms 13152 KB n=500
35 Correct 187 ms 12880 KB n=500
36 Correct 166 ms 12760 KB n=500
37 Correct 164 ms 12584 KB n=500
38 Correct 170 ms 12752 KB n=500
39 Correct 110 ms 11180 KB n=500
40 Correct 110 ms 11344 KB n=500
41 Correct 119 ms 11264 KB n=500
42 Correct 190 ms 12784 KB n=500
43 Correct 202 ms 12852 KB n=500
44 Correct 196 ms 12756 KB n=500
45 Correct 227 ms 13312 KB n=500
46 Correct 152 ms 12064 KB n=500
47 Correct 198 ms 13248 KB n=500
48 Correct 231 ms 13072 KB n=500
49 Correct 198 ms 13316 KB n=500
50 Correct 186 ms 12824 KB n=500
51 Correct 199 ms 12924 KB n=500
52 Correct 147 ms 11156 KB n=500
53 Correct 226 ms 13064 KB n=500
54 Correct 172 ms 12748 KB n=500
55 Correct 163 ms 12032 KB n=278
56 Correct 89 ms 9460 KB n=500
57 Correct 95 ms 10196 KB n=500
58 Correct 161 ms 12480 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 159 ms 12044 KB n=5
2 Correct 164 ms 11916 KB n=100
3 Correct 165 ms 11660 KB n=100
4 Correct 158 ms 11960 KB n=100
5 Correct 171 ms 11904 KB n=100
6 Correct 118 ms 10364 KB n=100
7 Correct 131 ms 10576 KB n=100
8 Correct 61 ms 8428 KB n=100
9 Correct 66 ms 8324 KB n=100
10 Correct 132 ms 10852 KB n=100
11 Correct 144 ms 10848 KB n=100
12 Correct 184 ms 12032 KB n=100
13 Correct 171 ms 12016 KB n=100
14 Correct 158 ms 11996 KB n=100
15 Correct 159 ms 11988 KB n=100
16 Correct 158 ms 12000 KB n=100
17 Correct 120 ms 10432 KB n=100
18 Correct 136 ms 10464 KB n=100
19 Correct 173 ms 12152 KB n=100
20 Correct 157 ms 11736 KB n=100
21 Correct 117 ms 10404 KB n=100
22 Correct 167 ms 11996 KB n=100
23 Correct 62 ms 8300 KB n=100
24 Correct 62 ms 8624 KB n=100
25 Correct 130 ms 10368 KB n=100
26 Correct 152 ms 11944 KB n=12
27 Correct 111 ms 10560 KB n=100
28 Correct 234 ms 13192 KB n=500
29 Correct 147 ms 11504 KB n=500
30 Correct 220 ms 13276 KB n=500
31 Correct 203 ms 13048 KB n=500
32 Correct 217 ms 13184 KB n=500
33 Correct 205 ms 13060 KB n=500
34 Correct 231 ms 13152 KB n=500
35 Correct 187 ms 12880 KB n=500
36 Correct 166 ms 12760 KB n=500
37 Correct 164 ms 12584 KB n=500
38 Correct 170 ms 12752 KB n=500
39 Correct 110 ms 11180 KB n=500
40 Correct 110 ms 11344 KB n=500
41 Correct 119 ms 11264 KB n=500
42 Correct 190 ms 12784 KB n=500
43 Correct 202 ms 12852 KB n=500
44 Correct 196 ms 12756 KB n=500
45 Correct 227 ms 13312 KB n=500
46 Correct 152 ms 12064 KB n=500
47 Correct 198 ms 13248 KB n=500
48 Correct 231 ms 13072 KB n=500
49 Correct 198 ms 13316 KB n=500
50 Correct 186 ms 12824 KB n=500
51 Correct 199 ms 12924 KB n=500
52 Correct 147 ms 11156 KB n=500
53 Correct 226 ms 13064 KB n=500
54 Correct 172 ms 12748 KB n=500
55 Correct 163 ms 12032 KB n=278
56 Correct 89 ms 9460 KB n=500
57 Correct 95 ms 10196 KB n=500
58 Correct 161 ms 12480 KB n=500
59 Correct 345 ms 16044 KB n=2000
60 Correct 357 ms 16300 KB n=2000
61 Correct 320 ms 16104 KB n=2000
62 Correct 315 ms 16064 KB n=2000
63 Correct 334 ms 16172 KB n=2000
64 Correct 311 ms 16088 KB n=2000
65 Correct 307 ms 15900 KB n=2000
66 Correct 307 ms 16344 KB n=2000
67 Correct 355 ms 15836 KB n=2000
68 Correct 312 ms 15956 KB n=2000
69 Correct 317 ms 16076 KB n=2000
70 Correct 307 ms 15888 KB n=2000
71 Correct 311 ms 15908 KB n=2000
72 Correct 208 ms 12840 KB n=2000
73 Correct 198 ms 12952 KB n=2000
74 Correct 349 ms 15920 KB n=1844
75 Correct 193 ms 12904 KB n=2000
76 Correct 321 ms 15972 KB n=2000
77 Correct 314 ms 15864 KB n=2000
78 Correct 306 ms 15988 KB n=2000
79 Correct 367 ms 15940 KB n=2000
80 Correct 312 ms 16064 KB n=2000
81 Correct 316 ms 16052 KB n=2000
82 Correct 328 ms 15900 KB n=2000
83 Correct 307 ms 15944 KB n=2000
84 Correct 293 ms 16000 KB n=2000
85 Correct 304 ms 16024 KB n=2000
86 Correct 318 ms 16084 KB n=2000
87 Correct 334 ms 15980 KB n=2000
88 Correct 220 ms 13536 KB n=2000
89 Correct 224 ms 14180 KB n=2000
90 Correct 301 ms 16176 KB n=2000
91 Correct 273 ms 15956 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 159 ms 12044 KB n=5
2 Correct 164 ms 11916 KB n=100
3 Correct 165 ms 11660 KB n=100
4 Correct 158 ms 11960 KB n=100
5 Correct 171 ms 11904 KB n=100
6 Correct 118 ms 10364 KB n=100
7 Correct 131 ms 10576 KB n=100
8 Correct 61 ms 8428 KB n=100
9 Correct 66 ms 8324 KB n=100
10 Correct 132 ms 10852 KB n=100
11 Correct 144 ms 10848 KB n=100
12 Correct 184 ms 12032 KB n=100
13 Correct 171 ms 12016 KB n=100
14 Correct 158 ms 11996 KB n=100
15 Correct 159 ms 11988 KB n=100
16 Correct 158 ms 12000 KB n=100
17 Correct 120 ms 10432 KB n=100
18 Correct 136 ms 10464 KB n=100
19 Correct 173 ms 12152 KB n=100
20 Correct 157 ms 11736 KB n=100
21 Correct 117 ms 10404 KB n=100
22 Correct 167 ms 11996 KB n=100
23 Correct 62 ms 8300 KB n=100
24 Correct 62 ms 8624 KB n=100
25 Correct 130 ms 10368 KB n=100
26 Correct 152 ms 11944 KB n=12
27 Correct 111 ms 10560 KB n=100
28 Correct 234 ms 13192 KB n=500
29 Correct 147 ms 11504 KB n=500
30 Correct 220 ms 13276 KB n=500
31 Correct 203 ms 13048 KB n=500
32 Correct 217 ms 13184 KB n=500
33 Correct 205 ms 13060 KB n=500
34 Correct 231 ms 13152 KB n=500
35 Correct 187 ms 12880 KB n=500
36 Correct 166 ms 12760 KB n=500
37 Correct 164 ms 12584 KB n=500
38 Correct 170 ms 12752 KB n=500
39 Correct 110 ms 11180 KB n=500
40 Correct 110 ms 11344 KB n=500
41 Correct 119 ms 11264 KB n=500
42 Correct 190 ms 12784 KB n=500
43 Correct 202 ms 12852 KB n=500
44 Correct 196 ms 12756 KB n=500
45 Correct 227 ms 13312 KB n=500
46 Correct 152 ms 12064 KB n=500
47 Correct 198 ms 13248 KB n=500
48 Correct 231 ms 13072 KB n=500
49 Correct 198 ms 13316 KB n=500
50 Correct 186 ms 12824 KB n=500
51 Correct 199 ms 12924 KB n=500
52 Correct 147 ms 11156 KB n=500
53 Correct 226 ms 13064 KB n=500
54 Correct 172 ms 12748 KB n=500
55 Correct 163 ms 12032 KB n=278
56 Correct 89 ms 9460 KB n=500
57 Correct 95 ms 10196 KB n=500
58 Correct 161 ms 12480 KB n=500
59 Correct 345 ms 16044 KB n=2000
60 Correct 357 ms 16300 KB n=2000
61 Correct 320 ms 16104 KB n=2000
62 Correct 315 ms 16064 KB n=2000
63 Correct 334 ms 16172 KB n=2000
64 Correct 311 ms 16088 KB n=2000
65 Correct 307 ms 15900 KB n=2000
66 Correct 307 ms 16344 KB n=2000
67 Correct 355 ms 15836 KB n=2000
68 Correct 312 ms 15956 KB n=2000
69 Correct 317 ms 16076 KB n=2000
70 Correct 307 ms 15888 KB n=2000
71 Correct 311 ms 15908 KB n=2000
72 Correct 208 ms 12840 KB n=2000
73 Correct 198 ms 12952 KB n=2000
74 Correct 349 ms 15920 KB n=1844
75 Correct 193 ms 12904 KB n=2000
76 Correct 321 ms 15972 KB n=2000
77 Correct 314 ms 15864 KB n=2000
78 Correct 306 ms 15988 KB n=2000
79 Correct 367 ms 15940 KB n=2000
80 Correct 312 ms 16064 KB n=2000
81 Correct 316 ms 16052 KB n=2000
82 Correct 328 ms 15900 KB n=2000
83 Correct 307 ms 15944 KB n=2000
84 Correct 293 ms 16000 KB n=2000
85 Correct 304 ms 16024 KB n=2000
86 Correct 318 ms 16084 KB n=2000
87 Correct 334 ms 15980 KB n=2000
88 Correct 220 ms 13536 KB n=2000
89 Correct 224 ms 14180 KB n=2000
90 Correct 301 ms 16176 KB n=2000
91 Correct 273 ms 15956 KB n=2000
92 Correct 2250 ms 184076 KB n=200000
93 Correct 2606 ms 188028 KB n=200000
94 Correct 2650 ms 188328 KB n=200000
95 Correct 2096 ms 177640 KB n=200000
96 Correct 2076 ms 180356 KB n=200000
97 Correct 2589 ms 187564 KB n=200000
98 Correct 1911 ms 175752 KB n=200000
99 Correct 2257 ms 181124 KB n=200000
100 Correct 1933 ms 175772 KB n=200000
101 Correct 2787 ms 190164 KB n=200000
102 Correct 1404 ms 174404 KB n=200000
103 Correct 1436 ms 174292 KB n=200000
104 Correct 1433 ms 175496 KB n=200000
105 Correct 1407 ms 173788 KB n=200000
106 Correct 1398 ms 173800 KB n=200000
107 Correct 1458 ms 174452 KB n=200000
108 Correct 2032 ms 181624 KB n=200000
109 Correct 2007 ms 181568 KB n=200000
110 Correct 2096 ms 179020 KB n=200000
111 Correct 1899 ms 173160 KB n=200000
112 Correct 2632 ms 190948 KB n=200000
113 Correct 2470 ms 183828 KB n=200000
114 Correct 1932 ms 177244 KB n=200000
115 Correct 2615 ms 183748 KB n=200000
116 Correct 1966 ms 174204 KB n=200000
117 Correct 2696 ms 191020 KB n=200000
118 Correct 2627 ms 185300 KB n=200000
119 Correct 1993 ms 175072 KB n=200000
120 Runtime error 1107 ms 262144 KB Execution killed with signal 9
121 Halted 0 ms 0 KB -