# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
343837 | mat_v | Birthday gift (IZhO18_treearray) | C++14 | 1211 ms | 84588 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int,int>
#define maxn 200005
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;/// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int n,m,q;
vector<int> graf[maxn];
int cale[maxn][20];
int in[maxn], out[maxn];
int t;
int niz[maxn];
void root(int x, int y){
cale[x][0] = y;
in[x] = t;
ff(i,1,19)cale[x][i] = cale[cale[x][i - 1]][i - 1];
for(auto c:graf[x]){
if(c == y)continue;
t++;
root(c, x);
}
out[x] = t;
}
bool subtr(int x, int y){
if(x == 0)return true;
if(in[y] >= in[x] && in[y] <= out[x])return true;
return false;
}
int lca(int x, int y){
if(subtr(x,y))return x;
if(subtr(y,x))return y;
fb(i,19,0){
if(subtr(cale[x][i],y))continue;
x = cale[x][i];
}
return cale[x][0];
}
set<int> op[maxn], p[maxn];
void obrisi(int pos){
op[niz[pos]].erase(pos);
if(pos > 1)p[lca(niz[pos - 1], niz[pos])].erase(pos - 1);
if(pos < m)p[lca(niz[pos], niz[pos + 1])].erase(pos);
}
void dodaj(int pos, int v){
niz[pos] = v;
op[v].insert(pos);
if(pos > 1)p[lca(niz[pos - 1], niz[pos])].insert(pos - 1);
if(pos < m)p[lca(niz[pos + 1], niz[pos])].insert(pos);
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> m >> q;
ff(i,1,n - 1){
int a,b;
cin >> a >> b;
graf[a].pb(b);
graf[b].pb(a);
}
root(1, 0);
ff(i,1,m){
cin >> niz[i];
op[niz[i]].insert(i);
}
ff(i,1,m - 1){
//cout << niz[i] << " " << niz[i + 1] << lca(niz[i], niz[i + 1]) << "\n";
p[lca(niz[i], niz[i + 1])].insert(i);
}
while(q--){
int idx;
cin >> idx;
if(idx == 1){
int pos,v;
cin >> pos >> v;
obrisi(pos);
dodaj(pos, v);
continue;
}
int l,r,v;
cin >> l >> r >> v;
auto it1 = op[v].lower_bound(l);
int v1 = m+5;
auto it2 = p[v].lower_bound(l);
int v2 = m+5;
if(it1 != op[v].end())v1 = *it1;
if(it2 != p[v].end())v2 = *it2;
if(v1 <= r){
cout << v1 << " " << v1 << "\n";
}
else if(v2 <= r-1){
cout << v2 << " " << v2 + 1 << "\n";
}
else cout << "-1 -1\n";
}
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
*/
Compilation message (stderr)
# | 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... |