QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#877690#7696. Forest for the TreesWeaRD276AC ✓105ms3840kbC++203.0kb2025-02-01 00:47:162025-02-01 00:47:17

Judging History

This is the latest submission verdict.

  • [2025-02-01 00:47:17]
  • Judged
  • Verdict: AC
  • Time: 105ms
  • Memory: 3840kb
  • [2025-02-01 00:47:16]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

#include  <ext/pb_ds/assoc_container.hpp>
#include  <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

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

#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)

typedef long long ll;
typedef double db;
typedef long double LD;
typedef pair<int, int> pii;
typedef pair<db, db> pdd;
typedef pair<ll, ll> pll;


int solve()
{
	int n, m, r;
	if (!(cin >> n >> m >> r))
		return 1;
	
	vector<pii> tr(n), de(m);
	FOR (i, 0, n)
		cin >> tr[i].x >> tr[i].y;
	FOR (i, 0, m)
		cin >> de[i].x >> de[i].y;
	
	sort(all(tr));
	sort(all(de));
	tr.erase(unique(all(tr)), tr.end());
	de.erase(unique(all(de)), de.end());
	m = sz(de);
	int x0 = de[0].x, y0 = de[0].y;
	
	array<int, 3> ans{-1, -1, -1};
	auto check = [&](int px, int py, int d) -> bool
	{
		//cerr << "x = " << px << " y = " << py << '\n';
		auto it = lower_bound(all(tr), pii{px, py});
		if (it != tr.end() && *it == pii{px, py})
			return false;
		int cnt = 0;
		for (const pii& tree: tr)
		{
			int x = tree.x, y = tree.y;
			if (abs(x - px) + abs(y - py) > r)
				continue;
			
			x -= px;
			y -= py;
			int cx, cy;
			if (d == 0)
			{
				cx = x;
				cy = y;
			}
			else if (d == 1)
			{
				cx = -x;
				cy = -y;
			}
			else if (d == 2)
			{
				cx = -y;
				cy = x;
			}
			else
			{
				cx = y;
				cy = -x;
			}
			
			pii pos = {cx, cy};
			auto it = lower_bound(all(de), pos);
			if (it == de.end() || *it != pos)
				return false;
			cnt++;
		}
		return cnt == m;
	};
	
	for (const pii& tree: tr)
	{
		int x = tree.x, y = tree.y;
		FOR (d, 0, 4)
		{
			int cx, cy;
			if (d == 0)
			{
				cx = x - x0;
				cy = y - y0;
			}
			else if (d == 1)
			{
				cx = x + x0;
				cy = y + y0;
			}
			else if (d == 2)
			{
				cx = x - y0;
				cy = y + x0;
			}
			else
			{
				cx = x + y0;
				cy = y - x0;
			}
			//cerr << "x = " << cx << " y = " << cy << '\n';
			if (check(cx, cy, d))
			{
				array<int, 3> cur{cx, cy, d};
				if (ans != array<int, 3>{-1, -1, -1} && ans != cur)
				{
					cout << "Ambiguous\n";
					return 0;
				}
				ans = cur;
			}
		}
	}
	if (ans == array<int, 3>{-1, -1, -1})
		cout << "Impossible\n";
	else
		cout << ans[0] << ' ' << ans[1] << '\n';
	
	return 0;
}

int32_t main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int TET = 1e9;
	//cin >> TET;
	for (int i = 1; i <= TET; i++)
	{
		if (solve())
		{
			break;
		}
		#ifdef ONPC
			cerr << "_____________________________\n";
		#endif
	}
	#ifdef ONPC
		cerr << "\nfinished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec\n";
	#endif
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3712kb

input:

4 4 100
1 1
2 2
2 1
3 3
0 1
0 2
-1 2
-2 3

output:

0 1

result:

ok single line: '0 1'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3712kb

input:

4 4 4
0 1
1 0
0 -1
-1 0
0 1
1 0
0 -1
-1 0

output:

Ambiguous

result:

ok single line: 'Ambiguous'

