답안 #865174

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
865174 2023-10-24T06:16:20 Z vjudge1 Birthday gift (IZhO18_treearray) C++17
0 / 100
2 ms 6748 KB
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <fstream>
#include <unordered_map>
using namespace std;

typedef long long ll;
typedef long double ld;

#define all(x) x.begin(),x.end()
#define pb push_back
#define ent "\n"

const int maxn = (int)2e5 + 13;
const ll inf = (long long)1e18 + 20;
const int mod = (int)1e9 + 7;

int n,m,q;
int a[maxn];
int up[maxn][12];
int sz[maxn];
int b[maxn];
vector<int>g[maxn];

void calc(int v,int p ){
	for(int to : g[v]){
		if(to != p){
			up[to][0] = v;
			sz[to] = sz[v] + 1;
			calc(to,v); 
		}
	}
}

int lca(int x,int y){
	if(!x){
		return y;
	}
	if(sz[x] > sz[y])swap(x,y);
	int dif = sz[y] - sz[x];
	for(int i = 0 ; i <= 11 ; i ++){
		if((dif >> i)&1){
			y = up[y][i];
		}
	}
	if(x == y)return x;
	for(int i = 11 ; i >= 0 ; i --){
		if(up[x][i]!=up[y][i] && min(up[x][i],up[y][i]) > 0){
			x = up[x][i];
			y = up[y][i];
		}
	}
	return up[x][0];
}

void solve(){
	cin >> n >> m >> q;
	for(int i = 1 ; i < n ; i ++){
		int x,y;cin >> x >> y;
		g[x].pb(y);
		g[y].pb(x);
	}
	for(int i = 1 ; i <= m ; i ++){
		cin >> a[i];
	}
	calc(1,0);
	for(int i = 1 ; i <= 11 ; i ++){
		for(int j = 1 ; j <= n ; j ++){
			up[j][i] = up[up[j][i - 1]][i - 1];
		}
	}
	while(q--){
		int tp;cin >> tp;
		if(tp == 2){
			int l,r,v;cin >> l >> r >> v;
			int lc = 0,ok = 0,ind = 0,ind1;
			for(int i = l ; i <= r ; i ++){
				int cheker = lca(a[i],v);
				if(cheker == v){
					lc = lca(lc,a[i]);
					if(!ind) ind = i;
					ind1 = i;
				}
			}
			if(lc == v){
				cout << ind << ' ' << ind1 << ent;
			}
			else{
				cout << "-1 -1\n";
			}
		}
		else{
			int pos,v;cin >> pos >> v;
			//mp[a[pos]] = v;
			a[pos] = v;

		}
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	//cin >> t;
	while(t --){
		solve();
	}
	return 0;
}

Compilation message

treearray.cpp: In function 'void solve()':
treearray.cpp:98:15: warning: unused variable 'ok' [-Wunused-variable]
   98 |    int lc = 0,ok = 0,ind = 0,ind1;
      |               ^~
treearray.cpp:34:13: warning: 'ind1' may be used uninitialized in this function [-Wmaybe-uninitialized]
   34 | #define ent "\n"
      |             ^~~~
treearray.cpp:98:30: note: 'ind1' was declared here
   98 |    int lc = 0,ok = 0,ind = 0,ind1;
      |                              ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6748 KB n=5
2 Incorrect 2 ms 6748 KB Wrong range.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6748 KB n=5
2 Incorrect 2 ms 6748 KB Wrong range.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6748 KB n=5
2 Incorrect 2 ms 6748 KB Wrong range.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6748 KB n=5
2 Incorrect 2 ms 6748 KB Wrong range.
3 Halted 0 ms 0 KB -