#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
vector<long long> g[N];
long long DP[18][N], depth[N];
void dfs(long long node, long long par) {
DP[0][node] = par;
for(long long i = 0; i < g[node].size(); i++) {
if(g[node][i] != par) {
depth[g[node][i]] = depth[node] + 1;
dfs(g[node][i], node);
}
}
}
void DPTable(long long n) {
for(long long j = 1; j < 18; j++) {
for(long long i = 1; i <= n; i++) {
if(DP[j - 1][i] == -1)
DP[j][i] = -1;
else
DP[j][i] = DP[j - 1][DP[j - 1][i]];
}
}
}
long long lca(long long a, long long b) {
if(depth[a] < depth[b])
swap(a, b);
long long diff = depth[a] - depth[b];
for(long long i = 17; i >= 0; i--) {
if(diff >= (1 << i)) {
diff -= (1 << i);
a = DP[i][a];
}
}
if(a == b)
return a;
for(long long i = 17; i >= 0; i--) {
if(DP[i][a] != DP[i][b]) {
a = DP[i][a];
b = DP[i][b];
}
}
return DP[0][a];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, m, q;
cin >> n >> m >> q;
for(long long i = 0; i < n - 1; i++) {
long long v, u;
cin >> v >> u;
g[v].push_back(u);
g[u].push_back(v);
}
depth[1] = 0;
dfs(1, -1);
DPTable(n);
long long a[m + 1];
for(long long i = 1; i <= m; i++) {
cin >> a[i];
}
multiset<long long> ones[n + 1], twos[n + 1];
for(long long i = 1; i < m; i++) {
ones[a[i]].insert(i);
twos[lca(a[i], a[i + 1])].insert(i);
}
ones[a[m]].insert(m);
for(long long i = 1; i <= q; i++) {
long long command;
cin >> command;
if(command == 1) {
long long idx, val;
cin >> idx >> val;
ones[a[idx]].erase(idx);
if(idx != m) {
twos[lca(a[idx], a[idx + 1])].erase(idx);
}
if(idx != 1) {
twos[lca(a[idx], a[idx - 1])].erase(idx - 1);
}
a[idx] = val;
ones[a[idx]].insert(idx);
if(idx != m) {
twos[lca(a[idx], a[idx + 1])].insert(idx);
}
if(idx != 1) {
twos[lca(a[idx], a[idx - 1])].insert(idx - 1);
}
}
else {
long long l, r, v;
cin >> l >> r >> v;
if(ones[v].size() != 0) {
auto temp = ones[v].upper_bound(r);
temp--;
long long x = *(temp);
if(x >= l) {
cout << x << " " << x << endl;
continue;
}
}
if(twos[v].size() != 0) {
auto temp = twos[v].lower_bound(r);
temp--;
long long x = *(temp);
if(x >= l && x < r) {
cout << x << " " << x + 1 << endl;
continue;
}
}
cout << "-1 -1" << endl;
}
}
}
Compilation message
treearray.cpp: In function 'void dfs(long long int, long long int)':
treearray.cpp:10:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | for(long long i = 0; i < g[node].size(); i++) {
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5100 KB |
n=5 |
2 |
Incorrect |
4 ms |
5228 KB |
Wrong output format. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5100 KB |
n=5 |
2 |
Incorrect |
4 ms |
5228 KB |
Wrong output format. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5100 KB |
n=5 |
2 |
Incorrect |
4 ms |
5228 KB |
Wrong output format. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5100 KB |
n=5 |
2 |
Incorrect |
4 ms |
5228 KB |
Wrong output format. |
3 |
Halted |
0 ms |
0 KB |
- |