Test #3:

score: 0
Accepted
time: 80ms
memory: 3840kb

input:

4899 957 32
-9961 -9999
-9970 -9968
-9942 -9986
-9991 -9991
-9950 -9990
-9958 -9994
-9939 -9944
-9992 -9987
-9973 -9937
-9981 -9941
-9940 -9940
-9989 -9945
-9961 -9963
-9970 -9932
-9969 -9967
-9977 -9971
-9949 -9989
-9958 -9958
-9957 -9993
-9937 -9935
-9938 -9980
-9965 -9997
-9992 -9951
-9946 -9984
...

output:

-9929 -9959

result:

ok single line: '-9929 -9959'

Test #4:

score: 0
Accepted
time: 3ms
memory: 3712kb

input:

4899 941 40
-9970 -9968
-9942 -9986
-9991 -9991
-9950 -9990
-9958 -9994
-9939 -9944
-9992 -9987
-9973 -9937
-9981 -9941
-9940 -9940
-9989 -9945
-9961 -9963
-9970 -9932
-9969 -9967
-9977 -9971
-9949 -9989
-9958 -9958
-9957 -9993
-9937 -9935
-9938 -9980
-9965 -9997
-9992 -9951
-9946 -9984
-10000 -9955...

output:

Impossible

result:

ok single line: 'Impossible'

Test #5:

score: 0
Accepted
time: 3ms
memory: 3840kb

input:

4899 940 40
-9970 -9968
-9942 -9986
-9991 -9991
-9950 -9990
-9958 -9994
-9939 -9944
-9992 -9987
-9973 -9937
-9981 -9941
-9940 -9940
-9989 -9945
-9961 -9963
-9970 -9932
-9969 -9967
-9977 -9971
-9949 -9989
-9958 -9958
-9957 -9993
-9937 -9935
-9938 -9980
-9965 -9997
-9992 -9951
-9946 -9984
-10000 -9955...

output:

Impossible

result:

ok single line: 'Impossible'

Test #6:

score: 0
Accepted
time: 1ms
memory: 3712kb

input:

3 3 1000
1 0
0 1
1 1
1 0
0 1
1 1

output:

0 0

result:

ok single line: '0 0'

Test #7:

score: 0
Accepted
time: 51ms
memory: 3712kb

input:

5000 2 1000
0 0
1000 1000
66740 84615
-16851 37613
31009 31589
-68041 -71122
21568 86889
53743 -31217
-73472 63365
9594 -12742
-25497 -5264
15942 13442
19537 -83361
93129 67319
-27565 73861
75273 -19266
-55048 -79726
-45975 -36987
-51309 35820
-99049 -10933
-45867 99815
-52121 99729
-89976 -15892
38...

output:

Ambiguous

result:

ok single line: 'Ambiguous'

Test #8:

score: 0
Accepted
time: 105ms
memory: 3712kb

input:

5000 3 1000
0 0
1000 1000
1000 999
13954 9751
75888 -54632
10747 -12901
37707 -37988
-49564 26056
-30528 -9620
6227 -95560
-82768 85135
-15530 89254
-39739 -79664
-81590 -75580
91135 -20238
-52264 -66253
-41259 -90627
-7096 -35158
-67316 13384
79722 57595
-40566 99205
35854 -48598
-83531 -59472
-286...

output:

600 400

result:

ok single line: '600 400'

Test #9:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

4 3 100
1 1
2 2
2 1
3 3
0 1
0 2
-1 2

output:

Impossible

result:

ok single line: 'Impossible'

Test #10:

score: 0
Accepted
time: 1ms
memory: 3712kb

input:

3 3 100
1 1
2 1
3 1
0 1
0 2
0 3

output:

Ambiguous

result:

ok single line: 'Ambiguous'

Test #11:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

4 3 2
1 1
2 2
2 1
3 3
0 1
0 2
-1 1

output:

2 0

result:

ok single line: '2 0'

Test #12:

score: 0
Accepted
time: 1ms
memory: 3712kb

