# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
40368 | model_code | Birthday gift (IZhO18_treearray) | C++11 | 1862 ms | 94912 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 <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <ctime>
#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define pii pair < int, int >
#define ull unsigned long long
#define pll pair < ll, ll >
#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it ++)
#define all(s) s.begin(), s.end()
#define sz(a) (int)a.size()
const int inf = (1ll << 30) - 1;
const int maxn = (int) 1e5 + 10;
using namespace std;
int p[20][200200];
int lev[200200];
vector<int> g[200200];
int n, k, q;
void dfs(int v, int pa){
p[0][v] = pa;
for(int i = 1; i < 19; i++){
p[i][v] = p[i-1][p[i-1][v]];
}
lev[v] = lev[pa] + 1;
for(int i = 0; i < g[v].size(); ++i){
int to = g[v][i];
if(to == pa) continue;
dfs(to, v);
}
}
int a[200200];
set<int> S[200200];
set<int> T[200200];
int lca(int u, int v){
if(lev[u] < lev[v]) swap(u, v);
for(int i = 18; i >= 0; i--){
if(lev[u] - (1<<i) >= lev[v]) {
u = p[i][u];
}
}
if(u == v) return u;
for(int i = 18; i >= 0; i--){
if(p[i][u] != p[i][v]) {
u = p[i][u];
v = p[i][v];
}
}
return p[0][v];
}
void solve(){
scanf("%d%d%d", &n, &k, &q);
for(int i = 1, x, y; i < n; i++){
scanf("%d%d", &x, &y);
g[x].pb(y);
g[y].pb(x);
}
for(int i = 1; i <= k;i++) scanf("%d", &a[i]);
dfs(1, 0);
for(int i = 1; i <= k;i++) S[a[i]].insert(i);
for(int i = 2; i <= k; i++) T[lca(a[i], a[i-1])].insert(i);
for(int i = 1; i <= n; i++) {
S[i].insert(inf);
T[i].insert(inf);
}
for(int i = 0, ty, l, r, x; i < q; i++){
scanf("%d", &ty);
if(ty == 1){
scanf("%d%d", &l, &x);
S[a[l]].erase(l);
if(l > 1) T[lca(a[l], a[l-1])].erase(l);
if(l < k) T[lca(a[l], a[l+1])].erase(l+1);
a[l] = x;
S[a[l]].insert(l);
if(l > 1) T[lca(a[l], a[l-1])].insert(l);
if(l < k) T[lca(a[l], a[l+1])].insert(l+1);
}
else{
scanf("%d%d%d", &l, &r, &x);
int d = *S[x].lower_bound(l);
int e = *T[x].lower_bound(l+1);
if(d <= r){
printf("%d %d\n", d, d);
}
else if(e <= r){
printf("%d %d\n", e-1, e);
}
else{
printf("-1 -1\n");
}
}
}
}
int main () {
#ifdef LOCAL
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
#endif
int t=1;
//scanf("%d", &t);
for(int i=1; i <= t; i++){
//printf("Case #%d\n", i);
solve();
}
return 0;
}
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... |