이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 200005;
const int inf = 1e9 + 5;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
int n, m, q;
int niz[maxn];
vector<int> g[maxn];
int d[maxn];
int deda[maxn][20];
void dfs(int v, int p) {
deda[v][0] = p;
ff(i,1,20)deda[v][i] = deda[deda[v][i - 1]][i - 1];
for(auto u : g[v]) {
if(u != p) {
d[u] = d[v] + 1;
dfs(u, v);
}
}
}
int lca(int x, int y) {
if(d[x] < d[y])swap(x, y);
fb(i,19,0) {
if((d[x] - d[y]) & (1 << i))x = deda[x][i];
}
fb(i,19,0) {
if(deda[x][i] != deda[y][i]) {
x = deda[x][i];
y = deda[y][i];
}
}
return (x == y ? x : deda[x][0]);
}
set<int> s[maxn];
set<int> poms[maxn];
int main() {
ios::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
cin >> n >> m >> q;
ff(i,1,n - 1) {
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
ff(i,1,m)cin >> niz[i];
dfs(1, 0);
ff(i,2,n) {
int a = lca(niz[i - 1], niz[i]);
s[a].insert(i);
}
ff(i,1,n)poms[niz[i]].insert(i);
while(q--) {
int t;
cin >> t;
if(t == 1) {
int poz, x;
cin >> poz >> x;
if(poz > 1) {
int a = lca(niz[poz - 1], niz[poz]);
s[a].erase(poz);
}
if(poz < n) {
int a = lca(niz[poz], niz[poz + 1]);
s[a].erase(poz + 1);
}
poms[niz[poz]].erase(poz);
niz[poz] = x;
if(poz > 1) {
int a = lca(niz[poz - 1], niz[poz]);
s[a].insert(poz);
}
if(poz < n) {
int a = lca(niz[poz], niz[poz + 1]);
s[a].insert(poz + 1);
}
poms[niz[poz]].insert(poz);
}
if(t == 2) {
int l, r, x;
cin >> l >> r >> x;
if(!poms[x].empty()) {
auto poz = poms[x].lower_bound(l);
if(poz != poms[x].end() && *poz <= r) {
cout << *poz << " " << *poz << endl;
continue;
}
}
if(s[x].empty()) {
cout << -1 << " " << -1 << endl;
continue;
}
auto poz = s[x].lower_bound(l + 1);
if(poz != s[x].end() && *poz <= r)cout << *poz - 1 << " " << *poz << endl;
else cout << -1 << " " << -1 << endl;
}
}
return 0;
}
/**
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
// probati bojenje sahovski ili slicno
**/
컴파일 시 표준 에러 (stderr) 메시지
treearray.cpp: In function 'void dfs(int, int)':
treearray.cpp:36:26: warning: iteration 19 invokes undefined behavior [-Waggressive-loop-optimizations]
36 | ff(i,1,20)deda[v][i] = deda[deda[v][i - 1]][i - 1];
| ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:11:32: note: within this loop
11 | #define ff(i,a,b) for(int i=a;i<=b;i++)
......
36 | ff(i,1,20)deda[v][i] = deda[deda[v][i - 1]][i - 1];
| ~~~~~~
treearray.cpp:36:5: note: in expansion of macro 'ff'
36 | ff(i,1,20)deda[v][i] = deda[deda[v][i - 1]][i - 1];
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |