제출 #446169

#제출 시각아이디문제언어결과실행 시간메모리
446169ak2006Birthday gift (IZhO18_treearray)C++14
0 / 100
26 ms30028 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using vb = vector<bool>; using vvb = vector<vb>; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using vc = vector<char>; using vvc = vector<vc>; using vs = vector<string>; const ll mod = 1e9 + 7,inf = 1e18; #define pb push_back #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); void setIO() { fast; } int n = 2e5 + 5,m,q,TIMER; vvi adj(n); vvi anc(n,vi(21)); vi in(n),out(n); void dfs(int u,int p) { in[u] = ++TIMER; anc[u][0] = p; for (int i = 1;i<=20;i++)anc[u][i] = anc[anc[u][i - 1]][i - 1]; for (int v:adj[u]){ if (v == p)continue; dfs(v,u); } out[u] = ++TIMER; } bool is_ancestor(int u,int v) { return in[u] <= in[v] && out[u] >= out[v]; } int LCA(int u,int v) { if (in[u] > in[v])swap(u,v); if (is_ancestor(u,v))return u; for (int i = 20;i>=0;i--){ if (!is_ancestor(anc[v][i],u))v = anc[v][i]; } return anc[v][0]; } int main() { setIO(); cin>>n>>m>>q; vi a(m + 1); vector<set<int>>lcas(n + 1); vector<set<int>>positions(n + 1); for (int i = 1;i<=n - 1;i++){ int u,v; cin>>u>>v; adj[u].pb(v),adj[v].pb(u); } for (int i = 1;i<=m;i++)cin>>a[i],positions[a[i]].insert(i); dfs(1,1); for (int i = 1;i<=m - 1;i++) lcas[LCA(a[i],a[i + 1])].insert(i); while (q--){ int type,v; cin>>type; if (type == 1){ int pos; cin>>pos>>v; if (pos >= 2)lcas[LCA(a[pos - 1],a[pos])].erase(pos - 1); if (pos + 1 <= n)lcas[LCA(a[pos],a[pos + 1])].erase(pos); positions[a[pos]].erase(pos); a[pos] = v; positions[a[pos]].insert(pos); if (pos >= 2)lcas[LCA(a[pos - 1],a[pos])].insert(pos - 1); if (pos + 1 <= n)lcas[LCA(a[pos],a[pos + 1])].insert(pos); } else{ int l,r; cin>>l>>r>>v; auto it = positions[v].lower_bound(l); int pos = *it; if (pos <= r && pos >= l){cout<<pos<<" "<<pos<<endl;continue;} auto it2 = lcas[v].lower_bound(l); pos = *it2; if (pos <= r && pos >= l){cout<<pos<<" "<<pos + 1<<endl;continue;} cout<<-1<<endl; } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...