Submission #341238

#TimeUsernameProblemLanguageResultExecution timeMemory
341238IZhO_2021_I_want_SilverBirthday gift (IZhO18_treearray)C++14
100 / 100
1255 ms84276 KiB
#include <iostream> #include <algorithm> #include <vector> #include <cmath> #include <set> #include <map> #include <iomanip> #include <cassert> #include <stack> #include <queue> #include <deque> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp>- using namespace std; //using namespace __gnu_pbds; typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; // template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // order_of_key (k) : Number of items strictly smaller than k . // find_by_order(k) : K-th element in a set (counting from zero). #define sz(a) (int)a.size() #define all(a) a.begin(), a.end() #define pb push_back #define ppb pop_back #define mkp make_pair #define F first #define S second #define lb lower_bound #define ub upper_bound #define show(a) cerr << #a <<" -> "<< a <<" " #define nl cerr <<"\n" //#define int ll const int N = 2e5 + 5; const int oo = 1e9 + 5; const int LG = 18; int n, m, q, arr[N]; vector <int> g[N]; int par[N][LG+1], tin[N], tout[N], tiktak; set <int> st1[N], st2[N]; void dfs(int v, int pr) { par[v][0] = pr; for (int i = 1; i <= LG; ++i) { par[v][i] = par[par[v][i-1]][i-1]; } tin[v] = ++tiktak; for (int to : g[v]) { if (to == pr) { continue; } dfs(to, v); } tout[v] = ++tiktak; } bool is_parent(int a, int b) { return (tin[a] <= tin[b] && tout[b] <= tout[a]); } int lca(int a, int b) { if (is_parent(a, b)) { return a; } if (is_parent(b, a)) { return b; } for (int i = LG; i >= 0; --i) { if (!is_parent(par[a][i], b)) { a = par[a][i]; } } return par[a][0]; } /*void Print() { for (int v = 1; v <= n; ++v) { show(v); nl; for (int pos1 : st1[v]) { show(pos1); } nl; for (int pos2 : st2[v]) { show(pos2); } nl; nl; } }*/ void solve() { cin >> n >> m >> q; for (int i = 1; i < n; ++i) { int a, b; cin >> a >> b; g[a].pb(b); g[b].pb(a); } for (int i = 1; i <= m; ++i) { cin >> arr[i]; } dfs(1, 1); for (int i = 1; i <= m; ++i) { st1[arr[i]].insert(i); if (i + 1 <= m) { st2[lca(arr[i], arr[i+1])].insert(i); } } for (int i = 1; i <= q; ++i) { int type; cin >> type; if (type == 1) { int pos, v; cin >> pos >> v; st1[arr[pos]].erase(pos); if (pos - 1 >= 1) { st2[lca(arr[pos-1], arr[pos])].erase(pos-1); } if (pos + 1 <= m) { st2[lca(arr[pos], arr[pos+1])].erase(pos); } arr[pos] = v; st1[arr[pos]].insert(pos); if (pos - 1 >= 1) { st2[lca(arr[pos-1], arr[pos])].insert(pos-1); } if (pos + 1 <= m) { st2[lca(arr[pos], arr[pos+1])].insert(pos); } } else { int l, r, v; cin >> l >> r >> v; auto it1 = st1[v].lower_bound(l); if (it1 != st1[v].end() && *it1 <= r) { cout << *it1 <<" "<< *it1 <<"\n"; continue; } auto it2 = st2[v].lower_bound(l); if (it2 != st2[v].end() && *it2 < r) { cout << *it2 <<" "<< *it2 + 1 <<"\n"; continue; } cout << -1 <<" "<< -1 <<"\n"; } } } main () { ios_base::sync_with_stdio(false); cin.tie(NULL); int tests = 1; //cin >> tests; while (tests --) { solve(); } return 0; } /* Just Chalish! 5 4 4 1 2 3 1 3 4 5 3 4 5 2 3 2 1 3 1 1 3 5 2 3 4 5 2 1 3 1 */

Compilation message (stderr)

treearray.cpp:131:8: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  131 |  main () {
      |        ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...