답안 #495999

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
495999 2021-12-20T10:37:34 Z Ziel Birthday gift (IZhO18_treearray) C++17
100 / 100
1545 ms 91716 KB
/**
 * LES GREATEABLES BRO TEAM
**/

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
#define sz(x) (int)x.size()
const bool FLAG = false;
void setIO(const string &f = "");

string to_string(const string s) {
	return '"' + s + '"';
}
string to_string(const char c) {
 	return char(39) + string(1, c) + char(39);
}
string to_string(const char* s) {
 	return to_string(string(s));
}
string to_string(bool f) {
	return (f ? "true" : "false");
}
template<class A, class B>
string to_string(const pair<A, B> x) {
	return "(" + to_string(x.first) + ", " + to_string(x.second) + ")";
}
template<class A, class B, class C>
string to_string(const tuple<A, B, C> x) {
	return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ")";
}
template<class A, class B, class C, class D>
string to_string(const tuple<A, B, C, D> x) {
	return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ", " + to_string(get<3>(x)) + ")";
}
template<class T>
string to_string(const T v) {
	string res = "{"; bool f = true;
	for (auto x: v)
		res += (f ? to_string(x) : ", " + to_string(x)), f = false;
	return res + "}";
}
void debug_args() { cerr << "]\n"; }
template<class H, class... T>
void debug_args(H x, T... y) {
	cerr << to_string(x);
	if (sizeof... (y))
	cerr << ", ";
	debug_args(y...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: [", debug_args(__VA_ARGS__);
#else
#define debug(...) 47
#endif

const int N = 3e5 + 14;

int n, m, q, level[N], up[22][N], a[N];
vector<vector<int>> g(N);

void dfs(int v, int pr) {
	level[v] = level[pr] + 1;
	up[0][v] = pr;
	for (int i = 1; i <= 20; i++)
		up[i][v] = up[i - 1][up[i - 1][v]];
	for (int to: g[v]) {
		if (to == pr)
			continue;
		dfs(to, v);
	}
}
int lca(int x, int y) {
	if (level[x] > level[y])
		swap(x, y);
	for (int i = 20; i >= 0; i--) {
		if (level[up[i][y]] >= level[x])
			y = up[i][y];
	}
	if (x == y)
		return x;
   	for (int i = 20; i >= 0; i--) {
   		if (up[i][x] != up[i][y])
   			x = up[i][x], y = up[i][y];
   	}
   	return up[0][x];
}

void solve() {
    cin >> n >> m >> q;
    for (int i = 1, x, y; i < n; i++) {
    	cin >> x >> y;
    	g[x].push_back(y), g[y].push_back(x);
    }
    dfs(1, 0);
    for (int i = 1; i <= m; i++)
    	cin >> a[i];
    vector<int> b(m + 1);
    set<int> s1[n + n], s2[n + n];
    for (int i = 1; i < m; i++) {
    	b[i] = lca(a[i], a[i + 1]);
    	s1[b[i]].insert(i);
    	s2[a[i]].insert(i);
    }
    s2[a[m]].insert(m);

    while (q--) {
    	int type;
    	cin >> type;
    	if (type == 1) {
    		int pos, v;
    		cin >> pos >> v;
    		s2[a[pos]].erase(pos);
    		a[pos] = v;
    		s2[a[pos]].insert(pos);
    		if (pos + 1 <= m) {
    			s1[b[pos]].erase(pos);
	    		b[pos] = lca(a[pos], a[pos + 1]);
	    		s1[b[pos]].insert(pos);
	    	}
	    	if (pos - 1 >= 1) {
	    		s1[b[pos - 1]].erase(pos - 1);
	    		b[pos - 1] = lca(a[pos - 1], a[pos]);
	    		s1[b[pos - 1]].insert(pos - 1);
	    	}
    	} else {
    		int l, r, v;
    		cin >> l >> r >> v;
    		bool found = 0;
    		if (sz(s2[v])) {
    			auto it = s2[v].lower_bound(l);
    			if (it != s2[v].end() && *it <= r) {
    				cout << *it << ' ' << *it << '\n';
    				found = 1;
    			}
    		}
    		if (!found && sz(s1[v])) {
    			auto it = s1[v].lower_bound(l);
    			if (it != s1[v].end() && *it <= r - 1) {
    				cout << *it << ' ' << (*it) + 1 << '\n';
    				found = 1;
    			}
    		}
    		if (!found)
    			cout << "-1 -1\n";
    	}
    }
}

signed main() {
    setIO();
    
    int tt = 1;
    if (FLAG) {
    	cin >> tt;
    }
    while (tt--) {
    	solve();
    }
    
    return 0;
}

void setIO(const string &f) {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen((f + ".in").c_str(), "r")) {
        freopen((f + ".in").c_str(), "r", stdin);
        freopen((f + ".out").c_str(), "w", stdout);
    }
}

Compilation message

treearray.cpp: In function 'void setIO(const string&)':
treearray.cpp:171:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  171 |         freopen((f + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:172:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  172 |         freopen((f + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7424 KB n=5
2 Correct 4 ms 7500 KB n=100
3 Correct 4 ms 7500 KB n=100
4 Correct 5 ms 7500 KB n=100
5 Correct 4 ms 7500 KB n=100
6 Correct 5 ms 7500 KB n=100
7 Correct 4 ms 7500 KB n=100
8 Correct 4 ms 7500 KB n=100
9 Correct 5 ms 7500 KB n=100
10 Correct 4 ms 7500 KB n=100
11 Correct 4 ms 7500 KB n=100
12 Correct 4 ms 7500 KB n=100
13 Correct 4 ms 7500 KB n=100
14 Correct 4 ms 7500 KB n=100
15 Correct 5 ms 7500 KB n=100
16 Correct 5 ms 7500 KB n=100
17 Correct 4 ms 7500 KB n=100
18 Correct 6 ms 7500 KB n=100
19 Correct 5 ms 7500 KB n=100
20 Correct 4 ms 7500 KB n=100
21 Correct 4 ms 7500 KB n=100
22 Correct 4 ms 7500 KB n=100
23 Correct 4 ms 7500 KB n=100
24 Correct 6 ms 7500 KB n=100
25 Correct 5 ms 7500 KB n=100
26 Correct 4 ms 7500 KB n=12
27 Correct 4 ms 7500 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7424 KB n=5
2 Correct 4 ms 7500 KB n=100
3 Correct 4 ms 7500 KB n=100
4 Correct 5 ms 7500 KB n=100
5 Correct 4 ms 7500 KB n=100
6 Correct 5 ms 7500 KB n=100
7 Correct 4 ms 7500 KB n=100
8 Correct 4 ms 7500 KB n=100
9 Correct 5 ms 7500 KB n=100
10 Correct 4 ms 7500 KB n=100
11 Correct 4 ms 7500 KB n=100
12 Correct 4 ms 7500 KB n=100
13 Correct 4 ms 7500 KB n=100
14 Correct 4 ms 7500 KB n=100
15 Correct 5 ms 7500 KB n=100
16 Correct 5 ms 7500 KB n=100
17 Correct 4 ms 7500 KB n=100
18 Correct 6 ms 7500 KB n=100
19 Correct 5 ms 7500 KB n=100
20 Correct 4 ms 7500 KB n=100
21 Correct 4 ms 7500 KB n=100
22 Correct 4 ms 7500 KB n=100
23 Correct 4 ms 7500 KB n=100
24 Correct 6 ms 7500 KB n=100
25 Correct 5 ms 7500 KB n=100
26 Correct 4 ms 7500 KB n=12
27 Correct 4 ms 7500 KB n=100
28 Correct 7 ms 7628 KB n=500
29 Correct 5 ms 7628 KB n=500
30 Correct 5 ms 7628 KB n=500
31 Correct 5 ms 7628 KB n=500
32 Correct 6 ms 7664 KB n=500
33 Correct 5 ms 7628 KB n=500
34 Correct 5 ms 7628 KB n=500
35 Correct 4 ms 7628 KB n=500
36 Correct 4 ms 7628 KB n=500
37 Correct 5 ms 7628 KB n=500
38 Correct 4 ms 7628 KB n=500
39 Correct 4 ms 7628 KB n=500
40 Correct 5 ms 7628 KB n=500
41 Correct 5 ms 7628 KB n=500
42 Correct 5 ms 7628 KB n=500
43 Correct 5 ms 7628 KB n=500
44 Correct 5 ms 7628 KB n=500
45 Correct 6 ms 7628 KB n=500
46 Correct 5 ms 7628 KB n=500
47 Correct 5 ms 7628 KB n=500
48 Correct 6 ms 7628 KB n=500
49 Correct 6 ms 7600 KB n=500
50 Correct 5 ms 7628 KB n=500
51 Correct 5 ms 7668 KB n=500
52 Correct 6 ms 7616 KB n=500
53 Correct 5 ms 7628 KB n=500
54 Correct 5 ms 7576 KB n=500
55 Correct 6 ms 7500 KB n=278
56 Correct 5 ms 7628 KB n=500
57 Correct 5 ms 7628 KB n=500
58 Correct 5 ms 7644 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7424 KB n=5
2 Correct 4 ms 7500 KB n=100
3 Correct 4 ms 7500 KB n=100
4 Correct 5 ms 7500 KB n=100
5 Correct 4 ms 7500 KB n=100
6 Correct 5 ms 7500 KB n=100
7 Correct 4 ms 7500 KB n=100
8 Correct 4 ms 7500 KB n=100
9 Correct 5 ms 7500 KB n=100
10 Correct 4 ms 7500 KB n=100
11 Correct 4 ms 7500 KB n=100
12 Correct 4 ms 7500 KB n=100
13 Correct 4 ms 7500 KB n=100
14 Correct 4 ms 7500 KB n=100
15 Correct 5 ms 7500 KB n=100
16 Correct 5 ms 7500 KB n=100
17 Correct 4 ms 7500 KB n=100
18 Correct 6 ms 7500 KB n=100
19 Correct 5 ms 7500 KB n=100
20 Correct 4 ms 7500 KB n=100
21 Correct 4 ms 7500 KB n=100
22 Correct 4 ms 7500 KB n=100
23 Correct 4 ms 7500 KB n=100
24 Correct 6 ms 7500 KB n=100
25 Correct 5 ms 7500 KB n=100
26 Correct 4 ms 7500 KB n=12
27 Correct 4 ms 7500 KB n=100
28 Correct 7 ms 7628 KB n=500
29 Correct 5 ms 7628 KB n=500
30 Correct 5 ms 7628 KB n=500
31 Correct 5 ms 7628 KB n=500
32 Correct 6 ms 7664 KB n=500
33 Correct 5 ms 7628 KB n=500
34 Correct 5 ms 7628 KB n=500
35 Correct 4 ms 7628 KB n=500
36 Correct 4 ms 7628 KB n=500
37 Correct 5 ms 7628 KB n=500
38 Correct 4 ms 7628 KB n=500
39 Correct 4 ms 7628 KB n=500
40 Correct 5 ms 7628 KB n=500
41 Correct 5 ms 7628 KB n=500
42 Correct 5 ms 7628 KB n=500
43 Correct 5 ms 7628 KB n=500
44 Correct 5 ms 7628 KB n=500
45 Correct 6 ms 7628 KB n=500
46 Correct 5 ms 7628 KB n=500
47 Correct 5 ms 7628 KB n=500
48 Correct 6 ms 7628 KB n=500
49 Correct 6 ms 7600 KB n=500
50 Correct 5 ms 7628 KB n=500
51 Correct 5 ms 7668 KB n=500
52 Correct 6 ms 7616 KB n=500
53 Correct 5 ms 7628 KB n=500
54 Correct 5 ms 7576 KB n=500
55 Correct 6 ms 7500 KB n=278
56 Correct 5 ms 7628 KB n=500
57 Correct 5 ms 7628 KB n=500
58 Correct 5 ms 7644 KB n=500
59 Correct 7 ms 8268 KB n=2000
60 Correct 7 ms 8268 KB n=2000
61 Correct 8 ms 8268 KB n=2000
62 Correct 9 ms 8268 KB n=2000
63 Correct 8 ms 8268 KB n=2000
64 Correct 8 ms 8268 KB n=2000
65 Correct 7 ms 8268 KB n=2000
66 Correct 8 ms 8396 KB n=2000
67 Correct 8 ms 8268 KB n=2000
68 Correct 7 ms 8268 KB n=2000
69 Correct 7 ms 8268 KB n=2000
70 Correct 7 ms 8268 KB n=2000
71 Correct 6 ms 8268 KB n=2000
72 Correct 6 ms 8268 KB n=2000
73 Correct 7 ms 8220 KB n=2000
74 Correct 8 ms 8136 KB n=1844
75 Correct 7 ms 8268 KB n=2000
76 Correct 8 ms 8268 KB n=2000
77 Correct 8 ms 8268 KB n=2000
78 Correct 7 ms 8324 KB n=2000
79 Correct 7 ms 8268 KB n=2000
80 Correct 8 ms 8268 KB n=2000
81 Correct 8 ms 8236 KB n=2000
82 Correct 8 ms 8308 KB n=2000
83 Correct 7 ms 8268 KB n=2000
84 Correct 7 ms 8268 KB n=2000
85 Correct 8 ms 8268 KB n=2000
86 Correct 8 ms 8268 KB n=2000
87 Correct 8 ms 8268 KB n=2000
88 Correct 7 ms 8232 KB n=2000
89 Correct 7 ms 8268 KB n=2000
90 Correct 7 ms 8324 KB n=2000
91 Correct 7 ms 8268 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7424 KB n=5
2 Correct 4 ms 7500 KB n=100
3 Correct 4 ms 7500 KB n=100
4 Correct 5 ms 7500 KB n=100
5 Correct 4 ms 7500 KB n=100
6 Correct 5 ms 7500 KB n=100
7 Correct 4 ms 7500 KB n=100
8 Correct 4 ms 7500 KB n=100
9 Correct 5 ms 7500 KB n=100
10 Correct 4 ms 7500 KB n=100
11 Correct 4 ms 7500 KB n=100
12 Correct 4 ms 7500 KB n=100
13 Correct 4 ms 7500 KB n=100
14 Correct 4 ms 7500 KB n=100
15 Correct 5 ms 7500 KB n=100
16 Correct 5 ms 7500 KB n=100
17 Correct 4 ms 7500 KB n=100
18 Correct 6 ms 7500 KB n=100
19 Correct 5 ms 7500 KB n=100
20 Correct 4 ms 7500 KB n=100
21 Correct 4 ms 7500 KB n=100
22 Correct 4 ms 7500 KB n=100
23 Correct 4 ms 7500 KB n=100
24 Correct 6 ms 7500 KB n=100
25 Correct 5 ms 7500 KB n=100
26 Correct 4 ms 7500 KB n=12
27 Correct 4 ms 7500 KB n=100
28 Correct 7 ms 7628 KB n=500
29 Correct 5 ms 7628 KB n=500
30 Correct 5 ms 7628 KB n=500
31 Correct 5 ms 7628 KB n=500
32 Correct 6 ms 7664 KB n=500
33 Correct 5 ms 7628 KB n=500
34 Correct 5 ms 7628 KB n=500
35 Correct 4 ms 7628 KB n=500
36 Correct 4 ms 7628 KB n=500
37 Correct 5 ms 7628 KB n=500
38 Correct 4 ms 7628 KB n=500
39 Correct 4 ms 7628 KB n=500
40 Correct 5 ms 7628 KB n=500
41 Correct 5 ms 7628 KB n=500
42 Correct 5 ms 7628 KB n=500
43 Correct 5 ms 7628 KB n=500
44 Correct 5 ms 7628 KB n=500
45 Correct 6 ms 7628 KB n=500
46 Correct 5 ms 7628 KB n=500
47 Correct 5 ms 7628 KB n=500
48 Correct 6 ms 7628 KB n=500
49 Correct 6 ms 7600 KB n=500
50 Correct 5 ms 7628 KB n=500
51 Correct 5 ms 7668 KB n=500
52 Correct 6 ms 7616 KB n=500
53 Correct 5 ms 7628 KB n=500
54 Correct 5 ms 7576 KB n=500
55 Correct 6 ms 7500 KB n=278
56 Correct 5 ms 7628 KB n=500
57 Correct 5 ms 7628 KB n=500
58 Correct 5 ms 7644 KB n=500
59 Correct 7 ms 8268 KB n=2000
60 Correct 7 ms 8268 KB n=2000
61 Correct 8 ms 8268 KB n=2000
62 Correct 9 ms 8268 KB n=2000
63 Correct 8 ms 8268 KB n=2000
64 Correct 8 ms 8268 KB n=2000
65 Correct 7 ms 8268 KB n=2000
66 Correct 8 ms 8396 KB n=2000
67 Correct 8 ms 8268 KB n=2000
68 Correct 7 ms 8268 KB n=2000
69 Correct 7 ms 8268 KB n=2000
70 Correct 7 ms 8268 KB n=2000
71 Correct 6 ms 8268 KB n=2000
72 Correct 6 ms 8268 KB n=2000
73 Correct 7 ms 8220 KB n=2000
74 Correct 8 ms 8136 KB n=1844
75 Correct 7 ms 8268 KB n=2000
76 Correct 8 ms 8268 KB n=2000
77 Correct 8 ms 8268 KB n=2000
78 Correct 7 ms 8324 KB n=2000
79 Correct 7 ms 8268 KB n=2000
80 Correct 8 ms 8268 KB n=2000
81 Correct 8 ms 8236 KB n=2000
82 Correct 8 ms 8308 KB n=2000
83 Correct 7 ms 8268 KB n=2000
84 Correct 7 ms 8268 KB n=2000
85 Correct 8 ms 8268 KB n=2000
86 Correct 8 ms 8268 KB n=2000
87 Correct 8 ms 8268 KB n=2000
88 Correct 7 ms 8232 KB n=2000
89 Correct 7 ms 8268 KB n=2000
90 Correct 7 ms 8324 KB n=2000
91 Correct 7 ms 8268 KB n=2000
92 Correct 1341 ms 90840 KB n=200000
93 Correct 1419 ms 89940 KB n=200000
94 Correct 1179 ms 89800 KB n=200000
95 Correct 1228 ms 90724 KB n=200000
96 Correct 1202 ms 90712 KB n=200000
97 Correct 1423 ms 90028 KB n=200000
98 Correct 1321 ms 90752 KB n=200000
99 Correct 1359 ms 90184 KB n=200000
100 Correct 1215 ms 90748 KB n=200000
101 Correct 1394 ms 89936 KB n=200000
102 Correct 723 ms 91716 KB n=200000
103 Correct 701 ms 91612 KB n=200000
104 Correct 735 ms 91612 KB n=200000
105 Correct 702 ms 91332 KB n=200000
106 Correct 690 ms 91380 KB n=200000
107 Correct 685 ms 91464 KB n=200000
108 Correct 1367 ms 90380 KB n=200000
109 Correct 1405 ms 90372 KB n=200000
110 Correct 1380 ms 90416 KB n=200000
111 Correct 1187 ms 90556 KB n=200000
112 Correct 1172 ms 89920 KB n=200000
113 Correct 1538 ms 90060 KB n=200000
114 Correct 1346 ms 90644 KB n=200000
115 Correct 1545 ms 89952 KB n=200000
116 Correct 1243 ms 90404 KB n=200000
117 Correct 1302 ms 89680 KB n=200000
118 Correct 1314 ms 89628 KB n=200000
119 Correct 1225 ms 90508 KB n=200000
120 Correct 1171 ms 89612 KB n=200000
121 Correct 1320 ms 89652 KB n=200000
122 Correct 1229 ms 89956 KB n=200000
123 Correct 739 ms 91144 KB n=200000
124 Correct 298 ms 24696 KB n=25264