Submission #1096263

#TimeUsernameProblemLanguageResultExecution timeMemory
1096263Zero_OPBirthday gift (IZhO18_treearray)C++14
100 / 100
515 ms127312 KiB
#include "bits/stdc++.h" using namespace std; #define sz(v) (int)v.size() #define all(v) begin(v), end(v) #define compact(v) v.erase(unique(all(v)), end(v)) #define rall(v) rbegin(v), rend(v) const int MAX = 4e5 + 5; int n, m, q, timerdfs, a[MAX], adjLCA[MAX], depth[MAX], pos[MAX], st[20][MAX]; vector<int> adj[MAX]; set<int> one[MAX], two[MAX]; int query(set<int>& S, int l, int r){ auto it = S.lower_bound(l); if(it == S.end()) return -1; if(*it <= r) return *it; return -1; } void dfs(int u){ st[0][pos[u] = timerdfs++] = u; for(int v : adj[u]){ adj[v].erase(find(all(adj[v]), u)); depth[v] = depth[u] + 1; dfs(v); st[0][timerdfs++] = u; } } int f(int u, int v){ return depth[u] < depth[v] ? u : v; } void prepareRMQ(){ for(int i = 1; (1 << i) <= timerdfs; ++i){ for(int j = 0; j + (1 << i) <= timerdfs; ++j){ st[i][j] = f(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]); } } } int getLCA(int u, int v){ u = pos[u]; v = pos[v]; if(u > v) swap(u, v); int k = 31 - __builtin_clz(v - u + 1); return f(st[k][u], st[k][v - (1 << k) + 1]); } void deleteAdj(int i){ two[adjLCA[i]].erase(i); } void refine(int i){ adjLCA[i] = getLCA(a[i], a[i + 1]); two[adjLCA[i]].insert(i); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); if(fopen("task.inp", "r")){ freopen("task.inp", "r", stdin); freopen("task.out", "w", stdout); } cin >> n >> m >> q; for(int i = 1; i < n; ++i){ int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } for(int i = 1; i <= m; ++i){ cin >> a[i]; } dfs(1); prepareRMQ(); for(int i = 1; i <= m; ++i){ one[a[i]].insert(i); if(i < m) { adjLCA[i] = getLCA(a[i], a[i + 1]); two[adjLCA[i]].insert(i); } } while(q--){ int type; cin >> type; if(type == 1){ int pos, val; cin >> pos >> val; one[a[pos]].erase(pos); a[pos] = val; one[a[pos]].insert(pos); if(pos < m) deleteAdj(pos), refine(pos); if(pos > 1) deleteAdj(pos - 1), refine(pos - 1); } else{ int l, r, x; cin >> l >> r >> x; int q1 = query(one[x], l, r); if(q1 != -1){ cout << q1 << ' ' << q1 << '\n'; // cout << "YES\n"; continue; } int q2 = query(two[x], l, r - 1); if(q2 != -1){ cout << q2 << ' ' << q2 + 1 << '\n'; // cout << "YES\n"; continue; } else{ // cout << "NO\n"; cout << -1 << ' ' << -1 << '\n'; } } } return 0; }

Compilation message (stderr)

treearray.cpp: In function 'int main()':
treearray.cpp:66:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |         freopen("task.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |         freopen("task.out", "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...