Submission #1347446

#TimeUsernameProblemLanguageResultExecution timeMemory
1347446hminhatBitaro's Travel 2 (JOI25_travel2)C++17
Compilation error
0 ms0 KiB
/*	
	ROAD TO TST 
*/
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define fi first
#define se second
#define pii pair<int, int>
#define el "\n"
#define pb push_back
#define all(v) v.begin(), v.end()

#define rep(i, x, y) for(int i = x, _y = y; i <= _y; i++)
#define rev(i, x, y) for(int i = x, _y = y; i >= _y; i--)

void file() {
	#define name "test"
	if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin);freopen(name".out", "w", stdout);}
	else {
		#undef name
		#define name "C:\\Users\\hminh\\Desktop\\2026\\AIO\\test"
		if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin);freopen(name".out", "w", stdout);}
	}
}

const int nmax = 6e5 + 7;

int n, m, q, L;
vector<vector<int>> a, nx[19];
int dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};
int par[nmax], maxx[nmax], val[nmax], up[19][nmax], h[nmax];
vector<int> g[nmax]; 
struct data {
	int u, v, w;
	data(int _u = 0, int _v = 0, int _w = 0) {
		u = _u, v = _v, w = _w;
	}
};
bool cmp(data a, data b) {
	return a.w == b.w ? a.v > b.v : a.w < b.w;
}
vector<data> E;

int find_par(int u) {
	return u == par[u] ? u : par[u] = find_par(par[u]);
}

void construct() {
	int cur = n * m - 1;
	rep(i, 0, 2 * n * m - 1) par[i] = i;
	for(data e : E) {
		int u = e.u, v = e.v;
		u = find_par(u), v = find_par(v);
		if(u == v) continue;
		cur++;
		val[cur] = e.w;
		g[cur].pb(u), g[cur].pb(v);
		par[u] = par[v] = cur;
	}
}

void dfs(int u) {
	for(int v : g[u]) {
		up[0][v] = u;
		h[v] = h[u] + 1;
		rep(i, 1, 18) {
			if((1 << i) > h[v]) continue;
			up[i][v] = up[i - 1][up[i - 1][v]];
		}
		dfs(v);
	}
}

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

void build_krt() {
	rep(i, 0, n - 1) {
		rep(j, 0, m - 1) {
			rep(p, 0, 3) {
				int x = i + dx[p], y = j + dy[p];
				if(x < 0 || y < 0 || x >= n || y >= m) continue;
				E.pb(data(i * m + j, x * m + y, max(a[i][j], a[x][y])));
			}
		}
	}
	sort(all(E), cmp);
	construct();
	dfs(2 * n * m - 2);
}

int pos[nmax];

void union_sets(int u, int v) {
	u = find_par(u), v = find_par(v);
	if(u == v) return;
	par[v] = u;
	if(maxx[u] < maxx[v]) pos[u] = pos[v];
	maxx[u] = max(maxx[u], maxx[v]);
}

void build_next() {
	rep(k, 0, 18) {
		nx[k].resize(n);
		rep(i, 0, n - 1) nx[k][i].resize(m, 0);
	}
	rep(i, 0, n * m - 1) par[i] = i, maxx[i] = a[i / m][i % m], pos[i] = i;
	rep(i, 0, n - 1) {
		rep(j, 0, m - 1) {
			E.pb(data(i * m + j, 1, a[i][j]));
			E.pb(data(i * m + j, -1, a[i][j] + L));
		}
	}
	sort(all(E), cmp);
	for(data e : E) {
		int id = e.u, t = e.v, w = e.w;
		int i = id / m, j = id % m;
		if(t == 1) {
			rep(p, 0, 3) {
				int x = i + dx[p], y = j + dy[p];
				if(x < 0 || y < 0 || x >= n || y >= m || a[x][y] > w) continue;
				union_sets(id, x * m + y);
				// cout << i << ' ' << j << ' ' << x << ' ' << y << el;
			}
		}
		else {
			int idx = pos[find_par(id)];
			nx[0][i][j] = idx;
		}
	}
	rep(k, 1, 18) rep(i, 0, n - 1) rep(j, 0, m - 1) {
		int id = nx[k - 1][i][j];
		nx[k][i][j] = nx[k - 1][id / m][id % m];
	} 
}

