# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
65819 | kingpig9 | Birthday gift (IZhO18_treearray) | C++11 | 1879 ms | 115428 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1 << 18, MAXLG = 18;
#define debug(...) fprintf(stderr, __VA_ARGS__)
//#define debug(...)
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define fillchar(a, s) memset((a), (s), sizeof(a))
int N, M, Q;
vector<int> adj[MAXN];
int par[MAXN][MAXLG], depth[MAXN];
int A[MAXN];
int getpar (int x, int d) {
for (int i = 0; d; d >>= 1, i++) {
if (d & 1) {
x = par[x][i];
}
}
return x;
}
int lca (int x, int y) {
if (depth[x] < depth[y]) {
swap(x, y);
}
x = getpar(x, depth[x] - depth[y]);
if (x == y) {
return x;
}
for (int i = MAXLG - 1; i >= 0; i--) {
if (par[x][i] != par[y][i]) {
x = par[x][i];
y = par[y][i];
}
}
return par[x][0];
}
void dfs (int x) {
for (int y : adj[x]) {
adj[y].erase(find(all(adj[y]), x));
par[y][0] = x;
for (int i = 1; i < MAXLG; i++) {
par[y][i] = par[par[y][i - 1]][i - 1];
}
depth[y] = depth[x] + 1;
dfs(y);
}
}
set<int> st[MAXN][2];
pii solve (int lt, int rt, int v) {
set<int> *s = st[v];
auto it = s[0].lower_bound(lt);
if (it != s[0].end() && *it <= rt) {
return pii(*it, *it);
}
it = s[1].lower_bound(lt);
if (it != s[1].end() && *it < rt) {
return pii(*it, *it + 1);
}
return pii(-1, -1);
}
int main() {
scanf("%d %d %d", &N, &M, &Q);
for (int i = 1; i < N; i++) {
int x, y;
scanf("%d %d", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1);
for (int i = 1; i <= M; i++) {
scanf("%d", &A[i]);
}
for (int i = 1; i <= M; i++) {
st[A[i]][0].insert(i);
if (i + 1 <= M) {
st[lca(A[i], A[i + 1])][1].insert(i);
}
}
//and now...queries
for (int qi = 1; qi <= Q; qi++) {
int qt, v;
scanf("%d", &qt);
if (qt == 1) {
int pos;
scanf("%d %d", &pos, &v);
st[A[pos]][0].erase(pos);
if (pos + 1 <= M) {
st[lca(A[pos], A[pos + 1])][1].erase(pos);
}
if (pos - 1 >= 1) {
st[lca(A[pos - 1], A[pos])][1].erase(pos - 1);
}
A[pos] = v;
st[A[pos]][0].insert(pos);
if (pos + 1 <= M) {
st[lca(A[pos], A[pos + 1])][1].insert(pos);
}
if (pos - 1 >= 1) {
st[lca(A[pos - 1], A[pos])][1].insert(pos - 1);
}
} else {
int lt, rt;
scanf("%d %d %d", <, &rt, &v);
pii ans = solve(lt, rt, v);
printf("%d %d\n", ans.fi, ans.se);
}
}
}
Compilation message (stderr)
# | 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... |