Submission #83213

# Submission time Handle Problem Language Result Execution time Memory
83213 2018-11-06T03:52:55 Z Good Birthday gift (IZhO18_treearray) C++11
100 / 100
2098 ms 96444 KB
/*
ID: blackha5
TASK: test
LANG: C++
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define ff first
#define ss second
#define Maxn 200003
#define ll long long
#define pb push_back
#define Inf 1000000009
#define ppb() pop_back()
#define pii pair <int , int>
#define mid(x, y) (x + y) / 2
#define all(x) x.begin(),x.end()
#define llInf 1000000000000000009
#define tr(i, c) for(__typeof(c).begin() i = (c).begin() ; i != (c).end() ; i++)
using namespace std;
using namespace __gnu_pbds;
typedef tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> order;

int n, m, q;
int A[Maxn];
int lvl[Maxn];
int mat[Maxn][21];

vector <int> adj[Maxn];
set <int> s[Maxn], t[Maxn];

void dfs (int nd, int p) {
	mat[nd][0] = p;
	lvl[nd] = lvl[p] + 1;
	
	for (auto i: adj[nd])
		if (i != p)
			dfs (i, nd);
}

void build () {
	for (int i = 1; i <= 19; i++)
		for (int j = 1; j <= n; j++)
			mat[j][i] = mat[mat[j][i - 1]][i - 1];
}

int lca (int x, int y) {
	if (lvl[x] < lvl[y])
		swap (x, y);
	
	for (int i = 19; i >= 0; i--)
		if (lvl[mat[x][i]] >= lvl[y])
			x = mat[x][i];
	
	if (x == y)
		return x;
	
	for (int i = 19; i >= 0; i--) 
		if (mat[x][i] != mat[y][i])
			x = mat[x][i], y = mat[y][i];
	
	return mat[x][0];					
}

int main () {
	//freopen ("file.in", "r", stdin);
	//freopen ("file.out", "w", stdout);
	
 	//srand ((unsigned) time ( NULL ));
	//int randomNumber = rand() % 10 + 1;

	scanf ("%d%d%d", &n, &m, &q);
	
	for (int i = 1; i < n; i++) {
		int u, v;
		
		scanf ("%d%d", &u, &v);
		adj[u].pb (v);
		adj[v].pb (u);
		s[i].insert (Inf);
		t[i].insert (Inf);
	}
	s[n].insert (Inf);
	t[n].insert (Inf);

	dfs (1, 0);
	build ();
	
	for (int i = 1; i <= m; i++) {
		scanf ("%d", A + i);
		t[A[i]].insert (i);
	}	
	
	for (int i = 1; i < m; i++)
		s[lca (A[i], A[i + 1])].insert (i);	
	
	while (q--) {
		int type, l, v;
		scanf ("%d%d", &type, &l);
		
		if (type == 2) {
			int r;
			scanf ("%d%d", &r, &v);
			int x = *s[v].lower_bound (l);
			int y = *t[v].lower_bound (l);
			
			if (y <= r) {
				assert (lca (A[y], A[y]) == v);
				printf ("%d %d\n", y, y);
			}
			else if (x <= r - 1) {
				assert (lca (A[x], A[x + 1]) == v);
				printf ("%d %d\n", x, x + 1);
			}	
			else
				puts ("-1 -1");		
		}	
		
		else {
			scanf ("%d", &v);
			t[A[l]].erase (l);
			t[v].insert (l);
			
			if (l > 1) {
				int x = lca (A[l - 1], A[l]);
				s[x].erase (l - 1);
				x = lca (A[l - 1], v);
				s[x].insert(l - 1);		
			}
			if (l < m) {
				int x = lca (A[l], A[l + 1]);
				s[x].erase (l);
				x = lca (v, A[l + 1]);
				s[x].insert (l);
			}
			A[l] = v;
		}
	}		
	return 0;
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf ("%d%d%d", &n, &m, &q);
  ~~~~~~^~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:78:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf ("%d%d", &u, &v);
   ~~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:91:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf ("%d", A + i);
   ~~~~~~^~~~~~~~~~~~~
treearray.cpp:100:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf ("%d%d", &type, &l);
   ~~~~~~^~~~~~~~~~~~~~~~~~~
treearray.cpp:104:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf ("%d%d", &r, &v);
    ~~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:121:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf ("%d", &v);
    ~~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 26 ms 23800 KB n=5
2 Correct 23 ms 23924 KB n=100
3 Correct 23 ms 24000 KB n=100
4 Correct 24 ms 24000 KB n=100
5 Correct 24 ms 24000 KB n=100
6 Correct 23 ms 24000 KB n=100
7 Correct 26 ms 24000 KB n=100
8 Correct 24 ms 24096 KB n=100
9 Correct 24 ms 24096 KB n=100
10 Correct 24 ms 24096 KB n=100
11 Correct 23 ms 24096 KB n=100
12 Correct 24 ms 24172 KB n=100
13 Correct 24 ms 24172 KB n=100
14 Correct 26 ms 24172 KB n=100
15 Correct 28 ms 24172 KB n=100
16 Correct 25 ms 24172 KB n=100
17 Correct 23 ms 24172 KB n=100
18 Correct 24 ms 24172 KB n=100
19 Correct 23 ms 24172 KB n=100
20 Correct 23 ms 24172 KB n=100
21 Correct 25 ms 24172 KB n=100
22 Correct 24 ms 24172 KB n=100
23 Correct 24 ms 24172 KB n=100
24 Correct 23 ms 24172 KB n=100
25 Correct 23 ms 24172 KB n=100
26 Correct 27 ms 24172 KB n=12
27 Correct 23 ms 24172 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 26 ms 23800 KB n=5
2 Correct 23 ms 23924 KB n=100
3 Correct 23 ms 24000 KB n=100
4 Correct 24 ms 24000 KB n=100
5 Correct 24 ms 24000 KB n=100
6 Correct 23 ms 24000 KB n=100
7 Correct 26 ms 24000 KB n=100
8 Correct 24 ms 24096 KB n=100
9 Correct 24 ms 24096 KB n=100
10 Correct 24 ms 24096 KB n=100
11 Correct 23 ms 24096 KB n=100
12 Correct 24 ms 24172 KB n=100
13 Correct 24 ms 24172 KB n=100
14 Correct 26 ms 24172 KB n=100
15 Correct 28 ms 24172 KB n=100
16 Correct 25 ms 24172 KB n=100
17 Correct 23 ms 24172 KB n=100
18 Correct 24 ms 24172 KB n=100
19 Correct 23 ms 24172 KB n=100
20 Correct 23 ms 24172 KB n=100
21 Correct 25 ms 24172 KB n=100
22 Correct 24 ms 24172 KB n=100
23 Correct 24 ms 24172 KB n=100
24 Correct 23 ms 24172 KB n=100
25 Correct 23 ms 24172 KB n=100
26 Correct 27 ms 24172 KB n=12
27 Correct 23 ms 24172 KB n=100
28 Correct 26 ms 24172 KB n=500
29 Correct 27 ms 24184 KB n=500
30 Correct 26 ms 24212 KB n=500
31 Correct 24 ms 24212 KB n=500
32 Correct 26 ms 24228 KB n=500
33 Correct 25 ms 24248 KB n=500
34 Correct 25 ms 24248 KB n=500
35 Correct 25 ms 24248 KB n=500
36 Correct 24 ms 24248 KB n=500
37 Correct 30 ms 24300 KB n=500
38 Correct 31 ms 24300 KB n=500
39 Correct 26 ms 24300 KB n=500
40 Correct 26 ms 24300 KB n=500
41 Correct 24 ms 24300 KB n=500
42 Correct 26 ms 24300 KB n=500
43 Correct 26 ms 24300 KB n=500
44 Correct 32 ms 24300 KB n=500
45 Correct 25 ms 24300 KB n=500
46 Correct 25 ms 24300 KB n=500
47 Correct 26 ms 24300 KB n=500
48 Correct 25 ms 24300 KB n=500
49 Correct 25 ms 24300 KB n=500
50 Correct 26 ms 24300 KB n=500
51 Correct 24 ms 24300 KB n=500
52 Correct 25 ms 24300 KB n=500
53 Correct 25 ms 24300 KB n=500
54 Correct 51 ms 24300 KB n=500
55 Correct 30 ms 24300 KB n=278
56 Correct 25 ms 24300 KB n=500
57 Correct 31 ms 24300 KB n=500
58 Correct 24 ms 24300 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 26 ms 23800 KB n=5
2 Correct 23 ms 23924 KB n=100
3 Correct 23 ms 24000 KB n=100
4 Correct 24 ms 24000 KB n=100
5 Correct 24 ms 24000 KB n=100
6 Correct 23 ms 24000 KB n=100
7 Correct 26 ms 24000 KB n=100
8 Correct 24 ms 24096 KB n=100
9 Correct 24 ms 24096 KB n=100
10 Correct 24 ms 24096 KB n=100
11 Correct 23 ms 24096 KB n=100
12 Correct 24 ms 24172 KB n=100
13 Correct 24 ms 24172 KB n=100
14 Correct 26 ms 24172 KB n=100
15 Correct 28 ms 24172 KB n=100
16 Correct 25 ms 24172 KB n=100
17 Correct 23 ms 24172 KB n=100
18 Correct 24 ms 24172 KB n=100
19 Correct 23 ms 24172 KB n=100
20 Correct 23 ms 24172 KB n=100
21 Correct 25 ms 24172 KB n=100
22 Correct 24 ms 24172 KB n=100
23 Correct 24 ms 24172 KB n=100
24 Correct 23 ms 24172 KB n=100
25 Correct 23 ms 24172 KB n=100
26 Correct 27 ms 24172 KB n=12
27 Correct 23 ms 24172 KB n=100
28 Correct 26 ms 24172 KB n=500
29 Correct 27 ms 24184 KB n=500
30 Correct 26 ms 24212 KB n=500
31 Correct 24 ms 24212 KB n=500
32 Correct 26 ms 24228 KB n=500
33 Correct 25 ms 24248 KB n=500
34 Correct 25 ms 24248 KB n=500
35 Correct 25 ms 24248 KB n=500
36 Correct 24 ms 24248 KB n=500
37 Correct 30 ms 24300 KB n=500
38 Correct 31 ms 24300 KB n=500
39 Correct 26 ms 24300 KB n=500
40 Correct 26 ms 24300 KB n=500
41 Correct 24 ms 24300 KB n=500
42 Correct 26 ms 24300 KB n=500
43 Correct 26 ms 24300 KB n=500
44 Correct 32 ms 24300 KB n=500
45 Correct 25 ms 24300 KB n=500
46 Correct 25 ms 24300 KB n=500
47 Correct 26 ms 24300 KB n=500
48 Correct 25 ms 24300 KB n=500
49 Correct 25 ms 24300 KB n=500
50 Correct 26 ms 24300 KB n=500
51 Correct 24 ms 24300 KB n=500
52 Correct 25 ms 24300 KB n=500
53 Correct 25 ms 24300 KB n=500
54 Correct 51 ms 24300 KB n=500
55 Correct 30 ms 24300 KB n=278
56 Correct 25 ms 24300 KB n=500
57 Correct 31 ms 24300 KB n=500
58 Correct 24 ms 24300 KB n=500
59 Correct 33 ms 24812 KB n=2000
60 Correct 29 ms 24812 KB n=2000
61 Correct 29 ms 24812 KB n=2000
62 Correct 30 ms 24812 KB n=2000
63 Correct 29 ms 24812 KB n=2000
64 Correct 31 ms 24812 KB n=2000
65 Correct 29 ms 24812 KB n=2000
66 Correct 29 ms 24812 KB n=2000
67 Correct 29 ms 24812 KB n=2000
68 Correct 29 ms 24820 KB n=2000
69 Correct 33 ms 24820 KB n=2000
70 Correct 27 ms 24820 KB n=2000
71 Correct 32 ms 24876 KB n=2000
72 Correct 27 ms 24876 KB n=2000
73 Correct 27 ms 24876 KB n=2000
74 Correct 38 ms 24876 KB n=1844
75 Correct 27 ms 24876 KB n=2000
76 Correct 28 ms 24876 KB n=2000
77 Correct 32 ms 24876 KB n=2000
78 Correct 35 ms 24876 KB n=2000
79 Correct 37 ms 24876 KB n=2000
80 Correct 28 ms 24876 KB n=2000
81 Correct 29 ms 24876 KB n=2000
82 Correct 29 ms 24876 KB n=2000
83 Correct 28 ms 24876 KB n=2000
84 Correct 30 ms 24876 KB n=2000
85 Correct 30 ms 24876 KB n=2000
86 Correct 29 ms 24876 KB n=2000
87 Correct 30 ms 24876 KB n=2000
88 Correct 34 ms 24880 KB n=2000
89 Correct 31 ms 24880 KB n=2000
90 Correct 29 ms 24880 KB n=2000
91 Correct 28 ms 24880 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 26 ms 23800 KB n=5
2 Correct 23 ms 23924 KB n=100
3 Correct 23 ms 24000 KB n=100
4 Correct 24 ms 24000 KB n=100
5 Correct 24 ms 24000 KB n=100
6 Correct 23 ms 24000 KB n=100
7 Correct 26 ms 24000 KB n=100
8 Correct 24 ms 24096 KB n=100
9 Correct 24 ms 24096 KB n=100
10 Correct 24 ms 24096 KB n=100
11 Correct 23 ms 24096 KB n=100
12 Correct 24 ms 24172 KB n=100
13 Correct 24 ms 24172 KB n=100
14 Correct 26 ms 24172 KB n=100
15 Correct 28 ms 24172 KB n=100
16 Correct 25 ms 24172 KB n=100
17 Correct 23 ms 24172 KB n=100
18 Correct 24 ms 24172 KB n=100
19 Correct 23 ms 24172 KB n=100
20 Correct 23 ms 24172 KB n=100
21 Correct 25 ms 24172 KB n=100
22 Correct 24 ms 24172 KB n=100
23 Correct 24 ms 24172 KB n=100
24 Correct 23 ms 24172 KB n=100
25 Correct 23 ms 24172 KB n=100
26 Correct 27 ms 24172 KB n=12
27 Correct 23 ms 24172 KB n=100
28 Correct 26 ms 24172 KB n=500
29 Correct 27 ms 24184 KB n=500
30 Correct 26 ms 24212 KB n=500
31 Correct 24 ms 24212 KB n=500
32 Correct 26 ms 24228 KB n=500
33 Correct 25 ms 24248 KB n=500
34 Correct 25 ms 24248 KB n=500
35 Correct 25 ms 24248 KB n=500
36 Correct 24 ms 24248 KB n=500
37 Correct 30 ms 24300 KB n=500
38 Correct 31 ms 24300 KB n=500
39 Correct 26 ms 24300 KB n=500
40 Correct 26 ms 24300 KB n=500
41 Correct 24 ms 24300 KB n=500
42 Correct 26 ms 24300 KB n=500
43 Correct 26 ms 24300 KB n=500
44 Correct 32 ms 24300 KB n=500
45 Correct 25 ms 24300 KB n=500
46 Correct 25 ms 24300 KB n=500
47 Correct 26 ms 24300 KB n=500
48 Correct 25 ms 24300 KB n=500
49 Correct 25 ms 24300 KB n=500
50 Correct 26 ms 24300 KB n=500
51 Correct 24 ms 24300 KB n=500
52 Correct 25 ms 24300 KB n=500
53 Correct 25 ms 24300 KB n=500
54 Correct 51 ms 24300 KB n=500
55 Correct 30 ms 24300 KB n=278
56 Correct 25 ms 24300 KB n=500
57 Correct 31 ms 24300 KB n=500
58 Correct 24 ms 24300 KB n=500
59 Correct 33 ms 24812 KB n=2000
60 Correct 29 ms 24812 KB n=2000
61 Correct 29 ms 24812 KB n=2000
62 Correct 30 ms 24812 KB n=2000
63 Correct 29 ms 24812 KB n=2000
64 Correct 31 ms 24812 KB n=2000
65 Correct 29 ms 24812 KB n=2000
66 Correct 29 ms 24812 KB n=2000
67 Correct 29 ms 24812 KB n=2000
68 Correct 29 ms 24820 KB n=2000
69 Correct 33 ms 24820 KB n=2000
70 Correct 27 ms 24820 KB n=2000
71 Correct 32 ms 24876 KB n=2000
72 Correct 27 ms 24876 KB n=2000
73 Correct 27 ms 24876 KB n=2000
74 Correct 38 ms 24876 KB n=1844
75 Correct 27 ms 24876 KB n=2000
76 Correct 28 ms 24876 KB n=2000
77 Correct 32 ms 24876 KB n=2000
78 Correct 35 ms 24876 KB n=2000
79 Correct 37 ms 24876 KB n=2000
80 Correct 28 ms 24876 KB n=2000
81 Correct 29 ms 24876 KB n=2000
82 Correct 29 ms 24876 KB n=2000
83 Correct 28 ms 24876 KB n=2000
84 Correct 30 ms 24876 KB n=2000
85 Correct 30 ms 24876 KB n=2000
86 Correct 29 ms 24876 KB n=2000
87 Correct 30 ms 24876 KB n=2000
88 Correct 34 ms 24880 KB n=2000
89 Correct 31 ms 24880 KB n=2000
90 Correct 29 ms 24880 KB n=2000
91 Correct 28 ms 24880 KB n=2000
92 Correct 1300 ms 87940 KB n=200000
93 Correct 1987 ms 91904 KB n=200000
94 Correct 1902 ms 95148 KB n=200000
95 Correct 1277 ms 95148 KB n=200000
96 Correct 1239 ms 95148 KB n=200000
97 Correct 2097 ms 95148 KB n=200000
98 Correct 1309 ms 95148 KB n=200000
99 Correct 1552 ms 95148 KB n=200000
100 Correct 1333 ms 95148 KB n=200000
101 Correct 1972 ms 96428 KB n=200000
102 Correct 796 ms 96428 KB n=200000
103 Correct 806 ms 96428 KB n=200000
104 Correct 790 ms 96428 KB n=200000
105 Correct 850 ms 96428 KB n=200000
106 Correct 819 ms 96428 KB n=200000
107 Correct 842 ms 96428 KB n=200000
108 Correct 1427 ms 96428 KB n=200000
109 Correct 1406 ms 96428 KB n=200000
110 Correct 1510 ms 96428 KB n=200000
111 Correct 1343 ms 96428 KB n=200000
112 Correct 2044 ms 96428 KB n=200000
113 Correct 2081 ms 96428 KB n=200000
114 Correct 1359 ms 96428 KB n=200000
115 Correct 2098 ms 96428 KB n=200000
116 Correct 1217 ms 96428 KB n=200000
117 Correct 1804 ms 96428 KB n=200000
118 Correct 1787 ms 96428 KB n=200000
119 Correct 1253 ms 96428 KB n=200000
120 Correct 1712 ms 96428 KB n=200000
121 Correct 1696 ms 96428 KB n=200000
122 Correct 1743 ms 96444 KB n=200000
123 Correct 783 ms 96444 KB n=200000
124 Correct 458 ms 96444 KB n=25264