int main()
{
	file();
	ios_base::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin >> n >> m >> L;
	a.resize(n);
	rep(i, 0, n - 1) {
		a[i].resize(m);
		rep(j, 0, m - 1) cin >> a[i][j];
	}
	build_krt();
	E.clear();
	build_next();
	cin >> q;
	while(q -- ) {
		int x, y, u, v;
		cin >> x >> y >> u >> v;
		x--,y--,u--,v--;
		int h = val[lca(x * m + y, u * m + v)], res = 0;
		rev(k, 18, 0) {
			int id = nx[k][x][y], i = id / m, j = id % m;
			if(a[i][j] < h) {
				res += 1 << k;
				x = i, y = j;
			}
		}
		// cout << h << ' ' << res << ' ' << x << ' ' << y << ' ' << a[x][y] << el;
		if(a[x][y] >= h - L) cout << res + 1;
		else cout << -1;
		cout << el;
	}
	return 0;
}

Compilation message (stderr)

Main.cpp:42:10: error: reference to 'data' is ambiguous
   42 | bool cmp(data a, data b) {
      |          ^~~~
In file included from /usr/include/c++/13/string:53,
                 from /usr/include/c++/13/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:52,
                 from Main.cpp:4:
/usr/include/c++/13/bits/range_access.h:346:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  346 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:336:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  336 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:325:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  325 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:314:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  314 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
Main.cpp:36:8: note:                 'struct data'
   36 | struct data {
      |        ^~~~
Main.cpp:42:18: error: reference to 'data' is ambiguous
   42 | bool cmp(data a, data b) {
      |                  ^~~~
/usr/include/c++/13/bits/range_access.h:346:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  346 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:336:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  336 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:325:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  325 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:314:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  314 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
Main.cpp:36:8: note:                 'struct data'
   36 | struct data {
      |        ^~~~
Main.cpp:42:24: error: expression list treated as compound expression in initializer [-fpermissive]
   42 | bool cmp(data a, data b) {
      |                        ^
Main.cpp:45:12: error: template argument 1 is invalid
   45 | vector<data> E;
      |            ^
Main.cpp:45:12: error: template argument 2 is invalid
Main.cpp: In function 'void construct()':
Main.cpp:54:13: error: reference to 'data' is ambiguous
   54 |         for(data e : E) {
      |             ^~~~
/usr/include/c++/13/bits/range_access.h:346:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  346 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:336:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  336 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:325:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  325 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:314:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  314 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
Main.cpp:36:8: note:                 'struct data'
   36 | struct data {
      |        ^~~~
Main.cpp:63:1: error: expected primary-expression before '}' token
   63 | }
      | ^
Main.cpp:62:10: error: expected ';' before '}' token
   62 |         }
      |          ^
      |          ;
   63 | }
      | ~         
Main.cpp:63:1: error: expected primary-expression before '}' token
   63 | }
      | ^
Main.cpp:62:10: error: expected ')' before '}' token
   62 |         }
      |          ^
      |          )
   63 | }
      | ~         
Main.cpp:54:12: note: to match this '('
   54 |         for(data e : E) {
      |            ^
Main.cpp:63:1: error: expected primary-expression before '}' token
   63 | }
      | ^
Main.cpp: In function 'void build_krt()':
Main.cpp:13:12: error: request for member 'push_back' in 'E', which is of non-class type 'int'
   13 | #define pb push_back
      |            ^~~~~~~~~
Main.cpp:96:35: note: in expansion of macro 'pb'
   96 |                                 E.pb(data(i * m + j, x * m + y, max(a[i][j], a[x][y])));
      |                                   ^~
Main.cpp:96:38: error: reference to 'data' is ambiguous
   96 |                                 E.pb(data(i * m + j, x * m + y, max(a[i][j], a[x][y])));
      |                                      ^~~~
