답안 #249309

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
249309 2020-07-14T15:53:50 Z aloo123 Birthday gift (IZhO18_treearray) C++14
0 / 100
41 ms 48324 KB
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <climits>
#define ll int
#define ld long double
#define mp make_pair
#define pb push_back
#define in insert
#define vll vector<ll>
#define endl "\n"
#define pll pair<ll,ll>
#define f first
#define s second
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
// #define int ll
#define sz(x) (ll)x.size()
#define all(x) (x.begin(),x.end())
using namespace std;

 
// const ll INF = 1e12;
const ll N =(2e5+5); // TODO : change value as per problem
const ll MOD = 1e9+7;
ll up[N][19];
ll n,m,q;
vll adj[N];
ll a[N];
int k;
set<int> wow[N];
set<int> pt[N];
int timer;
ll tin[N];
ll tout[N];

void dfs(int v, int p)
{
    tin[v] = ++timer;
    up[v][0] = p;
    for (int i = 1; i <= k; ++i)
        up[v][i] = up[up[v][i-1]][i-1];

    for (int u : adj[v]) {
        if (u != p)
            dfs(u, v);
    }

    tout[v] = ++timer;
}

bool is_ancestor(int u, int v)
{
    return tin[u] <= tin[v] && tout[u] >= tout[v];
}

int lca(int u, int v)
{
    if (is_ancestor(u, v))
        return u;
    if (is_ancestor(v, u))
        return v;
    for (int i = k; i >= 0; --i) {
        if (!is_ancestor(up[u][i], v))
            u = up[u][i];
    }
    return up[u][0];
}

void preprocess(int root) {
    timer = 0;
    k = 25;
    dfs(root, root);
}


void solve(){
	cin >> n >> m >> q;
	for(int i = 2;i <= n;i++){
		ll u,v;
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(int i =1;i<=m;i++) cin >> a[i],pt[a[i]].in(i);
	preprocess(1);
	for(int i = 1;i <= m-1;i++){
		ll u = lca(a[i],a[i+1]);
		wow[u].in(i);
	}
	while(q--){
		ll t;
		cin >> t;
		if(t == 1){
			ll pos,v;
			cin >> pos >> v;
			// ll old = lca(a[pos],a[pos-1]);
			// ll old2 = lca(a[pos],a[pos+1]);
			pt[a[pos]].erase(pt[a[pos]].find(pos));
			if(pos != 1){
				ll old = lca(a[pos],a[pos-1]);
				wow[old].erase(wow[old].find(pos-1));
			}
			if(pos != m){
				ll old = lca(a[pos],a[pos+1]);
				wow[old].erase(wow[old].find(pos));
			}
			a[pos] = v;
			if(pos != 1){
				ll ne = lca(a[pos],a[pos-1]);
				wow[ne].in(pos-1);
			}
			if(pos != m){
				ll ne = lca(a[pos],a[pos+1]);
				wow[ne].in(pos);
			}
			pt[a[pos]].in(pos);
		}
		else{
			ll l,r,v;
			cin >> l >> r >> v;
			bool yay = false;
			auto it = pt[v].lower_bound(l);
			if(it != pt[v].end()){
				// cout << "HERE " << l << ' ' << r << endl;
				ll nd = *it;
				if(nd <= r){
					cout << nd << " " << nd << endl;
					yay = true;
				}
			}
			if(yay) continue;
			it = wow[v].lower_bound(l);
			if(it != wow[v].end()){
				int nd = *it;
				if((nd + 1) <= r){
					cout << nd << " " << nd+1 << endl;
					yay = true;
				}
			}
			if(!yay)
				cout << "-1 -1\n";
		}
	}
}
signed main(){
 
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
     // freopen(".in","r",stdin);freopen(".out","w",stdout);
    
     ll tt=1;   
     // cin >> tt;
    while(tt--){    
        solve();
    }    
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23808 KB n=5
2 Runtime error 41 ms 48324 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23808 KB n=5
2 Runtime error 41 ms 48324 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23808 KB n=5
2 Runtime error 41 ms 48324 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23808 KB n=5
2 Runtime error 41 ms 48324 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -