Submission #865581

#TimeUsernameProblemLanguageResultExecution timeMemory
865581vjudge1Birthday gift (IZhO18_treearray)C++17
100 / 100
641 ms85216 KiB
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <string> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <climits> #include <cstring> #include <cstdlib> #include <fstream> #include <numeric> #include <sstream> #include <cassert> #include <iomanip> #include <iostream> #include <algorithm> #include <stdio.h> #include <fstream> #include <unordered_map> using namespace std; typedef long long ll; typedef long double ld; #define all(x) x.begin(),x.end() #define pb push_back #define ent "\n" const int maxn = (int)2e5 + 13; const ll inf = (long long)1e18 + 20; const int mod = (int)1e9 + 7; int n,m,q; int a[maxn]; int up[maxn][21]; int sz[maxn]; int b[maxn]; vector<int>g[maxn]; set<int>pos[maxn],pos1[maxn]; void calc(int v,int p ){ for(int to : g[v]){ if(to != p){ up[to][0] = v; sz[to] = sz[v] + 1; calc(to,v); } } } int lca(int x,int y){ if(sz[x] > sz[y])swap(x,y); int dif = sz[y] - sz[x]; for(int i = 0 ; i <= 20 ; i ++){ if((dif >> i)&1){ y = up[y][i]; } } if(x == y)return x; for(int i = 20 ; i >= 0 ; i --){ if(up[x][i]!=up[y][i] && min(up[x][i],up[y][i]) > 0){ x = up[x][i]; y = up[y][i]; } } return up[x][0]; } void solve(){ cin >> n >> m >> q; for(int i = 1 ; i < n ; i ++){ int x,y;cin >> x >> y; g[x].pb(y); g[y].pb(x); } for(int i = 1 ; i <= m ; i ++){ cin >> a[i]; pos[a[i]].insert(i); } calc(1,0); for(int i = 1 ; i <= 20 ; i ++){ for(int j = 1 ; j <= n ; j ++){ up[j][i] = up[up[j][i - 1]][i - 1]; } } for(int i = 1 ; i < m ; i ++){ pos1[lca(a[i],a[i + 1])].insert(i); } while(q--){ int tp;cin >> tp; if(tp == 2){ int l,r,v;cin >> l >> r >> v; if(pos[v].lower_bound(l) != pos[v].end() && *pos[v].lower_bound(l) <= r){ int x = *pos[v].lower_bound(l); cout << x <<' ' << x << ent; continue; } if(pos1[v].lower_bound(l) != pos1[v].end() && *pos1[v].lower_bound(l) < r){ int x = *pos1[v].lower_bound(l); cout << x << ' ' << x + 1 << ent; continue; } cout << "-1 -1\n"; } else{ int in,v;cin >> in >> v; pos[a[in]].erase(in); pos[v].insert(in); if(in < m)pos1[lca(a[in],a[in + 1])].erase(in); if(in > 1)pos1[lca(a[in - 1],a[in])].erase(in - 1); a[in] = v; if(in < m)pos1[lca(a[in],a[in + 1])].insert(in); if(in > 1)pos1[lca(a[in - 1],a[in])].insert(in - 1); } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; //cin >> t; while(t --){ solve(); } 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...