Submission #166767

#TimeUsernameProblemLanguageResultExecution timeMemory
166767abilBirthday gift (IZhO18_treearray)C++14
30 / 100
4078 ms5648 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;
}
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);
	}
	for(int i = 1;i <= m; i++){
		scanf("%d", &a[i]);
	}
	dfs(1, 1);
	int type, l, r, x;
	while(q--){
		scanf("%d", &type);
		if(type == 1){
			scanf("%d%d", &x, &v);
			a[x] = v;
		}
		else{
			scanf("%d%d%d",&l, &r, &v);
			int xl = -1, xr = -1;
			for(int i = l;i <= r; i++){
				int lc = a[i];
				for(int j = i;j <= r; j++){
					lc = lca(lc, a[j]);
					if(lc == v){
						xl = i;
						xr = j;
						break;
					}
				}
				if(xl != -1){
					break;
				}
			}
			printf("%d %d\n", xl, xr);
		}
	}
}

Compilation message (stderr)

treearray.cpp:52:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main()
      ^
treearray.cpp: In function 'int main()':
treearray.cpp:57: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:62:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a[i]);
   ~~~~~^~~~~~~~~~~~~
treearray.cpp:67:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &type);
   ~~~~~^~~~~~~~~~~~~
treearray.cpp:69: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:73: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...