diff --git a/Codeforces/CodeforcesRound720(Div2)/CNastiaandaHiddenPermutation/solution.cpp b/Codeforces/CodeforcesRound720(Div2)/CNastiaandaHiddenPermutation/solution.cpp new file mode 100644 index 0000000..3d27f6e --- /dev/null +++ b/Codeforces/CodeforcesRound720(Div2)/CNastiaandaHiddenPermutation/solution.cpp @@ -0,0 +1,437 @@ + +#pragma GCC optimize("Ofast") +///////////////////////////////////////////////////////// +/** + * Useful Macros + * by subcrip + * (requires C++17) + */ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::value +#define __DECOMPOSE_S(a, x) auto x = a; +#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; +constexpr void __() {} +#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() +#define __as_typeof(container) decltype(container)::value_type + +/* type aliases */ +using ll = int64_t; +using ull = uint64_t; +using pii = pair; +using pil = pair; +using pli = pair; +using pll = pair; + +/* constants */ +constexpr int INF = 0x3f3f3f3f; +constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; +constexpr ll MDL = 1e9 + 7; +constexpr ll PRIME = 998'244'353; +constexpr ll MDL1 = 8784491; +constexpr ll MDL2 = PRIME; + +/* random */ + +mt19937 rd(chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count()); + +/* bit-wise operations */ +#define lowbit(x) ((x) & -(x)) +#define popcount(x) (__builtin_popcountll(ll(x))) +#define parity(x) (__builtin_parityll(ll(x))) +#define msp(x) (63LL - __builtin_clzll(ll(x))) +#define lsp(x) (__builtin_ctzll(ll(x))) + +/* arithmetic operations */ +#define mod(x, y) ((((x) % (y)) + (y)) % (y)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1))) +#define u1(p) ((p) >> 32) +#define u2(p) ((p) & ((1ULL << 32) - 1)) +#define ult std::less +#define ugt std::greater + +#define ipair ull +#define imake(x, y) (umake(x, y)) +#define i1(p) (int(u1(ll(p)))) +#define i2(p) (ll(u2(p) << 32) >> 32) +struct ilt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) < i2(b); + else return i1(a) < i1(b); + } +}; +struct igt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) > i2(b); + else return i1(a) > i1(b); + } +}; + +/* conditions */ +#define loop while (1) +#define if_or(var, val) if (!(var == val)) var = val; else +#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;) +#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;) + +/* hash */ +struct safe_hash { + // https://codeforces.com/blog/entry/62393 + static uint64_t splitmix64(uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator()(uint64_t x) const { + static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64(x + FIXED_RANDOM); + } +}; + +struct pair_hash { + template + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +/* build data structures */ +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];) +#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];) +#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);) +#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};) +#define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} + +/* io */ +#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL)) +template void __read(T& x) { cin >> x; } +template void __read(T& x, U&... args) { cin >> x; __read(args...); } +#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);) +#define readvec(type, a, n) __AS_PROCEDURE(vector a(n); for (int i = 0; i < (n); ++i) cin >> a[i];) +#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;) +#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;) +template ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} + +/* pops */ +#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();) +#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();) +#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();) + +/* math */ +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +void __exgcd(ll a, ll b, ll& x, ll& y) { + if (b == 0) { + x = 1, y = 0; + return; + } + __exgcd(b, a % b, y, x); + y -= a / b * x; +} + +ll inverse(ll a, ll b) { + ll x, y; + __exgcd(a, b, x, y); + return mod(x, b); +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 && t[i] != t[j]) j = pi[j - 1]; + if (t[i] == t[j]) j++; + pi[i] = j; + } + return pi; +} +vector calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair prev = {1, -1}; + for (int i = 1; i < m; ++i) { + if (z[i - prev.first] + i <= prev.second) { + z.push_back(z[i - prev.first]); + } else { + int j = max(i, prev.second + 1); + while (j < m && t[j] == t[j - i]) ++j; + z.push_back(j - i); + prev = {i, j - 1}; + } + } + return z; +} +vector kmp(string s, string t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector lps = calc_next(cur); + for (int i = sz2 + 1; i <= sz1 + sz2; i++) { + if (lps[i] == sz2) v.push_back(i - 2 * sz2); + } + return v; +} +int period(string s) { // find the length of shortest recurring period + int n = s.length(); + auto z = calc_z(s); + for (int i = 1; i <= n / 2; ++i) { + if (n % i == 0 && z[i] == n - i) { + return i; + } + } + return n; +} +///////////////////////////////////////////////////////// + +// #define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 512 + +void dump() {} + +void prep() {} + +void solve() { + read(int, n); + // auto query_max =[&] (int i, int j, int val_i) -> int { + // if (val_i == n) { + // cout << "? 1 " << j << ' ' << i << ' ' << n - 1 << endl; + // } else { + // cout << "? 1 " << i << ' ' << j << ' ' << n - 1 << endl; + // } + // read(int, x); + // return x; + // }; + // auto query_min = [&] (int i, int j, int val_i) -> int { + // if (val_i == 1) { + // cout << "? 2 " << i << ' ' << j << ' ' << 1 << endl; + // } else { + // cout << "? 2 " << j << ' ' << i << ' ' << 1 << endl; + // } + // read(int, x); + // return x; + // }; + // auto query_custom = [] (int i, int j, int x) -> int { + // cout << "? 2 " << i << ' ' << j << ' ' << x << endl; + // read(int, res); + // return res; + // }; + // auto claim = [] (const vector& permu) -> void { + // cout << "! "; + // for (auto&& x : permu) cout << x << ' '; + // cout << endl; + // }; + // vector p(n); + // int prev_direction = 0; // 1 for upward, 0 for downward + // if (query_min(1, 2, 1) == 1) { + // p[0] = 1; + // } else if (query_max(1, 2, n) == n) { + // p[0] = n; + // } else { + // int mn = query_min(1, 2, 0), mx = query_max(1, 2, 0); + // if (query_custom(1, 2, mn) == mn) { + // p[0] = mn; + // } else { + // p[0] = mx; + // } + // } + // for (int i = 2; i <= n; ++i) { + // if (prev_direction == 0) { + // int mn = query_min(i - 1, i, p[i - 2]); + // if (mn == p[i - 2]) { + // p[i - 1] = query_max(i - 1, i, p[i - 2]); + // prev_direction = 1; + // } else { + // p[i - 1] = mn; + // } + // } else { + // int mx = query_max(i - 1, i, p[i - 2]); + // if (mx == p[i - 2]) { + // p[i - 1] = query_min(i - 1, i, p[i - 2]); + // prev_direction = 0; + // } else { + // p[i - 1] = mx; + // } + // } + // } + // claim(p); + auto query = [] (int type, int i, int j, int x) -> int { + cout << "? " << type << ' ' << i + 1 << ' ' << j + 1 << ' ' << x << endl; + read(int, res); + return res; + }; + auto claim = [] (const vector& permu) -> void { + cout << "! "; + for (auto&& x : permu) cout << x << ' '; + cout << endl; + }; + // auto detailed = [&] (int i, int j) -> pii { + // int x, y; + // if (query(2, i, j, 1) == 1) { + // x = 1; + // y = query(1, i, j, 1); + // } else if (query(1, j, i, n - 1) == n) { + // x = n; + // y = query(2, j, i, 1); + // } else { + // } + // return {x, y}; + // }; + auto detailed_ceil = [&] (int i, int j) -> int { + // who is n? + if (query(1, j, i, n - 1) == n) { + return i; + } else if (query(1, i, j, n - 1) == n) { + return j; + } + return -1; + }; + auto detailed_floor = [&] (int i, int j) -> int { + // who is 1? + if (query(2, i, j, 1) == 1) { + return i; + } else if (query(2, j, i, 1) == 1) { + return j; + } + return -1; + }; + vector p(n); + for (int i = 0; i + 1 < n; i += 2) { + int mx = query(1, i, i + 1, n - 1); + int mn = query(2, i, i + 1, 1); + if (mx >= n - 1) { + int who_is_n = detailed_ceil(i, i + 1); + if (who_is_n != -1) p[who_is_n] = n; + } + if (mn <= 2) { + int who_is_1 = detailed_floor(i, i + 1); + if (who_is_1 != -1) p[who_is_1] = 1; + } + if (p[i] == 1 && p[i + 1] == n) { + ;; + } else if (p[i] == 1 && p[i + 1] != n) { + p[i + 1] = mx; + } else if (p[i] == n && p[i + 1] == 1) { + ;; + } else if (p[i] == n && p[i + 1] != 1) { + p[i + 1] = mn; + } else if (p[i + 1] == n) { + p[i] = mn; + } else if (p[i + 1] == 1) { + p[i] = mx; + } else { + if (query(2, i, i + 1, mn) == mn) { + p[i] = mn, p[i + 1] = mx; + } else { + p[i] = mx, p[i + 1] = mn; + } + } + } + if (n % 2 == 1) { + int i = n - 2; + int mx = query(1, i, i + 1, n - 1); + int mn = query(2, i, i + 1, 1); + if (mx >= n - 1) { + int who_is_n = detailed_ceil(i, i + 1); + if (who_is_n != -1) p[who_is_n] = n; + } + if (mn <= 2) { + int who_is_1 = detailed_floor(i, i + 1); + if (who_is_1 != -1) p[who_is_1] = 1; + } + if (p[i] == 1 && p[i + 1] == n) { + ;; + } else if (p[i] == 1 && p[i + 1] != n) { + p[i + 1] = mx; + } else if (p[i] == n && p[i + 1] == 1) { + ;; + } else if (p[i] == n && p[i + 1] != 1) { + p[i + 1] = mn; + } else if (p[i + 1] == n) { + p[i] = mn; + } else if (p[i + 1] == 1) { + p[i] = mx; + } else { + if (query(2, i, i + 1, mn) == mn) { + p[i] = mn, p[i + 1] = mx; + } else { + p[i] = mx, p[i + 1] = mn; + } + } + } + claim(p); +} + +int main() { +#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__) + assert(false && "incompatible compiler variant detected."); +#endif + untie, cout.tie(NULL); + prep(); +#ifdef SINGLE_TEST_CASE + solve(); +#else + read(int, t); + for (int i = 0; i < t; ++i) { +#ifdef DUMP_TEST_CASE + if (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + solve(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/Codeforces/CodeforcesRound740(Div1,basedonVKCup2021Final(Engine))/CBottomTierReversals/solution.cpp b/Codeforces/CodeforcesRound740(Div1,basedonVKCup2021Final(Engine))/CBottomTierReversals/solution.cpp index 3d23aa8..a56dbfa 100644 --- a/Codeforces/CodeforcesRound740(Div1,basedonVKCup2021Final(Engine))/CBottomTierReversals/solution.cpp +++ b/Codeforces/CodeforcesRound740(Div1,basedonVKCup2021Final(Engine))/CBottomTierReversals/solution.cpp @@ -1,4 +1,6 @@ - +// #undef pragma +#define undef +#undef pragma #pragma GCC optimize("Ofast") ///////////////////////////////////////////////////////// /** diff --git a/src/bin/a.out b/src/bin/a.out index 3f175d5..f5414e6 100755 Binary files a/src/bin/a.out and b/src/bin/a.out differ diff --git a/src/bin/test.cc b/src/bin/test.cc index 90b693f..e652f19 100644 --- a/src/bin/test.cc +++ b/src/bin/test.cc @@ -1,326 +1,8 @@ - -#pragma GCC optimize("Ofast") -///////////////////////////////////////////////////////// -/** - * Useful Macros - * by subcrip - * (requires C++17) - */ - -#include -using namespace std; - -/* macro helpers */ -#define __NARGS(...) std::tuple_size::value -#define __DECOMPOSE_S(a, x) auto x = a; -#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; -constexpr void __() {} -#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() -#define __as_typeof(container) decltype(container)::value_type - -/* type aliases */ -using ll = int64_t; -using ull = uint64_t; -using pii = pair; -using pil = pair; -using pli = pair; -using pll = pair; - -/* constants */ -constexpr int INF = 0x3f3f3f3f; -constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; -constexpr ll MDL = 1e9 + 7; -constexpr ll PRIME = 998'244'353; -constexpr ll MDL1 = 8784491; -constexpr ll MDL2 = PRIME; - -/* random */ - -mt19937 rd(chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count()); - -/* bit-wise operations */ -#define lowbit(x) ((x) & -(x)) -#define popcount(x) (__builtin_popcountll(ll(x))) -#define parity(x) (__builtin_parityll(ll(x))) -#define msp(x) (63LL - __builtin_clzll(ll(x))) -#define lsp(x) (__builtin_ctzll(ll(x))) - -/* arithmetic operations */ -#define mod(x, y) ((((x) % (y)) + (y)) % (y)) - -/* fast pairs */ -#define upair ull -#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1))) -#define u1(p) ((p) >> 32) -#define u2(p) ((p) & ((1ULL << 32) - 1)) -#define ult std::less -#define ugt std::greater - -#define ipair ull -#define imake(x, y) (umake(x, y)) -#define i1(p) (int(u1(ll(p)))) -#define i2(p) (ll(u2(p) << 32) >> 32) -struct ilt { - bool operator()(const ipair& a, const ipair& b) const { - if (i1(a) == i1(b)) return i2(a) < i2(b); - else return i1(a) < i1(b); - } -}; -struct igt { - bool operator()(const ipair& a, const ipair& b) const { - if (i1(a) == i1(b)) return i2(a) > i2(b); - else return i1(a) > i1(b); - } -}; - -/* conditions */ -#define loop while (1) -#define if_or(var, val) if (!(var == val)) var = val; else -#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;) -#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;) - -/* hash */ -struct safe_hash { - // https://codeforces.com/blog/entry/62393 - static uint64_t splitmix64(uint64_t x) { - // http://xorshift.di.unimi.it/splitmix64.c - x += 0x9e3779b97f4a7c15; - x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; - x = (x ^ (x >> 27)) * 0x94d049bb133111eb; - return x ^ (x >> 31); - } - - size_t operator()(uint64_t x) const { - static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); - return splitmix64(x + FIXED_RANDOM); - } -}; - -struct pair_hash { - template - size_t operator()(const pair& a) const { - auto hash1 = safe_hash()(a.first); - auto hash2 = safe_hash()(a.second); - if (hash1 != hash2) { - return hash1 ^ hash2; - } - return hash1; - } -}; - -/* build data structures */ -#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];) -#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];) -#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);) -#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};) -#define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) -#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) -#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) -template pair> discretize(Iterator __first, Iterator __last) { - set st(__first, __last); - size_t N = 0; - map mp; - for (auto&& x : st) mp[x] = ++N; - return {N, mp}; -} -template pair> unordered_discretize(Iterator __first, Iterator __last) { - set st(__first, __last); - size_t N = 0; - unordered_map mp; - for (auto&& x : st) mp[x] = ++N; - return {N, mp}; -} - -/* io */ -#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL)) -template void __read(T& x) { cin >> x; } -template void __read(T& x, U&... args) { cin >> x; __read(args...); } -#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);) -#define readvec(type, a, n) __AS_PROCEDURE(vector a(n); for (int i = 0; i < (n); ++i) cin >> a[i];) -#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;) -#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) -#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;) -template ostream& operator<<(ostream& out, const pair& p) { - out << "{" << p.first << ", " << p.second << "}"; - return out; -} -template -void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { - using swallow = int[]; // guaranties left to right order - (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; -} -template -decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { - os << "{"; - print_tuple_impl(os, t, std::index_sequence_for{}); - return os << "}"; -} -template ostream& operator<<(ostream& out, const vector& vec) { - for (auto&& i : vec) out << i << ' '; - return out; -} - -/* pops */ -#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();) -#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();) -#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();) - -/* math */ -constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } - -void __exgcd(ll a, ll b, ll& x, ll& y) { - if (b == 0) { - x = 1, y = 0; - return; - } - __exgcd(b, a % b, y, x); - y -= a / b * x; -} - -ll inverse(ll a, ll b) { - ll x, y; - __exgcd(a, b, x, y); - return mod(x, b); -} - -/* string algorithms */ -vector calc_next(string t) { // pi function of t - int n = (int)t.length(); - vector pi(n); - for (int i = 1; i < n; i++) { - int j = pi[i - 1]; - while (j > 0 && t[i] != t[j]) j = pi[j - 1]; - if (t[i] == t[j]) j++; - pi[i] = j; - } - return pi; -} -vector calc_z(string t) { // z function of t - int m = t.length(); - vector z; - z.push_back(m); - pair prev = {1, -1}; - for (int i = 1; i < m; ++i) { - if (z[i - prev.first] + i <= prev.second) { - z.push_back(z[i - prev.first]); - } else { - int j = max(i, prev.second + 1); - while (j < m && t[j] == t[j - i]) ++j; - z.push_back(j - i); - prev = {i, j - 1}; - } - } - return z; -} -vector kmp(string s, string t) { // find all t in s - string cur = t + '#' + s; - int sz1 = s.size(), sz2 = t.size(); - vector v; - vector lps = calc_next(cur); - for (int i = sz2 + 1; i <= sz1 + sz2; i++) { - if (lps[i] == sz2) v.push_back(i - 2 * sz2); - } - return v; -} -int period(string s) { // find the length of shortest recurring period - int n = s.length(); - auto z = calc_z(s); - for (int i = 1; i <= n / 2; ++i) { - if (n % i == 0 && z[i] == n - i) { - return i; - } - } - return n; -} -///////////////////////////////////////////////////////// - -#define SINGLE_TEST_CASE -// #define DUMP_TEST_CASE 512 - -struct LCA { - vector depth; - vector> pa; - LCA(const vector>& g, int root = 1) { - int n = g.size() - 1; - int m = 32 - __builtin_clz(n); - depth.resize(n + 1); - pa.resize(n + 1, vector(m, -1)); - function dfs = [&](int x, int fa) { - pa[x][0] = fa; - for (int y: g[x]) { - if (y != fa) { - depth[y] = depth[x] + 1; - dfs(y, x); - } - } - }; - dfs(root, 0); - - for (int i = 0; i < m - 1; i++) - for (int x = 1; x <= n; x++) - if (int p = pa[x][i]; p != -1) - pa[x][i + 1] = pa[p][i]; - } - - int get_kth_ancestor(int node, int k) { - for (; k; k &= k - 1) - node = pa[node][__builtin_ctz(k)]; - return node; - } - - int query(int x, int y) { - if (depth[x] > depth[y]) - swap(x, y); - y = get_kth_ancestor(y, depth[y] - depth[x]); - if (y == x) - return x; - for (int i = pa[x].size() - 1; i >= 0; i--) { - int px = pa[x][i], py = pa[y][i]; - if (px != py) { - x = px; - y = py; - } - } - return pa[x][0]; - } -}; - - -void dump() {} - -void prep() {} - -void solve() { - read(int, n, m, s); - adj(ch, n); - for (int i = 0; i < n - 1; ++i) { - read(int, u, v); - edge(ch, u, v); - } - LCA model(ch, s); - while (m--) { - read(int, u, v); - cout << model.query(u, v) << '\n'; - } -} +#include +#define define undef +#define a 5 int main() { - untie, cout.tie(NULL); - prep(); -#ifdef SINGLE_TEST_CASE - solve(); -#else - read(int, t); - for (int i = 0; i < t; ++i) { -#ifdef DUMP_TEST_CASE - if (i + 1 == (DUMP_TEST_CASE)) { - dump(); - } else { - solve(); - } -#else - solve(); -#endif - } -#endif + using namespace std; + cout << a << endl; }