Submission #375914

# Submission time Handle Problem Language Result Execution time Memory
375914 2021-03-10T09:08:06 Z SavicS Pictionary (COCI18_pictionary) C++14
140 / 140
413 ms 24044 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 d[maxn];
int deda[maxn][20];
int mx[maxn][20];
void dfs(int v, int p, int pw){
	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);
		}
	}
}

int lca(int x, int y){
	if(d[x] < d[y])swap(x, y);
	fb(i,19,0){
		if((d[x] - d[y]) & (1 << i))x = deda[x][i];
	}
	fb(i,19,0){
		if(deda[x][i] != deda[y][i]){
			x = deda[x][i];
			y = deda[y][i];
		}
	}
	return (x == y ? x : 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

**/


# Verdict Execution time Memory Grader output
1 Correct 19 ms 2796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 3052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 187 ms 3308 KB Output is correct
2 Correct 199 ms 3308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 276 ms 3436 KB Output is correct
2 Correct 264 ms 3436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 148 ms 7900 KB Output is correct
2 Correct 151 ms 7788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 177 ms 9580 KB Output is correct
2 Correct 218 ms 10604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 240 ms 12544 KB Output is correct
2 Correct 186 ms 13164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 303 ms 16128 KB Output is correct
2 Correct 323 ms 17644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 354 ms 19564 KB Output is correct
2 Correct 358 ms 21916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 413 ms 23900 KB Output is correct
2 Correct 400 ms 24044 KB Output is correct