이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const int N = 1e5 + 2;
vector<int>G[200002];
int dp[20][200002], a[200002], lvl[200002], n,m , q;
void dfs (int x, int p) {
dp[0][x] = p;
lvl[x] = lvl[p] + 1;
for (int i = 1; (1 << i) <= n; i++)
dp[i][x] = dp[i - 1][dp[i - 1][x]];
for (auto it : G[x])
if (it != p)
dfs(it, x);
}
int lca (int x, int y) {
if (lvl[x] > lvl[y])
swap(x, y);
int d = lvl[y] - lvl[x];
for (int i = 0; (1 << i) <= n; i++)
if (d & (1 << i))
y = dp[i][y];
if (x == y)
return x;
for (int i = 19; i >= 0; i--)
if (dp[i][x] != dp[i][y]) {
x = dp[i][x];
y = dp[i][y];
}
return dp[0][x];
}
set<int>node[200002];
set<int>lol[200002];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m >> q;
for (int i = 1; i < n; i++) {
int x, y;
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];
lol[a[i]].insert(i);
if (i > 1) {
node[lca(a[i - 1], a[i])].insert(i - 1);
}
}
while (q--) {
int type;
cin >> type;
if (type == 1) {
int poz, x;
cin >> poz >> x;
if (poz > 1) {
node[lca(a[poz - 1], a[poz])].erase(poz - 1);
node[lca(x, a[poz - 1])].insert(poz - 1);
}
if (poz < m) {
node[lca(a[poz + 1], a[poz])].erase(poz);
node[lca(x, a[poz + 1])].insert(poz);
}
lol[a[poz]].erase(poz);
lol[x].insert(poz);
a[poz] = x;
}
else {
int l, r, x;
cin >> l >> r >> x;
auto it = node[x].lower_bound(l);
auto it2 = lol[x].lower_bound(l);
if (it2 != lol[x].end() && (*it2) <= r) {
cout << (*it2) << " " << (*it2) << "\n";
}
else if (it != node[x].end() && (*it) < r)
cout << (*it) << " " << (*it) + 1 << "\n";
else
cout << "-1 -1\n";
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |