답안 #375911

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
375911 2021-03-10T09:04:07 Z SavicS Pictionary (COCI18_pictionary) C++14
140 / 140
391 ms 25964 KB
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 100005;
const int inf = 1e9 + 5;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k)  print the k-th smallest number in os(0-based)

int n, m, q;

vector<pii> g[maxn];

int sz[maxn];
int par[maxn];
int findpar(int x){
	if(x == par[x])return x;
	return par[x] = findpar(par[x]);
}
void unite(int x, int y){
	int a = findpar(x);
	int b = findpar(y);
	if(sz[a] > sz[b])swap(a, b);
	par[b] = a;
	sz[a] += sz[b];
}
void init(){
	ff(i,1,n){
		par[i] = i;
		sz[i] = 1;
	}
}

int timer = 0;
int d[maxn];
int in[maxn];
int out[maxn];
int deda[maxn][20];
int mx[maxn][20];
void dfs(int v, int p, int pw){
	in[v] = ++timer;
	deda[v][0] = p;
	mx[v][0] = pw;
	ff(i,1,19){
		deda[v][i] = deda[deda[v][i - 1]][i - 1];
		mx[v][i] = max(mx[v][i - 1], mx[deda[v][i - 1]][i - 1]);
	}
	for(auto c : g[v]){
		int u = c.fi;
		int w = c.se;
		if(u != p){
			d[u] = d[v] + 1;
			dfs(u, v, w);
		}
	}
	out[v] = timer;
}

bool predak(int x, int y){
	return (in[x] <= in[y] && out[x] >= out[y]);
}

int lca(int x, int y){
	if(predak(x, y))return x;
	if(predak(y, x))return y;
	int najv = 0;
	fb(i,19,0){
		if(deda[x][i] && !predak(deda[x][i], y)){
			x = deda[x][i];
		}
	}
	return deda[x][0];	
}

int calc(int x, int y){
	int najv = 0;
	fb(i,19,0){
		if(y & (1 << i)){
			najv = max(najv, mx[x][i]);
			x = deda[x][i];
		}
	}
	return najv;
}

int main()
{
   	ios::sync_with_stdio(false);
   	cout.tie(nullptr);
  	cin.tie(nullptr);
	cin >> n >> m >> q;
	init();
	fb(i,m,1){
		for(int j=2*i;j<=n;j+=i){
			if(findpar(i) == findpar(j))continue;
			unite(i, j);
			g[i].pb({j, m - i + 1});
			g[j].pb({i, m - i + 1});
		}
	}
	dfs(1, 0, 0);
	while(q--){
		int a, b;
		cin >> a >> b;
		int lc = lca(a, b);
		int ans = max(calc(a, d[a] - d[lc]), calc(b, d[b] - d[lc]));
		cout << ans << endl;
	}	
   	return 0;
}
/**



// probati bojenje sahovski ili slicno

**/


Compilation message

pictionary.cpp: In function 'int lca(int, int)':
pictionary.cpp:83:6: warning: unused variable 'najv' [-Wunused-variable]
   83 |  int najv = 0;
      |      ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 2924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 3052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 181 ms 3820 KB Output is correct
2 Correct 189 ms 3692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 271 ms 4188 KB Output is correct
2 Correct 233 ms 4204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 144 ms 8300 KB Output is correct
2 Correct 147 ms 8172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 163 ms 10284 KB Output is correct
2 Correct 176 ms 11372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 236 ms 13768 KB Output is correct
2 Correct 179 ms 14020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 297 ms 17516 KB Output is correct
2 Correct 309 ms 19180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 359 ms 20972 KB Output is correct
2 Correct 351 ms 23788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 382 ms 25736 KB Output is correct
2 Correct 391 ms 25964 KB Output is correct