input:

121 121 50
4 0
-10 10
8 0
0 -4
-6 10
-8 -8
10 6
2 2
-8 10
-4 -8
6 2
-2 -2
-10 -6
-4 10
4 2
-6 -6
10 -10
8 2
0 -2
-8 -6
2 4
-4 -6
6 4
-2 0
-10 -4
-6 -4
10 -8
8 4
0 0
-8 -4
-4 -4
6 6
-2 2
-10 -2
-6 -2
10 -6
2 -10
0 2
-8 -2
6 -10
-4 -2
4 -10
-2 4
-10 0
8 -10
-6 0
2 -8
-8 0
6 -8
10 8
-4 0
-10 2
8 -8
-6 ...

output:

Ambiguous

result:

ok single line: 'Ambiguous'

Test #13:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

120 120 50
-10 10
8 0
0 -4
-6 10
-8 -8
10 6
2 2
-8 10
-4 -8
6 2
-2 -2
-10 -6
-4 10
4 2
-6 -6
10 -10
8 2
0 -2
-8 -6
2 4
-4 -6
6 4
-2 0
-10 -4
-6 -4
10 -8
8 4
0 0
-8 -4
-4 -4
6 6
-2 2
-10 -2
-6 -2
10 -6
2 -10
0 2
-8 -2
6 -10
-4 -2
4 -10
-2 4
-10 0
8 -10
-6 0
2 -8
-8 0
6 -8
10 8
-4 0
-10 2
8 -8
-6 2
4 ...

output:

5 5

result:

ok single line: '5 5'

Test #14:

score: 0
Accepted
time: 0ms
memory: 3712kb

input:

120 119 50
-10 10
8 0
0 -4
-6 10
-8 -8
10 6
2 2
-8 10
-4 -8
6 2
-2 -2
-10 -6
-4 10
4 2
-6 -6
10 -10
8 2
0 -2
-8 -6
2 4
-4 -6
6 4
-2 0
-10 -4
-6 -4
10 -8
8 4
0 0
-8 -4
-4 -4
6 6
-2 2
-10 -2
-6 -2
10 -6
2 -10
0 2
-8 -2
6 -10
-4 -2
4 -10
-2 4
-10 0
8 -10
-6 0
2 -8
-8 0
6 -8
10 8
-4 0
-10 2
8 -8
-6 2
4 ...

output:

Impossible

result:

ok single line: 'Impossible'

Test #15:

score: 0
Accepted
time: 0ms
memory: 3712kb

input:

121 121 50
4 0
-10 10
8 0
0 -4
-6 10
-8 -8
10 6
2 2
-8 10
-4 -8
6 2
-2 -2
-10 -6
-4 10
4 2
-6 -6
10 -10
8 2
0 -2
-8 -6
2 4
-4 -6
6 4
-2 0
-10 -4
-6 -4
10 -8
8 4
0 0
-8 -4
-4 -4
6 6
-2 2
-10 -2
-6 -2
10 -6
2 -10
0 2
-8 -2
6 -10
-4 -2
4 -10
-2 4
-10 0
8 -10
-6 0
2 -8
-8 0
6 -8
10 8
-4 0
-10 2
8 -8
-6 ...

output:

Ambiguous

result:

ok single line: 'Ambiguous'

Test #16:

score: 0
Accepted
time: 32ms
memory: 3712kb

input:

2598 217 20
-28 50
-36 46
-16 24
-24 20
50 6
-44 -38
42 2
6 48
-42 8
-50 4
-30 -18
-38 -22
-20 26
-18 -44
-38 14
2 50
-46 10
-18 -8
22 28
-26 -12
14 24
-34 -16
-6 -34
-14 -38
26 -2
-22 -42
18 -6
-4 12
36 48
-12 8
8 -14
48 22
0 -18
18 30
-8 -22
20 -40
12 -44
30 4
4 -48
-8 14
12 -8
4 -12
44 24
24 -34
...

output:

5 5

result:

ok single line: '5 5'