답안 #502045

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
502045 2022-01-05T07:05:20 Z ismoilov Birthday gift (IZhO18_treearray) C++14
0 / 100
12 ms 23796 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define fp(a,i,c) for(int (a) = (i); (a) < (c); (a)++)
#define fpp(a,i,c) for(int (a) = (i); (a) <= (c); (a)++)
#define fm(a,i,c) for(int (a) = (i); (a) > (c); (a)--)
#define fmm(a,i,c) for(int (a) = (i); (a) >= (c); (a)--)
const int maxx = 2e5+5;
vector <int> g[maxx];
int a[maxx], d[maxx], lc[maxx][19];
set <int> A[maxx], B[maxx];
int n, m, q;

void dfs(int v, int p = 0){
	lc[v][0] = p;
	fpp(i,1,18)
		lc[v][i] = lc[lc[v][i-1]][i-1];
	for(auto it : g[v]){
		if(it != p){
			d[it] = d[v]+1;
			dfs(it, v);
		}
	}
}

int lca(int u, int v){
	if(d[u] < d[v])
		swap(u, v);
	int k = d[u] - d[v];
	fmm(i,18,0){
		if(k>>i & 1)
			u = lc[u][i];
	}
	if(u == v)
		return u;
	fmm(i,18,0){
		if(lc[u][i] != lc[v][i])
			u = lc[u][i], v = lc[v][i];
	}
	return lc[u][0];
}

void upd(int id, int v){
	if(id > 1){
		int y = lca(a[id], a[id-1]);
		A[y].erase(id-1);
		A[lca(v, a[id-1])].insert(id-1);
	}
	if(id < m){
		int y = lca(a[id], a[id+1]);
		A[y].erase(id+1);
		A[lca(v, a[id+1])].insert(id+1);
	}
	B[a[id]].erase(id);
	B[v].insert(id);
	a[id] = v;
}

void get(int l, int r, int v){
	auto it = B[v].lower_bound(l);
	if(it != B[v].end() && *it <= r){
		cout << *it << " " << *it << "\n";
		return;
	}
	it = A[v].lower_bound(l);
	if(it != A[v].end() && *it+1 <= r){
		cout << *it << " " << *it+1 << "\n";
		return;
	}
	cout << "-1 -1\n";
}

void S()
{
//	int n, m, q;
	cin >> n >> m >> q;
	fp(i,1,n){
		int x, y;
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	dfs(1);
	fpp(i,1,m)
		cin >> a[i];
	fpp(i,1,m){
		int x = lca(a[i], a[i+1]);
	//	cout << i << " " << x << "\n";
		if(i < m)
			A[x].insert(i);
		B[a[i]].insert(i);
	}
/*	fpp(i,1,n){
		cout << i << " ";
		for(auto it : A[i])
			cout << it << " ";
		cout << "\n";
	}*/
	while(q --){
		int t;
		cin >> t;
		if(t == 1){
			int id, v;
			cin >> id >> v;
			upd(id, v);
		}
		else{
			int l, r, v;
			cin >> l >> r >> v;
			get(l, r, v);
		}
	}
}
int main()
{
	IOS;
	S();
}

Compilation message

treearray.cpp: In function 'void dfs(int, int)':
treearray.cpp:7:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #define fpp(a,i,c) for(int (a) = (i); (a) <= (c); (a)++)
      |                            ^
treearray.cpp:18:2: note: in expansion of macro 'fpp'
   18 |  fpp(i,1,18)
      |  ^~~
treearray.cpp: In function 'int lca(int, int)':
treearray.cpp:9:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    9 | #define fmm(a,i,c) for(int (a) = (i); (a) >= (c); (a)--)
      |                            ^
treearray.cpp:32:2: note: in expansion of macro 'fmm'
   32 |  fmm(i,18,0){
      |  ^~~
treearray.cpp:9:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    9 | #define fmm(a,i,c) for(int (a) = (i); (a) >= (c); (a)--)
      |                            ^
treearray.cpp:38:2: note: in expansion of macro 'fmm'
   38 |  fmm(i,18,0){
      |  ^~~
treearray.cpp: In function 'void S()':
treearray.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define fp(a,i,c) for(int (a) = (i); (a) < (c); (a)++)
      |                           ^
treearray.cpp:79:2: note: in expansion of macro 'fp'
   79 |  fp(i,1,n){
      |  ^~
treearray.cpp:7:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #define fpp(a,i,c) for(int (a) = (i); (a) <= (c); (a)++)
      |                            ^
treearray.cpp:86:2: note: in expansion of macro 'fpp'
   86 |  fpp(i,1,m)
      |  ^~~
treearray.cpp:7:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #define fpp(a,i,c) for(int (a) = (i); (a) <= (c); (a)++)
      |                            ^
treearray.cpp:88:2: note: in expansion of macro 'fpp'
   88 |  fpp(i,1,m){
      |  ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23756 KB n=5
2 Correct 11 ms 23796 KB n=100
3 Incorrect 12 ms 23788 KB Wrong range.
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23756 KB n=5
2 Correct 11 ms 23796 KB n=100
3 Incorrect 12 ms 23788 KB Wrong range.
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23756 KB n=5
2 Correct 11 ms 23796 KB n=100
3 Incorrect 12 ms 23788 KB Wrong range.
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23756 KB n=5
2 Correct 11 ms 23796 KB n=100
3 Incorrect 12 ms 23788 KB Wrong range.
4 Halted 0 ms 0 KB -