Submission #495999

#TimeUsernameProblemLanguageResultExecution timeMemory
495999ZielBirthday gift (IZhO18_treearray)C++17
100 / 100
1545 ms91716 KiB
/** * LES GREATEABLES BRO TEAM **/ #include <bits/stdc++.h> using namespace std; using ll = long long; #define sz(x) (int)x.size() const bool FLAG = false; void setIO(const string &f = ""); string to_string(const string s) { return '"' + s + '"'; } string to_string(const char c) { return char(39) + string(1, c) + char(39); } string to_string(const char* s) { return to_string(string(s)); } string to_string(bool f) { return (f ? "true" : "false"); } template<class A, class B> string to_string(const pair<A, B> x) { return "(" + to_string(x.first) + ", " + to_string(x.second) + ")"; } template<class A, class B, class C> string to_string(const tuple<A, B, C> x) { return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ")"; } template<class A, class B, class C, class D> string to_string(const tuple<A, B, C, D> x) { return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ", " + to_string(get<3>(x)) + ")"; } template<class T> string to_string(const T v) { string res = "{"; bool f = true; for (auto x: v) res += (f ? to_string(x) : ", " + to_string(x)), f = false; return res + "}"; } void debug_args() { cerr << "]\n"; } template<class H, class... T> void debug_args(H x, T... y) { cerr << to_string(x); if (sizeof... (y)) cerr << ", "; debug_args(y...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]: [", debug_args(__VA_ARGS__); #else #define debug(...) 47 #endif const int N = 3e5 + 14; int n, m, q, level[N], up[22][N], a[N]; vector<vector<int>> g(N); void dfs(int v, int pr) { level[v] = level[pr] + 1; up[0][v] = pr; for (int i = 1; i <= 20; i++) up[i][v] = up[i - 1][up[i - 1][v]]; for (int to: g[v]) { if (to == pr) continue; dfs(to, v); } } int lca(int x, int y) { if (level[x] > level[y]) swap(x, y); for (int i = 20; i >= 0; i--) { if (level[up[i][y]] >= level[x]) y = up[i][y]; } if (x == y) return x; for (int i = 20; i >= 0; i--) { if (up[i][x] != up[i][y]) x = up[i][x], y = up[i][y]; } return up[0][x]; } void solve() { cin >> n >> m >> q; for (int i = 1, x, y; i < n; i++) { cin >> x >> y; g[x].push_back(y), g[y].push_back(x); } dfs(1, 0); for (int i = 1; i <= m; i++) cin >> a[i]; vector<int> b(m + 1); set<int> s1[n + n], s2[n + n]; for (int i = 1; i < m; i++) { b[i] = lca(a[i], a[i + 1]); s1[b[i]].insert(i); s2[a[i]].insert(i); } s2[a[m]].insert(m); while (q--) { int type; cin >> type; if (type == 1) { int pos, v; cin >> pos >> v; s2[a[pos]].erase(pos); a[pos] = v; s2[a[pos]].insert(pos); if (pos + 1 <= m) { s1[b[pos]].erase(pos); b[pos] = lca(a[pos], a[pos + 1]); s1[b[pos]].insert(pos); } if (pos - 1 >= 1) { s1[b[pos - 1]].erase(pos - 1); b[pos - 1] = lca(a[pos - 1], a[pos]); s1[b[pos - 1]].insert(pos - 1); } } else { int l, r, v; cin >> l >> r >> v; bool found = 0; if (sz(s2[v])) { auto it = s2[v].lower_bound(l); if (it != s2[v].end() && *it <= r) { cout << *it << ' ' << *it << '\n'; found = 1; } } if (!found && sz(s1[v])) { auto it = s1[v].lower_bound(l); if (it != s1[v].end() && *it <= r - 1) { cout << *it << ' ' << (*it) + 1 << '\n'; found = 1; } } if (!found) cout << "-1 -1\n"; } } } signed main() { setIO(); int tt = 1; if (FLAG) { cin >> tt; } while (tt--) { solve(); } return 0; } void setIO(const string &f) { ios_base::sync_with_stdio(false); cin.tie(nullptr); if (fopen((f + ".in").c_str(), "r")) { freopen((f + ".in").c_str(), "r", stdin); freopen((f + ".out").c_str(), "w", stdout); } }

Compilation message (stderr)

treearray.cpp: In function 'void setIO(const string&)':
treearray.cpp:171:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  171 |         freopen((f + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:172:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  172 |         freopen((f + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...