Submission #166771

#TimeUsernameProblemLanguageResultExecution timeMemory
166771abilBirthday gift (IZhO18_treearray)C++14
100 / 100
2518 ms84984 KiB
#include <bits/stdc++.h>

#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
//#define int long long

using namespace std;

const int N = (2e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);

vector<int > g[N];
int up[20][N], tin[N], tout[N], timer, a[N];

void dfs(int v,int pr){
	tin[v] = ++timer;
	up[0][v] = pr;
	for(int i = 1;i <= 19; i++){
		up[i][v] = up[i - 1][up[i - 1][v]];
	}
	for(auto to : g[v]){
		if(to != pr){
			dfs(to, v);
		}
	}
	tout[v] = ++timer;
}

bool upper(int a,int b){
	return tin[a] <= tin[b] && tout[a] >= tout[b];
}

int lca(int a,int b){
	if(upper(a, b)){
		return a;
	}
	if(upper(b, a)){
		return b;
	}
	for(int i = 19;i >= 0; i--){
		if(!upper(up[i][a], b)){
			a = up[i][a];
		}
	}
	a = up[0][a];
	return a;
}

set<int > a_pos[N], lca_pos[N];
main()
{
	int n, m, q, v, u;
	cin >> n >> m >> q;
	for(int i = 1;i <= n - 1; i++){
		scanf("%d%d", &v, &u);
		g[u].pb(v);
		g[v].pb(u);
	}
	dfs(1, 1);
	for(int i = 1;i <= m; i++){
		scanf("%d", &a[i]);
		a_pos[a[i]].insert(i);
		if(i > 1){
			lca_pos[lca(a[i - 1], a[i])].insert(i - 1);
		}
	}
	int type, l, r, x;
	while(q--){
		scanf("%d", &type);
		if(type == 1){
			scanf("%d%d", &x, &v);
			a_pos[a[x]].erase(x);
			a_pos[v].insert(x);
			if(x > 1){
				lca_pos[lca(a[x - 1], a[x])].erase(x - 1);
				lca_pos[lca(a[x - 1], v)].insert(x - 1);
			}
			if(x < m){
				lca_pos[lca(a[x], a[x + 1])].erase(x);
				lca_pos[lca(a[x + 1], v)].insert(x);
			}
			a[x] = v;
		}
		else{
			scanf("%d%d%d",&l, &r, &v);
			auto it = a_pos[v].lower_bound(l);
			if(it != a_pos[v].end() && *it <= r){
				printf("%d %d\n", *it, *it);
				continue;
			}
			it = lca_pos[v].lower_bound(l);
			if(r > l && it != lca_pos[v].end() && *it < r){
				printf("%d %d\n", *it, *it + 1);
				continue;
			}
			printf("%d %d\n", -1, -1);
		}
	}
}

Compilation message (stderr)

treearray.cpp:54:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main()
      ^
treearray.cpp: In function 'int main()':
treearray.cpp:59:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &v, &u);
   ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a[i]);
   ~~~~~^~~~~~~~~~~~~
treearray.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &type);
   ~~~~~^~~~~~~~~~~~~
treearray.cpp:75:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d%d", &x, &v);
    ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:89:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d%d%d",&l, &r, &v);
    ~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...