/usr/include/c++/13/bits/range_access.h:346:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  346 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:336:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  336 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:325:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  325 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:314:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  314 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
Main.cpp:36:8: note:                 'struct data'
   36 | struct data {
      |        ^~~~
Main.cpp:14:18: error: request for member 'begin' in 'E', which is of non-class type 'int'
   14 | #define all(v) v.begin(), v.end()
      |                  ^~~~~
Main.cpp:100:14: note: in expansion of macro 'all'
  100 |         sort(all(E), cmp);
      |              ^~~
Main.cpp:14:29: error: request for member 'end' in 'E', which is of non-class type 'int'
   14 | #define all(v) v.begin(), v.end()
      |                             ^~~
Main.cpp:100:14: note: in expansion of macro 'all'
  100 |         sort(all(E), cmp);
      |              ^~~
Main.cpp: In function 'void build_next()':
Main.cpp:13:12: error: request for member 'push_back' in 'E', which is of non-class type 'int'
   13 | #define pb push_back
      |            ^~~~~~~~~
Main.cpp:123:27: note: in expansion of macro 'pb'
  123 |                         E.pb(data(i * m + j, 1, a[i][j]));
      |                           ^~
Main.cpp:123:30: error: reference to 'data' is ambiguous
  123 |                         E.pb(data(i * m + j, 1, a[i][j]));
      |                              ^~~~
/usr/include/c++/13/bits/range_access.h:346:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  346 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:336:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  336 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:325:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  325 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:314:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  314 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
Main.cpp:36:8: note:                 'struct data'
   36 | struct data {
      |        ^~~~
Main.cpp:13:12: error: request for member 'push_back' in 'E', which is of non-class type 'int'
   13 | #define pb push_back
      |            ^~~~~~~~~
Main.cpp:124:27: note: in expansion of macro 'pb'
  124 |                         E.pb(data(i * m + j, -1, a[i][j] + L));
      |                           ^~
Main.cpp:124:30: error: reference to 'data' is ambiguous
  124 |                         E.pb(data(i * m + j, -1, a[i][j] + L));
      |                              ^~~~
/usr/include/c++/13/bits/range_access.h:346:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  346 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:336:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  336 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:325:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  325 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:314:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  314 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
Main.cpp:36:8: note:                 'struct data'
   36 | struct data {
      |        ^~~~
Main.cpp:14:18: error: request for member 'begin' in 'E', which is of non-class type 'int'
   14 | #define all(v) v.begin(), v.end()
      |                  ^~~~~
Main.cpp:127:14: note: in expansion of macro 'all'
  127 |         sort(all(E), cmp);
      |              ^~~
Main.cpp:14:29: error: request for member 'end' in 'E', which is of non-class type 'int'
   14 | #define all(v) v.begin(), v.end()
      |                             ^~~
Main.cpp:127:14: note: in expansion of macro 'all'
  127 |         sort(all(E), cmp);
      |              ^~~
Main.cpp:128:13: error: reference to 'data' is ambiguous
  128 |         for(data e : E) {
      |             ^~~~
/usr/include/c++/13/bits/range_access.h:346:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  346 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:336:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  336 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:325:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  325 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:314:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  314 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
Main.cpp:36:8: note:                 'struct data'
   36 | struct data {
      |        ^~~~
Main.cpp:16:22: error: expected primary-expression before 'for'
   16 | #define rep(i, x, y) for(int i = x, _y = y; i <= _y; i++)
      |                      ^~~
Main.cpp:144:9: note: in expansion of macro 'rep'
  144 |         rep(k, 1, 18) rep(i, 0, n - 1) rep(j, 0, m - 1) {
      |         ^~~
Main.cpp:143:10: error: expected ';' before 'for'
  143 |         }
      |          ^
      |          ;
Main.cpp:16:22: error: expected primary-expression before 'for'
   16 | #define rep(i, x, y) for(int i = x, _y = y; i <= _y; i++)
      |                      ^~~
Main.cpp:144:9: note: in expansion of macro 'rep'
  144 |         rep(k, 1, 18) rep(i, 0, n - 1) rep(j, 0, m - 1) {
      |         ^~~
Main.cpp:143:10: error: expected ')' before 'for'
  143 |         }
      |          ^
      |          )
Main.cpp:128:12: note: to match this '('
  128 |         for(data e : E) {
      |            ^
Main.cpp: In function 'int main()':
Main.cpp:162:11: error: request for member 'clear' in 'E', which is of non-class type 'int'
  162 |         E.clear();
      |           ^~~~~
Main.cpp: In function 'void file()':
Main.cpp:21:44: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin);freopen(name".out", "w", stdout);}
      |                                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:21:76: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin);freopen(name".out", "w", stdout);}
      |                                                                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:25:52: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |                 if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin);freopen(name".out", "w", stdout);}
      |                                             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:25:84: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |                 if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin);freopen(name".out", "w", stdout);}
      |                                                                             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~