제출 #380374

#제출 시각아이디문제언어결과실행 시간메모리
380374SavicSBirthday gift (IZhO18_treearray)C++14
100 / 100
1713 ms79800 KiB
#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,19)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,m){
        int a = lca(niz[i - 1], niz[i]);
        s[a].insert(i);
    }
    ff(i,1,m)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 < m) {
                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 < m) {
                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

**/


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...