text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; #define all(V) V.begin(),V.end() using ll = long long; ll d3[31]; ll mh(ll a, ll b, ll c, ll d) { return abs(a - c) + abs(b - d); } int main() { d3[0] = 1; for (int i = 1;i < 31;i++) { d3[i] = d3[i - 1] * 3; } ll Q, a, b, c, d, A, B, C, D, ans; cin >> Q; for (int i = 0;i < Q;i++) { cin >> a >> b >> c >> d; a--;b--;c--;d--; ans = -1; if (abs(a - c) < abs(b - d)) { swap(a, b); swap(c, d); } if (a > c) { swap(a, c); swap(b, d); } if (b > d) { b = d3[30] - 1LL - b; d = d3[30] - 1LL - d; } //cout << a << " " << b << " " << c << " " << d << endl; for (int j = 29;j >= 0;j--) { A = a / d3[j] * d3[j] + d3[j]; B = b / d3[j] * d3[j]; //cout << A << " " << B << endl; if ((B / d3[j]) % 3 == 1) { if ((A / d3[j]) % 3 == 0) { A += d3[j]; } else if ((A / d3[j]) % 3 == 2) { A += d3[j] * 2; } if (d < B + d3[j] && A + d3[j] <= c) { ans = min( mh(a, b, A - 1, B - 1) + mh(A - 1, B - 1, A + d3[j], B - 1) + mh(A + d3[j], B - 1, c, d), mh(a, b, A - 1, B + d3[j]) + mh(A - 1, B + d3[j], A + d3[j], B + d3[j]) + mh(A + d3[j], B + d3[j], c, d)); cout << ans << endl; break; } } } if (ans < 0) { cout << mh(a, b, c, d) << endl; } } }
#include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #include <climits> #include <cstring> #include <cassert> #define rep(i, m, n) for(int i=int(m);i<int(n);i++) #define all(c) begin(c),end(c) template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } typedef long long int ll; using ll = long long int; using ull = long long unsigned int; using Int = long long int; using Double = long double; using namespace std; #define INF (1 << 30) - 1 #define INFl (ll)5e15 #define DEBUG 0 #define dump(x) cerr << #x << " = " << (x) << endl #define MOD 1000000007 //edit //template<typename T> Int pow(Int n, Int k) { Int ret = 1; while (k--) ret *= n; return ret; } //template<typename T> //T abs(T a) { // if (a < 0) return -a; // return a; //} class Solve { public: Int dist(Int x1, Int y1, Int x2, Int y2) { return abs(x1 - x2) + abs(y1 - y2); } // 3^l x 3^kの正方形を考える Int rec2(Int l, Int k, Int x1, Int y1, Int x2, Int y2) { // if (l == 0) { // // } Int br = pow(3ll, l - 1); Int r1 = x1 / br, c1 = y1 / br; Int r2 = x2 / br, c2 = y2 / br; if (r1 != r2) { Int ret = abs(x1 - x2) + abs(y1 - y2); return ret; } if (r1 == 1 && abs(c1 - c2) >= 2) { Int p = pow(3ll, l - 1) - 1; Int q = pow(3ll, l - 1) * 2; // Int alpha = min(abs(p - x1) + abs(p - x2), abs(q - x1) + abs(q - x2)); Int alpha = 2ll * min(min(abs(p - x1), abs(p - x2)), min(abs(q - x1), abs(q - x2))); Int ret = dist(x1, y1, x2, y2) + alpha; return ret; } if (l == 1) { Int ret = dist(x1, y1, x2, y2); if (r1 == 1 && abs(c1 - c2) >= 2) { ret += 2; } return ret; } Int ret = rec2(l - 1, k, x1 % br, y1, x2 % br, y2); return ret; } Int rec(Int k, Int x1, Int y1, Int x2, Int y2) { Int br = pow(3ll, k - 1); //それぞれの区画の大きさ Int r1 = x1 / br, c1 = y1 / br; Int r2 = x2 / br, c2 = y2 / br; if (r1 == r2 && c1 == c2) { Int ret = rec(k - 1, x1 % br, y1 % br, x2 % br, y2 % br); return ret; } else if (r1 != r2 && c1 != c2) { Int ret = dist(x1, y1, x2, y2); return ret; } else { if (c1 == c2) { Int ret = rec(k, y1, x1, y2, x2); return ret; } if (r1 == 1) { Int p = pow(3ll, k - 1) - 1; Int q = pow(3ll, k - 1) * 2; // Int alpha = min(abs(p - x1) + abs(p - x2), abs(q - x1) + abs(q - x2)); Int alpha = 2ll * min(min(abs(p - x1), abs(p - x2)), min(abs(q - x1), abs(q - x2))); Int ret = dist(x1, y1, x2, y2) + alpha; return ret; } if (k == 1) { Int ret = dist(x1, y1, x2, y2); // if (r1 == 1 && abs(c1 - c2) >= 2) { // ret += 2; // } return ret; } Int ret = rec2(k - 1, k, x1 % br, y1, x2 % br, y2); return ret; } } void solve() { Int Q; cin >> Q; while (Q--) { Int a, b, c, d; cin >> a >> b >> c >> d; a--, b--, c--, d--; Int ans = rec(30, a, b, c, d); cout << ans << endl; } } }; class Tester { public: Int sz; vector<vector<Int>> field; vector<vector<vector<vector<Int>>>> dist; Tester(Int k) { sz = pow(3ll, k); field.resize(sz, vector<Int>(sz, 0)); dist.resize(sz, vector<vector<vector<Int>>>(sz, vector<vector<Int>>(sz, vector<Int>(sz, INFl)))); rec(k, 0, 0); for (int ix = 0; ix < sz; ++ix) { for (int iy = 0; iy < sz; ++iy) { for (int jx = 0; jx < sz; ++jx) { for (int jy = 0; jy < sz; ++jy) { if (is_floor(ix, iy) && is_floor(jx, jy) && Solve().dist(ix, iy, jx, jy) == 1) { dist[ix][iy][jx][jy] = 1; dist[jx][jy][ix][iy] = 1; } } } } } for (int kx = 0; kx < sz; ++kx) { for (int ky = 0; ky < sz; ++ky) { for (int ix = 0; ix < sz; ++ix) { for (int iy = 0; iy < sz; ++iy) { for (int jx = 0; jx < sz; ++jx) { for (int jy = 0; jy < sz; ++jy) { // if (is_floor(ix, iy) && is_floor(jx, jy)) { // dist[ix][iy][jx][jy] = 1; // dist[jx][jy][ix][iy] = 1; // } chmin(dist[ix][iy][jx][jy], dist[ix][iy][kx][ky] + dist[kx][ky][jx][jy]); } } } } } } } bool is_floor(int x, int y) { return !field[x][y]; } // sx, syから始まり、3 ^ k の正方形で色塗り void rec(Int k, Int sx, Int sy) { if (k == 0) return; Int br = pow(3ll, k - 1); for (int r = 0; r < 3; ++r) { for (int c = 0; c < 3; ++c) { if (r == 1 && c == 1) { for (int i = 0; i < br; ++i) { for (int j = 0; j < br; ++j) { field[br + sx + i][br + sy + j] = 1; } } } else { rec(k - 1, sx + r * br, sy + c * br); } } } } void print_field() { for (auto line : field) { for (auto e : line) { if (e == 0) cout << '.'; else cout << '#'; } cout << endl; } } bool test() { Int a = rand() % sz, b = rand() % sz, c = rand() % sz, d = rand() % sz; while (!is_floor(a, b)) { a = rand() % sz; b = rand() % sz; } while (!is_floor(c, d) || (a == c && b == d)) { c = rand() % sz; d = rand() % sz; } cout << a << " " << b << " " << c << " " << d << endl; Int ans = Solve().rec(30, a, b, c, d); Int bf = dist[a][b][c][d]; cout << ans << " " << bf << endl; cout << endl; return ans == bf; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); // pow(3,5) // for (int i = 0; i < 60; ++i) { // cout << pow(2ll, (Int) i) << endl; // } bool debug = true; debug = false; if (debug) { Tester tester(3); // Int Q; // cin >> Q; // while (Q--) { // // Int a, b, c, d; // cin >> a >> b >> c >> d; // a--, b--, c--, d--; // // Int ans = Solve().rec(30, a, b, c, d); // // Int bf = tester.dist[a][b][c][d]; // // cout << ans << " " << bf << endl; // } while (tester.test()); return 0; } Solve().solve(); return 0; }
#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<functional> #include<vector> #include<queue> #include<stack> #include<set> #include<map> using namespace std; #define MOD 1000000007 #define f(i,n) for(int i=0;i<(int)(n);i++) #define N 200000 #define K 205891132094649 int main() { int t; long long ww, xx, yy, zz; bool v; long long w, x, y, z; long long k,s, ans; scanf("%d", &t); f(tt, t) { scanf("%lld %lld %lld %lld", &ww, &xx, &yy, &zz); ww--; xx--; yy--; zz--; k = K; v = true; while (k>1) { k /= 3; w = ww / k; x = xx / k; y = yy / k; z = zz / k; if ((w == y) && (x == z))continue; if ((w != y) && (x != z))break; if (w != y) { swap(w, x); swap(y, z); swap(ww, xx); swap(yy, zz); } if (w % 3 != 1)continue; if (abs(x - z) > 1) { s = (ww%k) + (yy%k); s = min(s+2, (2 * k) - s); printf("%lld\n", abs(zz - xx) + s); v = false; break; } } if (v) { printf("%lld\n", abs(zz - xx) + abs(yy - ww)); } } return 0; }
#include "bits/stdc++.h" using namespace std; using ll = long long; using vll = vector<ll>; using vvll = vector<vector<ll>>; using vvvll = vector<vector<vector<ll>>>; using vvvvll = vector<vector<vector<vector<ll>>>>; using pl4 = pair<ll,ll>; using str = string; using vpl4 = vector<pair<ll,ll>>; #define sz size() #define be begin() #define en end() #define fi first #define se second #define pb push_back #define mp make_pair #define llin(x) ll (x);cin >>(x); #define stin(x) str (x);cin >>(x); #define vllin(x,n) vll (x)(n);FOR(i,0,n-1){cin >>(x)[i];} #define vllin2(a,b,n) vll (a)(n);vll (b)(n);FOR(i,0,n-1){cin >>(a)[i]>>(b)[i];} #define vllin3(a,b,c,n) vll (a)(n);vll (b)(n);vll (c)(n);FOR(i,0,n-1){cin >>(a)[i]>>(b)[i]>>(c)[i];} #define vlling(x,n) (x).assign(n,0);FOR(i,0,n-1){cin >>(x)[i];} #define vlling2(a,b,n) (a).assign(n,0);(b).assign(n,0);FOR(i,0,n-1){cin >>(a)[i]>>(b)[i];} #define vlling3(a,b,c,n) (a).assign(n,0);(b).assign(n,0);(c).assign(n,0);FOR(i,0,n-1){cin >>(a)[i]>>(b)[i]>>(c)[i];} #define vpl4in(x,n) vpl4 (x)((n),mp(0,0));FOR(i,0,n-1){cin >>x[i].fi>>x[i].se;} #define FOR(i,a,b) for(ll i = a ; i <= b ; i++) #define rFOR(i,b,a) for(ll i = a; i >= b ; i--) #define SORT(x) sort(x.be, x.en) #define rSORT(x) sort(x.rbegin(), x.rend()) #define say(x) cout<<(x); #define sal(x) cout<<(x)<<endl; #define says(x) cout<<(x)<<(' '); #define sas cout<<(' '); #define sayR(x) cout<<fixed<<setprecision(10)<<(x); #define salR(x) cout<<fixed<<setprecision(10)<<(x)<<endl; #define yn(a) cout <<((a)?"yes":"no")<<endl; #define Yn(a) cout <<((a)?"Yes":"No")<<endl; #define YN(a) cout <<((a)?"YES":"NO")<<endl; #define Imp(a) cout <<((a)?"Possible":"Impossible")<<endl; #define IMP(a) cout <<((a)?"POSSIBLE":"IMPOSSIBLE")<<endl; #define pow(a,b) ll(pow(a,b)) ll MOD=1000000007; ll INF=100000000000001; vector<ll> value; // ノードの値を持つ配列 ll N; // 葉の数 void update(ll i, ll x) { // i 番目の葉の値を x に変える i += N - 1; // i 番目の葉のノード番号 value[i] = x; while (i > 0) { i = (i - 1) / 2; // ノード i の親ノードの番号に変える value[i] = min(value[i * 2 + 1], value[i * 2 + 2]); // 左右の子の min を計算しなおす } } ll query(ll a, ll b, ll k, ll l, ll r) { // [a, b) の区間に対するクエリについて // ノード k (区間 [l, r) 担当)が答える if (r <= a || b <= l) return INF; // 区間が被らない場合は INF を返す if (a <= l && r <= b) return value[k]; // ノード k の担当範囲がクエリ区間 [a, b) // に完全に含まれる else { ll c1 = query(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子に値を聞く ll c2 = query(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子に値を聞く return min(c1, c2); // 左右の子の値の min を取る } } signed main(){ ll p=1; FOR(i,1,29){ p=p*3; } llin(q); ll a,b,c,d,s; FOR(i,0,q-1){ cin >>a>>b>>c>>d; a--;b--;c--;d--; s=p; while(true){ if((a/s==c/s)&&(abs(b/s-d/s)>1)&&(a/s%3==1)){ sal(abs(b-d)+min(2+a%s+c%s,s*2-a%s-c%s)) break; }else if((abs(a/s-c/s)>1)&&(b/s==d/s)&&(b/s%3==1)){ sal(abs(a-c)+min(2+b%s+d%s,s*2-b%s-d%s)) break; }else if(s==1||((a/s!=c/s)&&(b/s!=d/s))){ sal(abs(a-c)+abs(b-d)); break; }else if(a/s==c/s){ a=a%s;c=c%s; }else if(b/s==d/s){ b=b%s;d=d%s; } s=s/3; } } }
#include <iostream> #include <algorithm> using namespace std; typedef long long ll; using ll = long long; bool between(ll x, ll y1, ll y2){ if(y1 > y2) swap(y1, y2); y1++; y2--; if(!(y1 <= y2)) return false; if(x % 3 == 1){ if(y2 - y1 > 3) return true; for(ll y=y1;y<=y2;y++) if(y % 3 == 1) return true; } return false; } ll get_extra(ll x1, ll y1, ll x2, ll y2) { ll ans = 0; ll three = 1; for (int i = 0; i < 35; ++i) { if(x1 / three == x2 / three && between(x1 / three, y1 / three, y2 / three)) { ll tmp = min(min(x1 % three, x2 % three) + 1, three - max(x1 % three, x2 % three)); ans = max(ans, tmp); } three *= 3; } return ans; } ll get_dist(ll x1, ll y1, ll x2, ll y2){ return abs(x1 - x2) + abs(y1 - y2) + 2 * max(get_extra(x1, y1, x2, y2), get_extra(y1, x1, y2, x2)); } int main() { int Q; cin >> Q; while (Q--) { long long a; long long b; long long c; long long d; cin >> a >> b >> c >> d; cout << get_dist(a-1, b-1, c-1, d-1) << endl; } return 0; }
#include <bits/stdc++.h> #define lowbit(x) x&(-x) using namespace std; typedef long long ll; const int N = 5e4; const int M = 2e3; ll f[35]; void f_table(){ f[0] = 1; for(int i = 1;i <= 30;++i) f[i] = f[i-1]*3; return ; } int main(){ f_table(); int n; ll x1,y1,x2,y2; scanf("%d",&n); for(int t = 0;t < n;++t){ scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2); x1--;y1--;x2--;y2--; ll ans = 0,flag = 0,sx,sy,ex,ey; for(int i = 30;i >= 0;--i){ sx = x1/f[i],sy = y1/f[i],ex = x2/f[i],ey = y2/f[i]; if(sx != ex && sy != ey) break; if(sx == ex && sx % 3 == 1 && abs(sy-ey) > 1){ ll xl = x1/f[i]*f[i]-1; ll xr = xl+f[i]+1; ll dx = min(abs(xl-x1)+abs(xl-x2),abs(xr-x1)+abs(xr-x2)); ll dy = abs(y1-y2); ans = dx+dy; flag = 1; break; } if(sy == ey && sy % 3 == 1 && abs(sx-ex) > 1){ ll yl = y1/f[i]*f[i]-1; ll yr = yl+f[i]+1; ll dx = abs(x1-x2); ll dy = min(abs(yl-y1)+abs(yl-y2),abs(yr-y1)+abs(yr-y2)); ans = dx+dy; flag = 1; break; } } if(flag) printf("%lld\n",ans); else printf("%lld\n",abs(x1-x2)+abs(y1-y2)); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define x first #define y second #define pii pair<int , int> #define mp make_pair #define pb push_back #define sz(a) (int)(a.size()) const int INF = 1000 * 1000 * 1000 + 7; const ll LINF = INF * (ll)INF; const int mod = 998244353; const int MAX = 2000005; ll pw3[31]; bool in(ll x , int bit) { return (x / pw3[bit]) % 3 == 1; } ll f(ll x , ll bit) { return (x + pw3[bit]) / pw3[bit + 1]; } ll dist(ll a , ll b , ll c , ll d) { return abs(a - c) + abs(b - d); } ll solve(ll a, ll b, ll c, ll d) { for(int bit = 29; bit >= 0; bit--) { if(in(a , bit) && in(c , bit) && (a / pw3[bit]) == (c / pw3[bit]) && f(b , bit) != f(d, bit)) { if(b > d) swap(b , d); return min( dist(a , b , (a / pw3[bit]) * pw3[bit] + pw3[bit] , (b / pw3[bit]) * pw3[bit] + pw3[bit] - 1) + dist(c , d , (a / pw3[bit]) * pw3[bit] + pw3[bit] , (b / pw3[bit]) * pw3[bit] + pw3[bit] - 1) , dist(a , b , (a / pw3[bit]) * pw3[bit] - 1, (b / pw3[bit]) * pw3[bit] + pw3[bit] - 1) + dist(c , d , (a / pw3[bit]) * pw3[bit] - 1 , (b / pw3[bit]) * pw3[bit] + pw3[bit] - 1)); } } return -1; } int main() { ios_base::sync_with_stdio(0); pw3[0] = 1; for(int i = 1; i <= 30; i++) pw3[i] = pw3[i - 1] * 3; int q; cin >> q; while(q--) { ll a,b,c,d; cin >> a >> b >> c >> d; a--;b--;c--;d--; ll answer = solve(a , b , c , d); swap(a , b); swap(c , d); if(answer == -1) answer = solve(a , b , c , d); if(answer == -1) cout << abs(a - c) + abs(b - d) << endl; else cout << answer << endl; } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i,n)for((i)=0;(i)<(int)(n);(i)++) #define _abs(x) ((x)>0?(x):-(x)) bool between(ll x,ll y1,ll y2){ if(y1>y2)swap(y1,y2); y1++;y2--; if(!(y1<=y2))return false; while(x> 0){ if(x% 3 == 1){ if(y2-y1> 3)return true; for(ll y=y1;y<=y2;y++)if(y% 3 == 1)return true; } x/= 3;y1/= 3;y2/= 3; } return false; } ll get_extra(ll x1,ll y1,ll x2,ll y2){ ll ans= 0;int i;ll three= 1; REP(i,35){ if(x1/three==x2/three&&between(x1/three,y1/three,y2/three)){ ll tmp=min(min(x1%three,x2%three) + 1,three-max(x1%three,x2%three)); ans=max(ans,tmp); } three*= 3; } return ans; } ll get_dist(ll x1,ll y1,ll x2,ll y2){ return _abs(x1-x2) +_abs(y1-y2) + 2 *max(get_extra(x1,y1,x2,y2),get_extra(y1,x1,y2,x2)); } int main(){ int Q; cin >> Q; for(int i=0; i<Q; ++i){ ll a, b, c, d; cin>>a>>b>>c>>d; --a;--b;--c;--d; cout<< get_dist(a,b,c,d) << endl; } }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i=(a);i<(b);++i) #define rep(i, n) FOR(i, 0, n) #define whole(x) (x).begin(),(x).end() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) using ll = long long; using P = pair<int, int>; const int mod = 1000000007; ll solve(ll ai, ll aj, ll bi, ll bj) { if (aj>bj) { swap(ai, bi); swap(aj, bj); } ll s = 1; rep(i, 30) s *= 3; ll dist = abs(ai-bi) + abs(aj-bj); ll res = dist; while(1) { s /= 3; if (s==0) { break; } ll nai = ai/s; ll nbi = bi/s; if (nai!=nbi) { break; } ll naj = aj/s; ll nbj = bj/s; if (abs(naj-nbj)>1 && nai%3==1) { ll loss = 2 * (min(ai, bi) - s*nai + 1); loss = min(loss, 2*((1+nai)*s-max(ai, bi))); res = dist + loss; break; } } return res; } int main(){ int q; cin >> q; rep(qi, q) { ll ai, aj, bi, bj; cin >> ai >> aj >> bi >> bj; ai--; aj--; bi--; bj--; ll ans = solve(ai, aj, bi, bj); ans = max(ans, solve(aj, ai, bj, bi)); cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> calc(int64_t v){ vector<int> res; for(int k=0; k<30; k++){ res.push_back(v%3); v /= 3; } return res; } int64_t pw3(int k){ int64_t res = 1; while(k--) res *= 3; return res; } int main(){ int Q; cin >> Q; while(Q--){ int64_t x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--; y1--; x2--; y2--; vector<int> vx1 = calc(x1), vy1 = calc(y1), vx2 = calc(x2), vy2 = calc(y2); int64_t dist = abs(x1-x2) + abs(y1-y2); int64_t ext = 0; for(int k=29; k>=0; k--){ if(vx1[k] != vx2[k]) break; int64_t B = pw3(k); if(vx1[k] == 1 && (y1+B)/3/B != (y2+B)/3/B){ int64_t L1 = x1 / B * B - 1; int64_t L2 = L1 + 1 + B; ext = max(ext, 2*min(L2-max(x1, x2), min(x1, x2)-L1)); } } for(int k=29; k>=0; k--){ if(vy1[k] != vy2[k]) break; int64_t B = pw3(k); if(vy1[k] == 1 && (x1+B)/3/B != (x2+B)/3/B){ int64_t L1 = y1 / B * B - 1; int64_t L2 = L1 + 1 + B; ext = max(ext, 2*min(L2-max(y1, y2), min(y1, y2)-L1)); } } cout << dist+ext << endl; } return 0; }
#include<bits/stdc++.h> #define LL long long using namespace std; LL poww[35],a,b,c,d,ans; int main() { int T; poww[0]=1; for(int i=1;i<=30;++i) poww[i]=poww[i-1]*3; scanf("%d",&T); while(T--) { int flag=1; scanf("%lld %lld %lld %lld",&a,&b,&c,&d); a--,b--,c--,d--,ans=0; for(int k=30;k>=0;--k) { LL sx=a/poww[k],sy=b/poww[k],tx=c/poww[k],ty=d/poww[k]; if(sx!=tx&&sy!=ty) break; if(sx==tx&&sx%3==1&&abs(sy-ty)>1) { LL xl=a/poww[k]*poww[k]-1,xr=xl+poww[k]+1; LL dy=abs(b-d),dx=min(abs(xl-a)+abs(xl-c),abs(xr-a)+abs(xr-c)); ans=dx+dy; flag=0; break; } if(sy==ty&&sy%3==1&&abs(sx-tx)>1) { LL yl=b/poww[k]*poww[k]-1,yr=yl+poww[k]+1; LL dx=abs(a-c),dy=min(abs(yl-b)+abs(yl-d),abs(yr-b)+abs(yr-d)); ans=dx+dy; flag=0; break; } } if(flag) printf("%lld\n",abs(a-c)+abs(b-d)); else printf("%lld\n",ans); } }
#include<bits/stdc++.h> #define rgi register int #define ll long long using namespace std; ll a,b,c,d; inline ll solve() { --a,--b,--c,--d; ll res=abs(a-c)+abs(b-d),t=1LL; for(rgi i=1;i<=33;++i) t*=3LL; for(rgi i=33;i>=0;--i) { for(rgi j=0;j<2;++j) { if(a/t==c/t&&(a/t)%3LL==1LL&&abs(b/t-d/t)!=1LL&&abs(b-d)>=t) { ll mn=std::min(a,c),mx=std::max(a,c); return res+std::min(mn-mn/t*t+1LL,(mx/t+1LL)*t-mx)*2LL; } std::swap(a,b),std::swap(c,d); } t/=3LL; } return res; } signed main() { rgi q; std::cin>>q; while(q--) { std::cin>>a>>b>>c>>d; std::cout<<solve()<<std::endl; } return 0; } // ---------------------------- // by imzzy
#include <iostream> #include <cmath> using namespace std; long long BZ[35]; long long work(long long x1, long long y1, long long x2, long long y2, int bz){ if(bz == 0) return abs(y1 - y2); long long Tempbz = BZ[bz - 1]; if(x1 / Tempbz != x2 / Tempbz) return abs(x1 - x2) + abs(y1 - y2); if(x1 / Tempbz == 1 && abs(y1 / Tempbz - y2 / Tempbz) >= 2) return abs(y1 - y2) + min(x1 % Tempbz + x2 % Tempbz + 2, (Tempbz - (x1 % Tempbz)) + (Tempbz - (x2 % Tempbz))); return work(x1 % Tempbz, y1, x2 % Tempbz, y2, bz - 1); } int main(){ BZ[0] = 1; for(int i = 1; i <= 29; i++) BZ[i] = BZ[i - 1] * 3; long long q, x1, y1, x2, y2; cin >> q; while(q--){ cin >> x1 >> y1 >> x2 >> y2; x1--, y1--, x2--, y2--; if(abs(y1 - y2) < abs(x1 - x2)) swap(x1, y1), swap(x2, y2); cout << work(x1, y1, x2, y2, 30) << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int,int>; ll solve(ll ai, ll aj, ll bi, ll bj) { if (aj > bj) { swap(ai,bi); swap(aj,bj); } ll s = 1; rep(i,30) s *= 3; ll dist = abs(ai-bi) + abs(aj-bj); ll res = dist; while (1) { s /= 3; if (s <= 0) break; ll nai = ai/s, nbi = bi/s; if (nai != nbi) break; ll naj = aj/s, nbj = bj/s; if (nai%3 == 1 && abs(naj-nbj) > 1) { ll up = s*nai-1; ll bot = nai*s + s; ll loss = min(ai,bi) - up; loss = min(loss, bot - max(ai,bi)); res = dist + loss*2; break; } } return res; } int main() { int q; cin >> q; rep(qi,q) { ll ai,aj,bi,bj; cin >> ai >> aj >> bi >> bj; ai--; aj--; bi--; bj--; ll ans = 0; ans = max(ans, solve(ai,aj,bi,bj)); ans = max(ans, solve(aj,ai,bj,bi)); cout << ans << endl; } return 0; }
#include <bits/stdc++.h> #define PB push_back #define MP make_pair #define F first #define S second #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(),(x).end() #ifdef _DEBUG_ #define debug(...) printf(__VA_ARGS__) #else #define debug(...) (void)0 #endif using namespace std; typedef long long ll; typedef pair<int,int> PII; typedef vector<int> VI; ll ff(ll x1, ll y1, ll x2, ll y2) { ll r = 0; ll b = 1; for (int i = 1; i < 33; i++) { if (x1 % 3 == 1 && x2 % 3 == 1 && x1 == x2) { // if ((y1 % 3 == 0 && y2 % 3 == 2) || (y1 % 3 == 2 && y2 % 3 == 0)) { if (abs(y1 - y2) > 1) { r = b; } } x1 /= 3; y1 /= 3; x2 /= 3; y2 /= 3; b *= 3; } return r; } ll solve(ll x1, ll y1, ll x2, ll y2) { ll big = ff(x1, y1, x2, y2); debug("big %lld\n", big); ll ans = abs(x2 - x1) + abs(y2 - y1); if (big > 0) { x1 %= big; x2 %= big; ans += min(min(x1, x2) * 2 + 2, min(big - x1, big - x2) * 2); } return ans; } int main() { int Q; scanf("%d", &Q); while (Q--) { ll x1, y1, x2, y2; scanf("%lld%lld%lld%lld", &x1, &y1, &x2, &y2); x1--, y1--, x2--, y2--; ll r1 = solve(x1, y1, x2, y2); ll r2 = solve(y1, x1, y2, x2); printf("%lld\n", max(r1, r2)); } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using ld = long double; using P = pair<int, int>; ll solve(ll ai, ll aj, ll bi, ll bj) { if (aj > bj) { swap(ai, bi); swap(aj, bj); } ll s = 1; rep(i, 30) s *= 3; ll dist = abs(ai - bi) + abs(aj - bj); ll res = dist; while (1) { s /= 3; if (s <= 0) break; ll nai = ai / s, nbi = bi / s; if (nai != nbi) break; ll naj = aj / s, nbj = bj / s; if (nai % 3 == 1 && abs(naj - nbj) > 1) { ll up = s * nai - 1; ll bot = s * (nai + 1); ll loss = min(ai,bi) - up; loss = min(loss, bot - max(ai, bi)); res = dist + loss * 2; break; } } return res; } int main(){ int q; cin >> q; rep(qi, q) { ll ai, aj, bi, bj; cin >> ai >> aj >> bi >> bj; ai--; aj--; bi--; bj--; ll ans = 0; ans = max(ans, solve(ai, aj, bi, bj)); ans = max(ans, solve(aj, ai, bj, bi)); cout << ans << endl; } return 0; }
#include<bits/stdc++.h> #define rgi register int #define ll long long // using namespace std; ll a,b,c,d; inline ll solve() { --a,--b,--c,--d; ll res=std::abs(a-c)+std::abs(b-d),t=1LL; for(rgi i=1;i<=33;++i) t*=3LL; for(rgi i=33;i>=0;--i) { for(rgi j=0;j<2;++j) { if(a/t==c/t&&(a/t)%3LL==1LL&&std::abs(b/t-d/t)!=1LL&&std::abs(b-d)>=t) { ll mn=std::min(a,c),mx=std::max(a,c); return res+std::min(mn-mn/t*t+1LL,(mx/t+1LL)*t-mx)*2LL; } std::swap(a,b),std::swap(c,d); } t/=3LL; } return res; } signed main() { rgi q; std::cin>>q; while(q--) { std::cin>>a>>b>>c>>d; std::cout<<solve()<<std::endl; } return 0; } // ---------------------------- // by imzzy
#include<bits/stdc++.h> #define rgi register int #define ll long long #define int ll using namespace std; int a,b,c,d; inline ll solve() { --a,--b,--c,--d; int res=abs(a-c)+abs(b-d),t=1LL; for(rgi i=1;i<=33;++i) t*=3LL; for(rgi i=33;i>=0;--i) { for(rgi j=0;j<2;++j) { if(a/t==c/t&&(a/t)%3LL==1LL&&abs(b/t-d/t)!=1&&abs(b-d)>=t) { int mn=min(a,c),mx=max(a,c); return res+min(mn-mn/t*t+1LL,(mx/t+1LL)*t-mx)*2LL; } swap(a,b),swap(c,d); } t/=3LL; } return res; } signed main() { rgi q; cin>>q; while(q--) { cin>>a>>b>>c>>d; cout<<solve()<<endl; } return 0; } // ---------------------------- // by imzzy
#include <bits/stdc++.h> #include <iomanip> using namespace std; #define reps(i,s,n) for(int i = s; i < n; i++) #define rep(i,n) reps(i,0,n) #define Rreps(i,n,e) for(int i = n - 1; i >= e; --i) #define Rrep(i,n) Rreps(i,n,0) #define ALL(a) a.begin(), a.end() #define fi first #define se second typedef long long ll; typedef vector<ll> vec; typedef vector<vec> mat; ll N,M,H,W,K,Q,A,B; //const ll MOD = 998244353; const ll MOD = (1e+9) + 7; const ll INF = 1LL<<60; typedef pair<ll, ll> P; vec san(32, 1); int bit3_t(ll a, int t){ return (a % san[t + 1]) / san[t]; } bool between(ll x, ll y1, ll y2){ if(y1 > y2) swap(y1, y2); ++y1; --y2; if(!(y1 <= y2)) return false; while(x > 0){ if(x%3 == 1){ if(y2 - y1 > 3) return true; for(ll y = y1; y <= y2; ++y) if(y%3 == 1) return true; } x /= 3; y1 /= 3; y2 /= 3; } return false; } ll f(ll a, ll b, ll c, ll d){ ll res = 0; Rrep(i, 31){ /*if(bit3_t(b, i) == 1 && bit3_t(d, i) == 1){ swap(a, b); swap(c, d); }*/ if(a / san[i] == c / san[i]){ //if(abs(d - b) >= 2 * san[i] //|| abs((d % (san[i + 1])) / san[i] - (b % (san[i + 1])) / san[i]) == 2){ if(between(a / san[i], b / san[i], d / san[i])){ ll diff1 = a % san[i], diff2 = c % san[i]; res = max(res, min({diff1 + 1, san[i] - diff1, diff2 + 1, san[i] - diff2}) * 2); } } } return res; } int main() { cin>>Q; reps(i, 1, 32) san[i] = san[i-1] * 3; rep(i,Q){ vec v(4); rep(j,4){ cin>>v[j]; --v[j]; } ll res = abs(v[0] - v[2]) + abs(v[1] - v[3]); //cout<<res<<endl; cout<<res + max(f(v[0], v[1], v[2], v[3]), f(v[1], v[0], v[3], v[2]))<<endl; } }
#include <iostream> #include <vector> #include <algorithm> #define llint long long #define inf 2e18 using namespace std; llint Q; llint sx, sy, tx, ty; vector<llint> SX, SY, TX, TY; llint beki[35]; vector<llint> get(llint x) { vector<llint> ret; for(int i = 0; i < 30; i++){ ret.push_back(x%3); x /= 3; } reverse(ret.begin(), ret.end()); return ret; } llint dist(llint sx, llint sy, llint tx, llint ty) { return abs(sx-tx) + abs(sy-ty); } llint trip(llint sx, llint sy, llint tx, llint ty, llint level) { llint ret = inf; llint size = beki[29-level]; llint px = tx/size*size, py = ty/size*size; ret = min(ret, dist(sx, sy, px-1, py-1) + dist(px-1, py-1, tx, ty)); ret = min(ret, dist(sx, sy, px+size, py-1) + dist(px+size, py-1, tx, ty)); ret = min(ret, dist(sx, sy, px-1, py+size) + dist(px-1, py+size, tx, ty)); ret = min(ret, dist(sx, sy, px+size, py+size) + dist(px+size, py+size, tx, ty)); return ret; } llint calc(llint sx, llint sy, llint tx, llint ty) { if(sy > ty) swap(sy, ty); SX = get(sx), SY = get(sy); TX = get(tx), TY = get(ty); llint diff = 30; bool flag = false; for(int i = 0; i < 30; i++){ if(flag){ if(SY[i] == 2 && TY[i] == 0) continue; else{ diff = i; break; } } if(SY[i] != TY[i]){ if(!flag && TY[i] - SY[i] == 1){ flag = true; continue; } diff = i; break; } } llint level = -1; for(int i = 0; i < 30; i++){ if(SX[i] != TX[i]) return -1; if(SX[i] == 1){ if(diff <= i){ level = i; break; } } } if(level == -1) return -1; return trip(sx, sy, tx, ty, level); } int main(void) { beki[0] = 1; for(int i = 1; i <= 30; i++) beki[i] = beki[i-1] * 3; cin >> Q; for(int q = 0; q < Q; q++){ cin >> sx >> sy >> tx >> ty; sx--, sy--, tx--, ty--; llint ret = calc(sx, sy, tx, ty); if(ret != -1){ cout << ret << endl; continue; } swap(sx, sy), swap(tx, ty); ret = calc(sx, sy, tx, ty); if(ret != -1){ cout << ret << endl; continue; } cout << dist(sx, sy, tx, ty) << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long int; #define rep(i, n) for(int i=0; i<(int)(n); ++i) const ll s = pow(3, 29); ll solve(ll a1, ll a2, ll b1, ll b2) { if (a2 > b2) { swap(a1, b1); swap(a2, b2); } ll res = abs(a1-b1) + abs(a2-b2); for (ll ss = s; ss > 0; ss /= 3) { ll na1 = a1 / ss, nb1 = b1 / ss; if (na1 != nb1) break; ll na2 = a2 / ss, nb2 = b2 / ss; if (na1%3 == 1 && abs(nb2 - na2) > 1) { ll up = ss*na1 - 1; ll down = ss*(na1+1); ll loss = min(min(a1, b1)-up, down-max(a1, b1)); res += 2*loss; break; } } return res; } int main () { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int q; cin >> q; rep(i, q) { ll ai, aj, bi, bj; cin >> ai >> aj >> bi >> bj; --ai, --aj, --bi, --bj; ll ans = 0; ans = max(ans, solve(ai, aj, bi, bj)); ans = max(ans, solve(aj, ai, bj, bi)); cout << ans << endl; } }
#include<bits/stdc++.h> //ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); //clock_t start=clock();clock_t end=clock();cout<<(double)(end-start)/CLOCKS_PER_SEC<<endl; using namespace std; typedef long long ll; typedef unsigned long long ull; typedef unsigned int ui; typedef pair<int,int> pii; typedef pair<pii,int> ppii; typedef pair<int,pii> pipi; typedef pair<ll,ll> pll; typedef pair<pll,ll> ppll; typedef pair<ll,pll> plpl; typedef tuple<ll,ll,ll> tl; typedef pair<double,double> pdd; typedef vector<vector<ll>> mat; ll mod=1000000007; ll mod2=998244353; ll mod3=1000003; ll mod4=998244853; ll mod5=1000000009; ll inf=1LL<<60; double pi=3.141592653589793238462643383279L; double eps=1e-8; #define rep(i,m,n) for(int i=m;i<n;i++) #define rrep(i,n,m) for(int i=n;i>=m;i--) #define srep(itr,st) for(auto itr=st.begin();itr!=st.end();itr++) #define mrep(itr,mp) for(auto itr=mp.begin();itr!=mp.end();itr++) #define Max(a,b) a=max(a,b) #define Min(a,b) a=min(a,b) int dh[4]={1,-1,0,0}; int dw[4]={0,0,1,-1}; int ddh[8]={-1,-1,-1,0,0,1,1,1}; int ddw[8]={-1,0,1,-1,1,-1,0,1}; ll gcd(ll a,ll b){ if(a<b)swap(a,b); if(b==0)return a; if(a%b==0)return b; return gcd(b,a%b); } ll Pow(ll n,ll k){ ll ret=1; ll now=n; while(k>0){ if(k&1)ret*=now; now*=now; k/=2; } return ret; } ll beki(ll n,ll k,ll md){ ll ret=1; ll now=n; while(k>0){ if(k%2==1){ ret*=now; ret%=md; } now*=now; now%=md; k/=2; } return ret; } ll gyaku(ll n,ll md){ return beki(n,md-2,md); } ll popcount(ll n){ ll ret=0; ll u=n; while(u>0){ ret+=u%2; u/=2; } return ret; } int main(){ ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); pll han[31]; ll y=1; rep(i,1,31){ han[i]={y+1,y*2}; y*=3; } int q;cin>>q; rep(i,0,q){ ll a,b,c,d;cin>>a>>b>>c>>d; a--;b--;c--;d--; ll ans=abs(c-a)+abs(d-b); ll yy=y; rrep(j,30,1){ yy/=3; //if(j==2)cout<<aa<<" "<<bb<<" "<<cc<<" "<<dd<<endl; if(a/yy==c/yy&&(a/yy)%3==1&&abs(b/yy-d/yy)>=2){ ll aa=a%yy,cc=c%yy; Max(ans,abs(b-d)+min(1+aa+1+cc,yy-aa+yy-cc)); } if(b/yy==d/yy&&(b/yy)%3==1&&abs(a/yy-c/yy)>=2){ ll bb=b%yy,dd=d%yy; Max(ans,abs(a-c)+min(1+bb+1+dd,yy-bb+yy-dd)); } } cout<<ans<<endl; } }
#include <iostream> #include <algorithm> using namespace std; typedef long long ll; using ll = long long; bool between(ll x, ll y1, ll y2){ if(y1 > y2) swap(y1, y2); if(x % 3 == 1){ if(y2 - y1 >= 3) return true; for(ll y=y1;y<=y2;y++) if(y % 3 == 1) return true; } return false; } ll get_extra(ll x1, ll y1, ll x2, ll y2) { ll ans = 0; ll three = 1; for (int i = 0; i < 35; ++i) { if(x1 / three == x2 / three && between(x1 / three, y1 / three, y2 / three)) { ll tmp = min(min(x1 % three, x2 % three) + 1, three - max(x1 % three, x2 % three)); ans = max(ans, tmp); } three *= 3; } return ans; } ll get_dist(ll x1, ll y1, ll x2, ll y2){ return abs(x1 - x2) + abs(y1 - y2) + 2 * max(get_extra(x1, y1, x2, y2), get_extra(y1, x1, y2, x2)); } int main() { int Q; cin >> Q; while (Q--) { long long a; long long b; long long c; long long d; cin >> a >> b >> c >> d; cout << get_dist(a-1, b-1, c-1, d-1) << endl; } return 0; }
#include <iostream> #include <algorithm> using namespace std; typedef long long ll; using ll = long long; bool between(ll x, ll y1, ll y2){ if(y1 > y2) swap(y1, y2); y1++; y2--; if(!(y1 <= y2)) return false; while(x > 0){ if(x % 3 == 1){ if(y2 - y1 > 3) return true; for(ll y=y1;y<=y2;y++) if(y % 3 == 1) return true; } x /= 3; y1 /= 3; y2 /= 3; } return false; } ll get_extra(ll x1, ll y1, ll x2, ll y2) { ll ans = 0; ll three = 1; for (int i = 0; i < 35; ++i) { if(x1 / three == x2 / three && between(x1 / three, y1 / three, y2 / three)) { ll tmp = min(min(x1 % three, x2 % three) + 1, three - max(x1 % three, x2 % three)); ans = max(ans, tmp); } three *= 3; } return ans; } ll get_dist(ll x1, ll y1, ll x2, ll y2){ return abs(x1 - x2) + abs(y1 - y2) + 2 * max(get_extra(x1, y1, x2, y2), get_extra(y1, x1, y2, x2)); } int main() { int Q; cin >> Q; while (Q--) { long long a; long long b; long long c; long long d; cin >> a >> b >> c >> d; cout << get_dist(a-1, b-1, c-1, d-1) << endl; } return 0; }
#include <iostream> #include <vector> #include <map> #include <algorithm> #include <deque> using namespace std; using VI = vector <int> ; using VVI = vector <VI> ; using VLL = vector <long long>; using VVLL = vector <VLL>; using PLL = pair<long long, long long>; const int MAXD = 35; const long long INF = 1000000000LL * 1000000000LL; inline bool checkCross(long long dblk1, long long dblk2) { if (dblk2 - dblk1 >= 3) { return true; } else { for (long long y = dblk1+1; y < dblk2; ++y) { if (y % 3 == 1) { return true; } } } return false; } inline long long getDist(long long div, long long a, long long b, long long da, long long db) { long long blk1 = a / div; long long blk2 = b / div; long long dblk1 = da / div; long long dblk2 = db / div; if (blk1 == blk2 && blk1%3 == 1 && checkCross(min(dblk1, dblk2), max(dblk1, dblk2))) { long long from = blk1 * div - 1; long long to = (blk1+1) * div; return min(abs(a-from)+abs(b-from), abs(a-to)+abs(b-to)); } return abs(a-b); } long long solve(long long a, long long b, long long c, long long d) { long long bx = abs(a-c); long long by = abs(b-d); long long dx = bx; long long dy = by; long long div = 1; for (int i = 0; i < MAXD; ++i) { dx = max(dx, getDist(div, a, c, b, d)); dy = max(dy, getDist(div, b, d, a, c)); div *= 3; } return max(dx + by, bx + dy); } int main() { int Q; cin >> Q; while (Q--) { long long a; long long b; long long c; long long d; cin >> a >> b >> c >> d; cout << solve(a-1, b-1, c-1, d-1) << endl; } return 0; }
#include <iostream> #include <cmath> using namespace std; long long solve(long long a, long long b, long long c, long long d){ long long res = abs(a-c) + abs(b-d); auto calc = [](long long x1, long long y1, long long x2, long long y2){ auto sx = x1, sy = y1, gx = x2, gy = y2; long long res = 0; long long block = 1; for(int i=0;i<30;i++){ if(sx == gx && sx%3 == 1 && abs(sy - gy) >= 2){ long long c1 = x1 % block; long long c2 = x2 % block; res = max(res, min(c1+c2+2, 2*block-c1-c2) + abs(y1 - y2)); } sx /= 3; sy /= 3; gx /= 3; gy /= 3; block *= 3; } return res; }; return max(res, max(calc(a, b, c, d), calc(b, a, d, c))); } int main(){ int Q; while(cin >> Q){ while(Q--){ long long a, b, c, d; cin >> a >> b >> c >> d; cout << solve(a-1, b-1, c-1, d-1) << endl; } } }
#include<bits/stdc++.h> //ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); //clock_t start=clock();clock_t end=clock();cout<<(double)(end-start)/CLOCKS_PER_SEC<<endl; using namespace std; typedef long long ll; typedef unsigned long long ull; typedef unsigned int ui; typedef pair<int,int> pii; typedef pair<pii,int> ppii; typedef pair<int,pii> pipi; typedef pair<ll,ll> pll; typedef pair<pll,ll> ppll; typedef pair<ll,pll> plpl; typedef tuple<ll,ll,ll> tl; typedef pair<double,double> pdd; typedef vector<vector<ll>> mat; ll mod=1000000007; ll mod2=998244353; ll mod3=1000003; ll mod4=998244853; ll mod5=1000000009; ll inf=1LL<<60; double pi=3.141592653589793238462643383279L; double eps=1e-8; #define rep(i,m,n) for(int i=m;i<n;i++) #define rrep(i,n,m) for(int i=n;i>=m;i--) #define srep(itr,st) for(auto itr=st.begin();itr!=st.end();itr++) #define mrep(itr,mp) for(auto itr=mp.begin();itr!=mp.end();itr++) #define Max(a,b) a=max(a,b) #define Min(a,b) a=min(a,b) int dh[4]={1,-1,0,0}; int dw[4]={0,0,1,-1}; int ddh[8]={-1,-1,-1,0,0,1,1,1}; int ddw[8]={-1,0,1,-1,1,-1,0,1}; ll gcd(ll a,ll b){ if(a<b)swap(a,b); if(b==0)return a; if(a%b==0)return b; return gcd(b,a%b); } ll Pow(ll n,ll k){ ll ret=1; ll now=n; while(k>0){ if(k&1)ret*=now; now*=now; k/=2; } return ret; } ll beki(ll n,ll k,ll md){ ll ret=1; ll now=n; while(k>0){ if(k%2==1){ ret*=now; ret%=md; } now*=now; now%=md; k/=2; } return ret; } ll gyaku(ll n,ll md){ return beki(n,md-2,md); } ll popcount(ll n){ ll ret=0; ll u=n; while(u>0){ ret+=u%2; u/=2; } return ret; } int main(){ ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); pll han[31]; ll y=1; rep(i,1,31){ han[i]={y+1,y*2}; y*=3; } int q;cin>>q; rep(i,0,q){ ll a,b,c,d;cin>>a>>b>>c>>d; a--;b--;c--;d--; if(a>c){ swap(a,c); swap(b,d); } if(b>d){ b=y-1-b; d=y-1-d; } ll ans=c-a+d-b; ll yy=y; rrep(j,30,1){ yy/=3; //if(j==2)cout<<aa<<" "<<bb<<" "<<cc<<" "<<dd<<endl; if(a/yy==c/yy&&(a/yy)%3==1&&abs(b/yy-d/yy)>=2){ ll aa=a%yy,cc=c%yy; Max(ans,abs(b-d)+min(1+aa+1+cc,yy-aa+yy-cc)); } if(b/yy==d/yy&&(b/yy)%3==1&&abs(a/yy-c/yy)>=2){ ll bb=b%yy,dd=d%yy; Max(ans,abs(a-c)+min(1+bb+1+dd,yy-bb+yy-dd)); } } cout<<ans<<endl; } }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <unordered_map> #include <vector> #include <string.h> #include <set> using namespace std; #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl template<class T> void printvec(const vector<T>& v) { for (auto x : v) { cout << x << " "; } cout << endl; } template<class T> void printtree(const vector< vector<T> >& tree) { for (long long i = 0; i < tree.size(); ++i) { cout << i + 1 << ": "; printvec(tree[i]); } } template<class T, class U> void printmap(const map<T, U>& mp) { for (auto x : mp) { cout << x.first << "=>" << x.second << endl; } } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } #define rep(i, n) for(ll i = 0; i < n; ++i) #define all(s) s.begin(), s.end() #define fr first #define sc second #define mp make_pair #define pb push_back typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; typedef tuple<ll, ll, ll> triple; typedef double D; const ll INF = 1e9; const ll MOD = 1000000007; // 1e9 + 7 // Calculate the distance between (ai,aj) and (bi,bj) ll solve(ll ai, ll aj, ll bi, ll bj) { if (aj > bj) { swap(ai, bi); swap(bj, bj); } ll s = 1; rep(i, 30) { s *= 3; } ll dist = abs(ai-bi) + abs(aj-bj); // Manhattan distance ll res = dist; while (1) { s /= 3; if (s <= 0) { break; } ll nai = ai/s, nbi = bi/s; // location at scale s. if (nai != nbi) { break; } // if not in same row, result should be Manhattan distance ll naj = aj/s, nbj = bj/s; if ((nai%3) == 1 && abs(naj-nbj) > 1) { // not in same column ll up = s*nai-1; ll bot = s*(nai+1); ll loss = min(ai,bi) - up; loss = min(loss, bot - max(ai,bi)); res = dist + loss*2; break; } } return res; } int main(int argc, char** argv) { cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); //cout << setprecision(10) << fixed; ll q; cin >> q; rep(qi, q) { ll ai, aj, bi, bj; cin >> ai >> aj >> bi >> bj; --ai; --aj; --bi; --bj; // 0-indexed ll ans = 0; ans = max(ans, solve(ai, aj, bi, bj)); // horizontal ans = max(ans, solve(aj, ai, bj, bi)); // vertical cout << ans << endl; } }
//By Don4ick //#define _GLIBCXX_DEBUG #include <bits/stdc++.h> typedef long long ll; typedef long double ld; typedef unsigned int ui; #define forn(i, n) for (int i = 1; i <= n; i++) #define pb push_back #define all(x) x.begin(), x.end() #define y1 qewr1234 const double PI = acos(-1.0); const int DIR = 4; const int X[] = {1, 0, -1, 0}; const int Y[] = {0, 1, 0, -1}; using namespace std; ll mypow(ll a, int k) { ll res = 1; forn(i, k) res *= a; return res; } ll calc(ll l, ll r, ll x1, ll x2) { return min(x1 - l + 1 + x2 - l + 1, r - x1 + 1 + r - x2 + 1); } ll solve() { ll x1, y1, x2, y2; scanf("%lld%lld%lld%lld", &x1, &y1, &x2, &y2); if (x1 > x2) swap(x1, x2); if (y1 > y2) swap(y1, y2); for (int i = 30; i >= 0; i--) { ll sz = mypow(3, i); ll l1 = ((x1 - 1) / sz) * sz + 1; ll r1 = l1 + sz -1 ; ll l2 = ((y1 - 1) / sz) * sz + 1; ll r2 = l2 + sz - 1; if (((x1 - 1) / sz) % 3 == 1 && r1 >= x2) { if ((r2 < y2 && (r2 / sz) % 3 == 1) || (r2 + sz < y2 && (r2 / sz) % 3 == 0)) return y2 - y1 + calc(l1, r1, x1, x2); } if (((y1 - 1) / sz) % 3 == 1 && r2 >= y2) { if ((r1 < x2 && (r1 / sz) % 3 == 1) || (r1 + sz < x2 && (r1 / sz) % 3 == 0)) return x2 - x1 + calc(l2, r2, y1, y2); } } return x2 - x1 + y2 - y1; } int main() { //ios_base::sync_with_stdio(false); //cin.tie(); //cout.tie(); //freopen(".in", "r", stdin); //freopen(".out", "w", stdout); int T; scanf("%d", &T); while(T--) { printf("%lld\n", solve()); } return 0; }
#line 1 "base.hpp"//6 #ifndef __clang__ #pragma GCC optimize ("O3") #endif void solve( #ifdef GCJ_CASE long long case_id #endif ); #if defined(EBUG) && !defined(ONLINE_JUDGE) #define debug true #define _GLIBCXX_DEBUG // #define _LIBCPP_DEBUG 0 #define _LIBCPP_DEBUG2 0 #else #define NDEBUG #define debug false #endif #include<algorithm> #include<bitset> #include<functional> #include<iomanip> #include<iostream> #include<limits> #include<map> #include<numeric> #include<queue> #include<set> #include<type_traits> #include<vector> #include<cassert> #include<climits> #include<cmath> #include<cstdio> #include<cstdlib> using namespace std; using LL=long long; using ULL=unsigned long long; #define int LL #define CS const #define CX constexpr #define IL inline #define OP operator #define RT return #define TL template #define TN typename #define lambda [&] #define rep(f,t,i,c,u)for(int Rb_=(t),i=(f);i c Rb_;u(i)) #define upto(f,t,i)rep(f,t,i,<=,++) #define uptil(f,t,i)rep(f,t,i,<,++) #define downto(f,t,i)rep(f,t,i,>=,--) #define downtil(f,t,i)rep(f,t,i,>,--) #define times(n,i)uptil(0,n,i) #define rtimes(n,i)downto((n)-1,0,i) #define iter(v)begin(v),end(v) #define citer(v)cbegin(v),cend(v) #define riter(v)rbegin(v),rend(v) #define criter(v)crbegin(v),crend(v) #define IF(a,b,c)((a)?(b):(c)) #if debug #define ln <<endl #else #define ln <<'\n' #endif #define tb <<'\t' #define sp <<' ' #line 1 "base_util.hpp"//6b #define BINOPa(t,u,op)t OP op(CS u&o)CS{RT t(*this)op##=o;} #define CMPOP(t,op,f1,f2,x)bool OP op(CS t&x)CS{RT f1 op f2;} #define CMPOPS(t,f1,f2,x)CMPOP(t,==,f1,f2,x)CMPOP(t,!=,f1,f2,x)\ CMPOP(t,<,f1,f2,x)CMPOP(t,<=,f1,f2,x)CMPOP(t,>,f1,f2,x)CMPOP(t,>=,f1,f2,x) #line 1 "mod.hpp"//6b #ifndef MOD #define MOD 1000000007 #endif #if !defined(FORCE_MOD)&&MOD!=1000000007&&MOD!=1000000009&&MOD!=998244353 #error mod #endif #line 1 "power.hpp"//6bm TL<TN T>IL T power(T x,int n){T r(1);for(;n;n/=2){if(n%2)r*=x;x*=x;}RT r;}IL int pow_mod(int x,int n,int m){int r=1; for(;n;n/=2){if(n%2)r=r*x%m;x=x*x%m;}RT r;} #line 2001 "mod.hpp"//6b IL CX int modulo(int a,int b){a%=b;RT a&&(a>0)!=(b>0)?a+b:a;}IL CX int divide(int a,int b){RT(a-modulo(a,b))/b;} TL<int d=MOD>struct MInt{ /*!https://ei1333.github.io/luzhiled/snippets/other/mod-int.html*/ int v;CX MInt():v(0){}explicit CX MInt(int i):v(modulo(i,d)){}MInt&OP+=(CS MInt&m){v+=m.v;if(v>=d)v-=d;RT*this;} MInt&OP-=(CS MInt&m){v-=m.v;if(v<0)v+=d;RT*this;}MInt&OP*=(CS MInt&m){v=v*m.v%d;RT*this;}MInt&OP/=(CS MInt&m){ RT*this*=m.inv();}BINOPa(MInt,MInt,+)BINOPa(MInt,MInt,-)BINOPa(MInt,MInt,*)BINOPa(MInt,MInt,/)MInt OP-()CS{ RT MInt()-=*this;}CMPOP(MInt,==,v,m.v,m)CMPOP(MInt,!=,v,m.v,m)MInt pow(int n)CS{RT power(*this,n);}MInt inv()CS{ int a=v,b=d,x=1,y=0;while(b){swap(y,x-=a/b*y);swap(b,a%=b);}RT(MInt)x;} friend ostream&OP<<(ostream&o,CS MInt&m){RT o<<m.v;}friend istream&OP>>(istream&i,MInt&m){i>>m.v;m.v%=d;RT i;}}; using mint=MInt<>;CX mint OP"" _m(ULL n){RT mint(n);}CX MInt<998244353>OP"" _m998244353(ULL n){RT MInt<998244353>(n);} CX MInt<1000000007>OP"" _m1e9_7(ULL n){RT MInt<1000000007>(n);} CX MInt<1000000009>OP"" _m1e9_9(ULL n){RT MInt<1000000009>(n);} #line 1 "typedefs.hpp"//6b using unit = tuple<>;using LD=long double;TL<TN T>using vec=vector<T>; TL<TN T>using vvec=vec<vec<T>>;TL<TN T>using vvvec=vec<vvec<T>>;TL<TN T>using vvvvec=vec<vvvec<T>>; #line 1 "alias.hpp"//6b #define EB emplace_back #define PB push_back #define foldl accumulate #define scanl partial_sum #line 1 "util.hpp"//6b TL<TN T>IL bool amax(T&v,CS T&a){RT v<a&&(v=a,1);}TL<TN T>IL bool amin(T&v,CS T&a){RT v>a&&(v=a,1);} TL<TN T>IL int sizeRAB(T t){RT t.size();} #define size sizeRAB #define clamp clampRAB TL<TN T>IL CX CS T&clamp(CS T&a,CS T&l,CS T&r){RT a<l?l:r<a?r:a;}TL<TN V>IL void uniq2(V&v){ v.erase(unique(iter(v)),v.end());}TL<TN V>IL void uniq(V&v){sort(iter(v));uniq2(v);} #define leftmost_ge lower_bound #define leftmost_gt upper_bound TL<TN C,TN D>IL C rightmost_le(CS C&from,CS C&to,CS D&d){auto l=leftmost_gt(from,to,d);RT l==from?to:--l;} TL<TN C,TN D>IL C rightmost_lt(CS C&from,CS C&to,CS D&d){auto l=leftmost_ge(from,to,d);RT l==from?to:--l;} namespace rab{TL<TN I>IL bool is_in(I x,I l,I r){RT l<=x&&x<r;}TL<TN T>IL T fetch(CS T&d,CS vec<T>&v,int i){ RT 0<=i&&i<size(v)?v[i]:d;}} #line 1 "debug.hpp"//6b TL<TN T>IL istream&OP>>(istream&s,vec<T>&v){for(auto&&p:v)s>>p;RT s;}TL<TN T,TN S> IL ostream&OP<<(ostream&s,CS pair<T,S>&p){RT s<<"("<<p.first<<","<<p.second<<")";} #define Rdebug1(sep, ...)IL ostream& OP<<(ostream&s,CS __VA_ARGS__&v){\ int i=0;for(CS auto&e:v){i++&&s<<sep;s<<e;}RT s;} TL<TN T>Rdebug1(' ',vec<T>)TL<TN T,TN S>Rdebug1(' ',map<T,S>)TL<TN T>Rdebug1('\n',vvec<T>) TL<TN T,TN S>Rdebug1('\n',vec<map<T,S>>) #line 6001 "base.hpp"//6 signed main(){if(debug)cerr<<"MOD: "<<MOD ln;else cin.tie(0),cerr.tie(0),ios::sync_with_stdio(0); auto p=setprecision(20);cout<<fixed<<p;cerr<<fixed<<p; #ifdef GCJ_CASE int T;cin>>T;times(T,t){cout<<"Case #"<<t+1<<": ";solve(t);} #else solve(); #endif RT 0;} #line 1001 "6.cpp"// //#include "consts.hpp" int tri[31]; inline bool sameRow(int l, int a, int c) { return a / tri[l] == c / tri[l]; } inline bool freeColumnRange(int l, int a, int b, int d) { return a / tri[l] % 3 != 1 || d / tri[l] - b / tri[l] <= 1; } int dist00(int l, int i, int j) { if(l == 0) { assert(i == 0 && j == 0); return 0; } else { return dist00(l-1, i % tri[l-1], j % tri[l-1]) + tri[l-1] * (i / tri[l-1] + j / tri[l-1]); } } inline int distLU(int l, int i, int j) { return dist00(l, i % tri[l], j % tri[l]); } inline int distLD(int l, int i, int j) { return dist00(l, tri[l] - 1 - i % tri[l], j % tri[l]); } inline int distRU(int l, int i, int j) { return dist00(l, i % tri[l], tri[l] - 1 - j % tri[l]); } inline int distRD(int l, int i, int j) { return dist00(l, tri[l] - 1 - i % tri[l], tri[l] - 1 - j % tri[l]); } void solve() { tri[0] = 1; times(30, x) tri[x + 1] = tri[x] * 3; int Q; cin >> Q; times(Q, q) { int a, b, c, d; cin >> a >> b >> c >> d; --a; --b; --c; --d; if(abs(a - c) > abs(b - d)) { swap(a, b); swap(c, d); } if(b > d) { swap(a, c); swap(b, d); } int l = 30; while(l > 0 && sameRow(l, a, c) && freeColumnRange(l, a, b, d)) { --l; } // dd l; a; b; c; d; if(sameRow(l, a, c) && freeColumnRange(l, a, b, d)) { assert(l == 0); cout << d - b ln; } else if(sameRow(l, a, c)) { assert(!freeColumnRange(l, a, b, d)); cout << min( distRU(l, a, b) + distLU(l, c, d) + tri[l] * (d / tri[l] - b / tri[l] - 1), distRD(l, a, b) + distLD(l, c, d) + tri[l] * (d / tri[l] - b / tri[l] - 1) ) + 3 ln; } else if(a < c) { {if(debug)cerr<<'#'<<__LINE__ ln<<" l: "<<(l)ln<<" a: "<<(a)ln<<" b: "<<(b)ln<<" c: "<<(c)ln<<" d: "<<(d)ln<<" distRD(l, a, b): "<<(distRD(l, a, b))ln<<" distLU(l, c, d): "<<(distLU(l, c, d))ln<<" tri[l] * (d / tri[l] - b / tri[l] + c / tri[l] - a / tri[l] - 2): "<<(tri[l] * (d / tri[l] - b / tri[l] + c / tri[l] - a / tri[l] - 2))ln;} cout << distRD(l, a, b) + distLU(l, c, d) + tri[l] * (d / tri[l] - b / tri[l] + c / tri[l] - a / tri[l] - 2) + 2 ln; } else { cout << distRU(l, a, b) + distLD(l, c, d) + tri[l] * (d / tri[l] - b / tri[l] + a / tri[l] - c / tri[l] - 2) + 2 ln; } } }
#include<iostream> #include<vector> #include<algorithm> using namespace std; int Q; long a,b,c,d; long T[31]; int main() { cin>>Q; T[0]=1; for(int i=1;i<=30;i++)T[i]=T[i-1]*3; for(;Q--;) { cin>>a>>b>>c>>d; a--,b--,c--,d--; if(a>c)swap(a,c); if(b>d)swap(b,d); long ans=abs(a-c)+abs(b-d); long plus=0; for(int i=30;i>=1;i--) { if(a/T[i-1]==c/T[i-1]&&a/T[i-1]%3==1) { long B=b/T[i-1],D=d/T[i-1]; if(B>D)swap(B,D); while(B%3!=1)B++; while(D>=0&&D%3!=1)D--; if(B<=D) { a%=T[i]; c%=T[i]; plus=min(min(a,c)+1-T[i-1],2*T[i-1]-max(a,c)); break; } } if(b/T[i-1]==d/T[i-1]&&b/T[i-1]%3==1) { long A=a/T[i-1],C=c/T[i-1]; if(A>C)swap(A,C); while(A%3!=1)A++; while(C>=0&&C%3!=1)C--; if(A<=C) { b%=T[i]; d%=T[i]; plus=min(min(b,d)+1-T[i-1],2*T[i-1]-max(b,d)); break; } } } cout<<ans+plus*2<<endl; } }
#include <iostream> #include <algorithm> #include <vector> #include <queue> #include <string> #include <cmath> using ll = long long; using namespace std; using P = pair<ll, ll>; #define fi first #define se second int main() { int q; cin >> q; ll pow3_30 = 1; for (int i=0; i<30; i++) pow3_30 *= 3; while (q--) { P p1, p2, b1, b2, r1, r2; cin >> p1.fi >> p1.se >> p2.fi >> p2.se; p1.fi--; p1.se--; p2.fi--; p2.se--; ll pow = pow3_30; ll dist = abs(p1.fi - p2.fi) + abs(p1.se - p2.se); while (pow /= 3) { b1 = {p1.fi / pow, p1.se / pow}; b2 = {p2.fi / pow, p2.se / pow}; r1 = {p1.fi % pow, p1.se % pow}; r2 = {p2.fi % pow, p2.se % pow}; if (b1.fi != b2.fi && b1.se != b2.se) { break; } else if (b1.fi == b2.fi && b1.se != b2.se && b1.fi % 3 == 1 && (b1.se+1) / 3 != (b2.se+1) / 3) { dist = abs(p1.se - p2.se) + min(r1.fi + r2.fi + 2, (pow-r1.fi) + (pow-r2.fi)); break; } else if (b1.fi != b2.fi && b1.se == b2.se && b1.se % 3 == 1 && (b1.fi+1) / 3 != (b2.fi+1) / 3) { dist = abs(p1.fi - p2.fi) + min(r1.se + r2.se + 2, (pow-r1.se) + (pow-r2.se)); break; } } cout << dist << '\n'; } return 0; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author aajisaka */ #include<bits/stdc++.h> using namespace std; void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif #define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr) #define rep(i,n) for(int i=0; i<(int)(n); i++) #define all(v) v.begin(), v.end() template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using P = pair<ll, ll>; constexpr long double PI = 3.14159265358979323846264338327950288L; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); class FFractalShortestPath { public: vector<ll> e3; ll calc(ll a, ll b, ll c, ll d) { a--; b--; c--; d--; for(int i=29; i>=0; i--) { //debug(i); auto e = e3[i]; auto a3 = a/e; auto b3 = b/e; auto c3 = c/e; auto d3 = d/e; if (a3 != c3 && b3 != d3) { return abs(b-d) + abs(a-c); } //debug(a3, b3, c3, d3); if (a3 == c3 && b3 == d3) { a %= e; b %= e; c %= e; d %= e; continue; } if (a3 != c3) { swap(a, b); swap(c, d); swap(a3, b3); swap(c3, d3); } //debug(a, b, c, d); // a3 == c3, b3 != d3 while(a3 == c3 && e > 0) { //debug(a, c, a3, c3, e); if (a3%3 == 1 && c3%3 == 1 && abs(b/e-d/e) > 1) { ll left = a%e+c%e+2; ll right = e*2-a%e-c%e; return abs(b-d)+min(left, right); } e /= 3; if (e==0) break; a3 = a/e; c3 = c/e; //debug(a3, c3, e); } return abs(b-d) + abs(a-c); } return abs(b-d) + abs(a-c); } void solve(istream& cin, ostream& cout) { SPEED; int q; cin >> q; e3.resize(31, 1); for(int i=1; i<=30; i++) { e3[i] = e3[i-1]*3; } while(q--) { ll a, b, c, d; cin >> a >> b >> c >> d; cout << calc(a, b, c, d) << '\n'; } } }; signed main() { FFractalShortestPath solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; // typedef __int128 ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; #define FR first #define SE second #define MP make_pair #define PB push_back #define vc vector #define db double #define all(x) (x).begin(),(x).end() #define sz(x) ((int)(x).size()) #define bin(x) (1ll<<(x)) #define fo(i,l,r) for(int i=(l),I=(r);i<=I;i++) #define fd(i,r,l) for(int i=(r),I=(l);i>=I;i--) #define mem(x,val) memset(x,val,sizeof x) #define Swap(a,b,n) for(int I=0;I<=n;I++) swap(a[I],b[I]) #define PC __builtin_popcountll #ifdef DEBUG #define debug(A,args...) fprintf(stderr,A,##args) #else #define debug(A,args...) printf("") #endif #define deb debug("line %d\n",__LINE__) namespace mine { ll qread() { ll ans=0,f=1;char c=getchar(); while(c<'0' or c>'9') {if(c=='-')f=-1;c=getchar();} while('0'<=c and c<='9') ans=ans*10+c-'0',c=getchar(); return ans*f; } void write(ll num){if(num<0) putchar('-'),num=-num;if(num>=10) write(num/10);putchar('0'+num%10);} void write1(ll num){write(num);putchar(' ');} void write2(ll num){write(num);putchar('\n');} template<typename T>inline bool chmax(T&a,const T&b){return a<b?a=b,1:0;} template<typename T>inline bool chmin(T&a,const T&b){return a>b?a=b,1:0;} ll gcd(ll x,ll y){return y?gcd(y,x%y):x;} bool IN(ll x,ll l,ll r){return l<=x and x<=r;} void GG(){puts("-1");exit(0);} const db eps=1e-8; const int INF=0x3f3f3f3f; const int MOD=1e9+7; int mm(const int x){return x>=MOD?x-MOD:x;} template<typename T> void add(T &x,const int &y){x=(x+y>=MOD?x+y-MOD:x+y);} ll qpower(ll x,ll e,int mod=MOD){ll ans=1;while(e){if(e&1)ans=ans*x%mod;x=x*x%mod;e>>=1;}return ans;} ll invm(ll x){return qpower(x,MOD-2);} const int MM=1e6+10; ll fac[MM],facinv[MM],Inv[MM];ll Comb(int n,int m){return n<0 or n<m?0:fac[n]*facinv[m]%MOD*facinv[n-m]%MOD;} void PRE() { fac[0]=1;fo(i,1,MM-1) fac[i]=fac[i-1]*i%MOD; facinv[MM-1]=invm(fac[MM-1]);fd(i,MM-1,1) facinv[i-1]=facinv[i]*i%MOD; Inv[1]=1;fo(i,2,MM-1) Inv[i]=(MOD-MOD/i)*Inv[MOD%i]%MOD; } const int N=2e3+10; //------------------FIXED------------------ ll pp[N]; ll solve(ll T,ll x1,ll y1,ll x2,ll y2) { ll xx1=x1/T,yy1=y1/T,xx2=x2/T,yy2=y2/T; if(xx1==xx2 and yy1==yy2) return solve(T/3,x1,y1,x2,y2); if(xx1==xx2 and xx1%3==1 and abs(yy1-yy2)>=2) {x1%=(T*3),x2%=(T*3);return min(x1+x2-(T-1)*2,(T+T)*2-x1-x2)+abs(y1-y2);} if(yy1==yy2 and yy1%3==1 and abs(xx1-xx2)>=2) {y1%=(T*3),y2%=(T*3);return min(y1+y2-(T-1)*2,(T+T)*2-y1-y2)+abs(x1-x2);} if(T==1) return abs(x1-x2)+abs(y1-y2); else return solve(T/3,x1,y1,x2,y2); } void main() { pp[0]=1;fo(i,1,30) pp[i]=pp[i-1]*3; fo(q,1,qread()) { ll x1=qread()-1,y1=qread()-1,x2=qread()-1,y2=qread()-1; write2( solve(pp[29],x1,y1,x2,y2) ); } } };//变量重名! signed main() { #ifdef DEBUG freopen("a.in","r",stdin); //freopen("z.txt","r",stdin); //freopen("a.out","w",stdout); #endif srand(time(0)); mine::PRE(); mine::main(); debug("\n------------------------------------------\nTime: %.2lf s",1.0*clock()/CLOCKS_PER_SEC); }
#include<bits/stdc++.h> using namespace std; typedef long long LL; LL w[30]={1}; LL calc(LL x1,LL y1,LL x2,LL y2,int d){ if(d==-1)return abs(y2-y1); LL a=x1/w[d],b=y1/w[d],c=x2/w[d],d1=y2/w[d]; if(a!=c)return abs(x2-x1)+abs(y2-y1); else if(a==1&&abs(b-d1)>=2){ x1-=w[d],x2-=w[d]; return abs(y2-y1)+min(x1+x2+2,w[d]*2-x1-x2); } x1-=w[d]*a,x2-=w[d]*a; return calc(x1,y1,x2,y2,d-1); } LL work(){ LL a,b,c,d;scanf("%lld%lld%lld%lld",&a,&b,&c,&d); a--,b--,c--,d--;if(abs(a-c)>abs(b-d))swap(a,b),swap(c,d); return calc(a,b,c,d,29); } int main(){ for(int i=1;i<30;i++)w[i]=w[i-1]*3; int t;scanf("%d",&t); while(t--)printf("%lld\n",work()); return 0; }
#include <iostream> #include <cmath> #include <vector> using lint = long long; void solve() { lint x1, y1, x2, y2; std::cin >> x1 >> y1 >> x2 >> y2; --x1, --y1, --x2, --y2; lint ans = std::abs(x2 - x1) + std::abs(y2 - y1); lint three = 1; for (int i = 0; i < 30; ++i) { for (int j = 0; j < 2; ++j) { std::swap(x1, y1); std::swap(x2, y2); if (x1 / three != x2 / three || (x1 / three) % 3 != 1 || std::abs(y1 / three - y2 / three) <= 1) continue; lint d1 = x1 % three, d2 = x2 % three; lint d = std::min(d1 + d2 + 2, three - d1 + three - d2); ans = std::max(ans, d + std::abs(y2 - y1)); } three *= 3; } std::cout << ans << std::endl; } int main() { std::cin.tie(nullptr); std::cout.tie(nullptr); std::ios::sync_with_stdio(false); int q; std::cin >> q; while (q--) solve(); return 0; }
// #pragma GCC target("avx") // CPU 処理並列化 // #pragma GCC optimize("O3") // CPU 処理並列化 // #pragma GCC optimize("unroll-loops") // 条件処理の呼び出しを減らす // #define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ = malloc(size);void * stack_extend_origin_memory_;char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024)); // #define END_STACK_EXTEND asm volatile("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_); #include<stdio.h> #include<math.h> #include<algorithm> #include<queue> #include<deque> #include<stack> #include<string> #include<string.h> #include<vector> #include<set> #include<map> #include<bitset> #include<stdlib.h> #include<cassert> #include<time.h> #include<bitset> #include<numeric> #include<unordered_set> #include<unordered_map> #include<complex> using namespace std; const long long mod=1000000007; const long long inf=mod*mod; const long long d2=(mod+1)/2; const long double EPS=1e-9; const double INF=1e+10; const double PI=acos(-1.0); const int C_SIZE = 3100000; const int UF_SIZE = 3100000; namespace{ long long fact[C_SIZE]; long long finv[C_SIZE]; long long inv[C_SIZE]; long long Comb(int a,int b){ if(a<b||b<0)return 0; return fact[a]*finv[b]%mod*finv[a-b]%mod; } void init_C(int n){ fact[0]=finv[0]=inv[1]=1; for(int i=2;i<n;i++){ inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod; } for(int i=1;i<n;i++){ fact[i]=fact[i-1]*i%mod; finv[i]=finv[i-1]*inv[i]%mod; } } long long pw(long long a,long long b){ if(a<0LL)return 0; if(b<0LL)return 0; long long ret=1; while(b){ if(b%2)ret=ret*a%mod; a=a*a%mod; b/=2; } return ret; } long long pw_mod(long long a,long long b,long long M){ if(a<0LL)return 0; if(b<0LL)return 0; long long ret=1; while(b){ if(b%2)ret=ret*a%M; a=a*a%M; b/=2; } return ret; } int pw_mod_int(int a,int b,int M){ if(a<0)return 0; if(b<0)return 0; int ret=1; while(b){ if(b%2)ret=(long long)ret*a%M; a=(long long)a*a%M; b/=2; } return ret; } int ABS(int a){return max(a,-a);} long long ABS(long long a){return max(a,-a);} double ABS(double a){return max(a,-a);} int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; } int UF[UF_SIZE]; void init_UF(int n){ for(int i=0;i<n;i++)UF[i]=-1; } int FIND(int a){ if(UF[a]<0)return a; return UF[a]=FIND(UF[a]); } void UNION(int a,int b){ a=FIND(a);b=FIND(b);if(a==b)return; if(UF[a]>UF[b])swap(a,b); UF[a]+=UF[b];UF[b]=a; } } // ここから編集しろ long long p3[32]; int main(){ p3[0]=1; for(int i=1;i<32;i++)p3[i]=p3[i-1]*3; int a;scanf("%d",&a); while(a--){ long long X1,X2,Y1,Y2; scanf("%lld%lld%lld%lld",&X1,&Y1,&X2,&Y2); X1--;Y1--;X2--;Y2--; int yk=0; if(ABS(X1-X2)>ABS(Y1-Y2)){ swap(X1,Y1); swap(X2,Y2); } for(int i=0;i<30;i++){ if(X1/p3[i]%3!=1)continue; if(X2/p3[i]%3!=1)continue; if(X1/p3[i]!=X2/p3[i])continue; long long Z1=(Y1/p3[i]+2)/3; long long Z2=(Y2/p3[i]+2)/3; if(Z1==Z2)continue; yk=i+1; } long long ret=inf; if(yk==0){ ret=ABS(X1-X2)+ABS(Y1-Y2); }else{ ret=min(ret,ABS(Y1-Y2)+(X1%p3[yk]-p3[yk-1]+1)+(X2%p3[yk]-p3[yk-1]+1)); ret=min(ret,ABS(Y1-Y2)+(p3[yk-1]*2-X1%p3[yk])+(p3[yk-1]*2-X2%p3[yk])); } printf("%lld\n",ret); } }
#include <bits/stdc++.h> #define SORT(x) sort((x).begin(), (x).end()) #define RSORT(x) sort((x).rbegin(), (x).rend()) #define ALL(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < n; i++) #define reps(i, m, n) for (ll i = m; i < n; i++) #define repr(i, m, n) for (ll i = m; i >= n; i--) #define de(x) cout << #x << "=" << x << endl; #define SP << " " << template <class T> bool maxi(T &a, const T &b) { if (a < b) { a = b; return 1;} return 0;} template <class T> bool mini(T &a, const T &b) { if (b < a) { a = b; return 1;} return 0;} #define dame {cout<< "-1" <<"\n"; return;} #define INF2 1000000000000000037 #define INF 1000000007 #define MOD 1000000007 using namespace std; using ll = long long; using ld = long double; using P = pair<ll,ll>; //--GLOBAL--------------------------------- ll sans[31]; //--MAIN----------------------------------- void Main() { ll Q; cin>>Q; sans[0]=1; rep(i,30){ sans[i+1]=sans[i]*3; } while(Q--){ ll a,b,c,d; cin>>a>>b>>c>>d; --a,--b,--c,--d; ll ans=abs(a-c)+abs(b-d); repr(i,29,0){ ll x1=(a/sans[i]); ll y1=(b/sans[i]); ll x2=(c/sans[i]); ll y2=(d/sans[i]); if(x1==x2 and x1%3==1 and abs(y1-y2)>=2){ ans=abs(b-d)+min(a%sans[i]+c%sans[i]+2, sans[i]-(a%sans[i]) + sans[i]-(c%sans[i])); break; } if(y1==y2 and y1%3==1 and abs(x1-x2)>=2){ ans=abs(a-c)+min(b%sans[i]+d%sans[i]+2, sans[i]-(b%sans[i]) + sans[i]-(d%sans[i])); break; } } cout<< ans <<"\n"; } } //--START---------------------------------- int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); } //-----------------------------------------
#include<bits/stdc++.h> using namespace std; int64_t len(int64_t a,int64_t b,int64_t c,int64_t d,int64_t p){ if(a/p!=c/p&&b/p!=d/p) return abs(c-a)+abs(d-b); if(a/p==c/p&&b/p==d/p) return len(a%p,b%p,c%p,d%p,p/3); if(a/p==c/p) return len(b,a,d,c,p); int64_t xa=a; int64_t xc=c; int k=0; while(abs(xc-xa)>1){ k++; xc/=3; xa/=3; } int64_t x=(b+d)/2; vector<int> t; while(x>0){ t.push_back(x%3); x/=3; } int64_t l=0; int64_t g=0; bool f=0; for(int i=(int)t.size()-1;i>=0;i--){ l*=3; g*=3; if(f){ l+=2; continue; } if(i<k&&t.at(i)==1){ g+=2; f=1; continue; } l+=t.at(i); g+=t.at(i); } return min(abs(c-a)+abs(l-b)+abs(l-d),abs(c-a)+abs(g-b)+abs(g-d)); } int main(){ int q; cin>>q; for(int _=0;_<q;_++){ int64_t a,b,c,d; cin>>a>>b>>c>>d; a--; b--; c--; d--; int64_t p=1; while(p<=max({a,b,c,d})) p*=3; cout<<len(a,b,c,d,p/3)<<endl; } }
#include <bits/stdc++.h> using namespace std; long long blocked(long long a, long long b, long long c,long long d, long long width) { if (width == 1) return 0; long long block_width = width / 3; if (b / block_width == d / block_width) { if (b / block_width == 1 && abs(a / block_width - c / block_width) >= 2) { return min(min(b - block_width + 1, d - block_width + 1), min(2 * block_width - b, 2 * block_width - d)) * 2; } else { return blocked(a, b % block_width, c, d % block_width, block_width); } } else { return 0; } } long long dist(long long a, long long b, long long c, long long d) { if (abs(a - c) < abs(b - d)) { swap(a, b); swap(c, d); } if (b > d) { swap(a, c); swap(b, d); } // b <= d and abs(a - c) >= abs(b - d) cerr << a << " " << b << " " << c << " " << d << " " << endl; return blocked(a, b, c, d, 205891132094649LL) + abs(a - c) + abs(b - d); } int main() { int q; cin >> q; for (int i = 0; i < q; i++) { long long a, b, c, d; cin >> a >> b >> c >> d; cout << dist(a - 1, b - 1, c - 1, d - 1) << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int,int>; ll solve(ll ai, ll aj, ll bi, ll bj) { if (aj > bj) { swap(ai,bi); swap(aj,bj); } ll s = 1; rep(i,30) s *= 3; ll dist = abs(ai-bi) + abs(aj-bj); ll res = dist; while (1) { s /= 3; if (s <= 0) break; ll nai = ai/s, nbi = bi/s; if (nai != nbi) break; ll naj = aj/s, nbj = bj/s; if (nai%3 == 1 && abs(naj-nbj) > 1) { ll up = s*nai-1; ll bot = s*(nai+1); ll loss = min(ai,bi) - up; loss = min(loss, bot - max(ai,bi)); res = dist + loss*2; break; } } return res; } int main() { int q; cin >> q; rep(qi,q) { ll ai,aj,bi,bj; cin >> ai >> aj >> bi >> bj; ai--; aj--; bi--; bj--; ll ans = 0; ans = max(ans, solve(ai,aj,bi,bj)); ans = max(ans, solve(aj,ai,bj,bi)); cout << ans << endl; } return 0; }
#include <iostream> #include <fstream> #include <cstdio> #include <cmath> #include <vector> #include <string> #include <set> #include <map> #include <stack> #include <queue> #include <deque> #include <bitset> #include <algorithm> #include <complex> #include <array> #include <functional> using namespace std; #define REP(i,n) for(int i=0; i<n; ++i) #define FOR(i,a,b) for(int i=a; i<=b; ++i) #define FORR(i,a,b) for (int i=a; i>=b; --i) #define ALL(c) (c).begin(), (c).end() typedef long long ll; typedef vector<int> VI; typedef vector<ll> VL; typedef vector<double> VD; typedef vector<VI> VVI; typedef vector<VL> VVL; typedef vector<VD> VVD; typedef pair<int,int> P; typedef pair<ll,ll> PL; template<typename T> void chmin(T &a, T b) { if (a > b) a = b; } template<typename T> void chmax(T &a, T b) { if (a < b) a = b; } int in() { int x; scanf("%d", &x); return x; } ll lin() { ll x; scanf("%lld", &x); return x; } const int N = 30; ll three[N + 1]; ll calc2(ll x1, ll y1, ll x2, ll y2, ll n){ // cout << "calc2" << endl; // cout << x1 << " " << y1 << " " << x2 << " " << y2 << " " << n << endl; ll d = abs(x1 - x2) + abs(y1 - y2); if (n == 0) return d; if (y1 > y2){ swap(x1, x2); swap(y1, y2); } y2 -= y1 / three[n] * three[n]; y1 %= three[n]; ll bx1 = x1 / three[n - 1]; ll by1 = y1 / three[n - 1]; ll bx2 = x2 / three[n - 1]; ll by2 = y2 / three[n - 1]; if (bx1 == bx2){ if (bx1 != 1){ return calc2(x1 % three[n - 1], y1, x2 % three[n - 1], y2, n - 1); } if (by1 == 2 && by2 == 3){ return calc2(x1 - three[n - 1], y1 - 2 * three[n - 1], x2 - three[n - 1], y2 - 2 * three[n - 1], n - 1); } ll du1 = x1 - (y1 % three[n - 1]); ll dl1 = (2 * three[n - 1] - x1) + (three[n - 1] - (y1 % three[n - 1])) - 1; ll du2 = x2 - three[n - 1] + (y2 % three[n - 1]) + 1; ll dl2 = (2 * three[n - 1] - x2) + (y2 % three[n - 1]); // cout << du1 << " " << dl1 << " " << du2 << " " << dl2 << endl; return min(du1 + du2, dl1 + dl2) + (by2 - by1 - 1) * three[n - 1] + 1; } return d; } ll calc(ll x1, ll y1, ll x2, ll y2, ll n){ // cout << "calc" << endl; ll d = abs(x1 - x2) + abs(y1 - y2); ll bx1 = x1 / three[n - 1]; ll by1 = y1 / three[n - 1]; ll bx2 = x2 / three[n - 1]; ll by2 = y2 / three[n - 1]; // cout << bx1 << " " << by1 << " " << bx2 << " " << by2 << endl; if (bx1 == bx2 && by1 == by2){ return calc(x1 % three[n - 1], y1 % three[n - 1], x2 % three[n - 1], y2 % three[n - 1], n - 1); } if ((bx1 == bx2 && bx1 == 1 && abs(by1 - by2) == 2) || (by1 == by2 && by1 == 1 && abs(bx1 - bx2) == 2)){ if (by1 == 1){ swap(x1, y1); swap(x2, y2); } if (y1 > y2){ swap(x1, x2); swap(y1, y2); } // cout << x1 << " " << y1 << " " << x2 << " " << y2 << endl; ll du1 = abs(three[n - 1] - 1 - x1) + abs(three[n - 1] - 1 - y1); ll dl1 = abs(2 * three[n - 1] - x1) + abs(three[n - 1] - 1 - y1); ll du2 = abs(three[n - 1] - 1 - x2) + abs(2 * three[n - 1] - y2); ll dl2 = abs(2 * three[n - 1] - x2) + abs(2 * three[n - 1] - y2); return min(du1 + du2, dl1 + dl2) + three[n - 1] + 1; } if (bx1 == bx2){ return calc2(x1 % three[n - 1], y1, x2 % three[n - 1], y2, n - 1); } if (by1 == by2){ return calc2(y1 % three[n - 1], x1, y2 % three[n - 1], x2, n - 1); } return d; } VVI field(int n){ int m = 1; REP(i,n) m *= 3; VVI ret(m, VI(m)); m /= 3; if (n == 0) return ret; VVI f = field(n - 1); REP(i,3) REP(j,3){ REP(x,m) REP(y,m){ ret[i*m+x][j*m+y] = ((i == 1 && j == 1) || f[x][y]); } } return ret; } int di[] = {-1, 1, 0, 0}, dj[] = {0, 0, -1, 1}; int naive(int x1, int y1, int x2, int y2){ VVI f = field(3); int n = f.size(); VVI dist(n, VI(n, -1)); dist[x1][y1] = 0; queue<P> que; que.push(P(x1, y1)); while (!que.empty()){ P p = que.front(); que.pop(); int i = p.first, j = p.second; REP(k,4){ int ii = i + di[k], jj = j + dj[k]; if (ii < 0 || ii >= n || jj < 0 || jj >= n || f[ii][jj] || dist[ii][jj] != -1) continue; dist[ii][jj] = dist[i][j] + 1; que.push(P(ii, jj)); } } return dist[x2][y2]; } int main(void){ three[0] = 1; REP(i,N) three[i + 1] = 3 * three[i]; int q; cin >> q; while (q--){ ll a = lin()-1; ll b = lin()-1; ll c = lin()-1; ll d = lin()-1; ll x = calc(a, b, c, d, N); printf("%lld\n", x); } // VVI f = field(3); // int n = f.size(); // REP(i,n){ // REP(j,n) cout << f[i][j]; // cout << endl; // } // REP(x1,n) REP(y1,n) REP(x2,n) REP(y2,n){ // if (f[x1][y1] || f[x2][y2] || (x1 == x2 && y1 == y2)) continue; // ll d1 = calc(x1, y1, x2, y2, N); // ll d2 = naive(x1, y1, x2, y2); // if (d1 != d2){ // cout << x1 << " " << y1 << " " << x2 << " " << y2 << endl; // } // } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { vector<int64_t> p(31); p.at(0) = 1; for (int i = 1; i <= 30; i++) { p.at(i) = 3 * p.at(i - 1); } int q; cin >> q; vector<int64_t> a(q), b(q), c(q), d(q); for (int i = 0; i < q; i++) { cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i); } for (int i = 0; i < q; i++) { int64_t dh = abs(a.at(i) - c.at(i)), dw = abs(b.at(i) - d.at(i)); int64_t direct = dh + dw; vector<int> a3(30), b3(30), c3(30), d3(30); a.at(i)--; b.at(i)--; c.at(i)--; d.at(i)--; vector<int64_t> ac = a, bc = b, cc = c, dc = d; for (int j = 0; j < 30; j++) { a3.at(j) = ac.at(i) % 3; ac.at(i) /= 3; } for (int j = 0; j < 30; j++) { b3.at(j) = bc.at(i) % 3; bc.at(i) /= 3; } for (int j = 0; j < 30; j++) { c3.at(j) = cc.at(i) % 3; cc.at(i) /= 3; } for (int j = 0; j < 30; j++) { d3.at(j) = dc.at(i) % 3; dc.at(i) /= 3; } int64_t add_h = 0, add_w = 0; bool same_h = true, same_w = true; for (int j = 29; j >= 0; j--) { if (add_h == 0 && same_h && a3.at(j) == 1 && c3.at(j) == 1 && ((same_w && dw >= p.at(j)) || dw >= 2 * p.at(j))) { int64_t ar = a.at(i) % p.at(j), cr = c.at(i) % p.at(j); add_h = 2 * min({ar + 1, p.at(j) - ar, cr + 1, p.at(j) - cr}); } if (add_w == 0 && same_w && b3.at(j) == 1 && d3.at(j) == 1 && ((same_h && dh >= p.at(j)) || dh >= 2 * p.at(j))) { int64_t br = b.at(i) % p.at(j), dr = d.at(i) % p.at(j); add_w = 2 * min({br + 1, p.at(j) - br, dr + 1, p.at(j) - dr}); } if (a3.at(j) != c3.at(j)) same_h = false; if (b3.at(j) != d3.at(j)) same_w = false; } cout << direct + add_h + add_w << endl; } }
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <iostream> #include <set> #include <map> #include <vector> #include <string> using namespace std; #define LL long long const LL dx[4] = {1, -1, 0, 0}; const LL dy[4] = {0, 0, 1, -1}; LL _abs(LL x) { if (x > 0) { return x; } return (-x); } vector<int> getBit(LL x, int bitn = 35) { vector<int> res; res.clear(); while (x > (LL)(0)) { res.push_back((int)(x % (LL)(3))); x /= (LL)(3); } while (res.size() < bitn) { res.push_back(0); } return res; } int bitn = 35; LL getDis(LL a, LL b, LL sz) { LL down = (a + b + 2); LL up = (sz - a) + (sz - b); return min(up, down) - _abs(a - b); } LL getX(vector<int> x, int k) { LL res = 0; for (int i = k - 1; i >= 0; --i) { res = res * (LL)(3) + (LL)(x[i]); } return res; } LL getM3(int k) { LL res = 1; for (int i = 0; i < k; ++i) { res *= (LL)(3); } return res; } vector<int> cutV(vector<int> &a, int x) { vector<int> res; res.clear(); for (int i = 0; i < x; ++i) { res.push_back(a[i]); } return res; } LL shortestNextNeighbor(vector<int> x1, vector<int> y1, vector<int> x2, vector<int> y2, bool sameX) { int n = x1.size(); bool diffX = !sameX; LL res = 0; for (int i = n - 1; i >= 0; --i) { if (diffX) { if (y1[i] == 1 && y2[i] == 1) { LL yy1 = getX(y1, i); LL yy2 = getX(y2, i); LL sz = getM3(i); return getDis(yy1, yy2, sz); } else if (y1[i] != y2[i]) { return res; } } else { // cout << "here" << endl; if (x1[i] == 1 && x2[i] == 1) { LL xx1 = getX(x1, i); LL xx2 = getX(x2, i); LL sz = getM3(i); // printf("xx1 = %lld, xx2 = %lld, sz = %lld\n", xx1, xx2, sz); // printf("dis = %lld\n", getDis(xx1, xx2, sz)); return getDis(xx1, xx2, sz); } else if (x1[i] != x2[i]) { return res; } } } return res; } LL shortestNeighbor(vector<int> x1, vector<int> y1, vector<int> x2, vector<int> y2, int xx1, int yy1, int xx2, int yy2) { int n = x1.size(); bool noContact = false; if (yy1 == yy2) { if (xx1 > xx2) { swap(y1, y2); swap(x1, x2); } for (int i = n - 1; i >= 0; --i) { // cout << "i = " << i << endl; if (y1[i] == 1 && y2[i] == 1 && (noContact || (x1[i] < 1 || x2[i] > 1))) { LL yy1 = getX(y1, i); LL yy2 = getX(y2, i); LL sz = getM3(i); // cout << "stop at i = " << i << endl; return getDis(yy1, yy2, sz); } else if (y1[i] != y2[i]) { return 0; } else if (x1[i] <= 1 || x2[i] >= 1) { noContact = true; } } } if (xx1 == xx2) { if (yy1 > yy2) { swap(x1, x2); swap(y1, y2); } for (int i = n - 1; i >= 0; --i) { if (x1[i] == 1 && x2[i] == 1 && (noContact || (y1[i] < 1 || y2[i] > 1))) { LL xx1 = getX(x1, i); LL xx2 = getX(x2, i); LL sz = getM3(i); // printf("xx1 = %lld, xx2 = %lld, sz = %lld\n", xx1, xx2, sz); // printf("dis = %lld\n", getDis(xx1, xx2, sz)); return getDis(xx1, xx2, sz); } else if (x1[i] != x2[i]) { return 0; } else if (y1[i] <= 1 || y2[i] >= 1) { noContact = true; } } } return 0; } void printVector(vector<int> x) { for (int i = 0; i < x.size(); ++i) { cout << x[i]; } cout << endl; } LL getPath(LL x1, LL y1, LL x2, LL y2) { vector<int> xb1 = getBit(x1), xb2 = getBit(x2); vector<int> yb1 = getBit(y1), yb2 = getBit(y2); // cout << "x1 = "; // printVector(xb1); // cout << "x2 = "; // printVector(xb2); // cout << "y1 = "; // printVector(yb1); // cout << "y2 = "; // printVector(yb2); LL res = _abs(x1 - x2) + _abs(y1 - y2); for (int i = bitn - 1; i >= 0; --i) { if (xb1[i] == xb2[i] && yb1[i] == yb2[i]) { continue; } int xx1 = xb1[i], xx2 = xb2[i]; int yy1 = yb1[i], yy2 = yb2[i]; // printf("(%d, %d), (%d, %d)\n", xx1, yy1, xx2, yy2); bool corner1 = true; /* if (xx1 == 0 && yy1 == 1) { corner1 = false; } if (xx1 == 2 && yy1 == 1) { corner1 = false; } if (xx1 == 1 && yy1 == 0) { corner1 = false; } if (xx1 == 1 && yy1 == 2) { corner1 = false; } */ corner1 = (((xx1 + yy1) % 2) == 0); //LL zero = (LL)(0), one = (LL)(1), two = (LL)(2); if (corner1) { if (xx2 != xx1 && yy2 != yy1) { return res; } else { int diff = abs(xx1 - xx2) + abs(yy1 - yy2); if (diff == 2) { // cout << "here" << endl; return res + shortestNextNeighbor(cutV(xb1, i), cutV(yb1, i), cutV(xb2, i), cutV(yb2, i), (xx1 == xx2)); } else { return res + shortestNeighbor(cutV(xb1, i), cutV(yb1, i), cutV(xb2, i), cutV(yb2, i), xx1, yy1, xx2, yy2); } // continue; } } else { int diff = _abs(xx1 - xx2) + _abs(yy1 - yy2); if (diff == 1) { // cout << "here" << endl; return res + shortestNeighbor(cutV(xb1, i), cutV(yb1, i), cutV(xb2, i), cutV(yb2, i), xx1, yy1, xx2, yy2); // continue; } else { // return res; if (xx2 == xx1 && yy2 + yy1 == 2 && xx1 == 1) { LL sz = getM3(i); LL xz1 = getX(xb1, i), xz2 = getX(xb2, i); return res + getDis(xz1, xz2, sz); } if (yy2 == yy1 && xx2 + xx1 == 2 && yy1 == 1) { LL sz = getM3(i); LL yz1 = getX(yb1, i), yz2 = getX(yb2, i); return res + getDis(yz1, yz2, sz); } return res; } } } return res; } int q; LL a, b, c, d; int main() { cin >> q; for (int i = 0; i < q; ++i) { cin >> a >> b >> c >> d; a -= (LL)(1); b -= (LL)(1); c -= (LL)(1); d -= (LL)(1); cout << getPath(a, b, c, d) << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int,int>; ll solve(ll ai, ll aj, ll bi, ll bj) { if (aj > bj) { swap(ai,bi); swap(aj,bj); } ll s = 1; rep(i,30) s *= 3; ll dist = abs(ai-bi) + abs(aj-bj); ll res = dist; while (s>1) { s /= 3; ll nai = ai/s, nbi = bi/s; if (nai != nbi) break; ll naj = aj/s, nbj = bj/s; if (nai%3 == 1 && abs(naj-nbj) > 1) { ll up = s*nai-1; ll bot = nai*s + s; ll loss = min(ai,bi) - up; loss = min(loss, bot - max(ai,bi)); res += loss*2; break; } } return res; } int main() { int q; cin >> q; while(q--) { ll ai,aj,bi,bj; cin >> ai >> aj >> bi >> bj; ai--; aj--; bi--; bj--; ll ans1 = solve(ai,aj,bi,bj); ll ans2 = solve(aj,ai,bj,bi); cout << max(ans1, ans2) << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll pw[31]; ll dist(ll a, ll b, ll c, ll d, int x = -1) { if (x == -1) x = upper_bound(pw, pw + 31, max(max(a, c), max(b, d))) - pw - 1; ll A = a / pw[x], B = b / pw[x], C = c / pw[x], D = d / pw[x]; if (A != C && B != D) { return abs(a - c) + abs(b - d); } if (A == C && B == D) { return dist(a % pw[x], b % pw[x], c % pw[x], d % pw[x], x - 1); } if (B == D) { swap(A, B), swap(C, D), swap(a, b), swap(c, d); } if (A == 1) { if (abs(B - D) <= 1) return dist(a - pw[x], b, c - pw[x], d, x - 1); return abs(b - d) + min(4 * pw[x] - a - c, a + c - 2 * (pw[x] - 1)); } return x > 0 ? dist(a % pw[x], b, c % pw[x], d, x - 1) : abs(b - d); } int main() { int q; scanf("%d", &q); pw[0] = 1; for (int i = 1; i <= 30; i++) pw[i] = 3 * pw[i - 1]; while (q--) { ll a, b, c, d; scanf("%lld%lld%lld%lld", &a, &b, &c, &d); printf("%lld\n", dist(a - 1, b - 1, c - 1, d - 1)); } }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; typedef long long int ll; vector<ll> power(31); bool sameline(ll a, ll b, int k){ ll line1=(a-1)/power[k-1]; ll line2=(b-1)/power[k-1]; if(line1==line2&&line1%3==1)return true; return false; } bool otherside(ll a, ll b, int k){ ll line1=(a-1)/power[k-1]; ll line2=(b-1)/power[k-1]; if(line2-line1>1)return true; return false; } ll min_path(ll a,ll b, ll c,ll d){ ll x1,x2,y1,y2; if(abs(a-c)<abs(b-d)){ x1=min(a,c); x2=max(a,c); y1=min(b,d); y2=max(b,d); }else{ x1=min(b,d); x2=max(b,d); y1=min(a,c); y2=max(a,c); } for (int k = 30; k > 0 ; k--){ if(sameline(x1,x2,k)){ if(otherside(y1,y2,k)){ ll upl=(x1-1)%power[k-1]+1; ll upr=(x2-1)%power[k-1]+1; ll downl=power[k-1]-upl+1; ll downr=power[k-1]-upr+1; return min(upl+upr,downl+downr)+y2-y1; } } } return x2-x1+y2-y1; } int main() { power[0]=1; for (int i = 1; i <= 30; i++){ power[i]=3*power[i-1]; } int Q; cin>>Q; for (int i = 0; i < Q; i++){ ll a,b,c,d; cin>>a>>b>>c>>d; cout<<min_path(a,b,c,d)<<endl; } }
// ※※※ 解答不能 ※※※ // https://img.atcoder.jp/panasonic2020/editorial.pdf #include <bits/stdc++.h> using namespace std; using LL = long long; #define repex(i, a, b, c) for(int i = a; i < b; i += c) #define repx(i, a, b) repex(i, a, b, 1) #define rep(i, n) repx(i, 0, n) #define repr(i, a, b) for(int i = a; i >= b; i--) #define _abs(x) ((x) > 0 ? (x) : -(x)) // 二点間を妨害するような黒マスの連結成分があるかを計算? // @param x: x座標(スタート地点). // @param y1: y座標(スタート地点). // @param y2: y座標(ゴール地点). // @return : 妨害する黒マスの連結成分があるかどうか. bool between(LL x, LL y1, LL y2){ if(y1 > y2) swap(y1, y2); y1++, y2--; if(!(y1 <= y2)) return false; while(x > 0){ if(x % 3 == 1){ if(y2 - y1 > 3) return true; for(LL y = y1; y <= y2; y++) if(y % 3 == 1) return true; } x /= 3; y1 /= 3; y2 /= 3; } return false; } // 迂回する距離?を計算. // @param x1: x座標(スタート地点). // @param y1: y座標(スタート地点). // @param x2: x座標(ゴール地点). // @param y2: y座標(ゴール地点). // @return : スタート地点からゴール地点までの迂回する距離を返却. LL get_extra(LL x1, LL y1, LL x2, LL y2){ LL ans = 0, three = 1; int i; rep(i, 35){ if(x1 / three == x2 / three && between(x1 / three, y1 / three, y2 / three)){ LL tmp = min(min(x1 % three, x2 % three) + 1, three - max(x1 % three, x2 % three)); ans = max(ans, tmp); } three *= 3; } return ans; } // 二点間の距離を求める. // @param x1: x座標(スタート地点). // @param y1: y座標(スタート地点). // @param x2: x座標(ゴール地点). // @param y2: y座標(ゴール地点). // @return : スタート地点からゴール地点までの距離を返却. LL get_dist(LL x1, LL y1, LL x2, LL y2){ return _abs(x1 - x2) + _abs(y1 - y2) + 2 * max(get_extra(x1, y1, x2, y2), get_extra(y1, x1, y2, x2)); } int main(){ // 1. 入力情報. int Q; scanf("%d", &Q); // 2. クエリに回答. rep(i, Q){ LL a, b, c, d; scanf("%lld %lld %lld %lld", &a, &b, &c, &d); a--, b--, c--, d--; printf("%lld\n", get_dist(a, b, c, d)); } return 0; }
// Copyright [2020] <unknown> #include <bits/stdc++.h> using namespace std; #define ONLINE_JUDGE #ifndef ONLINE_JUDGE #define dbg(x...) { cerr << "\033[32;1m" << #x << " -> "; err(x); } void err() { cerr << "\033[39;0m" << endl; } template<typename T, typename... A> void err(T a, A... x) { cerr << a << ' '; err(x...); } #else #define dbg(...) #endif #define sz(x) ((int)x.size()) typedef long long LL; typedef pair<LL, LL> P; P s, t; LL p3[100]; int findK(const P &p) { int k = 0; LL mx = max(p.first, p.second); while (p3[k] < mx) ++k; return k; } P idp(const P &p, int k) { if (k == 0) return p; else { return P(p.first / p3[k-1] % 3, p.second / p3[k-1] % 3); } } LL dis(const P &a, const P &b) { return llabs(a.first - b.first) + llabs(a.second - b.second); } P basep(const P &p, int k) { return P(p.first / p3[k] * p3[k], p.second / p3[k] * p3[k]); } int main(int argc, char const *argv[]) { // code p3[0] = 1; for (int i=1; i<=30; ++i) p3[i] = p3[i-1] * 3; // dbg(p3[30]); int tt; scanf("%d", &tt); for (int kk=0; kk<tt; ++kk) { // cin >> s.first >> s.second >> t.first >> t.second; scanf("%lld%lld%lld%lld", &s.first, &s.second, &t.first, &t.second); if (s == t) { puts("0"); continue; } --s.first; --t.first; --s.second; --t.second; if (llabs(s.first - t.first) < llabs(s.second - t.second)) { swap(s.first, s.second); swap(t.first, t.second); } if (s.first > t.first) swap(s, t); // int k = max(findK(s), findK(t)); // LL ans = dis(s, t); // P ids = idp(s, k), idt = idp(t, k); // while () LL ans = dis(s, t); // dbg(s.first, s.second, t.first, t.second); for (int k=30; k>=1; --k) { P ids = idp(s, k), idt = idp(t, k); if (basep(s, k-1) == basep(t, k-1)) continue; dbg(ids.first, ids.second, idt.first, idt.second, k); if (ids.second == 1 && (ids.first == 0 || idt.first == 2 || llabs(s.first - t.first) > p3[k])) { // LL l = p3[k-1]-1, r = 2 * p3[k-1]; // LL sx = s.second % p3[k], st = t.second % p3[k]; // ans += min(sx + st - 2 * l, 2 * r - sx - st); P bp = basep(s, k); // dbg(bp.first, bp.second); P l(s.first, bp.second + p3[k-1] - 1), r(s.first, bp.second + 2 * p3[k-1]); ans = min(dis(s, l) + dis(l, t), dis(s, r) + dis(r, t)); break; } } printf("%lld\n", ans); } return 0; }
#include<iostream> #include<vector> #include<algorithm> using namespace std; int Q; long a,b,c,d; long T[31]; int main() { cin>>Q; T[0]=1; for(int i=1;i<=30;i++)T[i]=T[i-1]*3; for(;Q--;) { cin>>a>>b>>c>>d; a--,b--,c--,d--; long ans=abs(a-c)+abs(b-d); long plus=0; for(int i=30;i>=1;i--) { if(a/T[i-1]==c/T[i-1]&&a/T[i-1]%3==1) { long B=b/T[i-1],D=d/T[i-1]; if(B>D)swap(B,D); while(B%3!=1)B++; while(D>=0&&D%3!=1)D--; if(B<=D) { a%=T[i]; c%=T[i]; plus=min(min(a,c)+1-T[i-1],2*T[i-1]-max(a,c)); break; } } if(b/T[i-1]==d/T[i-1]&&b/T[i-1]%3==1) { long A=a/T[i-1],C=c/T[i-1]; if(A>C)swap(A,C); while(A%3!=1)A++; while(C>=0&&C%3!=1)C--; if(A<=C) { b%=T[i]; d%=T[i]; plus=min(min(b,d)+1-T[i-1],2*T[i-1]-max(b,d)); break; } } } cout<<ans+plus*2<<endl; } }
#include <bits/stdc++.h> using namespace std; ////////////////////////////// Begin Macros #define ALL(x) (x).begin(), (x).end() #define rep(i, N) for (int i = 0; i < N; i++) #define rep1(i, N) for (int i = 1; i <= N; i++) using ll = long long int; using pii = pair<int, int>; using pll = pair<ll, ll>; template <typename T> bool chmax(T &m, const T q) { if (m < q) { m = q; return true; } else return false; } template <typename T> bool chmin(T &m, const T q) { if (m > q) { m = q; return true; } else return false; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return {l.first + r.first, l.second + r.second}; } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return {l.first - r.first, l.second - r.second}; } template <typename T> pair<T, T> operator*(const pair<T, T> &l, const T &r) { return {l.first * r, l.second * r}; } template <typename T> pair<T, T> operator/(const pair<T, T> &l, const T &r) { return {l.first / r, l.second / r}; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename T> void reset(vector<T> &v, const T reset_to) { for (auto &x : v) x = reset_to; } inline int popcount(const unsigned int x) { return __builtin_popcount(x); } #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; const int intinf = numeric_limits<int>::max(); #define MOD 1000000007 const pii udlr[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; ////////////////////////////// End Macros ll L1norm(pll xy) { return abs(xy.first) + abs(xy.second); } ll dist(pll ab, pll cd, int level) { if (level == 0) return abs(ab.second - cd.second); const ll pow_three_x = pow(3, level - 1); const int A = ab.first / pow_three_x; const int C = cd.first / pow_three_x; if (A != C) return L1norm(ab - cd); ab.first -= A * pow_three_x; cd.first -= A * pow_three_x; level--; if (A != 1) return dist(ab, cd, level); // A==C==1 const ll B = (ab.second + pow_three_x) / pow_three_x / 3; const ll D = (cd.second + pow_three_x) / pow_three_x / 3; if (B == D) return dist(ab, cd, level); const ll dx = min(abs(ab.first - (-1)) + abs(cd.first - (-1)), abs(ab.first - pow_three_x) + abs(cd.first - pow_three_x)); const ll dy = abs(ab.second - cd.second); return dx + dy; } void solve() { int Q; cin >> Q; rep(q, Q) { ll a, b, c, d; cin >> a >> b >> c >> d; a--; b--; c--; d--; const int level_init = 30; ll ans = max(dist({a, b}, {c, d}, level_init), dist({b, a}, {d, c}, level_init)); cout << ans << endl; } } int main() { solve(); return 0; }
#include <bits/stdc++.h> long long solve(long long ai, long long aj, long long bi, long long bj) { long long size = 1; for (int i = 0; i < 30; ++i) { size *= 3; } long long dist = std::abs(ai - bi) + std::abs(aj - bj); while (size > 1) { size /= 3; long long nai = ai / size, naj = aj / size, nbi = bi / size, nbj = bj / size; if (nai != nbi) { break; } if (std::abs(naj - nbj) >= 2 && nai % 3 == 1) { long long up = ai - nai * size + 1 + bi - nbi * size + 1; long long bottom = (nai + 1) * size - ai + (nbi + 1) * size - bi; dist = std::abs(aj - bj) + std::min(up, bottom); break; } } return dist; } int main() { int q; std::cin >> q; std::vector<std::array<std::array<long long, 2>, 2>> in(q); for (int i = 0; i < q; ++i) { long long ax, ay, bx, by; std::cin >> ax >> ay >> bx >> by; --ax; --ay; --bx; --by; in[i][0][0] = ax; in[i][0][1] = ay; in[i][1][0] = bx; in[i][1][1] = by; } for (int i = 0; i < q; ++i) { std::cout << std::max(solve(in[i][0][0], in[i][0][1], in[i][1][0], in[i][1][1]), solve(in[i][0][1], in[i][0][0], in[i][1][1], in[i][1][0])) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll=long long; #define int ll #define rng(i,a,b) for(int i=int(a);i<int(b);i++) #define rep(i,b) rng(i,0,b) #define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--) #define per(i,b) gnr(i,0,b) #define pb push_back #define eb emplace_back #define a first #define b second #define bg begin() #define ed end() #define all(x) x.bg,x.ed #define si(x) int(x.size()) #ifdef LOCAL #define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl #else #define dmp(x) void(0) #endif template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;} template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;} template<class t> using vc=vector<t>; template<class t> using vvc=vc<vc<t>>; using pi=pair<int,int>; using vi=vc<int>; template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p){ return os<<"{"<<p.a<<","<<p.b<<"}"; } template<class t> ostream& operator<<(ostream& os,const vc<t>& v){ os<<"{"; for(auto e:v)os<<e<<","; return os<<"}"; } #define mp make_pair #define mt make_tuple #define one(x) memset(x,-1,sizeof(x)) #define zero(x) memset(x,0,sizeof(x)) #ifdef LOCAL void dmpr(ostream&os){os<<endl;} template<class T,class... Args> void dmpr(ostream&os,const T&t,const Args&... args){ os<<t<<" "; dmpr(os,args...); } #define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__) #else #define dmp2(...) void(0) #endif using uint=unsigned; using ull=unsigned long long; template<class t,size_t n> ostream& operator<<(ostream&os,const array<t,n>&a){ return os<<vc<t>(all(a)); } template<int i,class T> void print_tuple(ostream&,const T&){ } template<int i,class T,class H,class ...Args> void print_tuple(ostream&os,const T&t){ if(i)os<<","; os<<get<i>(t); print_tuple<i+1,T,Args...>(os,t); } template<class ...Args> ostream& operator<<(ostream&os,const tuple<Args...>&t){ os<<"{"; print_tuple<0,tuple<Args...>,Args...>(os,t); return os<<"}"; } template<class t> void print(t x,int suc=1){ cout<<x; if(suc==1) cout<<"\n"; if(suc==2) cout<<" "; } ll read(){ ll i; cin>>i; return i; } vi readvi(int n,int off=0){ vi v(n); rep(i,n)v[i]=read()+off; return v; } template<class T> void print(const vector<T>&v,int suc=1){ rep(i,v.size()) print(v[i],i==int(v.size())-1?suc:2); } string readString(){ string s; cin>>s; return s; } template<class T> T sq(const T& t){ return t*t; } //#define CAPITAL void yes(bool ex=true){ #ifdef CAPITAL cout<<"YES"<<"\n"; #else cout<<"Yes"<<"\n"; #endif if(ex)exit(0); } void no(bool ex=true){ #ifdef CAPITAL cout<<"NO"<<"\n"; #else cout<<"No"<<"\n"; #endif if(ex)exit(0); } void possible(bool ex=true){ #ifdef CAPITAL cout<<"POSSIBLE"<<"\n"; #else cout<<"Possible"<<"\n"; #endif if(ex)exit(0); } void impossible(bool ex=true){ #ifdef CAPITAL cout<<"IMPOSSIBLE"<<"\n"; #else cout<<"Impossible"<<"\n"; #endif if(ex)exit(0); } constexpr ll ten(int n){ return n==0?1:ten(n-1)*10; } const ll infLL=LLONG_MAX/3; #ifdef int const int inf=infLL; #else const int inf=INT_MAX/2-100; #endif int topbit(signed t){ return t==0?-1:31-__builtin_clz(t); } int topbit(ll t){ return t==0?-1:63-__builtin_clzll(t); } int botbit(signed a){ return a==0?32:__builtin_ctz(a); } int botbit(ll a){ return a==0?64:__builtin_ctzll(a); } int popcount(signed t){ return __builtin_popcount(t); } int popcount(ll t){ return __builtin_popcountll(t); } bool ispow2(int i){ return i&&(i&-i)==i; } ll mask(int i){ return (ll(1)<<i)-1; } bool inc(int a,int b,int c){ return a<=b&&b<=c; } template<class t> void mkuni(vc<t>&v){ sort(all(v)); v.erase(unique(all(v)),v.ed); } ll rand_int(ll l, ll r) { //[l, r] #ifdef LOCAL static mt19937_64 gen; #else static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); #endif return uniform_int_distribution<ll>(l, r)(gen); } template<class t> void myshuffle(vc<t>&a){ rep(i,si(a))swap(a[i],a[rand_int(0,i)]); } template<class t> int lwb(const vc<t>&v,const t&a){ return lower_bound(all(v),a)-v.bg; } const int L=30; int p3[L+1]; void slv(){ int a,b,c,d;cin>>a>>b>>c>>d; a--;b--;c--;d--; int ans=inf; per(lv,30){ int w=p3[lv]; int e=a/w; int f=b/w; int g=c/w; int h=d/w; if(e!=g&&f!=h){ ans=abs(a-c)+abs(b-d); break; }else if(e==g&&f==h){ a%=w; b%=w; c%=w; d%=w; }else{ if(f==h){ swap(a,b); swap(c,d); swap(e,f); swap(g,h); } assert(e==g); while(lv>=0){ if(a/w!=c/w){ chmin(ans,abs(b-d)+abs(a-c)); break; }else if(a/w==1&&abs(b/w-d/w)>1){ chmin(ans,abs(b-d)+min(a%w+1+c%w+1,w-a%w+w-c%w)); break; }else{ if(lv==0){ chmin(ans,abs(b-d)); break; } a%=w; c%=w; lv--; w/=3; } } break; } } assert(ans!=inf); print(ans); } signed main(){ cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20); p3[0]=1; rep(i,L)p3[i+1]=p3[i]*3; int q;cin>>q; rep(_,q)slv(); }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); } } iosetup; int main() { const int X = 30; vector<ll> n(X, 1); FOR(i, 1, X) n[i] = n[i - 1] * 3; function<void()> solve = [&]() { ll a, b, c, d; cin >> a >> b >> c >> d; --a; --b; --c; --d; ll ans = abs(c - a) + abs(d - b); for (int level = X; level > 0; --level) { ll a_div = a / n[level - 1], b_div = b / n[level - 1], c_div = c / n[level - 1], d_div = d / n[level - 1]; // cout << a_div << ' ' << b_div << ' ' << c_div << ' ' << d_div << '\n'; if (a_div != c_div && b_div != d_div) break; if (a_div == c_div && b_div == d_div) continue; if (a_div == c_div && a_div % 3 == 1 && abs(b_div - d_div) >= 2) { ans += min({a % n[level - 1] + 1, c % n[level - 1] + 1, n[level - 1] - a % n[level - 1], n[level - 1] - c % n[level - 1]}) * 2; break; } else if (b_div == d_div && b_div % 3 == 1 && abs(a_div - c_div) >= 2) { ans += min({b % n[level - 1] + 1, d % n[level - 1] + 1, n[level - 1] - b % n[level - 1], n[level - 1] - d % n[level - 1]}) * 2; break; } } cout << ans << '\n'; }; int q; cin >> q; while (q--) solve(); return 0; }
#include<iostream> #include<cstdint> #include<algorithm> int64_t calc(int64_t a1, int64_t a2, int64_t b1, int64_t b2, int lv, int64_t L) { if(lv == 0) return 0; int64_t Lb = L/3; int64_t bi_a1 = a1/Lb; int64_t bi_a2 = a2/Lb; int64_t bi_b1 = b1/Lb; int64_t bi_b2 = b2/Lb; if(bi_a1 == bi_b1 && bi_a2 == bi_b2) { return calc(a1%Lb, a2%Lb, b1%Lb, b2%Lb, lv-1, Lb); // same block } if(bi_a2 == bi_b2) { std::swap(a1, a2); std::swap(b1, b2); } // bi_a2 != bi_b2 while(Lb > 0) { bi_a1 = a1/Lb; bi_b1 = b1/Lb; if(bi_a1 != bi_b1) return std::abs(a1-b1) + std::abs(a2-b2); if(bi_a1 == 1 && std::abs(a2/Lb - b2/Lb) > 1) { a1 %= Lb; b1 %= Lb; return std::abs(a2 - b2) + std::min(a1+b1+2, Lb*2-a1-b1); } a1 %= Lb; b1 %= Lb; Lb /= 3; } return std::abs(a2 - b2); } int main() { int64_t L = 1; for(int i = 0; i < 30; ++i) L *= 3; int Q; std::cin >> Q; for(int i = 0; i < Q; ++i) { int64_t a, b, c, d; std::cin >> a >> b >> c >> d; std::cout << calc(a-1, b-1, c-1, d-1, 30, L) << std::endl; } return 0; }
#include <bits/stdc++.h> typedef long long ll; using namespace std; const ll MOD = 1e9 + 7; #define all(x) (x).begin(), (x).end() #define int long long vector<int> three(ll x) { vector<int> a; for (int i = 0; i < 30; i++) a.push_back(x % 3), x /= 3; reverse(all(a)); return a; } ll num(vector<int> th) { ll x = 0; for (int i = 0; i < th.size(); i++) x = x * 3 + th[i]; return x; } int Plus(vector<int> &d, int pos) { for (int a = pos; a >= 0; a--) { d[a]++; if (d[a] != 3) return 1; d[a] = 0; } return 0; } int Minus(vector<int> &d, int pos) { for (int a = pos; a >= 0; a--) { d[a]--; if (d[a] != -1) return 1; d[a] = 2; } return 0; } ll penalty(ll x1, ll y1, ll x2, ll y2) { vector<int> a1 = three(x1), b1 = three(y1), a2 = three(x2), b2 = three(y2); for (int i = 0; i < 30; i++) { if (a1[i] == a2[i] && b1[i] == b2[i]) { a1[i] = 0, a2[i] = 0, b1[i] = 0, b2[i] = 0; x1 = num(a1); x2 = num(a2); y1 = num(b1); y2 = num(b2); continue; } if (a1[i] != a2[i] && b1[i] != b2[i]) { return 0; } if (b1[i] == b2[i]) { swap(a1, b1); swap(a2, b2); swap(x1, y1); swap(x2, y2); } if (a1[i] == 1 && a2[i] == 1) { vector<int> g1(30, 0); for (int j = i + 1; j < 30; j++) g1[j] = 2; ll r1 = num(g1); vector<int> g2(30, 0); g2[i] = 2; ll r2 = num(g2); ll pen1 = min(x1, x2) - r1; ll pen2 = r2 - max(x1, x2); return 2 * min(pen1, pen2); } if (y1 > y2) { swap(y1, y2); swap(x1, x2); swap(a1, a2); swap(b1, b2); } //cout << "PENALTY IS EQUAL TO " << x1 << " " << y1 << " " << x2 << " " << y2 << "\n"; for (int j = i + 1; j < 30; j++) { if (a1[j] != a2[j]) return 0; if (a1[j] != 1) { a1[j] = 0, a2[j] = 0, x1 = num(a1), x2 = num(a2); continue; } vector<int> s1 = b1; for (int k = j + 1; k < 30; k++) s1[k] = 0; while (s1[j] != 1) { if (Plus(s1, j) == 0) return 0; } ll col1 = num(s1); if (y1 < col1 && col1 < y2) { vector<int> r1 = a1; for (int k = j + 1; k < 30; k++) r1[k] = 2; r1[j]--; ll R1 = num(r1); ll pen1 = min(x1, x2) - R1; vector<int> r2 = a1; for (int k = j + 1; k < 30; k++) r2[k] = 0; r2[j]++; assert(r2[j] <= 2); ll R2 = num(r2); ll pen2 = R2 - max(x1, x2); assert(R1 < x1); assert(x1 < R2); assert(R1 < x2); assert(x2 < R2); return 2 * min(pen1, pen2); } } return 0; } return 0; } long long solve(ll x1, ll y1, ll x2, ll y2) { return abs(x1 - x2) + abs(y1 - y2) + penalty(x1, y1, x2, y2); } void solve() { ll x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--, y1--, x2--, y2--; cout << solve(x1, y1, x2, y2) << "\n"; } const int N = 27; string s[N]; int dist[N][N]; bool ok(int x, int y) { return 0 <= x && x < N && 0 <= y && y < N && s[x][y] == '.'; } int cx[4] = {0, 0, -1, 1}; int cy[4] = {1, -1, 0, 0}; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) solve(); return 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { vector<int> a = three(i), b = three(j); int t = 0; for (int k = 0; k < 30; k++) if (a[k] == 1 && b[k] == 1) t = 1; if (t) s[i] += "#"; else s[i] += "."; } } int c1, d1; cin >> c1 >> d1; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { dist[i][j] = -1; } } vector<pair<int, int> > q = {{c1, d1}}; dist[c1][d1] = 0; for (int i = 0; i < q.size(); i++) { int x = q[i].first, y = q[i].second; for (int k = 0; k < 4; k++) { int nx = x + cx[k]; int ny = y + cy[k]; if (ok(nx, ny) && dist[nx][ny] == -1) { dist[nx][ny] = dist[x][y] + 1; q.push_back({nx, ny}); } } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (s[i][j] != '#' && dist[i][j] != solve(i, j, c1, d1)) { cout << "WA " << i << " " << j << " " << c1 << " " << d1 << " EXP: " << dist[i][j] << " MY: " << solve(i, j, c1, d1) << "\n"; //cout << "*#&(#^$%^*$"; //exit(485); } } } }
#include <iostream> #include <cmath> #include <algorithm> #include <vector> #include <map> #include <string> using namespace std; #define ll signed long long int main() { int Q; cin >> Q; for (int q = 0; q < Q; ++q) { ll x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; --x1; --y1; --x2; --y2; ll ans = -1; for (ll i = pow(3ll, 30ll); i > 1; i /= 3) { if (x1 / i == x2 / i && i / 3 <= x1 % i && x1 % i < i / 3 * 2 && i / 3 <= x2 % i && x2 % i < i / 3 * 2) { if (y1 / (i / 3) < y2 / (i / 3) - 1 || y2 / (i / 3) < y1 / (i / 3) - 1) { ans = abs(y2 - y1); ans += min((x1 % i) - (i / 3 - 1) + (x2 % i) - (i / 3 - 1), i / 3 * 2 - (x1 % i) + i / 3 * 2 - (x2 % i)); break; } } if (y1 / i == y2 / i && i / 3 <= y1 % i && y1 % i < i / 3 * 2 && i / 3 <= y2 % i && y2 % i < i / 3 * 2) { if (x1 / (i / 3) < x2 / (i / 3) - 1 || x2 / (i / 3) < x1 / (i / 3) - 1) { ans = abs(x2 - x1); ans += min((y1 % i) - (i / 3 - 1) + (y2 % i) - (i / 3 - 1), i / 3 * 2 - (y1 % i) + i / 3 * 2 - (y2 % i)); break; } } if (ans >= 0) { break; } } if (ans >= 0) { cout << ans << endl; } else { cout << abs(x2 - x1) + (abs(y2 - y1)) << endl; } } return 0; } /* ........................... .*..*..*..*..*..*..*..*..*. ........................... ...***......***......***... .*.***.*..*.***.*..*.***.*. ...***......***......***... ........................... .*..*..*..*..*..*..*..*..*. ........................... .........*********......... .*..*..*.*********.*..*..*. .........*********......... ...***...*********...***... .*.***.*.*********.*.***.*. ...***...*********...***... .........*********......... .*..*..*.*********.*..*..*. .........*********......... ........................... .*..*..*..*..*..*..*..*..*. ........................... ...***......***......***... .*.***.*..*.***.*..*.***.*. ...***......***......***... ........................... .*..*..*..*..*..*..*..*..*. ........................... */
#include<bits/stdc++.h> int main(){ using namespace std; unsigned long Q; cin >> Q; constexpr unsigned long M{205891132094649UL}; const auto& calc = [](unsigned long a, unsigned long b, unsigned long c, unsigned long d) -> unsigned long { auto ub{b}, lb{b}, p{M}; while(p /= 3)if(a / p % 3 == 1 || a / p * p + p * (1 + (a / p % 3 == 2)) <= c){ if(ub / p % 3 == 1)ub = ub / (3 * p) * 3 * p + 2 * p; if(lb / p % 3 == 1)lb = lb / (3 * p) * 3 * p + p - 1; } return c - a + min(ub - b + max(ub, d) - min(ub, d), b + d - 2 * lb); }; for(unsigned long _{0}, a, b, c, d; _ < Q; ++_){ cin >> a >> b >> c >> d; if(--a > --c){ swap(a, c); swap(b, d); } if(--b > --d){ b = M - b - 1; d = M - d - 1; } cout << max(calc(a, b, c, d), calc(b, a, d, c)) << endl; } return 0; }
#include <bits/stdc++.h> //#include <unistd.h> //#include <iostream> using namespace std; #define DEBUG(x) cerr<<#x<<": "<<x<<endl; #define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl; #define DEBUG_MAT(v) cerr<<#v<<endl;for(int i=0;i<v.size();i++){for(int j=0;j<v[i].size();j++) {cerr<<v[i][j]<<" ";}cerr<<endl;} typedef long long ll; #define int ll #define vi vector<int> #define vl vector<ll> #define vii vector< vector<int> > #define vll vector< vector<ll> > #define vs vector<string> #define pii pair<int,int> #define pis pair<int,string> #define psi pair<string,int> #define pll pair<ll,ll> template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); } template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); } template<class S, class T> ostream& operator<<(ostream& os, pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; } #define X first #define Y second #define rep(i,n) for(int i=0;i<(n);i++) #define rep1(i,n) for(int i=1;i<=(n);i++) #define rrep(i,n) for(int i=(n)-1;i>=0;i--) #define rrep1(i,n) for(int i=(n);i>0;i--) #define REP(i,a,b) for(int i=a;i<b;i++) #define in(x, a, b) (a <= x && x < b) #define all(c) c.begin(),c.end() template<class T> bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; } #define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end()); const ll inf = 1000000001; const ll INF = (ll)1e18 + 1; const long double pi = 3.1415926535897932384626433832795028841971L; #define Sp(p) cout<<setprecision(25)<< fixed<<p<<endl; //int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; //int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 }; vi dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1}; vi dx2 = { 1,1,0,-1,-1,-1,0,1 }, dy2 = { 0,1,1,1,0,-1,-1,-1 }; #define fio() cin.tie(0); ios::sync_with_stdio(false); const ll MOD = 1000000007; //const ll MOD = 998244353; // #define mp make_pair //#define endl '\n' void solve(ll y1, ll x1, ll y2, ll x2) { ll div = 1; rep (i, 29) div *= 3; ll ans = abs(y1 - y2) + abs(x1 - x2); while (div > 0) { ll y11 = y1 / div, x11 = x1 / div, y22 = y2 / div, x22 = x2 / div; if (y11 == y22 and y11 % 3 == 1 and abs(x11 - x22) >= 2) { // DEBUG(div); // DEBUG(pll(y11, x11)); // DEBUG(pll(y22, x22)); ll y3 = y1 % div, y4 = y2 % div; chmax(ans, abs(x1 - x2) + min(1 + y3 + 1 + y4, div - y3 + div - y4)); } if (x11 == x22 and x11 % 3 == 1 and abs(y11 - y22) >= 2) { // DEBUG(div); // DEBUG(pll(y11, x11)); // DEBUG(pll(y22, x22)); ll x3 = x1 % div, x4 = x2 % div; chmax(ans, abs(y1 - y2) + min(1 + x3 + 1 + x4, div - x3 + div - x4)); } div /= 3; } cout << ans << endl; } signed main() { fio(); int q; cin >> q; while (q--) { ll y1, x1, y2, x2; cin >> y1 >> x1 >> y2 >> x2; y1--; x1--; y2--; x2--; solve(y1, x1, y2, x2); } }
#include<iostream> #include<cstdint> #include<algorithm> int64_t calc(int64_t a1, int64_t a2, int64_t b1, int64_t b2, int lv, int64_t L) { if(lv == 0) return 0; int64_t Lb = L/3; int64_t bi_a1 = a1/Lb; int64_t bi_a2 = a2/Lb; int64_t bi_b1 = b1/Lb; int64_t bi_b2 = b2/Lb; if(bi_a1 == bi_b1 && bi_a2 == bi_b2) { return calc(a1%Lb, a2%Lb, b1%Lb, b2%Lb, lv-1, Lb); // same block } if(bi_a2 == bi_b2) { std::swap(a1, a2); std::swap(b1, b2); } // bi_a2 != bi_b2 while(L > 1) { Lb = L/3; bi_a1 = a1/Lb; bi_b1 = b1/Lb; if(bi_a1 != bi_b1) return std::abs(a1-b1) + std::abs(a2-b2); if(bi_a1 == 1 && std::abs(a2/Lb - b2/Lb) > 1) { a1 %= Lb; b1 %= Lb; return std::abs(a2 - b2) + std::min(a1+b1+2, Lb*2-a1-b1); } a1 %= Lb; b1 %= Lb; L = Lb; } return std::abs(a2 - b2); } int main() { int64_t L = 1; for(int i = 0; i < 30; ++i) L *= 3; int Q; std::cin >> Q; for(int i = 0; i < Q; ++i) { int64_t a, b, c, d; std::cin >> a >> b >> c >> d; std::cout << calc(a-1, b-1, c-1, d-1, 30, L) << std::endl; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll cost=0; ll a,b,c,d; void solve(ll sz) { ll x1=a/sz,y1=b/sz,x2=c/sz,y2=d/sz; if(x1!=x2||y1!=y2) { if(x1==x2&&x1%3==1&&abs(y1-y2)>1) { cost=min(a%sz+c%sz+2,2*sz-a%sz-c%sz)+abs(d-b); return; } if(y1==y2&&y1%3==1&&abs(x1-x2)>1) { cost=min(b%sz+d%sz+2,2*sz-b%sz-d%sz)+abs(a-c); return; } } if(sz>1) { solve(sz/3); } } int main(void) { ll q; ll three=1; scanf("%lld",&q); for(ll i=0;i<30;i++) three*=3; while(q--) { scanf("%lld%lld%lld%lld",&a,&b,&c,&d); a--,b--,c--,d--; cost=abs(a-c)+abs(b-d); solve(three/3); printf("%lld\n",cost); } }
/** * author: otera **/ #include<iostream> #include<string> #include<cstdio> #include<cstring> #include<vector> #include<cmath> #include<algorithm> #include<functional> #include<iomanip> #include<queue> #include<deque> #include<ciso646> #include<random> #include<map> #include<set> #include<complex> #include<bitset> #include<stack> #include<unordered_map> #include<utility> #include<cassert> using namespace std; #define int long long typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; typedef long double ld; const int inf=1e9+7; const ll INF=1LL<<60 ; const ll mod=1e9+7 ; #define rep(i,n) for(int i=0;i<n;i++) #define per(i,n) for(int i=n-1;i>=0;i--) #define Rep(i,sta,n) for(int i=sta;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define per1(i,n) for(int i=n;i>=1;i--) #define Rep1(i,sta,n) for(int i=sta;i<=n;i++) typedef complex<ld> Point; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair<int, int> P; typedef pair<ld, ld> LDP; typedef pair<ll, ll> LP; #define fr first #define sc second #define all(c) c.begin(),c.end() #define pb push_back #define debug(x) cerr << #x << " = " << (x) << endl; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } bool between(int x, int y1, int y2) { if(y1 > y2) swap(y1, y2); if(x % 3 == 1) { if(y2 - y1 >= 2) return true; } return false; } int extra(int x1, int y1, int x2, int y2) { int res = 0; int three = 1; rep(i, 32) { if(x1 / three == x2 / three and between(x1 / three, y1 / three, y2 / three)) { int tmp = min(min(x1 % three, x2 % three) + 1, min(three - x1 % three, three - x2 % three)); chmax(res, tmp); } three *= 3; } return res; } int dist(int x1, int y1, int x2, int y2) { return abs(x1 - x2) + abs(y1 - y2) + 2 * max(extra(x1, y1, x2, y2), extra(y1, x1, y2, x2)); } void solve() { int q; cin >> q; rep(i, q) { int a, b, c, d; cin >> a >> b >> c >> d; -- a, -- b, -- c, -- d; cout << dist(a, b, c, d) << endl; } } signed main() { ios::sync_with_stdio(false); cin.tie(0); //cout << fixed << setprecision(10); //int t; cin >> t; rep(i, t)solve(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef double db; typedef string str; typedef pair<int,int> pi; typedef pair<ll,ll> pl; typedef pair<ld,ld> pd; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<ld> vd; typedef vector<str> vs; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<pd> vpd; #define mp make_pair #define f first #define s second #define sz(x) (int)x.size() #define all(x) begin(x), end(x) #define rall(x) (x).rbegin(), (x).rend() #define rsz resize #define ins insert #define ft front() #define bk back() #define pf push_front #define pb push_back #define eb emplace_back #define lb lower_bound #define ub upper_bound #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define F0R(i,a) FOR(i,0,a) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) #define R0F(i,a) ROF(i,0,a) #define trav(a,x) for (auto& a: x) const int MOD = 1e9+7; // 998244353; const int MX = 2e5+5; const ll INF = 1e18; const ld PI = acos((ld)-1); const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1}; template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } int pct(int x) { return __builtin_popcount(x); } namespace input { template<class T> void re(complex<T>& x); template<class T1, class T2> void re(pair<T1,T2>& p); template<class T> void re(vector<T>& a); template<class T, size_t SZ> void re(array<T,SZ>& a); template<class T> void re(T& x) { cin >> x; } void re(double& x) { string t; re(t); x = stod(t); } void re(ld& x) { string t; re(t); x = stold(t); } template<class T, class... Ts> void re(T& t, Ts&... ts) { re(t); re(ts...); } template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = {a,b}; } template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); } template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); } template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); } } using namespace input; namespace output { void pr(int x) { cout << x; } void pr(long x) { cout << x; } void pr(ll x) { cout << x; } void pr(unsigned x) { cout << x; } void pr(unsigned long x) { cout << x; } void pr(unsigned long long x) { cout << x; } void pr(float x) { cout << x; } void pr(double x) { cout << x; } void pr(ld x) { cout << x; } void pr(char x) { cout << x; } void pr(const char* x) { cout << x; } void pr(const string& x) { cout << x; } void pr(bool x) { pr(x ? "true" : "false"); } template<class T> void pr(const complex<T>& x) { cout << x; } template<class T1, class T2> void pr(const pair<T1,T2>& x); template<class T> void pr(const T& x); template<class T, class... Ts> void pr(const T& t, const Ts&... ts) { pr(t); pr(ts...); } template<class T1, class T2> void pr(const pair<T1,T2>& x) { pr("{",x.f,", ",x.s,"}"); } template<class T> void pr(const T& x) { pr("{"); // const iterator needed for vector<bool> bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0; pr("}"); } void ps() { pr("\n"); } // print w/ spaces template<class T, class... Ts> void ps(const T& t, const Ts&... ts) { pr(t); if (sizeof...(ts)) pr(" "); ps(ts...); } void pc() { pr("]\n"); } // debug w/ commas template<class T, class... Ts> void pc(const T& t, const Ts&... ts) { pr(t); if (sizeof...(ts)) pr(", "); pc(ts...); } #define dbg(x...) pr("[",#x,"] = ["), pc(x); } using namespace output; namespace io { void setIn(string s) { freopen(s.c_str(),"r",stdin); } void setOut(string s) { freopen(s.c_str(),"w",stdout); } void setIO(string s = "") { ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O // cin.exceptions(cin.failbit); // throws exception when do smth illegal // ex. try to read letter into int if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO } } using namespace io; mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); ll po3[30]; ll dis(ll x, ll y, ll z) { return abs(x-z)+abs(y-z); } int main() { setIO(); po3[0] = 1; FOR(i,1,30) po3[i] = 3*po3[i-1]; int Q; re(Q); F0R(i,Q) { ll a,b,c,d; re(a,b,c,d); if (a > c) swap(a,c); if (b > d) swap(b,d); a--,b--,c--,d--; if (c-a > d-b) { swap(a,b); swap(c,d); } ll ans = c-a+d-b; R0F(j,30) if (a/po3[j] == c/po3[j]) if (a/po3[j]%3 == 1) { ll z = b/po3[j]+1; while (z%3 != 1) z++; if (z < d/po3[j]) { ans = min(dis(a,c,a/po3[j]*po3[j]-1),dis(a,c,(a/po3[j]+1)*po3[j]))+d-b; break; } } ps(ans); } // you should actually read the stuff at the bottom } /* stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized */
#include<iostream> #include<vector> #include<algorithm> using namespace std; int Q; long a,b,c,d; long T[31]; int main() { cin>>Q; T[0]=1; for(int i=1;i<=30;i++)T[i]=T[i-1]*3; for(;Q--;) { cin>>a>>b>>c>>d; a--,b--,c--,d--; if(a>c)swap(a,c); if(b>d)swap(b,d); long ans=c-a+d-b; long plus=0; for(int i=30;i>=1;i--) { if(a/T[i-1]==c/T[i-1]&&a/T[i-1]%3==1) { long B=b/T[i-1],D=d/T[i-1]; if(B>D)swap(B,D); if(B+2<=D) { a%=T[i]; c%=T[i]; plus=min(min(a,c)+1-T[i-1],2*T[i-1]-max(a,c)); break; } } if(b/T[i-1]==d/T[i-1]&&b/T[i-1]%3==1) { long A=a/T[i-1],C=c/T[i-1]; if(A>C)swap(A,C); if(A+2<=C) { b%=T[i]; d%=T[i]; plus=min(min(b,d)+1-T[i-1],2*T[i-1]-max(b,d)); break; } } } cout<<ans+plus*2<<endl; } }
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(0); int q; cin >> q; for (int _ = 0; _ < q; _++) { ll a, b, c, d; cin >> a >> b >> c >> d; a--; b--; c--; d--; if (abs(c - a) > abs(d - b)) swap(a, b), swap(c, d); if (b > d) swap(a, c), swap(b, d); //右上か右下へ進む //柱無視して(a, b)から(a, d)まで右へ進んで当たった最大の柱はどれか //bからdになる途中で、aが1な位が1になるか ll s = 1; ll i, l = 0; for (int k = 0; k < 30; k++) { if (a / s == c / s && a / s % 3 == 1) { ll t = b % (s * 3); if (t >= s) t -= s * 3; if (t + (d - b) >= s * 2) { i = a / s * s; l = s; } } s *= 3; } //cout << i << ' ' << l << endl; //cout << a << ' ' << b << ' ' << c << ' ' << d << endl; ll r; if (l == 0) { r = abs(c - a) + abs(d - b); } else { r = min((a - i + 1) + (c - i + 1), (i + l - a) + (i + l - c)) + abs(d - b); } cout << r << '\n'; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long LL; LL Pow[40]={1}; LL calc(LL x1,LL y1,LL x2,LL y2,int d){ if(d==-1)return abs(y2-y1); LL a=x1/Pow[d],b=y1/Pow[d],c=x2/Pow[d],d1=y2/Pow[d]; if(a!=c)return abs(x2-x1)+abs(y2-y1); else if(a==1&&b!=d1){ x1-=Pow[d],x2-=Pow[d]; return abs(y2-y1)+min(x1+x2+2,Pow[d]*2-x1-x2); } x1-=Pow[d]*a,x2-=Pow[d]*a; return calc(x1,y1,x2,y2,d-1); } LL cal(LL x1,LL y1,LL x2,LL y2,int level) { if(level==0) return abs(y1-y2); LL w=Pow[level-1]; if(x1/w!=x2/w) return abs(x1-x2)+abs(y1-y2);//不同层 if(x1/w==1&&abs(y1/w-y2/w)>=2) {//同层且在456这层 return min(x1%w+x2%w+2,w*2-x1%w-x2%w)+abs(y1-y2); } return cal(x1%w,y1,x2%w,y2,level-1); } LL work(){ LL a,b,c,d;scanf("%lld%lld%lld%lld",&a,&b,&c,&d); a--,b--,c--,d--;if(abs(a-c)>abs(b-d))swap(a,b),swap(c,d); return cal(a,b,c,d,30); } int main(){ for(int i=1;i<30;i++)Pow[i]=Pow[i-1]*3; int t;scanf("%d",&t); while(t--)printf("%lld\n",work()); return 0; }
#include <cstdlib> #include <cmath> #include <climits> #include <cfloat> #include <map> #include <utility> #include <set> #include <iostream> #include <memory> #include <string> #include <vector> #include <algorithm> #include <functional> #include <sstream> #include <deque> #include <complex> #include <stack> #include <queue> #include <cstdio> #include <cctype> #include <cstring> #include <ctime> #include <iterator> #include <bitset> #include <numeric> #include <list> #include <iomanip> #include <cassert> #if __cplusplus >= 201103L #include <array> #include <tuple> #include <initializer_list> #include <unordered_set> #include <unordered_map> #include <forward_list> #define cauto const auto& #define ALL(v) begin(v),end(v) #else #define ALL(v) (v).begin(),(v).end() #endif using namespace std; namespace{ typedef long long LL; typedef pair<int,int> pii; typedef pair<LL,LL> pll; typedef vector<int> vint; typedef vector<vector<int> > vvint; typedef vector<long long> vll, vLL; typedef vector<vector<long long> > vvll, vvLL; #define VV(T) vector<vector< T > > template <class T> void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){ v.assign(a, vector<T>(b, t)); } template <class T> inline T &chmin(T &x, const T &y){ return x = min(x, y); } template <class T> inline T &chmax(T &x, const T &y){ return x = max(x, y); } template <class F, class T> void convert(const F &f, T &t){ stringstream ss; ss << f; ss >> t; } #define REP(i,n) for(int i=0;i<int(n);++i) #define RALL(v) (v).rbegin(),(v).rend() #define MOD 1000000007LL #define EPS 1e-8 LL p3[31] = {1}; LL solve(LL a, LL b, LL c, LL d){ if(a > c){ a = p3[30] - 1 - a; c = p3[30] - 1 - c; } if(b > d){ b = p3[30] - 1 - b; d = p3[30] - 1 - d; } int l; for(l = 29; ; --l){ if(l < 0){ return c + d - a - b; } LL e = c / p3[l], f = d / p3[l]; if(e % 3 != 1){ --e; } if(e % 3 != 1){ --e; } if(f % 3 != 1){ --f; } if(f % 3 != 1){ --f; } if(e >= 0 && f >= 0 && (e + 1) * p3[l] - 1 >= a && (f + 1) * p3[l] - 1 >= b){ auto calc = [](LL x, LL y, LL z){ return abs(x - z) + abs(y - z); }; LL ret = 0; ret += min(calc(a, c, e * p3[l] - 1), calc(a, c, (e + 1) * p3[l])); ret += min(calc(b, d, f * p3[l] - 1), calc(b, d, (f + 1) * p3[l])); return ret; } } } void mainmain(){ for(int i = 1; i <= 30; ++i){ p3[i] = p3[i - 1] * 3; } int q; scanf("%d", &q); for(int i = 0; i < q; ++i){ LL a, b, c, d; scanf("%lld%lld%lld%lld", &a, &b, &c, &d); LL ans = solve(a - 1, b - 1, c - 1, d - 1); printf("%lld\n", ans); } } } int main() try{ // ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(4); mainmain(); } catch(...){ throw; }
#include<bits/stdc++.h> using lint=long long; int main(){ lint q;std::cin>>q; while(q--){ lint L=1;for(lint i=0;i<30;i++)L*=3; lint i0,j0,i1,j1;std::cin>>i0>>j0>>i1>>j1; i0--,j0--,i1--,j1--; for(;i0/L==i1/L&&j0/L==j1/L;L/=3); if(j0/L==j1/L)std::swap(i0,j0),std::swap(i1,j1); lint d=0; for(;L;L/=3){ lint I0=i0/L,I1=i1/L,J0=j0/L,J1=j1/L; if(I0!=I1)break; if(I0%3==1&&1<std::abs(J0-J1)){ lint r0=i0%L,r1=i1%L; d=std::min({r0+1,r1+1,L-r0,L-r1}); break; } } std::cout<<std::abs(i0-i1)+std::abs(j0-j1)+2*d<<'\n'; } }
#include<bits/stdc++.h> #define STDIO #define MOD 1000000007 #define testbit(mask,i) (((mask)>>(i))&1) #define setbit(mask,i) ((mask)|(1<<i)) #define delbit(mask,i) ((mask)^(1<<i)) #define lowbit(x) ((x)&(-x)) #define INF MOD using namespace std; int Q; long long a,b,c,d; bool between(long long x1,long long y1,long long y2){ if(y1>y2) swap(y1,y2); y1++; y2--; if(y1>y2) return false; while(x1>0){ if(x1%3LL==1LL){ if(y2-y1>3LL) return true; for(long long j=y1;j<=y2;j++) if(j%3LL==1LL) return true; } x1/=3LL; y1/=3LL; y2/=3LL; } return false; } long long get_detour(long long x1,long long y1,long long x2,long long y2){ long long ans=0LL,tmp,p3=1LL; for(int i=0;i<35;i++){ if(((x1/p3)==(x2/p3))&&between(x1/p3,y1/p3,y2/p3)){ tmp=min(min(x1%p3,x2%p3)+1LL,p3-max(x1%p3,x2%p3)); ans=max(ans,tmp); } p3*=3LL; } return ans; } long long get_path(long long x1,long long y1,long long x2,long long y2){ return llabs(x1-x2)+llabs(y1-y2)+2LL*max(get_detour(x1,y1,x2,y2),get_detour(y1,x1,y2,x2)); } int main(){ #ifndef STDIO freopen("input.in","r",stdin); freopen("output.out","w",stdout); #endif ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin>>Q; while(Q--){ cin>>a>>b>>c>>d; a--,b--,c--,d--; cout<<get_path(a,b,c,d)<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; // template {{{ #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple #define lb lower_bound #define ub upper_bound #define f first #define s second #define resz resize #define sz(x) int((x).size()) #define all(x) (x).begin(), (x).end() #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define F0R(i, a) for (int i = 0; i < (a); i++) #define FORd(i, a, b) for (int i = (b)-1; i >= (a); i--) #define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--) #define trav(a, x) for (auto& a : x) #define sort_by(x, y) sort(all(x), [&](const auto& a, const auto& b) { return y; }) using ll = long long; using ld = long double; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vd = vector<double>; using vs = vector<string>; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<double, double>; using vpii = vector<pii>; using vvpii = vector<vpii>; using vpll = vector<pll>; using vvpll = vector<vpll>; using vpdd = vector<pdd>; using vvpdd = vector<vpdd>; template<typename T> void ckmin(T& a, const T& b) { a = min(a, b); } template<typename T> void ckmax(T& a, const T& b) { a = max(a, b); } template<typename T> using max_heap = priority_queue<T>; template<typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); namespace __input { template<class T1, class T2> void re(pair<T1,T2>& p); template<class T> void re(vector<T>& a); template<class T, size_t SZ> void re(array<T,SZ>& a); template<class T> void re(T& x) { cin >> x; } void re(double& x) { string t; re(t); x = stod(t); } template<class Arg, class... Args> void re(Arg& first, Args&... rest) { re(first); re(rest...); } template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); } template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); } template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); } } using namespace __input; namespace __output { template<class T1, class T2> void pr(const pair<T1,T2>& x); template<class T, size_t SZ> void pr(const array<T,SZ>& x); template<class T> void pr(const vector<T>& x); template<class T> void pr(const deque<T>& x); template<class T> void pr(const set<T>& x); template<class T1, class T2> void pr(const map<T1,T2>& x); template<class T> void pr(const T& x) { cout << x; } template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) { pr(first); pr(rest...); } template<class T1, class T2> void pr(const pair<T1,T2>& x) { pr("{",x.f,", ",x.s,"}"); } template<class T, bool pretty = true> void prContain(const T& x) { if (pretty) pr("{"); bool fst = 1; for (const auto& a: x) pr(!fst?pretty?", ":" ":"",a), fst = 0; if (pretty) pr("}"); } template<class T> void pc(const T& x) { prContain<T, false>(x); pr("\n"); } template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); } template<class T> void pr(const vector<T>& x) { prContain(x); } template<class T> void pr(const deque<T>& x) { prContain(x); } template<class T> void pr(const set<T>& x) { prContain(x); } template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); } void ps() { pr("\n"); } template<class Arg> void ps(const Arg& first) { pr(first); ps(); } template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) { pr(first," "); ps(rest...); } } using namespace __output; #define TRACE(x) x #define __pn(x) pr(#x, " = ") #define pd(...) __pn((__VA_ARGS__)), ps(__VA_ARGS__), cout << flush namespace __numeric { template<typename T> typename enable_if<is_integral<T>::value, T>::type floor(T n, T d) { assert(d != 0); if (d < 0) tie(n, d) = mp(-n, -d); return n / d - ((n < 0) && (n % d)); } template<typename T> typename enable_if<is_integral<T>::value, T>::type ceil(T n, T d) { assert(d != 0); if (d < 0) tie(n, d) = mp(-n, -d); return n / d + ((n > 0) && (n % d)); } }; using namespace __numeric; namespace __algorithm { template<typename T> void dedup(vector<T>& v) { sort(all(v)); v.erase(unique(all(v)), v.end()); } template<typename T> typename vector<T>::iterator find(vector<T>& v, const T& x) { auto it = lower_bound(all(v), x); return it != v.end() && *it == x ? it : v.end(); } template<typename T> size_t index(vector<T>& v, const T& x) { auto it = find(v, x); assert(it != v.end() && *it == x); return it - v.begin(); } template<typename C, typename T, typename OP> vector<T> prefixes(const C& v, T id, OP op) { vector<T> r(sz(v)+1, id); F0R (i, sz(v)) r[i+1] = op(r[i], v[i]); return r; } template<typename C, typename T, typename OP> vector<T> suffixes(const C& v, T id, OP op) { vector<T> r(sz(v)+1, id); F0Rd (i, sz(v)) r[i] = op(v[i], r[i+1]); return r; } } using namespace __algorithm; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-parameter" struct monostate { friend istream& operator>>(istream& is, const monostate& ms) { return is; } friend ostream& operator<<(ostream& os, const monostate& ms) { return os; } friend monostate operator+(const monostate& a, const monostate& b) { return a; } } ms; #pragma GCC diagnostic pop namespace __io { void setIn(string s) { freopen(s.c_str(),"r",stdin); } void setOut(string s) { freopen(s.c_str(),"w",stdout); } void setIO(string s = "") { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(15); if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } } } using namespace __io; // }}} ll count(ll v) { return v/3 + ((v % 3) >= 1); } int main() { setIO(); int Q; re(Q); F0R (q, Q) { ll a, b, c, d; re(a, b, c, d); --a, --b, --c, --d; ll ans = abs(a - c) + abs(b - d); ll sz = 1; F0R (pw, 30) { ll A = a/sz, B = b/sz, C = c/sz, D = d/sz; //pd(A, B, C, D, sz); if (A == C && B != D && (((a % (3 * sz)) / sz) == 1)) { //pd(a, b, c, d, sz); ll betw = count(max(B, D)) - count(min(B, D)); if (betw > 0) { //pd(a, b, c, d, sz); ll ar = a % sz, cr = c % sz; //pd(ar, cr); ans = abs(b - d) + min(ar + cr + 2, 2 * sz - ar - cr); } } if (A != C && B == D && (((b % (3 * sz)) / sz) == 1)) { //pd(a, b, c, d, sz); ll betw = count(max(A, C)) - count(min(A, C)); if (betw > 0) { //pd(a, b, c, d, sz); ll br = b % sz, dr = d % sz; ans = abs(a - c) + min(br + dr + 2, 2 * sz - br - dr); } } sz *= 3; } ps(ans); } // did you check N=1? did you mix up N,M? // check your "infinity" and "null" values against the bounds return 0; }
#include<bits/stdc++.h> #define rgi register int #define ll long long class fastin{ private: int _ch,_f; public: template<typename _Tp> inline fastin& operator>>(_Tp&_x){ _x=0; while(!isdigit(_ch)) _f|=(_ch==45),_ch=getchar(); while(isdigit(_ch)) _x=(_x<<1)+(_x<<3)+(_ch^48),_ch=getchar(); _f&&(_x=-_x,_f=0); return *this;} fastin() {_ch=_f=0;} }fin; class fastout{ private: int _num[98],_head; public: int SUF; template<typename _Tp> inline fastout& operator<<(_Tp _x){ _Tp _k; if(_x==0) {putchar('0'),putchar(SUF);return *this;} if(_x<0) putchar('-'),_x=-_x; while(_x>0) _k=_x/10,++_head,_num[_head]=(_x-(_k<<1)-(_k<<3))^48,_x=_k; while(_head>0) putchar(_num[_head]),--_head; putchar(SUF); return *this;} fastout() {_head=0,SUF=' ';} }fout; // ---------------------------- #define int ll // #define abs(x) (x>0?x:-x) using namespace std; const int maxn=200004; const int mod=998244353,inf=1000000007; int a,b,c,d; inline ll solve() { --a,--b,--c,--d; int res=abs(a-c)+abs(b-d),t=1LL; for(rgi i=1;i<=33;++i) t*=3LL; for(rgi i=33;i>=0;--i) { for(rgi j=0;j<2;++j) { if(a/t==c/t&&(a/t)%3LL==1LL&&abs(b/t-d/t)!=1&&abs(b-d)>=t) { int mn=min(a,c),mx=max(a,c); // cout<<t<<endl; // cout<<mn<<" "<<mx<<endl; // cout<<mn-((mn-1)/t)*t<<" "<<((mx-1)/t+1)*t-mx+1<<endl; return res+min(mn-mn/t*t+1LL,(mx/t+1LL)*t-mx)*2LL; } swap(a,b),swap(c,d); } t/=3LL; } return res; } signed main() { rgi q; cin>>q; while(q--) { cin>>a>>b>>c>>d; cout<<solve()<<endl; } return 0; } // ---------------------------- // by imzzy /* 10 4 2 7 4 9 9 1 9 2 1 2 3 4 3 4 7 5 3 5 7 6 3 6 7 4 3 6 7 10 9 19 9 */
#include <iostream> #include <vector> #include <algorithm> #include <functional> using namespace std; using ll = long long; vector<ll> memo3(31); ll pow3(ll a) { if (a == 0) return 1; if (memo3[a] == 0) return memo3[a] = pow3(a - 1) * 3; else return memo3[a]; } ll getIth(ll x, ll i) { return (x / pow3(i)) % 3; } ll getOverIth(ll x, ll i) { return x / pow3(i); } ll mdist(ll a, ll b, ll c, ll d) { return abs(a - c) + abs(b - d); } ll sameXBlackBoxDist(ll rank, ll a, ll b, ll c, ll d) { return min(a % pow3(rank) + c % pow3(rank) + 2, pow3(rank) * 2 - a % pow3(rank) - c % pow3(rank)) + abs(b - d); } ll sameXDist(ll rank, ll a, ll b, ll c, ll d) { if (rank == 0) return mdist(a, b, c, d); else { ll abit = getIth(a, rank - 1); ll bbit = getOverIth(b, rank - 1); ll cbit = getIth(c, rank - 1); ll dbit = getOverIth(d, rank - 1); if (abit != cbit) return mdist(a, b, c, d); if (abit == 1) { if (abs(bbit - dbit) <= 1) { return sameXDist(rank - 1, a, b, c, d); } else { return sameXBlackBoxDist(rank - 1, a, b, c, d); } } else { return sameXDist(rank - 1, a, b, c, d); } } } ll dist(ll rank, ll a, ll b, ll c, ll d) { if (rank == 0) return 0; else { ll abit = getIth(a, rank - 1); ll bbit = getIth(b, rank - 1); ll cbit = getIth(c, rank - 1); ll dbit = getIth(d, rank - 1); if (abit == cbit && bbit == dbit) return dist(rank - 1, a, b, c, d); if (abit == cbit) { if (abit == 1) return sameXBlackBoxDist(rank - 1, a, b, c, d); return sameXDist(rank - 1, a, b, c, d); } if (bbit == dbit) { if (bbit == 1) return sameXBlackBoxDist(rank - 1, b, a, d, c); return sameXDist(rank - 1, b, a, d, c); } if (abit != cbit && bbit != dbit) return mdist(a, b, c, d); throw "Fool!"; } } int main() { /* for (int i = 0; i < 30; i++) cout << pow3(i) << endl; /*/ int Q; cin >> Q; for (int i = 0; i < Q; i++) { ll a, b, c, d; cin >> a >> b >> c >> d; cout << dist(30, a - 1, b - 1, c - 1, d - 1) << endl; } //*/ }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (n); i++) #define repr(i, n) for (int i = (n) - 1; i >= 0; i--) #define range(a) a.begin(), a.end() ll f(ll n, ll a, ll b, ll c, ll d) { if (a > c) { a = n - 1 - a; c = n - 1 - c; } if (b > d) { b = n - 1 - b; d = n - 1 - d; } if (n == 3) { if (a == 1 && b == 0 && c == 1 && d == 2) { return 4; } if (a == 0 && b == 1 && c == 2 && d == 1) { return 4; } return abs(c - a) + abs(d - b); } ll m = n / 3; if (a / m == c / m && b / m == d / m) { return f(m, a % m, b % m, c % m, d % m); } if (a / m != c / m && b / m != d / m) { return abs(c - a) + abs(d - b); } if (a / m == 0 && b / m == 1 && c / m == 2 && d / m == 1) { swap(a, b); swap(c, d); } if (a / m == 1 && b / m == 0 && c / m == 1 && d / m == 2) { return min( f(n, a, b, m - 1, m - 1) + f(n, m - 1, m - 1, c, d), f(n, a, b, 2*m, m-1) + f(n, 2*m, m-1, c, d) ); } if (a / m == c / m) { a %= m; c %= m; } else { b %= m; d %= m; } if (a / m < c / m) { swap(a, b); swap(c, d); } ll o = m / 3; if (a / o != c / o && b / o != d / o) { return abs(c - a) + abs(d - b); } if (a / o == 2) { a %= m; c %= m; } if (d / o - b / o == 1) { return f(m, a, b % o, c, d % o + o); } return f(m, a, b % o, c, d % o + 2 * o) + (d / o - b / o - 2) * o; } int main() { int Q; cin >> Q; ll p = 1; for (int i = 1; i <= 30; i++) { p *= 3; } while (Q--) { ll a, b, c, d; cin >> a >> b >> c >> d; a--; b--; c--; d--; cout << f(p, a, b, c, d) << endl; } }
#pragma warning (disable: 6031) #pragma warning (disable: 26451) #include <stdio.h> #include <algorithm> #include <string> #include <vector> #include <numeric> #include <queue> #include <map> //const int mod = 1000000007; const int mod = 998244353; template <class T> T maximum(T a, T b) { return a < b ? b : a; } template <class T> T minimum(T a, T b) { return a > b ? b : a; } template <class T> void swap(T& a, T& b) { T c = a; a = b; b = c; } template <class T> T abs_(T a) { return a < 0 ? -a : a; } using std::pair; using std::vector; long long size[31]{ 1 }; long long rec(long long, long long, long long, long long, long long, bool); long long sq(long long a, long long b, long long c, long long d, long long s) { s /= 3; long long aa = a / s; long long bb = b / s; long long cc = c / s; long long dd = d / s; if (bb == dd) { swap(a, b); swap(c, d); swap(aa, bb); swap(cc, dd); } if (aa != cc)return abs_(a - c) + abs_(b - d); if (bb == dd) return sq(a % s, b % s, c % s, d % s, s); return rec(a % s, b, c % s, d, s, aa == 1); } long long rec(long long a, long long b, long long c, long long d, long long w, bool bl) { if (!bl) { if (w == 1)return abs_(a - c) + abs_(b - d); w /= 3; long long aa = a / w; long long cc = c / w; if (aa == cc)return rec(a % w, b, c % w, d, w, aa == 1); return abs_(a - c) + abs_(b - d); } else { if (abs_((b / w) - (d / w)) == 1)return rec(a, b, c, d, w, false); return abs_(b - d) + minimum(a + c + 2, 2 * w - a - c); } } int main() { for (int i = 0; i < 30; i++) { size[i + 1] = size[i] * 3; } int Q; scanf("%d", &Q); auto res = new long long[Q]; for (int i = 0; i < Q; i++) { long long a, b, c, d; scanf("%lld %lld %lld %lld", &a, &b, &c, &d); res[i] = sq(a - 1, b - 1, c - 1, d - 1, size[30]); } for (int i = 0; i < Q; i++)printf("%lld\n", res[i]); return 0; }
// includes #include <bits/stdc++.h> using namespace std; // macros #define pb emplace_back #define mk make_pair #define FOR(i, a, b) for(int i=(a);i<(b);++i) #define rep(i, n) FOR(i, 0, n) #define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--) #define irep(itr, st) for(auto itr = (st).begin(); itr != (st).end(); ++itr) #define irrep(itr, st) for(auto itr = (st).rbegin(); itr != (st).rend(); ++itr) #define whole(x) (x).begin(),(x).end() #define sz(x) ((int)(x).size()) #define bit(n) (1LL<<(n)) // functions template <typename T> void unique(T& c){c.erase(std::unique(c.begin(), c.end()), c.end());} template <class T>bool chmax(T &a, const T &b){if(a < b){a = b; return 1;} return 0;} template <class T>bool chmin(T &a, const T &b){if(a > b){a = b; return 1;} return 0;} template <typename T> istream &operator>>(istream &is, vector<T> &vec){for(auto &v: vec)is >> v; return is;} template <typename T> ostream &operator<<(ostream &os, const vector<T>& vec){for(int i = 0; i < vec.size(); i++){ os << vec[i]; if(i + 1 != vec.size())os << " ";} return os;} template <typename T> ostream &operator<<(ostream &os, const set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;} template <typename T> ostream &operator<<(ostream &os, const unordered_set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;} template <typename T> ostream &operator<<(ostream &os, const multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;} template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;} template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p){os << "(" << p.first << ", " << p.second << ")"; return os;} template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if(++titr != mp.end())os << " "; } return os;} template <typename T1, typename T2> ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if(++titr != mp.end())os << " "; } return os;} // types using ll = long long int; using P = pair<int, int>; // constants const int inf = 1e9; const ll linf = 1LL << 50; const double EPS = 1e-10; const int mod = 1000000007; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, -1, 0, 1}; // io struct fast_io{ fast_io(){ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(20);} } fast_io_; ll th[33]; int main(int argc, char const* argv[]) { int q; cin >> q; th[0] = 1; FOR(i, 1, 31)th[i] = 3LL * th[i-1]; rep(i_, q){ ll a, b, c, d; cin >> a >> b >> c >> d; a--; b--; c--; d--; ll res = abs(a - c) + abs(b - d); for(int k = 30; k >= 1; k--){ ll ba = a / th[k]; ll bb = b / th[k]; ll bc = c / th[k]; ll bd = d / th[k]; if(bb == bd){ if(a > c){ swap(a, c); swap(b, d); swap(ba, bc); swap(bb, bd); } if(bb * th[k] + th[k-1] <= b && b < bb * th[k] + 2*th[k-1]){ if(bb * th[k] + th[k-1] <= d && d < bb * th[k] + 2 * th[k-1]){ bool ok = false; if(a < ba * th[k] + th[k-1] && c >= ba * th[k] + 2 * th[k-1])ok = true; if(ba * th[k] + 2 * th[k-1] <= a && c >= (ba + 1) * th[k] + 2 * th[k-1])ok = true; if(ok){ res = linf; ll tmp = (b - bb * th[k] - th[k-1] + 1) + (d - bb * th[k] - th[k-1] + 1) + (c - a); chmin(res, tmp); tmp = (bb * th[k] + 2 * th[k-1] - b) + (bb * th[k] + 2 * th[k-1] - d) + (c - a); chmin(res, tmp); break; } } } } if(ba == bc){ if(b > d){ swap(a, c); swap(b, d); swap(ba, bc); swap(bb, bd); } if(ba * th[k] + th[k-1] <= a && a < ba * th[k] + 2 * th[k-1]){ if(ba * th[k] + th[k-1] <= c && c < ba * th[k] + 2 * th[k-1]){ bool ok = false; if(bb * th[k] + th[k-1] > b && (d >= bb * th[k] + 2 * th[k-1]))ok = true; if(bb * th[k] + 2 * th[k-1] <= b && d >= (bb + 1) * th[k] + 2 * th[k-1])ok = true; if(ok){ res = linf; ll tmp = (a - ba * th[k] - th[k-1] + 1) + (c - ba * th[k] - th[k-1] + 1) + (d - b); chmin(res, tmp); tmp = (ba * th[k] + 2 * th[k-1] - a) + (ba * th[k] + 2 * th[k-1] - c) + (d - b); chmin(res, tmp); break; } } } } } cout << res << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; ////////////////////////////// Begin Macros #define ALL(x) (x).begin(), (x).end() #define rep(i, N) for (int i = 0; i < N; i++) #define rep1(i, N) for (int i = 1; i <= N; i++) using ll = long long int; using pii = pair<int, int>; using pll = pair<ll, ll>; template <typename T> bool chmax(T &m, const T q) { if (m < q) { m = q; return true; } else return false; } template <typename T> bool chmin(T &m, const T q) { if (m > q) { m = q; return true; } else return false; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return {l.first + r.first, l.second + r.second}; } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return {l.first - r.first, l.second - r.second}; } template <typename T> pair<T, T> operator*(const pair<T, T> &l, const T &r) { return {l.first * r, l.second * r}; } template <typename T> pair<T, T> operator/(const pair<T, T> &l, const T &r) { return {l.first / r, l.second / r}; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename T> void reset(vector<T> &v, const T reset_to) { for (auto &x : v) x = reset_to; } inline int popcount(const unsigned int x) { return __builtin_popcount(x); } #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; const int intinf = numeric_limits<int>::max(); #define MOD 1000000007 const pii udlr[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; ////////////////////////////// End Macros ll L1norm(pll xy) { return abs(xy.first) + abs(xy.second); } ll dist(pll ab, pll cd, pii level) { if (level.first == 0) return abs(ab.second - cd.second); ll pow_three_x = pow(3, level.first - 1); int A = ab.first / pow_three_x; int C = cd.first / pow_three_x; if (A != C) return L1norm(ab - cd); ab.first -= A * pow_three_x; cd.first -= A * pow_three_x; level.first--; if (A != 1) return dist(ab, cd, level); // A==C==1 ll B = (ab.second + pow_three_x) / pow_three_x / 3; ll D = (cd.second + pow_three_x) / pow_three_x / 3; if (B == D) return dist(ab, cd, level); ll dx = min(abs(ab.first - (-1)) + abs(cd.first - (-1)), abs(ab.first - pow_three_x) + abs(cd.first - pow_three_x)); ll dy = abs(ab.second - cd.second); return dx + dy; } void solve() { int Q; cin >> Q; rep(q, Q) { ll a, b, c, d; cin >> a >> b >> c >> d; a--; b--; c--; d--; const pii level_init = {30, 30}; ll ans = max(dist({a, b}, {c, d}, level_init), dist({b, a}, {d, c}, level_init)); cout << ans << endl; } } const int maxlevel = 7; const int size_fractal = pow(3, maxlevel); vector<vector<bool>> fractal(size_fractal, vector<bool>(size_fractal, false)); void make_fractal(int x, int y, int level) { if (level == 0) { fractal[x][y] = false; return; } ll pow_three = pow(3, level - 1); rep(i, 3) rep(j, 3) { if (i == 1 and j == 1) { rep(ii, pow_three) rep(jj, pow_three) fractal[x + pow_three + ii][y + pow_three + jj] = true; continue; } make_fractal(x + i * pow_three, y + j * pow_three, level - 1); } } ll distTest(pll ab, pll cd) { ll ans = 0; vector<vector<bool>> is_reached(size_fractal, vector<bool>(size_fractal, false)); queue<pll> q; q.push(ab); is_reached[ab.first][ab.second] = true; while (!is_reached[cd.first][cd.second]) { queue<pll> nextq; while (!q.empty()) { pll xy = q.front(); q.pop(); rep(k, 4) { int nx = xy.first + udlr[k].first; int ny = xy.second + udlr[k].second; if (!(0 <= nx and nx < size_fractal and 0 <= ny and ny < size_fractal)) continue; if (!fractal[nx][ny] and !is_reached[nx][ny]) { nextq.push({nx, ny}); is_reached[nx][ny] = true; } } } q = nextq; ans++; } return ans; } void test() { make_fractal(0, 0, maxlevel); int cnt = 0; while (cnt < 100) { // rep(i, size_fractal) cout << i << fractal[i] << endl; ll a, b, c, d; a = b = c = d = 1; // cin >> a >> b >> c >> d; while (fractal[a][b] or fractal[c][d]) { a = rand() % size_fractal; b = rand() % size_fractal; c = rand() % size_fractal; d = rand() % size_fractal; } ll dTest = distTest({a, b}, {c, d}); ll dAns = max(dist({a, b}, {c, d}, {maxlevel, maxlevel}), dist({b, a}, {d, c}, {maxlevel, maxlevel})); if (dAns != dTest) { cout << "Hack" << endl; cout << a << " " << b << " " << c << " " << d << endl; cout << "distTest: " << dTest << endl; cout << "distAns: " << dAns << endl; cin.ignore(); } cnt++; } cout << "OK" << endl; } int main() { solve(); return 0; }
// ...Jggg+J+JJJg@NQmgJa....., // ....gH@@@@HHB""""7"YYYYHMMMMMMM@@@@@@@Hma,. // ...JH@@MMHY"! ? __""YH@@@@N&... // ..JH@@HY"~ _TW@@@Mme. // .Jg@@#"= _TTM@@N.. // .Jg@@MY! ?T@@@h,. // .-@@@B! (TM@@@L // .(H@MY ?W@@@+ // .W@@@ .T@@@[ // .d@@M! .T@@N, // .d@M@' -W@@o. // (@@M\ ?M@N, // -d@M% .., .., j@@b // d@@M= TM9 ?MD W@@[ // .H@M: .W@H, // H@Ht ,@@# // (@@M~ .@@h. // .@@M% ..gmHHJ. .JdHga. .H@@e // j@@#_ .@@@@@@@b J@@@@@@@h. .H@@\ // d@@@ .4@@@@@@MF (@@@@@@@@ H@@b // d@@[ ?"BMY"= .d@@@@H, ?TWHHY"! d@@e // J@@b .JJJ-.., ,@@@@@@M% ......... -@@@M. // ?@@M\ ?YHHM@@@@b .. .W@@HP X@@HMWY"= d@@@# // ,@@@L. ?H@Ng&+gd@@#H@@NHaJ+gH@[ J@@@] // X@@@[ ...... ?"YYYYY"" ?"YHHHB"^ ..... (@@@# // WH@N+. .W@@@@MHB= .TWH@M@Hmc .H@@M~ // .H@@@@N, _!~ .d@@@N, // .J@@#T@@@N, .d@@@@@@@b. // (@@@@! .T@@@n, .(H@@@H>.W@@@x // (@@@F 4@@@@MaJ. .d@@@@Y77 4@@@r //.H@@P ?TM@@@@N... .-JH@HMY= d@@N, //(@@@F ?"WM@@@MQa-,. .(J(JN@@M#" Z@@@L // d@@H, (M@@@@@@@Ng&maJ.... .. ...J.J+W@@@@@@HY! .dH@b // ?M@@@N&. ..(JW@@@MM"?7""TYHMH@@HH@@@@@HHHgkHagHa(mggdmmagH@H@@Q@@HMMMHY"7!TMM@@@HaJ,. ..H@@@M^ // ?"W@@@@MN@@@@@H@@MMY" _???!"= ?WMMBYYTMH=7""Y@""?"~^ _"YM@@@@@@@@MH@@@@@#"^ // ?77WMMMYB""! _7"WWMMMY"7= // need #include <iostream> #include <algorithm> // data structure #include <bitset> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #include <complex> //#include <deque> #include <valarray> #include <unordered_map> #include <unordered_set> #include <array> // etc #include <cassert> #include <cmath> #include <functional> #include <iomanip> #include <chrono> #include <random> #include <numeric> #include <fstream> // input #define INIT std::ios::sync_with_stdio(false);std::cin.tie(0); #define VAR(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__); template<typename T> void MACRO_VAR_Scan(T& t) { std::cin >> t; } template<typename First, typename...Rest>void MACRO_VAR_Scan(First& first, Rest& ...rest) { std::cin >> first; MACRO_VAR_Scan(rest...); } #define VEC_ROW(type, n, ...)std::vector<type> __VA_ARGS__;MACRO_VEC_ROW_Init(n, __VA_ARGS__); for(int w_=0; w_<n; ++w_){MACRO_VEC_ROW_Scan(w_, __VA_ARGS__);} template<typename T> void MACRO_VEC_ROW_Init(int n, T& t) { t.resize(n); } template<typename First, typename...Rest>void MACRO_VEC_ROW_Init(int n, First& first, Rest& ...rest) { first.resize(n); MACRO_VEC_ROW_Init(n, rest...); } template<typename T> void MACRO_VEC_ROW_Scan(int p, T& t) { std::cin >> t[p]; } template<typename First, typename...Rest>void MACRO_VEC_ROW_Scan(int p, First& first, Rest& ...rest) { std::cin >> first[p]; MACRO_VEC_ROW_Scan(p, rest...); } #define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i; #define MAT(type, c, m, n) std::vector<std::vector<type>> c(m, std::vector<type>(n));for(auto& R:c)for(auto& w:R)std::cin>>w; // output template<typename T>void MACRO_OUT(const T t) { std::cout << t; } template<typename First, typename...Rest>void MACRO_OUT(const First first, const Rest...rest) { std::cout << first << " "; MACRO_OUT(rest...); } #define OUT(...) MACRO_OUT(__VA_ARGS__); #define FOUT(n, dist) std::cout<<std::fixed<<std::setprecision(n)<<(dist); #define SOUT(n, c, dist) std::cout<<std::setw(n)<<std::setfill(c)<<(dist); #define EOUT(...) { OUT(__VA_ARGS__)BR; exit(0); } #define SP std::cout<<" "; #define TAB std::cout<<"\t"; #define BR std::cout<<"\n"; #define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' '); #define ENDL std::cout<<std::endl; #define FLUSH std::cout<<std::flush; #define SHOW(dist) {std::cerr << #dist << "\t: " << (dist) << "\n";} #define SHOWVECTOR(v) {std::cerr << #v << "\t: ";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";} #define SHOWVECTOR2(v) {std::cerr << #v << "\t:\n";for(const auto& xxx : v){for(const auto& yyy : xxx){std::cerr << yyy << " ";}std::cerr << "\n";}} #define SHOWQUEUE(a) {auto tmp(a);std::cerr << #a << "\t: ";while(!tmp.empty()){std::cerr << tmp.front() << " ";tmp.pop();}std::cerr << "\n";} #define SHOWSTACK(a) {auto tmp(a);std::cerr << #a << "\t: ";while(!tmp.empty()){std::cerr << tmp.top() << " ";tmp.pop();}std::cerr << "\n";} // utility #define ALL(a) (a).begin(),(a).end() #define FOR(w, a, n) for(int w=(a);w<(n);++w) #define RFOR(w, a, n) for(int w=(n)-1;w>=(a);--w) #define REP(w, n) for(int w=0;w<int(n);++w) #define RREP(w, n) for(int w=int(n)-1;w>=0;--w) #define IN(a, x, b) (a<=x && x<b) template<class T> inline T CHMAX(T & a, const T b) { return a = (a < b) ? b : a; } template<class T> inline T CHMIN(T& a, const T b) { return a = (a > b) ? b : a; } // test template<class T> using V = std::vector<T>; template<class T> using VV = V<V<T>>; template<typename S, typename T> std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; } // type/const #define int ll using ll = long long; using ull = unsigned long long; using ld = long double; using PAIR = std::pair<int, int>; using PAIRLL = std::pair<ll, ll>; constexpr int INFINT = (1 << 30) - 1; // 1.07x10^ 9 constexpr int INFINT_LIM = (1LL << 31) - 1; // 2.15x10^ 9 constexpr ll INFLL = 1LL << 60; // 1.15x10^18 constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); // 9.22x10^18 constexpr double eps = 1e-6; constexpr int MOD = 1000000007; constexpr double PI = 3.141592653589793238462643383279; template<class T, size_t N> void FILL(T(&a)[N], const T & val) { for (auto& x : a) x = val; } template<class ARY, size_t N, size_t M, class T> void FILL(ARY(&a)[N][M], const T & val) { for (auto& b : a) FILL(b, val); } template<class T> void FILL(std::vector<T> & a, const T & val) { for (auto& x : a) x = val; } template<class ARY, class T> void FILL(std::vector<std::vector<ARY>> & a, const T & val) { for (auto& b : a) FILL(b, val); } // ------------>8------------------------------------->8------------ signed main() { INIT; V<int> p3(31, 1); FOR(i, 1, 31) p3[i] = p3[i - 1] * 3; VAR(int, Q); REP(_, Q) { VAR(int, x1, y1, x2, y2); --x1; --y1; --x2; --y2; int ans = std::abs(x1 - x2) + std::abs(y1 - y2); REP(i_, 2) { std::swap(x1, y1); std::swap(x2, y2); if (x1 == x2) continue; if (x1 > x2) { std::swap(x1, x2); std::swap(y1, y2); } assert(x1 < x2); REP(k, 30) { int X = x1 % p3[k + 1] / p3[k]; int Y = y1 % p3[k + 1] / p3[k]; if (Y == 1) { int X1 = x1 / p3[k] * p3[k]; int Y1 = y1 / p3[k] * p3[k]; if (X == 0) X1 += p3[k]; else X1 += 2 * p3[k]; if (X1 <= x2 && IN(Y1, y2, Y1 + p3[k])) { CHMAX(ans, std::min(std::abs(y1 - (Y1 - 1)) + std::abs(x1 - x2) + std::abs((Y1 - 1) - y2), std::abs(y1 - (Y1 + p3[k])) + std::abs(x1 - x2) + std::abs((Y1 + p3[k]) - y2))); } } } } OUT(ans)BR; } return 0; }
#include<bits/stdc++.h> #define rgi register int #define ll long long ll a,b,c,d; inline ll solve() { --a,--b,--c,--d; ll res=std::abs(a-c)+std::abs(b-d),t=1LL; for(rgi i=1;i<=33;++i) t*=3LL; for(rgi i=33;i>=0;--i) { for(rgi j=0;j<2;++j) { if(a/t==c/t&&(a/t)%3LL==1LL&&std::abs(b/t-d/t)!=1LL&&std::abs(b-d)>=t) { ll mn=std::min(a,c),mx=std::max(a,c); return res+std::min(mn-mn/t*t+1LL,(mx/t+1LL)*t-mx)*2LL; } std::swap(a,b),std::swap(c,d); } t/=3LL; } return res; } signed main() { rgi q; std::cin>>q; while(q--) { std::cin>>a>>b>>c>>d; std::cout<<solve()<<std::endl; } return 0; } // ---------------------------- // by imzzy
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; #define pb push_back #define eb emplace_back #define mp make_pair #define fi first #define se second #define rep(i, n) rep2(i, 0, n) #define rep2(i, m, n) for (int i = m; i < (n); i++) #define ALL(c) (c).begin(), (c).end() constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } template <class T, class U> void chmin(T& t, const U& u) { if (t > u) t = u; } template <class T, class U> void chmax(T& t, const U& u) { if (t < u) t = u; } template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << "(" << p.first << "," << p.second << ")"; return os; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { os << "{"; rep(i, v.size()) { if (i) os << ","; os << v[i]; } os << "}"; return os; } #ifdef LOCAL void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...); } #define debug(...) \ cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif bool obs(ll a, ll b) { if (a > b) swap(a, b); for (ll m = a + 1; m < min(b, a + 4); ++m) { if (m % 3 == 1) return true; } return false; } ll solve(ll a, ll b, ll c, ll d) { ll bl = 1; ll loss = 0; rep(i, 30) { // yoko ll p = a / bl, q = b / bl, r = c / bl, s = d / bl; if (p == r && p % 3 == 1 && obs(q, s)) { ll d1 = min(a % bl, c % bl) + 1; ll d2 = bl - max(a % bl, c % bl); chmax(loss, min(d1, d2)); debug(i, d1, d2); } // tate if (q == s && q % 3 == 1 && obs(p, r)) { ll d1 = min(b % bl, d % bl) + 1; ll d2 = bl - max(b % bl, d % bl); chmax(loss, min(d1, d2)); debug(i, d1, d2); } bl *= 3; } return loss * 2 + abs(a - c) + abs(b - d); } int main() { int Q; cin >> Q; while (Q--) { ll a, b, c, d; cin >> a >> b >> c >> d; --a, --b, --c, --d; cout << solve(a, b, c, d) << endl; } return 0; }
#include <bits/stdc++.h> #include <iomanip> using namespace std; #define reps(i,s,n) for(int i = s; i < n; i++) #define rep(i,n) reps(i,0,n) #define Rreps(i,n,e) for(int i = n - 1; i >= e; --i) #define Rrep(i,n) Rreps(i,n,0) #define ALL(a) a.begin(), a.end() #define fi first #define se second typedef long long ll; typedef vector<ll> vec; typedef vector<vec> mat; ll N,M,H,W,K,Q,A,B; //const ll MOD = 998244353; const ll MOD = (1e+9) + 7; const ll INF = 1LL<<60; typedef pair<ll, ll> P; vec san(32, 1); int bit3_t(ll a, int t){ return (a % san[t + 1]) / san[t]; } bool between(ll x, ll y1, ll y2){ if(y1 > y2) swap(y1, y2); ++y1; --y2; if(!(y1 <= y2)) return false; while(x > 0){ if(x%3 == 1){ if(y2 - y1 > 3) return true; for(ll y = y1; y <= y2; ++y) if(y%3 == 1) return true; } x /= 3; y1 /= 3; y2 /= 3; } return false; } ll f(ll a, ll b, ll c, ll d){ ll res = 0; Rrep(i, 31){ /*if(bit3_t(b, i) == 1 && bit3_t(d, i) == 1){ swap(a, b); swap(c, d); }*/ if(bit3_t(a, i) == 1 && bit3_t(c, i) == 1 && a / san[i] == c / san[i]){ //if(abs(d - b) >= 2 * san[i] //|| abs((d % (san[i + 1])) / san[i] - (b % (san[i + 1])) / san[i]) == 2){ if(between(a / san[i], b / san[i], d / san[i])){ ll diff1 = a % san[i], diff2 = c % san[i]; res = max(res, min({diff1 + 1, san[i] - diff1, diff2 + 1, san[i] - diff2}) * 2); } } } return res; } int main() { cin>>Q; reps(i, 1, 32) san[i] = san[i-1] * 3; rep(i,Q){ vec v(4); rep(j,4){ cin>>v[j]; --v[j]; } ll res = abs(v[0] - v[2]) + abs(v[1] - v[3]); //cout<<res<<endl; cout<<res + max(f(v[0], v[1], v[2], v[3]), f(v[1], v[0], v[3], v[2]))<<endl; } }
/* Generated by powerful Codeforces Tool * You can download the binary file in here https://github.com/xalanq/cf-tool (win, osx, linux) * Author: step_by_step * Time: 2020-03-03 17:35:02 **/ //#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("unroll-loops") #include <iostream> #include <stdlib.h> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <random> #include <assert.h> #include <memory.h> #include <time.h> #include <bitset> #define uint unsigned int #define ll long long #define ull unsigned long long #define ld long double #define rep(i, l, r) for (int i = (l); i < (r); i++) #define repb(i, r, l) for (int i = (r); i > (l); i--) #define sz(a) (int)a.size() #define fi first #define se second #define mp(a, b) make_pair(a, b) #define rank qwertyuio #define next dfghjk #define prev fhsgfhjf #define plus fsghsf #define minus ytryr #define y1 gfvtydvtd using namespace std; inline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; } inline bool setmax(int &x, int y) { return (y > x) ? x = y, 1 : 0; } inline bool setmin(ll &x, ll y) { return (y < x) ? x = y, 1 : 0; } inline bool setmax(ll &x, ll y) { return (y > x) ? x = y, 1 : 0; } const int N = 100000; const int inf = (int)1e9 + 1; const ll big = (ll)1e18 + 1; const int P = 239; const int P1 = 31; const int P2 = 57; const int MOD = (int)1e9 + 7; const int MOD1 = (int)1e9 + 9; const int MOD2 = 998244353; const ld eps = 1e-9; const double pi = atan2(0, -1); const int ABC = 26; int x1[30], y1[30], x2[30], y2[30]; ll pw[30]; void init() { pw[0] = 1; rep(i, 1, 30) { pw[i] = pw[i - 1] * 3; } } ll get_val(int len, int *a) { ll res = 0; rep(i, 0, len) { res += a[i] * pw[i]; } return res; } void convert(ll v, int *a) { rep(i, 0, 30) { a[i] = v % 3; v /= 3; } } void rotate_counter(int *x, int *y, int len) { rep(i, 0, len) { y[i] = (2 - y[i]); swap(x[i], y[i]); } } ll solve(int pos) { //cout << pos << endl; //cout << x1[pos] << " " << y1[pos] << " " << x2[pos] << " " << y2[pos] << endl; if (x1[pos] != x2[pos] && y1[pos] != y2[pos]) { return abs(get_val(pos + 1, x1) - get_val(pos + 1, x2)) + abs(get_val(pos + 1, y1) - get_val(pos + 1, y2)); } if (x1[pos] == x2[pos] && y1[pos] == y2[pos]) { if (pos == 0) { return 0; } return solve(pos - 1); } while (true) { //cout << x1[pos] << " " << y1[pos] << " " << x2[pos] << " " << y2[pos] << endl; if ((y1[pos] == 0 && y2[pos] == 0) || (y1[pos] == 1 && y2[pos] == 1)) { break; } rotate_counter(x1, y1, pos + 1); rotate_counter(x2, y2, pos + 1); } if (x1[pos] > x2[pos]) { swap(x1, x2); swap(y1, y2); } //cout << x1[pos] << " " << y1[pos] << " " << x2[pos] << " " << y2[pos] << endl; if (y1[pos] == 0) { if (pos == 0) { return abs(x1[pos] - x2[pos]); } int p1 = x1[pos] * 3 + x1[pos - 1]; int p2 = x2[pos] * 3 + x2[pos - 1]; assert(p1 < p2); if (p2 - p1 == 1) { if (y1[pos - 1] == y2[pos - 1]) { x1[pos - 1] = 0; y1[pos - 1] = 0; x2[pos - 1] = 1; y2[pos - 1] = 0; return solve(pos - 1); } else { return abs(get_val(pos + 1, x1) - get_val(pos + 1, x2)) + abs(get_val(pos + 1, y1) - get_val(pos + 1, y2)); } } else { x1[pos - 1] = 0; x2[pos - 1] = 2; return solve(pos - 1) + ((p2 - p1) - 2) * pw[pos - 1]; } } else { //cout << x1[pos - 1] << " " << y1[pos - 1] << " " << x2[pos - 1] << " " << y2[pos - 1] << endl; ll v1 = ((pw[pos] - 1) - get_val(pos, x1)) + get_val(pos, y1); ll v2 = ((pw[pos] - 1) - get_val(pos, x1)) + ((pw[pos] - 1) - get_val(pos, y1)); ll v3 = get_val(pos, x2) + get_val(pos, y2); ll v4 = get_val(pos, x2) + ((pw[pos] - 1) - get_val(pos, y2)); //cout << v1 << " " << v2 << " " << v3 << " " << v4 << endl; return min(v1 + v3, v2 + v4) + 2 + 1 + pw[pos]; } } int main() { //freopen("a.in", "r", stdin); //freopen("a.out", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(20); cout << fixed; //ll TL = 10.95 * CLOCKS_PER_SEC; //clock_t time = clock(); init(); int q; cin >> q; while (q--) { ll a, b, c, d; cin >> a >> b >> c >> d; a--; b--; c--; d--; convert(a, x1); convert(b, y1); convert(c, x2); convert(d, y2); cout << solve(29) << "\n"; } /*int x, y; cin >> x >> y; rep(i, 0, 9) { rep(j, 0, 9) { if ((i % 3 == 1 && j % 3 == 1) || (i / 3 == 1 && j / 3 == 1)) { cout << "xx "; } else { convert(x, x1); convert(y, y1); convert(i, x2); convert(j, y2); int res = solve(29); if (res < 10) { cout << " "; } cout << res << " "; } } cout << "\n"; }*/ return 0; }
#include<bits/stdc++.h> #define endl enjoy_codeforces using lint=long long; int main(){ std::cin.tie(nullptr);std::ios_base::sync_with_stdio(false); lint q;std::cin>>q; while(q--){ lint L=1;for(lint i=0;i<30;i++)L*=3; lint i0,j0,i1,j1;std::cin>>i0>>j0>>i1>>j1; i0--,j0--,i1--,j1--; for(;i0/L==i1/L&&j0/L==j1/L;L/=3); assert(1<=L); if(j0/L==j1/L){ std::swap(i0,j0);std::swap(i1,j1); } assert(j0/L!=j1/L); lint d=0; for(;L;L/=3){ lint I0=i0/L,I1=i1/L,J0=j0/L,J1=j1/L; assert(J0!=J1); if(I0!=I1)break; if(I0%3==1&&1<std::abs(J0-J1)){ lint r0=i0%L,r1=i1%L; d=std::min({r0+1,r1+1,L-r0,L-r1}); break; } } std::cout<<std::abs(i0-i1)+std::abs(j0-j1)+2*d<<'\n'; } } /* * 避けなければならないものは高々一つです。 * 大きい方から順にためします。 * * i を L で割った商 I が異なるか、j を L で割った商 J が異なるかである最小の L にセットです。 * これらが両方とも成り立っていたら、コストは 0 です。 * I が同じで J が異なるとしましょう。 * I%3==1 であり、J の差が 2 以上ならば壁はこれです。 * このとき、コストは min(i0%L+1,i1%L+1,L-i0%L,L-j0%L) です。 * そうでないときには、L / 3 をチェックです。 * このように繰り返します。繰り返しのたびに、i の方まで違っていないかをチェックです。 */
#include <fstream> #include <iostream> #include <functional> #include <algorithm> #include <sstream> #include <cstring> #include <vector> #include <string> #include <cstdio> #include <cmath> #include <queue> #include <stack> #include <deque> #include <ctime> #include <list> #include <set> #include <map> #include <bitset> using namespace std; typedef long long ll; typedef unsigned long long ull; #define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end())))) #define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin()) #define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++) #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define EPS 1e-8 #define LL_INF 0x3fffffffffffffff #define INF 0x3f3f3f3f3f3f3f3f #define MEM(a, b) memset(a, b, sizeof(a)) #define PPER(i, n, m) for (int i = n; i >= m; i--) #define REPP(i, n, m) for (int i = n; i <= m; i++) #define REP(i, n, m) for (int i = n; i < m; i++) #define PER(i, n, m) for (int i = n; i > m; i--) #define SA(n) scanf("%d", &(n)) #define SLLA(n) scanf("%lld", &(n)) #define MP make_pair #define FF first #define SS second #define PB push_back #define DE(val) cout << #val << ": " << val << endl; const int dx44[6] = {0, -1, -1, 1, 1}; const int dy44[6] = {0, -1, 1, -1, 1}; const int dx4[6] = {0, 1, 0,-1}; const int dy4[6] = {-1, 0, 1,0}; const int dx8[9] = {0, -1, 0, 1, 0, 1, 1, -1, -1}; const int dy8[9] = {0, 0, 1, 0, -1, 1, -1, 1, -1}; const int dx82[9] = {0, -1, -1, 1, 1, 2, 2, -2, -2}; const int dy82[9] = {0, 2, -2, 2, -2, 1, -1, 1, -1}; void update(ll& x,ll v){ if(x==-1)x=v; else if(x>v)x=v; } ll po(ll a, ll b, ll mod) { ll res = 1; a %= mod; for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } ll gcd(ll a, ll b) { if (a == 0) { return b; } else { return gcd(b % a, a); } } void extgcd(ll a, ll b, ll &d, ll &x, ll &y) { if (!b) { d = a; x = 1; } else { extgcd(b, a % b, d, y, x); y -= x * (a / b); } } ll inverse(ll a, ll n) { ll d, x, y; extgcd(a, n, d, x, y); return d == 1 ? (x + n) % n : -1; } #define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end())))) const int maxn = 1e5 + 5; const double PI = acos(-1.0); const ll mod = 1000000007; /*************************************************************************/ ll wid[maxn]; ll dfs(ll x1, ll y1, ll x2, ll y2, int level) { if (level == 0) { return abs(y1 - y2); } ll w = wid[level-1]; if(x1 / w != x2 / w) { return abs(x1 - x2) + abs(y1 - y2); } if(x1 / w == 1 && abs(y1 / w - y2 /w) >= 2) { return abs( y1 - y2 ) + min(x1 % w + 1 + x2 % w + 1, w - x1%w + w - x2%w); } return dfs(x1 % w, y1, x2 % w, y2, level-1); } int q; void solve() { wid[0] = 1; REPP(i,1,30) { wid[i] = wid[i-1] * 3; } SA(q); REPP(i,1,q) { ll a1,b1,c1,d1; SLLA(a1); SLLA(b1); SLLA(c1); SLLA(d1); a1--; b1--; c1--; d1--; if(abs(a1 - c1) > abs(b1 - d1)) { swap(b1, a1); swap(c1, d1); } cout<< dfs(a1, b1, c1, d1, 30) <<endl; } } int main() { // #ifndef ONLINE_JUDGE // freopen("i.txt", "r", stdin); // freopen("o.txt", "w", stdout); // #endif solve(); return 0; }
#pragma GCC optimize("O3") #include <bits/stdc++.h> #define ll long long #define rep2(i,a,b) for(ll i=a;i<=b;++i) #define rep(i,n) for(ll i=0;i<n;i++) #define rep3(i,a,b) for(ll i=a;i>=b;i--) #define pii pair<int,int> #define pll pair<ll,ll> #define pq priority_queue #define pb push_back #define eb emplace_back #define vec vector<int> #define vecll vector<ll> #define vecpii vector<pii> #define vec2(a,b) vector<vec>(a,vec(b)) #define vec2ll(a,b) vector<vecll>(a,vecll(b)) #define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c)) #define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c)) #define fi first #define se second #define endl "\n" #define all(c) begin(c),end(c) #define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); #define lb(c,x) distance(c.begin(),lower_bound(all(c),x)) #define ub(c,x) distance(c.begin(),upper_bound(all(c),x)) #define MM " " #define INF INT_MAX using namespace std; int in() {int x;cin>>x;return x;} ll lin() {ll x;cin>>x;return x;} template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;} template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";} template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;} template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;} void go(); int n; int f(ll x,ll y){ int ma=-1; rep(i,32){ if((x%3)==1 and (y%3)==1)chmax(ma,(int)i); x/=3;y/=3; } if(ma==-1)return 0; ll res=1; rep(i,ma)res*=3; return res; } vec to_3(ll x){ vec res; rep(i,32){ res.pb(x%3);x/=3; } // reverse(all(res)); return res; } int main(){ ios int q=in(); while(q--){ ll a=lin()-1,b=lin()-1,c=lin()-1,d=lin()-1; if(abs(a-c) > abs(b-d)){ swap(a,b);swap(c,d); } vec A=to_3(a),B=to_3(b),C=to_3(c),D=to_3(d); int t=-1; vec s={-1}; rep(i,32)if(A[i] == 1 and C[i]==1)s.pb(i); while(1){ t=s.back();s.pop_back(); if(t==-1){ cout<<abs(a-c)+abs(b-d)<<endl;break; } if(b > d){ swap(b,d);vec t=B;B=C;C=t; } bool f=0; ll T=1; rep(i,t){ T*=3; } if(b/T == d/T)f=1; if(b/T + 1 == d/T and (b/T%3)==2 and (d/T%3)==0)f=1; if(f) {continue;} ll ans=0; if(a>c)swap(a,c); ll L=(a/T)*T-1, R=(a/T)*T+T; ans=abs(a-L)+abs(c-L)+abs(b-d); chmin(ans,abs(R-a)+abs(R-c)+abs(b-d)); cout<<ans<<endl; break; } } }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cfloat> #include <chrono> #include <climits> #include <cmath> #include <cstdio> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; template <class T1, class T2> ostream& operator << (ostream& out, pair <T1, T2> p) { out << '(' << p.first << ',' << p.second << ')'; return out; } template <class T1, class T2> istream& operator >> (istream& in, pair<T1, T2> &p) { in >> p.first >> p.second; return in; } template <class T> istream& operator >> (istream& in, vector<T> &v) { for (T &x : v) in >> x; return in; } template <class T> ostream& operator << (ostream& out, vector<vector<T>> &v) { for (vector<T> &x : v) out << x << '\n'; return out; } template <class T> ostream& operator << (ostream& out, vector<T> &v) { for (T &x : v) out << x << ' '; return out; } long long gcd (long long a, long long b) { if (b > a) swap(a, b); return (b ? gcd(b, a % b) : a); } using ll = long long; using pii = pair<int, int>; using pll = pair<long long, long long>; using tiii = pair<pair<int, int>, int>; using vi = vector<int>; using vl = vector<long long>; using vvi = vector<vector<int>>; using vvl = vector<vector<long long>>; #define F first #define S second #define First first.first #define Second first.second #define Third second #define mp make_pair #define rep(i,a,b) for (int i = a; i < b; i++) #define per(i,b,a) for (int i = b; i > a; i--) #define all(x) x.begin(), x.end() #define ret(x) return cout << x, 0; #define throwex throw runtime_error ("Found the error."); const int h = 1000000007; ll dist(ll a1, ll a2, ll b1, ll b2) { // cerr << a1 << ' ' << a2 << ' ' << b1 << ' ' << b2 << '\n'; return abs(a1 - b1) + abs(a2 - b2); } ll f(ll a1, ll a2, ll b1, ll b2, int level, ll size) { // cerr << a1 << ' ' << a2 << ' ' << b1 << ' ' << b2 << ' ' << level << ' ' << size << '\n'; if(level == 0) return 0; int sec_index_a_i = a1 / (size / 3); int sec_index_a_j = a2 / (size / 3); int sec_index_b_i = b1 / (size / 3); int sec_index_b_j = b2 / (size / 3); if(sec_index_a_i == sec_index_b_i and sec_index_a_j == sec_index_b_j) return f(a1 % (size / 3), a2 % (size / 3), b1 % (size / 3), b2 % (size / 3), level - 1, size / 3); if(sec_index_a_j == sec_index_b_j) { swap(a1, a2); swap(b1, b2); } while(size != 1) { int sec_index_a = a1 / (size / 3); int sec_index_b = b1 / (size / 3); if(sec_index_a != sec_index_b) return dist(a1, a2, b1, b2); if(sec_index_a == 1 and abs((a2 / (size / 3)) - (b2 / (size / 3))) > 1) return abs(a2 - b2) + min((a1 % (size / 3) + 1 + (b1 % (size / 3)) + 1), 2 * (size / 3) - (a1 % (size / 3) + (b1 % (size / 3)))); a1 %= (size / 3); b1 %= (size / 3); size /= 3; } return abs(a2 - b2); } signed main() { ios::sync_with_stdio(false); #ifdef ONLINE_JUDGE cin.tie(nullptr); cout.tie(nullptr); cerr.setstate(ios::failbit); #endif int q; cin >> q; while(q--) { ll a, b, c, d; cin >> a >> b >> c >> d; a--, b--, c--, d--; ll size = 1; rep(i,0,30) size *= 3; cout << f(a, b, c, d, 30, size) << '\n'; } }
#include <bits/stdc++.h> #include <iomanip> using namespace std; #define reps(i,s,n) for(int i = s; i < n; i++) #define rep(i,n) reps(i,0,n) #define Rreps(i,n,e) for(int i = n - 1; i >= e; --i) #define Rrep(i,n) Rreps(i,n,0) #define ALL(a) a.begin(), a.end() #define fi first #define se second typedef long long ll; typedef vector<ll> vec; typedef vector<vec> mat; ll N,M,H,W,K,Q,A,B; //const ll MOD = 998244353; const ll MOD = (1e+9) + 7; const ll INF = 1LL<<60; typedef pair<ll, ll> P; vec san(32, 1); int bit3_t(ll a, int t){ return (a % san[t + 1]) / san[t]; } bool between(ll x, ll y1, ll y2){ if(y1 > y2) swap(y1, y2); ++y1; --y2; if(y1 > y2) return false; while(x > 0){ if(x%3 == 1){ if(y2 - y1 > 3) return true; for(ll y = y1; y <= y2; ++y) if(y%3 == 1) return true; } x /= 3; y1 /= 3; y2 /= 3; } return false; } ll f(ll a, ll b, ll c, ll d){ ll res = 0; Rrep(i, 31){ /*if(bit3_t(b, i) == 1 && bit3_t(d, i) == 1){ swap(a, b); swap(c, d); }*/ if(bit3_t(a, i) == 1 && bit3_t(c, i) == 1 && a / san[i] == c / san[i]){ if(abs((d / san[i]) - (b / san[i])) >= 2){ //if(between(a / san[i], b / san[i], d / san[i])){ ll diff1 = a % san[i], diff2 = c % san[i]; res = max(res, min({diff1 + 1, san[i] - diff1, diff2 + 1, san[i] - diff2}) * 2); } } } return res; } int main() { cin>>Q; reps(i, 1, 32) san[i] = san[i-1] * 3; rep(i,Q){ vec v(4); rep(j,4){ cin>>v[j]; --v[j]; } ll res = abs(v[0] - v[2]) + abs(v[1] - v[3]); //cout<<res<<endl; cout<<res + max(f(v[0], v[1], v[2], v[3]), f(v[1], v[0], v[3], v[2]))<<endl; } }
#include <bits/stdc++.h> using namespace std; template <class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',') cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } template<class T> ostream & operator<<(ostream & os, vector<T> V){ os<<"["; for(auto vv: V) os << vv <<","; return os << "]"; } template<class L, class R> ostream & operator <<(ostream & os, pair<L,R> P){ return os <<"("<<P.first <<","<<P.second <<")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif long long pot[35]; long long solve2(long long xa, long long xb, int level) { if (level == 0) { return abs(xb - xa); } int ra = xa / pot[level - 1]; int rb = xb / pot[level - 1]; if (ra == rb) { if (ra == 1) { xa = xa % pot[level - 1]; xb = xb % pot[level - 1]; return min(xa + xb + 2, pot[level - 1] - xa + pot[level - 1] - xb); } if (rb == 2) { xa = xa % pot[level - 1]; xb = xb % pot[level - 1]; } return solve2(xa, xb, level - 1); } else { return abs(xa - xb); } } long long solve3(long long xa, long long xb, long long y, int dir, int level) { // cerr << xa << " " << xb << " " << y << " " << level << endl; if (level == 0) { return abs(xa - xb); } if (dir == 1) { y = pot[level] - y - 1; return solve3(xa, xb, y, 0, level); } int ra = xa / pot[level - 1]; int rb = xb / pot[level - 1]; int c = y / pot[level - 1]; if (ra == rb) { if (c == 0) { return solve3(xa % pot[level - 1], xb % pot[level - 1], y % pot[level - 1], 0, level - 1); } // if (ra == 1) { // xa = xa % pot[level - 1]; // xb = xb % pot[level - 1]; // return min(xa + xb + 2, pot[level - 1] - xa + pot[level - 1] - xb); // } // if (rb == 2) { // xa = xa % pot[level - 1]; // xb = xb % pot[level - 1]; // } if (c == 1) return solve2(xa % pot[level - 1], xb % pot[level - 1], level - 1); return solve2(xa, xb, level); } else { return abs(xa - xb); } } long long solve(long long xa, long long ya, long long xb, long long yb, int level) { if (level == 0) { return abs(xb - xa) + abs(yb - ya); } int ra = xa / pot[level - 1]; int ca = ya / pot[level - 1]; int rb = xb / pot[level - 1]; int cb = yb / pot[level - 1]; if (ra == rb && ca == cb) { return solve(xa % pot[level - 1], ya % pot[level - 1], xb % pot[level - 1], yb % pot[level - 1], level - 1); } if (ra != rb && ca != cb) { return abs(xa - xb) + abs(ya - yb); } if (ra == rb) { long long d = abs(ya - yb); if (abs(cb - ca) == 2) { return d + solve2(xa, xb, level); } // cerr << max(solve3(xa % pot[level - 1], xb % pot[level - 1], min(ya, yb) % pot[level - 1], 1, level - 1), solve3(xa, xb, max(ya, yb) % pot[level - 1], 0, level - 1)) << endl; return d + max(solve3(xa % pot[level - 1], xb % pot[level - 1], min(ya, yb) % pot[level - 1], 1, level - 1), solve3(xa % pot[level - 1], xb % pot[level - 1], max(ya, yb) % pot[level - 1], 0, level - 1)); } if (ca == cb) { long long d = abs(xa - xb); if (abs(rb - ra) == 2) { return d + solve2(ya, yb, level); } return d + max(solve3(ya % pot[level - 1], yb % pot[level - 1], min(xa, xb) % pot[level - 1], 1, level - 1), solve3(ya % pot[level - 1], yb % pot[level - 1], max(xa, xb) % pot[level - 1], 0, level - 1)); } return 0; } int main() { pot[0] = 1; for (int i = 1; i <= 30; i++) { pot[i] = pot[i - 1] * 3; } int t; scanf("%d", &t); while (t--) { long long xa, xb, ya, yb; scanf("%lld %lld %lld %lld", &xa, &ya, &xb, &yb); xa--; ya--; xb--; yb--; printf("%lld\n", solve(xa, ya, xb, yb, 30)); } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=35; ll f[N]; ll dis(ll x1,ll y1,ll x2,ll y2){return abs(x1-x2)+abs(y1-y2);} ll solve(ll x1,ll y1,ll x2,ll y2,ll level) { if(level==0) return abs(y1-y2); ll w=f[level-1]; if(x1/w!=x2/w) return dis(x1,y1,x2,y2); if(x1/w==1&&abs(y1/w-y2/w)>=2) return abs(y1-y2)+min(x1%w+x2%w+2,(w-x1%w)+(w-x2%w)); return solve(x1%w,y1,x2%w,y2,level-1); } int main() { f[0]=1;for(int i=1;i<N;i++) f[i]=f[i-1]*3; int q;scanf("%d",&q); while(q--) { ll x1,y1,x2,y2; scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2); x1--;y1--;x2--;y2--; if(abs(x1-x2)>abs(y1-y2)) swap(x1,y1),swap(x2,y2); printf("%lld\n",solve(x1,y1,x2,y2,30)); } }
#include<bits/stdc++.h> #define ll long long #define fornum(A,B,C) for(A=B;A<C;++A) #define pii pair<int,int> #define pll pair<ll,ll> using namespace std; ///////////////////////////////////////////////////// ll Q; ll p3[40]; ll mk[5][40]; ll i, j, k; pll zn(ll a,ll* mk){ ll i,j=0; fornum(i,0,30){ a %= p3[29 - i]; } } int main(){ p3[0] = 1; fornum(i,0,32){ p3[i + 1] = p3[i] * 3; } scanf("%lld", &Q); while(Q--){ ll a, b, c, d; scanf("%lld%lld%lld%lld", &a, &b, &c, &d); ll aa = a, bb = b, cc = c, dd = d; --a; --b; --c; --d; if(abs(a-c)>abs(b-d)){ swap(a, b); swap(c, d); } ll flg = 0; for (i = 30; i > 0;--i){ if(a/p3[i-1]==1&&a/p3[i-1]==c/p3[i-1]){ if (abs((b / p3[i - 1] - d / p3[i - 1])) > 1) { a %= p3[i - 1]; c %= p3[i - 1]; //printf("%lld,%lld,%lld,%lld,%lld,", a, b, c, d, p3[i - 1]); printf("%lld\n", min(a+1 + c+1, abs(a - p3[i - 1]) + abs(c - p3[i - 1])) + abs(b - d)); flg = 1; i = 1; } } if(a/p3[i-1]!=c/p3[i-1]) { break; } a %= p3[i - 1]; c %= p3[i - 1]; } if(!flg){ printf("%lld\n", abs(aa - cc) + abs(bb - dd)); } } return 0; }
#include <bits/stdc++.h> using namespace std; using ll=long long; const int L = 30; void solve() { vector<ll> pw(L+1); pw[0] = 1; for (int i = 1; i <= L; i++) { pw[i] = pw[i-1]*3; } // 3 2 4 2 // pw[i] may not between, depend y0,y1. auto f = [&](ll x0, ll x1){ for (int i = L-1; i >= 0; x0%=pw[i], x1%=pw[i], i--) { int b0 = (x0 / pw[i])%3; int b1 = (x1 / pw[i])%3; if (b0 != b1) break; if (b0 != 1) continue; if (x0 > x1) swap(x0,x1); return 2ll * min(2ll*pw[i] - x1, x0 - (pw[i]-1)); } return 0ll; }; auto g = [&](ll x0, ll x1, ll y0, ll y1){ for (int i = L-1; i >= 0; x0%=pw[i], x1%=pw[i], y0%=pw[i], y1%=pw[i], i--) { int b0 = (x0/pw[i]); int b1 = (x1/pw[i]); int c0 = (y0/pw[i]); int c1 = (y1/pw[i]); if (b0 != b1 && c0 != c1) return 0ll; if (b0 == b1 && c0 == c1) continue; if (c0 == c1) { swap(x0,y0); swap(x1,y1); } if (y0 > y1) { swap(y0,y1); } bool exist = (c0==0 && c1==2); for (; i >= 0; x0%=pw[i], x1%=pw[i], i--) { int b0 = (x0 / pw[i])%3; int b1 = (x1 / pw[i])%3; if (b0 != b1) return 0ll; if (b0 != 1 || !exist) { y0%=pw[i], y1%=pw[i]; if(i) { int c0 = (y0/pw[i-1]); int c1 = (y1/pw[i-1]); exist = exist || !(c0==2&&c1==0); } continue; } if (x0 > x1) swap(x0,x1); return 2ll * min(2ll*pw[i] - x1, x0 - (pw[i]-1)); } return 0ll; } return 0ll; }; auto h = [&](ll x0, ll x1, ll y0, ll y1){ if (x0>x1)swap(x0,x1); if (y0>y1)swap(y0,y1); if (x1-x0 > y1-y0) { swap(x0,y0); swap(x1,y1); } for (int i = L-1; i >= 0; i--) { if (x0/pw[i] == x1/pw[i] && (x0/pw[i])%3 == 1) { if (y1/pw[i] - y0/pw[i] > 1) { return 2ll*min(x0 - x0/pw[i]*pw[i] + 1, (x0/pw[i] + 1)*pw[i] - x1); } } } return 0ll; }; int q; cin >> q; while (q--) { ll x,y,u,v; cin >> x >> y >> u >> v; x--;y--;u--;v--; ll res = abs(x-u) + abs(y-v); //res += f(x,u) + f(y,v); //res += g(x,u,y,v); res += h(x,u,y,v); cout << res << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
/* Author: Nguyen Tan Bao Status: Idea: */ #include <bits/stdc++.h> #define FI first #define SE second #define EPS 1e-9 #define ALL(a) a.begin(),a.end() #define SZ(a) int((a).size()) #define MS(s, n) memset(s, n, sizeof(s)) #define FOR(i,a,b) for (int i = (a); i <= (b); i++) #define FORE(i,a,b) for (int i = (a); i >= (b); i--) #define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) //__builtin_ffs(x) return 1 + index of least significant 1-bit of x //__builtin_clz(x) return number of leading zeros of x //__builtin_ctz(x) return number of trailing zeros of x using namespace std; using ll = long long; using ld = double; typedef pair<int, int> II; typedef pair<II, int> III; typedef complex<ld> cd; typedef vector<cd> vcd; const ll MODBASE = 1000000007LL; const int MAXN = 110; const int MAXM = 1000; const int MAXK = 16; const int MAXQ = 200010; int q; ll p3[35]; int main() { ios::sync_with_stdio(0); cin.tie(nullptr); int q; cin >> q; p3[0] = 1; FOR(i,1,30) p3[i] = p3[i-1] * 3; while (q--) { ll X1, Y1, X2, Y2; cin >> X1 >> Y1 >> X2 >> Y2; X1--; X2--; Y1--; Y2--; ll res = abs(X1 - X2) + abs(Y1 - Y2); FORE(i,29,0) { ll x1 = X1 / p3[i]; ll y1 = Y1 / p3[i]; ll x2 = X2 / p3[i]; ll y2 = Y2 / p3[i]; if (x1 == x2 && x1 % 3 == 1 && abs(y1 - y2) >= 2) { res = abs(Y1 - Y2) + min(X1 % p3[i] + 1 + X2 % p3[i] + 1, p3[i] - X1 % p3[i] + p3[i] - X2 % p3[i]); break; } if (y1 == y2 && y1 % 3 == 1 && abs(x1 - x2) >= 2) { res = abs(X1 - X2) + min(Y1 % p3[i] + 1 + Y2 % p3[i] + 1, p3[i] - Y1 % p3[i] + p3[i] - Y2 % p3[i]); break; } } cout << res << "\n"; } return 0; }
#include <iostream> #include <cmath> #include <complex> #include <string> #include <sstream> #include <limits> #include <algorithm> #include <functional> using namespace std; long long process(long long x1, long long y1, long long x2, long long y2) { long long d = 1; for (int i = 0; i < 30; i++) d *= 3; if (y1 > y2) { swap(x1, x2); swap(y1, y2); } long long result = abs(x1 - x2) + abs(y1 - y2); while (d > 1) { d /= 3; long long x1_idx = x1 / d; long long x2_idx = x2 / d; long long y1_idx = y1 / d; long long y2_idx = y2 / d; if (x1_idx != x2_idx) break; if (x1_idx % 3 == 1 && abs(y1_idx - y2_idx) > 1) { long long top = x1_idx * d - 1; long long bottom = x1_idx * d + d; long long loss = min(x1, x2) - top; loss = min(loss, bottom - max(x1, x2)); result += loss * 2; break; } } return result; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int Q; cin >> Q; while (Q--) { long long x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--; y1--; x2--; y2--; long long result1 = process(x1, y1, x2, y2); long long result2 = process(y1, x1, y2, x2); cout << max(result1, result2) << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; #define pp pair<int,int> #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define ll long long #define ld long double #define all(a) (a).begin(),(a).end() #define mk make_pair ll mod=998244353; int inf=1000001000; ll INF=1e18+5; ll MOD=1000000007; int main() { int q; cin >> q; rep(i,q){ ll a,b,c,d,s=3; ll f=1; rep(i,30) f*=3; cin >> a >> b >> c >> d; a--;b--;c--;d--; ll ans=abs(a-c)+abs(b-d); rep(i,30){ f/=3; ll aa=a/f,bb=b/f,cc=c/f,dd=d/f; if (aa%3==1){ if (aa==cc && abs(bb-dd)>1){ ll u=a-aa*f,uu=c-cc*f; ll h=min(min(u,uu)*2ll+2ll,min(f-u,f-uu)*2ll); ans=ans+h; break; } } if (bb%3==1){ if (bb==dd && abs(aa-cc)>1){ ll u=b-bb*f,uu=d-dd*f; ll h=min(min(u,uu)*2ll+2ll,min(f-u,f-uu)*2ll); ans=ans+h; break; } } } cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<int(n);++i) typedef long long ll; vector<ll> width = {1LL}; ll solve(ll s1, ll s2, ll l1, ll l2, int level){ if (level == 0) return abs(l1-l2); ll w = width[level-1]; if (s1/w != s2/w) { return abs(s1-s2) + abs(l1-l2); } else if (s1/w == 1 && abs(l1/w - l2/w) >= 2) { return min(s1%w + s2%w + 2, w*2 - s1%w - s2%w) + abs(l1-l2); } else { return solve(s1%w, s2%w, l1, l2, level-1); } } int main(void){ rep(i,30){ width.push_back(width.back() * 3LL); } int Q; cin >> Q; rep(q,Q){ ll x1,y1,x2,y2; cin >> x1 >> y1 >> x2 >> y2; x1--,y1--,x2--,y2--; if (abs(x1-x2) > abs(y1-y2)) { swap(x1,y1), swap(x2,y2); } cout << solve(x1,x2,y1,y2,30) << endl; } return 0; }
#include <cstdio> #include <algorithm> using namespace std; long long get(long long a, int x) { int i; for (i = 0; i < x; i++) a /= 3; return a; } long long get2(long long a, int x) { int i; for (i = 0; i < x; i++) a *= 3; return a; } long long calc(long long a, long long b, long long c, long long d) { int i; for (i = 29; i >= 0; i--) { long long pa = get(a, i); long long pb = get(b, i); long long pc = get(c, i); long long pd = get(d, i); if (pa == pc && pa % 3 == 1 && abs(pb - pd) >= 2) { long long x, y; x = get2(pa, i) - 1; y = get2(pa + 1, i); return min(abs(a - x) + abs(c - x), abs(a - y) + abs(c - y)) + abs(b - d); } else if (pb == pd && pb % 3 == 1 && abs(pa - pc) >= 2) { long long x, y; x = get2(pb, i) - 1; y = get2(pb + 1, i); return min(abs(b - x) + abs(d - x), abs(b - y) + abs(d - y)) + abs(a - c); } } return abs(a - c) + abs(b - d); } int main() { int q, i; scanf("%d", &q); for (i = 0; i < q; i++) { long long a, b, c, d; scanf("%lld %lld %lld %lld", &a, &b, &c, &d); printf("%lld\n", calc(a - 1, b - 1, c - 1, d - 1)); } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int,int>; ll solve(ll ai, ll aj, ll bi, ll bj) { if (aj > bj) { swap(ai,bi); swap(aj,bj); } ll s = 1; rep(i,30) s *= 3; ll dist = abs(ai-bi) + abs(aj-bj); ll res = dist; while (1) { s /= 3; if (s <= 0) break; ll nai = ai/s, nbi = bi/s; if (nai != nbi) break; ll naj = aj/s, nbj = bj/s; if (nai%3 == 1 && abs(naj-nbj) > 1) { ll up = s*nai-1; ll bot = nai*s + s; ll loss = min(ai,bi) - up; loss = min(loss, bot - max(ai,bi)); res += loss*2; break; } } return res; } int main() { int q; cin >> q; while(q--) { ll ai,aj,bi,bj; cin >> ai >> aj >> bi >> bj; ai--; aj--; bi--; bj--; ll ans1 = solve(ai,aj,bi,bj); ll ans2 = solve(aj,ai,bj,bi); cout << max(ans1, ans2) << endl; } return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; ll width[30]={1}; ll cal(ll x1,ll y1,ll x2,ll y2,int level) { if(level==0) return abs(y1-y2); ll w=width[level-1]; if(x1/w!=x2/w) return abs(x1-x2)+abs(y1-y2);//不同层 if(x1/w==1&&abs(y1/w-y2/w)>=2) {//同层且在456这层 return min(x1%w+x2%w+2,w*2-x1%w-x2%w)+abs(y1-y2); } return cal(x1%w,y1,x2%w,y2,level-1); } int main() { for(int i=1;i<30;i++) width[i]=width[i-1]*3; int T; scanf("%d",&T); while(T--) { ll a,b,c,d; scanf("%lld%lld%lld%lld",&a,&b,&c,&d),a--,b--,c--,d--; if(abs(a-c)>abs(b-d)) swap(a,b),swap(c,d);//避免(2,8) printf("%lld\n",cal(a,b,c,d,30)); } return 0; }
#include <bits/stdc++.h> using namespace std; template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;} using Int = long long; const char newl = '\n'; //INSERT ABOVE HERE signed main(){ cin.tie(0); ios::sync_with_stdio(0); const Int n = 30; vector<Int> po(n,1); for(Int i=0;i+1<n;i++) po[i+1]=po[i]*3; auto solve=[&](Int a,Int b,Int c,Int d)->Int{ for(Int k=n-1;k>=0;k--){ Int A=a/po[k]; Int B=b/po[k]; Int C=c/po[k]; Int D=d/po[k]; if(A==C and A%3==1 and abs(B-D)>=2){ Int x1=a%po[k]; Int x2=c%po[k]; return abs(b-d)+min(x1+1+1+x2,(po[k]-(x1+1))+1+1+(po[k]-(x2+1))); } if(B==D and B%3==1 and abs(A-C)>=2){ Int y1=b%po[k]; Int y2=d%po[k]; return abs(a-c)+min(y1+1+1+y2,(po[k]-(y1+1))+1+1+(po[k]-(y2+1))); } } return abs(a-c)+abs(b-d); }; Int q; cin>>q; for(Int i=0;i<q;i++){ Int a,b,c,d; cin>>a>>b>>c>>d; a--;b--;c--;d--; cout<<solve(a,b,c,d)<<newl; } return 0; }
#include <bits/stdc++.h> int ri() { int n; scanf("%d", &n); return n; } int main() { int q = ri(); int64_t kai3[31]; kai3[0] = 1; for (int i = 0; i < 30; i++) kai3[i + 1] = kai3[i] * 3; for (int i = 0; i < q; i++) { int64_t x0, y0, x1, y1; std::cin >> x0 >> y0 >> x1 >> y1; x0--; y0--; x1--; y1--; int64_t x0_bak = x0; int64_t y0_bak = y0; int64_t x1_bak = x1; int64_t y1_bak = y1; int xlevel = -1; int64_t xcost = 0; for (int j = 29; j >= 0; j--) { bool intersect = false; if (x0 / kai3[j + 1] == x1 / kai3[j + 1]) { if (x0 % kai3[j + 1] / kai3[j] == 1 && x1 % kai3[j + 1] / kai3[j] == 1) { int64_t y_block0 = y0 / kai3[j]; int64_t y_block1 = y1 / kai3[j]; if (y_block0 > y_block1) std::swap(y_block0, y_block1); int64_t next = y_block0 / 3 * 3 + 1; if (next <= y_block0) next += 3; if (next < y_block1) { intersect = true; xcost = std::min((x0 % kai3[j + 1] - kai3[j] + 1) + (x1 % kai3[j + 1] - kai3[j] + 1), (kai3[j] * 2 - x0 % kai3[j + 1]) + (kai3[j] * 2 - x1 % kai3[j + 1])); } } } if (intersect) { xlevel = j; break; } } x0 = x0_bak; y0 = y0_bak; x1 = x1_bak; y1 = y1_bak; int ylevel = -1; int64_t ycost = 0; for (int j = 29; j >= 0; j--) { bool intersect = false; if (y0 / kai3[j + 1] == y1 / kai3[j + 1]) { if (y0 % kai3[j + 1] / kai3[j] == 1 && y1 % kai3[j + 1] / kai3[j] == 1) { int64_t x_block0 = x0 / kai3[j]; int64_t x_block1 = x1 / kai3[j]; if (x_block0 > x_block1) std::swap(x_block0, x_block1); int64_t next = x_block0 / 3 * 3 + 1; if (next <= x_block0) next += 3; if (next < x_block1) { intersect = true; ycost = std::min((y0 % kai3[j + 1] - kai3[j] + 1) + (y1 % kai3[j + 1] - kai3[j] + 1), (kai3[j] * 2 - y0 % kai3[j + 1]) + (kai3[j] * 2 - y1 % kai3[j + 1])); } } } if (intersect) { ylevel = j; break; } } x0 = x0_bak; y0 = y0_bak; x1 = x1_bak; y1 = y1_bak; int64_t res; if (xlevel > ylevel) { res = xcost + std::abs(y0 - y1); } else if (ylevel > xlevel) { res = ycost + std::abs(x0 - x1); } else res = std::abs(x0 - x1) + std::abs(y0 - y1); printf("%" PRId64 "\n", res); } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=35; ll f[N]; ll dis(ll x1,ll y1,ll x2,ll y2){return abs(x1-x2)+abs(y1-y2);} ll solve(ll x1,ll y1,ll x2,ll y2,ll level) { if(level==0) return abs(y1-y2); ll w=f[level-1]; if(x1/w!=x2/w) return dis(x1,y1,x2,y2); if(x1/w==1&&abs(y1/w-y2/w)>=2) return abs(y1-y2)+min(x1%w+x2%w+2,2*w-x1%w-x2%w); return solve(x1%w,y1,x2%w,y2,level-1); } int main() { f[0]=1;for(int i=1;i<N;i++) f[i]=f[i-1]*3; int q;scanf("%d",&q); while(q--) { ll x1,y1,x2,y2; scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2); x1--;y1--;x2--;y2--; if(abs(x1-x2)>abs(y1-y2)) swap(x1,y1),swap(x2,y2); printf("%lld\n",solve(x1,y1,x2,y2,30)); } }