Submission #495871

# Submission time Handle Problem Language Result Execution time Memory
495871 2021-12-20T06:41:41 Z ergagan Birthday gift (IZhO18_treearray) C++17
0 / 100
123 ms 262148 KB
//я так много думал, что опять попал
#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define f first
#define s second
#define left(v) v + v
#define right(v) v + v + 1
#define ub upper_bound
#define lb lower_bound
#define pll pair<ll, ll>

using namespace std;
typedef int ll;

//17 SEVENTEEN
const long double Pi = acos(-1.0);
const ll dx[] = {0,0,1,-1};
const ll dy[] = {1,-1,0,0};
const ll N = (ll) 2e6 + 17;
const ll M = (ll) 5e3 + 69;
const ll inf = (ll) 1e14 + 3;
const ll mod = (ll) 1e9 + 7;
ll sq(ll x) { return x * x; }

ll zxc = 1, a[N];
ll h[N], up[N][50];
ll n, m, q, tp, pos, u, v, l, r;

vector<ll> g[N];

set<ll> st[N], ts[N];

void dfs(ll v, ll p, ll lvl) {

	up[v][0] = p, h[v] = lvl;

	for(ll to : g[v]) {
		if(to != p)
			dfs(to, v, lvl + 1);
	}

}

bool bit(ll x, ll p) {
	return x & (1 << p);
}

ll lift(ll v, ll k) {
	for(ll i = 0; i < 26; i++) {
		if(bit(k, i))
			v = up[v][i];
	}
	return v;
}

ll lca(ll a, ll b) {
	if(h[a] < h[b]) swap(a, b);

	a = lift(a, h[a] - h[b]);

	if(a == b) return a;

	for(ll i = 25; i >= 0; i--) {
		if(up[a][i] != up[b][i])
			a = up[a][i], b = up[b][i];
	}

	return up[a][0];
}

ll dis(ll a, ll b) {
    return h[a] + h[b] - h[lca(a, b)] * 2;
}

void solve() {

    cin >> n >> m >> q;

    for(ll i = 1; i < n; i++) {
        cin >> u >> v;
        g[u].pb(v), g[v].pb(u);
    }

    for(ll i = 1; i <= m; i++) {
        cin >> a[i];
        st[a[i]].insert(i);
    }

    dfs(1, -1, 0);

    for(ll i = 1; i <= 25; i++) {
    	for(ll v = 1; v <= n; v++) {
    		up[v][i] = up[up[v][i - 1]][i - 1];
    	}
    }

    for(ll i = 1; i < m; i++) {
        ll x = lca(a[i], a[i + 1]);
        ts[x].insert(i);
    }

    while(q--) {
        cin >> tp;
        if(tp == 1) {
            cin >> pos >> v;
            ll x = lca(a[pos], a[pos + 1]), y = lca(a[pos], a[pos - 1]);

            if(st[a[pos]].count(pos)) st[a[pos]].erase(pos);
            if(pos < m && ts[x].count(pos)) ts[x].erase(pos);
            if(pos > 1 && ts[y].count(pos - 1)) ts[y].erase(pos - 1);

            a[pos] = v;
            x = lca(a[pos], a[pos + 1]), y = lca(a[pos], a[pos - 1]);

            st[a[pos]].insert(pos);
            if(pos < m) ts[x].insert(pos);
            if(pos > 1) ts[y].insert(pos - 1);
        }
        else {

            cin >> l >> r >> v;

            ll x = *st[v].lb(l);

            if(x <= r) { cout << x << " " << x << "\n"; continue; }

            x = *ts[v].lb(l);
            if(x < r) { cout << x << " " << x + 1 << "\n"; continue; }

            cout << "-1 -1\n";
        }
    }
}

/*
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
*/

int main(/*Уверенно*/) {
ios_base::sync_with_stdio(0);
    cin.tie(0);
/*
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);
*/
//    cin >> zxc;

    for(ll i = 0; i < N; i++)
        st[i].insert(inf), ts[i].insert(inf);

    while(zxc--) {
        solve();
    }
  	return 0;
}
// さよならさ いかなくちゃ
# Verdict Execution time Memory Grader output
1 Runtime error 123 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 123 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 123 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 123 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -