#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define ll long long
#define ld long double
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define prc(n) fixed << setprecision(n)
#define fastios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pi acos(-1);
const int inf = 1e9+7;
const int N = 505;
int n, m, q, a[N], par[N], used[N], s, dis[N], fir[N];
vector<int>edges[N];
vector<int>order;
vector<pii>tree;
void build(int lx, int rx, int x){
if(lx == rx){
if(lx > sz(order)) return;
tree[x].ff = dis[order[lx-1]];
tree[x].ss = order[lx-1];;
return;
}
int m = (lx + rx)/2;
build(lx, m, x*2);
build(m+1, rx, x*2+1);
if(tree[x*2].ff <= tree[x*2+1].ff) tree[x] = tree[x*2];
else tree[x] = tree[x*2+1];
}
pii find(int l, int r, int lx, int rx, int x){
if(lx > r || rx < l) return {inf, inf};
if(lx >= l && rx <= r) return tree[x];
int m = (lx + rx)/2;
pii res1 = find(l, r, lx, m, x*2);
pii res2 = find(l, r, m+1, rx, x*2+1);
return (res1.ff <= res2.ff ? res1 : res2);
}
void dfs(int u, int p, int d){
order.pb(u);
dis[u] = d;
if(fir[u] == -1) fir[u] = sz(order);
for(auto x:edges[u]){
if(x == p) continue;
dfs(x, u, d+1);
order.pb(u);
}
}
void solve(){
cin>>n>>m>>q;
for(int i=1;i<n;i++){
int a, b;
cin>>a>>b;
edges[a].pb(b);
edges[b].pb(a);
}
memset(fir, -1, sizeof(fir));
dfs(1, 0, 0);
s = 2;
while(s < sz(order)) s*=2;
tree.assign(2*s, {inf, inf});
build(1, s, 1);
for(int i=1;i<=m;i++) cin>>a[i];
while(q--){
int t;
cin>>t;
if(t == 1){
int pos, val;
cin>>pos>>val;
a[pos] = val;
}
else{
int l, r, val, rr;
cin>>l>>r>>val;
int la = -1, ra = -1;
for(;l<= r; l++){
int prev = -1;
for(rr = l; rr <= r; rr++){
int lx = min(fir[a[l]], fir[a[rr]]), rx = max(fir[a[l]], fir[a[rr]]);
int res = find(lx, rx, 1, s, 1).ss;
if(rr - l){
lx = min(fir[prev], fir[res]), rx = max(fir[prev], fir[res]);
res = find(lx, rx, 1, s, 1).ss;
}
if(res == val) if(rr - l +1 >= ra - la +1) la = l, ra = rr;
prev = res;
}
}
cout<<la<<" "<<ra<<"\n";
}
}
return;
}
int main(){
fastios
int t = 1;
//cin>>t;
while(t--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=5 |
2 |
Correct |
16 ms |
364 KB |
n=100 |
3 |
Correct |
18 ms |
364 KB |
n=100 |
4 |
Correct |
17 ms |
364 KB |
n=100 |
5 |
Correct |
14 ms |
364 KB |
n=100 |
6 |
Correct |
100 ms |
364 KB |
n=100 |
7 |
Correct |
98 ms |
364 KB |
n=100 |
8 |
Correct |
11 ms |
364 KB |
n=100 |
9 |
Correct |
9 ms |
364 KB |
n=100 |
10 |
Correct |
53 ms |
364 KB |
n=100 |
11 |
Correct |
54 ms |
364 KB |
n=100 |
12 |
Correct |
10 ms |
364 KB |
n=100 |
13 |
Correct |
11 ms |
364 KB |
n=100 |
14 |
Correct |
11 ms |
364 KB |
n=100 |
15 |
Correct |
10 ms |
364 KB |
n=100 |
16 |
Correct |
15 ms |
364 KB |
n=100 |
17 |
Correct |
23 ms |
492 KB |
n=100 |
18 |
Correct |
15 ms |
364 KB |
n=100 |
19 |
Correct |
13 ms |
364 KB |
n=100 |
20 |
Correct |
13 ms |
364 KB |
n=100 |
21 |
Correct |
14 ms |
364 KB |
n=100 |
22 |
Correct |
12 ms |
364 KB |
n=100 |
23 |
Correct |
23 ms |
364 KB |
n=100 |
24 |
Correct |
21 ms |
364 KB |
n=100 |
25 |
Correct |
48 ms |
364 KB |
n=100 |
26 |
Correct |
2 ms |
364 KB |
n=12 |
27 |
Correct |
33 ms |
364 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=5 |
2 |
Correct |
16 ms |
364 KB |
n=100 |
3 |
Correct |
18 ms |
364 KB |
n=100 |
4 |
Correct |
17 ms |
364 KB |
n=100 |
5 |
Correct |
14 ms |
364 KB |
n=100 |
6 |
Correct |
100 ms |
364 KB |
n=100 |
7 |
Correct |
98 ms |
364 KB |
n=100 |
8 |
Correct |
11 ms |
364 KB |
n=100 |
9 |
Correct |
9 ms |
364 KB |
n=100 |
10 |
Correct |
53 ms |
364 KB |
n=100 |
11 |
Correct |
54 ms |
364 KB |
n=100 |
12 |
Correct |
10 ms |
364 KB |
n=100 |
13 |
Correct |
11 ms |
364 KB |
n=100 |
14 |
Correct |
11 ms |
364 KB |
n=100 |
15 |
Correct |
10 ms |
364 KB |
n=100 |
16 |
Correct |
15 ms |
364 KB |
n=100 |
17 |
Correct |
23 ms |
492 KB |
n=100 |
18 |
Correct |
15 ms |
364 KB |
n=100 |
19 |
Correct |
13 ms |
364 KB |
n=100 |
20 |
Correct |
13 ms |
364 KB |
n=100 |
21 |
Correct |
14 ms |
364 KB |
n=100 |
22 |
Correct |
12 ms |
364 KB |
n=100 |
23 |
Correct |
23 ms |
364 KB |
n=100 |
24 |
Correct |
21 ms |
364 KB |
n=100 |
25 |
Correct |
48 ms |
364 KB |
n=100 |
26 |
Correct |
2 ms |
364 KB |
n=12 |
27 |
Correct |
33 ms |
364 KB |
n=100 |
28 |
Correct |
1825 ms |
460 KB |
n=500 |
29 |
Correct |
2248 ms |
484 KB |
n=500 |
30 |
Correct |
1846 ms |
472 KB |
n=500 |
31 |
Correct |
1990 ms |
492 KB |
n=500 |
32 |
Correct |
2039 ms |
364 KB |
n=500 |
33 |
Correct |
1983 ms |
492 KB |
n=500 |
34 |
Correct |
1986 ms |
492 KB |
n=500 |
35 |
Correct |
2079 ms |
492 KB |
n=500 |
36 |
Execution timed out |
4065 ms |
364 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=5 |
2 |
Correct |
16 ms |
364 KB |
n=100 |
3 |
Correct |
18 ms |
364 KB |
n=100 |
4 |
Correct |
17 ms |
364 KB |
n=100 |
5 |
Correct |
14 ms |
364 KB |
n=100 |
6 |
Correct |
100 ms |
364 KB |
n=100 |
7 |
Correct |
98 ms |
364 KB |
n=100 |
8 |
Correct |
11 ms |
364 KB |
n=100 |
9 |
Correct |
9 ms |
364 KB |
n=100 |
10 |
Correct |
53 ms |
364 KB |
n=100 |
11 |
Correct |
54 ms |
364 KB |
n=100 |
12 |
Correct |
10 ms |
364 KB |
n=100 |
13 |
Correct |
11 ms |
364 KB |
n=100 |
14 |
Correct |
11 ms |
364 KB |
n=100 |
15 |
Correct |
10 ms |
364 KB |
n=100 |
16 |
Correct |
15 ms |
364 KB |
n=100 |
17 |
Correct |
23 ms |
492 KB |
n=100 |
18 |
Correct |
15 ms |
364 KB |
n=100 |
19 |
Correct |
13 ms |
364 KB |
n=100 |
20 |
Correct |
13 ms |
364 KB |
n=100 |
21 |
Correct |
14 ms |
364 KB |
n=100 |
22 |
Correct |
12 ms |
364 KB |
n=100 |
23 |
Correct |
23 ms |
364 KB |
n=100 |
24 |
Correct |
21 ms |
364 KB |
n=100 |
25 |
Correct |
48 ms |
364 KB |
n=100 |
26 |
Correct |
2 ms |
364 KB |
n=12 |
27 |
Correct |
33 ms |
364 KB |
n=100 |
28 |
Correct |
1825 ms |
460 KB |
n=500 |
29 |
Correct |
2248 ms |
484 KB |
n=500 |
30 |
Correct |
1846 ms |
472 KB |
n=500 |
31 |
Correct |
1990 ms |
492 KB |
n=500 |
32 |
Correct |
2039 ms |
364 KB |
n=500 |
33 |
Correct |
1983 ms |
492 KB |
n=500 |
34 |
Correct |
1986 ms |
492 KB |
n=500 |
35 |
Correct |
2079 ms |
492 KB |
n=500 |
36 |
Execution timed out |
4065 ms |
364 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=5 |
2 |
Correct |
16 ms |
364 KB |
n=100 |
3 |
Correct |
18 ms |
364 KB |
n=100 |
4 |
Correct |
17 ms |
364 KB |
n=100 |
5 |
Correct |
14 ms |
364 KB |
n=100 |
6 |
Correct |
100 ms |
364 KB |
n=100 |
7 |
Correct |
98 ms |
364 KB |
n=100 |
8 |
Correct |
11 ms |
364 KB |
n=100 |
9 |
Correct |
9 ms |
364 KB |
n=100 |
10 |
Correct |
53 ms |
364 KB |
n=100 |
11 |
Correct |
54 ms |
364 KB |
n=100 |
12 |
Correct |
10 ms |
364 KB |
n=100 |
13 |
Correct |
11 ms |
364 KB |
n=100 |
14 |
Correct |
11 ms |
364 KB |
n=100 |
15 |
Correct |
10 ms |
364 KB |
n=100 |
16 |
Correct |
15 ms |
364 KB |
n=100 |
17 |
Correct |
23 ms |
492 KB |
n=100 |
18 |
Correct |
15 ms |
364 KB |
n=100 |
19 |
Correct |
13 ms |
364 KB |
n=100 |
20 |
Correct |
13 ms |
364 KB |
n=100 |
21 |
Correct |
14 ms |
364 KB |
n=100 |
22 |
Correct |
12 ms |
364 KB |
n=100 |
23 |
Correct |
23 ms |
364 KB |
n=100 |
24 |
Correct |
21 ms |
364 KB |
n=100 |
25 |
Correct |
48 ms |
364 KB |
n=100 |
26 |
Correct |
2 ms |
364 KB |
n=12 |
27 |
Correct |
33 ms |
364 KB |
n=100 |
28 |
Correct |
1825 ms |
460 KB |
n=500 |
29 |
Correct |
2248 ms |
484 KB |
n=500 |
30 |
Correct |
1846 ms |
472 KB |
n=500 |
31 |
Correct |
1990 ms |
492 KB |
n=500 |
32 |
Correct |
2039 ms |
364 KB |
n=500 |
33 |
Correct |
1983 ms |
492 KB |
n=500 |
34 |
Correct |
1986 ms |
492 KB |
n=500 |
35 |
Correct |
2079 ms |
492 KB |
n=500 |
36 |
Execution timed out |
4065 ms |
364 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |