Submission #1324472

#TimeUsernameProblemLanguageResultExecution timeMemory
1324472sonarchtPictionary (COCI18_pictionary)C++20
140 / 140
177 ms26580 KiB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(v) begin(v), end(v)
#define pll pair<ll, ll>
#define fi first
#define se second
#define vll vector<ll>
#define mll map<ll, ll>
mt19937_64 rng(chrono::high_resolution_clock().now().time_since_epoch().count());
const ll N = 1e5 + 6, inf = 1e18 + 7, mod = 1e9 + 7;
ll n, m, k, q, ans[N], a[N], l[N], r[N], par[N*2], sz[N*2];
pll qry[N];
vll v[N];

ll find(ll v) {
	return v == par[v] ? v : par[v] = find(par[v]);
}

void unite(ll a, ll b) {
	a = find(a);
	b = find(b);
	if (a != b) {
		if (sz[a] < sz[b]) swap(a, b);
		par[b] = a;
		sz[a] += sz[b];
	}
}
void solve() {
	cin >> n >> m >> q;
	for (int i = 1; i <= q; ++i) {
		cin >> qry[i].fi >> qry[i].se;
		l[i] = 1;
		r[i] = m;
	}
	ll pbs = 17;
	while (pbs--) {
		for (int i = 1; i <= m; ++i) v[i].clear();
		for (int i = 1; i <= q; ++i) {
			if (l[i] <= r[i]) {
				ll mid = (l[i] + r[i]) / 2;
				v[mid].pb(i);
			}
		}
		for (int i = 1; i <= n+m; ++i) {
			par[i] = i;
			sz[i] = 1;
		}
		for (int i = 1; i <= m; ++i) {
			int t = m - i + 1;
			for (int j = t; j <= n; j += t) unite(j, n+t);
			for (ll j : v[i]) {
				if (find(qry[j].fi) == find(qry[j].se)) {
					ans[j] = i;
					r[j] = i-1;
				} else l[j] = i+1;
			}
		}
	}
	for (int i = 1; i <= q; ++i) cout << ans[i] << "\n";
	return;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	if (fopen(".INP", "r")) {
		freopen(".INP", "r", stdin);
		freopen(".OUT", "w", stdout);
	}
	ll tc = 1;
//	cin >> tc;
	while (tc--) {
		solve();
	}
	return 0;
}

Compilation message (stderr)

pictionary.cpp: In function 'int main()':
pictionary.cpp:72:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |                 freopen(".INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~
pictionary.cpp:73:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |                 freopen(".OUT", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...