text stringlengths 49 983k |
|---|
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int N,A[200000];
long long int ans;
cin>>N;
for(int i=0;i<N;i++){cin>>A[i];A[i]*=-1;}
sort(A,A+N);
for(int i=0;i<N;i++)A[i]*=-1;
ans=A[0];
for(int i=2;i<N;i++){
ans+=A[i/2];
}
cout<<ans;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int a[200005];
int main() {
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i)
scanf("%d", &a[i]);
sort(a, a+n);
LL sum = a[n-1];
for(int i = 2; i < n; ++i)
sum += a[n-1-i/2];
printf("%lld\n", sum);
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) for (int i=0;i<n;i++)
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
rep(i,n) cin >> a[i];
sort(a.begin(),a.end());
reverse(a.begin(),a.end());
long long ans=0;
for (int i=1;i<n;i++) {
int b=i/2;
ans += a[b];
}
cout << ans << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
const int maxx=2e5+10;
typedef long long ll;
ll a[maxx];
int main(){
int n; cin>>n;
for(int i=1;i<=n;i++){
scanf("%lld",a+i);
}
sort(a+1,a+1+n);
reverse(a+1,a+1+n);
ll ans=0;
for(int i=2;i<=n;i++){
ans+=a[(i+1)/2];
}
cout<<ans;
}
|
#include<bits/stdc++.h>
using namespace std;
int main(){
int N;
cin >> N;
vector<int> A(N);
for(int i=0;i<N;i++){
cin >> A.at(i);
}
sort(A.begin(),A.end());
reverse(A.begin(),A.end());
long long sum=0;
for(int i=0;i<N-1;i++){
sum+=A.at((i+1)/2);
}
cout << sum << endl;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<long long> a(n);
for(int i=0; i<n; i++) cin>>a[i];
sort(a.begin(),a.end(),greater<long long>());
long long ans = 0;
for(int i=1; i<n; i++){
ans += a[(i/2)];
}
cout<<ans<<endl;
} |
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
int n;
cin>>n;
ll arr[n];
for(auto& i:arr)
cin>>i;
sort(arr,arr+n,greater<ll>());
ll ans=0;
int id=0;
for(int i=0;i<n-1;i++)
{
ans+=(ll)arr[id];
if(i%2==0)
id++;
}
cout<<ans<<endl;
}
|
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
int n;cin>>n;
int a[n];
for (int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n,greater<int>());
ll ans=0;
for (int i=0;i<n-1;i++)
ans+=a[(i+1)/2];
cout<<ans<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ll n;
cin>>n;
ll arr[n];
for(ll i=0;i<n;i++){
cin>>arr[i];
}
sort(arr,arr+n);
reverse(arr,arr+n);
ll count=0;
ll index=0;
for(ll i=1;i<=n-1;i++){
count+=arr[index];
if(i%2==1){
index++;
}
}
cout<<count<<"\n";
return 0;
} |
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long n,a[300000];
long long ans;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
sort(a+1,a+1+n,greater<int>());
for(int i=2;i<=n;i++){
ans+=a[(i+1)>>1];
}
cout<<ans;
return 0;
} |
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int a[200005];
int main()
{
int n;cin>>n;
for(int i=1;i<=n;++i)
cin>>a[i];
sort(a+1,a+n+1);
ll sum=a[n];int num=n-2,t=0,x=n-1;
while(num--)
{
sum+=a[x];
t++;
if(t==2)
{
t=0;
x--;
}
}
cout<<sum;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n ; cin>>n;
int ar[n];
for(int i=0;i<n;i++)
cin>>ar[i];
sort(ar,ar+n,greater<int>());
int c=1;
long long sum=ar[0];
for(int i=1;i<=n-2;i+=2)
sum+=(2*ar[c++]);
if(n&1)
sum-=ar[--c];
cout<<sum;
}
|
#include <bits/stdc++.h>
using namespace std;
int a[200000];
int main()
{
int n; scanf("%d", &n);
for(int i = 0; i < n; ++i)
{
scanf("%d", a + i);
}
sort(a, a + n);
reverse(a, a + n);
long long ans = a[0];
for(int i = 0; i < n - 2; ++i)
{
ans += a[i / 2 + 1];
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++) cin >> A.at(i);
sort(A.rbegin(), A.rend());
long ans = A.at(0);
for (int i = 2; i < N; i++) ans += A.at(i / 2);
cout << ans << "\n";
} |
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1000000007
using namespace std;
int main(){
int n;
cin >> n;
long a[n];
rep(i,n)cin>> a[i];
sort(a,a+n);
long ans = 0;
rep(i,n-1){
ans += a[n-1-(i+1)/2];
}
cout << ans << endl;
return 0;
} |
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
sort(A.begin(), A.end(), greater<long long>());
long long ans = 0;
for (int i = 0; i < N - 1; i++) ans += A[(i+1) / 2];
cout << ans << endl;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
long a[n];
for(int i=0;i<n;i++)cin >> a[i];
long ans = 0;
sort(a,a+n,greater<long>());
ans += a[0];
for(int i=2;i<n;i++){
ans += a[i/2];
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
int A[N];
for (int i=0; i<N; i++) {
cin >> A[i];
}
sort(A,A+N);
long long count=0;
for (int i=1; i<N; i++) {
count+=A[N-i/2-1];
}
cout << count << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int N;
cin>>N;
vector<ll> A(N);
for(int i=0; i<N; i++) cin>>A[i];
ll ans = 0;
sort(A.begin(),A.end());
for(int i=0; i<N-1; i++){
ans += A[N-1-((i+1)/2)];
}
cout<<ans<<endl;
} |
#include<cstdio>
#include<algorithm>
using namespace std;
bool cmp(int x,int y)
{
return x<y;
}
int n,a[200005];
long long ans;
int main()
{
scanf("%d",&n);
for (int i=1 ; i<=n ; i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1,cmp);
for (int i=1 ; i<n ; i++)
{
ans+=a[n-i/2];
}
printf("%lld\n",ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
long long a[200005];
int main()
{
int i,j;
long long t,n;
scanf("%lld",&t);
for(i=1;i<=t;i++)
scanf("%lld",&a[i]);
sort(a+1,a+t+1);
n=a[t];
j=t-1;
for(i=(t-2)/2;i>0;i--)
n+=2*a[j--];
if(t%2==1)
n+=a[j];
printf("%lld",n);
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mx 200005
int main()
{
int n;
cin>>n;
int a[mx];
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n);
reverse(a,a+n);
ll sum=0;
int id=0;
for(int i=0;i<n-1;i++)
{
sum+=a[id];
if(i%2==0)
id++;
}
cout<<sum<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int N;
cin >> N;
vector<ll> A(N);
for (auto& x : A) cin >> x;
sort(A.rbegin(), A.rend());
ll res = 0;
for (int i = 1; i < N; ++ i) {
res += A[i/2];
}
cout << res << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
using ll=int64_t;
int main(){
ll n;
cin >>n;
vector<ll>a(n);
for(ll i=0;i<n;i++){
cin >> a.at(i);
}
sort(a.begin(),a.end());
ll ans=a.at(n-1);
ll tmp=n-2;
for(ll i=0;i<n-2;i++){
ans+=a.at(tmp);
if(i%2!=0)tmp--;
}
cout <<ans<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
using ll=long long;
int main(){
int N;
cin>>N;
vector<ll> A(N);
for(int i=0;i<N;i++){
cin>>A[i];
}
sort(A.begin(),A.end());
reverse(A.begin(),A.end());
ll ans=0;
for(int i=1;i<N;i++){
ans+=A[i/2];
}
cout<<ans<<endl;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int n;cin>>n;
vector<ll>arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
sort(arr.rbegin(),arr.rend());
ll sum=arr[0];
for(int i=2;i<n;i++){
sum+=arr[i/2];
}
cout<<sum;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int N;
scanf("%d\n",&N);
vector<int> A(N);
for(int i=0; i<N; i++) cin >> A[i];
sort(A.begin(),A.end(),greater<int>());
long long ans=0;
for(int i=1; i<N; i++){
ans += A[i/2];
}
printf("%lld\n",ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long llint;
int main() {
int n;
cin >> n;
vector<llint> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.rbegin(), a.rend());
llint ans = 0;
for (int i = 1; i <= n - 1; i++) {
ans += a[i / 2];
}
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll n;cin >> n;
vector<ll>a(n);
for(int i = 0;i<n;i++) cin >> a[i];
sort(a.rbegin(),a.rend());
ll sum = a[0];
int t = n-2;
int j = 1;
while(t){
if(t==1) {sum += a[j];break;}
sum += 2*a[j];
j++;
t = t-2;
}
cout << sum;
return 0;
} |
#import<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long LL;
LL h,i,j,k,l,m,n,p,t,v,w,x,y,a[200040];
string s[6];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(;i<n;i++)cin>>a[i],a[i]=-a[i];
sort(a,a+n);
for(i=0;++i<n;)t-=a[i/2];
cout<<t;
} |
#include<iostream>
#include<algorithm>
using namespace std;
int main(void){
int N;
cin >> N;
int A[N];
for(int i=0;i<N;i++) cin >> A[i];
sort(A,A+N);
long int ans=A[N-1];
for(int i=2;i<N;i++){
ans+=A[N-1-i/2];
}
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
scanf("%d", &n);
vector<long long int>v(n);
for (int i = 0; i < n; i++) scanf("%lld", &v[i]);
sort(v.rbegin(), v.rend());
long long int ans = 0;
for (int i = 1; i < n; i++) ans += v[i/2];
printf("%lld\n", ans);
return 0;
} |
#include<iostream>
#include<algorithm>
#include<vector>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
int n;
cin>>n;
vector<int>a(n);
rep(i,n)cin>>a[i];
sort(a.begin(),a.end());
long long int sum=0;
sum+=a[n-1];
rep(i,n-2){
sum+=a[n-2-(i/2)];
}
cout<<sum<<endl;
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
int main() {
int n;
cin >> n;
vector<int64_t> a(n);
rep(i,n) cin >> a[i];
sort(a.rbegin(), a.rend());
int64_t res = 0;
for (int i = 1; i < n; ++i) res += a[i/2];
cout << res << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll n,i,ans = 0;scanf("%lld",&n);
ll a[n];
for(i=0;i<n;i++) scanf("%lld",&a[i]);
sort(a,a+n,greater<ll>());
for(i=1;i<n;i++) ans += a[i/2];
printf("%lld\n",ans);
} |
#include <bits/stdc++.h>
#define REP(i, a, n) for(int i = a; i < n; i++)
using namespace std;
long long n, ans;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
long long p[n];
REP(i, 0, n) cin >> p[i];
sort(p, p + n);
REP(i, 1, n) ans += p[n-i/2-1];
cout << ans;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int ara[200002];
int main()
{
int n,i;
ll ans;
cin>>n;
for(i=1;i<=n;i++)scanf("%d",&ara[i]);
sort(ara+1,ara+n+1,greater<int>());
ans=ara[1];
for(i=3;i<=n;i++)ans+=ara[(i+1)/2];
cout<<ans<<endl;
return 0;
}
|
#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define pb push_back
using namespace std;
typedef long long ll;
int main(){
int n;
cin>>n;
vector<int> a(n);
rep(i,n) cin>>a[i];
sort(a.begin(),a.end(),greater<int>());
ll ans=a[0];
rep(i,n-2){
ans+=a[(i+2)/2];
}
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;
int main() {
int n;
priority_queue<ll> a;
ll t,ans=0;
cin>>n;
rep(i,n){
cin>>t;
a.push(t);
a.push(t);
}
a.pop();
rep(i,n-1){
ans+=a.top();
a.pop();
}
cout<<ans;
} |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
long long int a[n+1],ans=0;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1);
int cnt=n-2,i=n-1;
ans+=a[n];
while(cnt>0)
{
if(cnt==1)
ans+=a[i];
else
ans=ans+2LL*a[i];
i--;
cnt-=2;
}
cout<<ans;
}
|
#include<bits/stdc++.h>
using namespace std;
const int maxn=200009;
int n;
int a[maxn];
queue<int>q;
long long ans;
int main(){
cin>>n;
for(int i=1;i<=n;++i)cin>>a[i];
sort(a+1,a+1+n);
q.push(a[n]);
for(int i=n-1;i>=1;--i){
ans+=q.front();q.pop();
q.push(a[i]);q.push(a[i]);
}
printf("%lld\n",ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int q;
cin >> q;
vector<ll> p(31, 1);
for (int i = 0; i < 30; i++) {
p[i + 1] = p[i] * 3;
}
while (q--) {
ll a, b, c, d;
cin >> a >> b >> c >> d;
a--, b--, c--, d--;
if (abs(b - d) < abs(a - c)) {
swap(a, b);
swap(c, d);
}
ll ans = abs(a - c) + abs(b - d);
for (int i = 29; i >= 0; i--) {
if (a / p[i] % 3 != 1 || c / p[i] % 3 != 1) {
continue;
}
if (a / p[i] != c / p[i]) {
continue;
}
if ((b / p[i] + 2) / 3 == (d / p[i] + 2) / 3) {
continue;
}
ans = abs(b - d);
ans += min(a % p[i + 1] - p[i] + 1 + c % p[i + 1] - p[i] + 1, p[i] * 2 - a % p[i + 1] + p[i] * 2 - c % p[i + 1]);
break;
}
cout << ans << '\n';
}
return 0;
}
|
#include <iostream>
using namespace std;
bool between(long long x, long long y1, long long y2) {
if (y1 > y2) swap(y1, y2);
y1++, y2--;
if (y1 > y2) return false;
while (x) {
if (x % 3 == 1) {
if (y2 - y1 > 3) return true;
for (long long y = y1; y <= y2; y++)
if (y % 3 == 1) return true;
}
x /= 3, y1 /= 3, y2 /= 3;
}
return false;
}
long long get_extra(long long x1, long long y1, long long x2, long long y2) {
long long ans = 0, three = 1;
for (int i = 0; i < 35; i++) {
if (x1 / three == x2 / three && between(x1 / three, y1 / three, y2 / three)) {
long long t = min(min(x1 % three, x2 % three) + 1, three - max(x1 % three, x2 % three));
ans = max(ans, t);
}
three *= 3;
}
return ans;
}
long long get_dist(long long x1, long long y1, long long x2, long long 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;
scanf("%d", &q);
while (q--) {
long long a, b, c, d;
scanf("%lld %lld %lld %lld", &a, &b, &c, &d);
printf("%lld\n", get_dist(a - 1, b - 1, c - 1, d - 1));
}
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 w=Pow[d];
if(x1/w!=x2/w)return abs(x2-x1)+abs(y2-y1);
if(x1/w==1&&abs(y1/w-y2/w)>=2){
x1%=w,x2%=w;
return abs(y2-y1)+min(x1+x2+2,w*2-x1-x2);
}
x1%=Pow[d],x2%=Pow[d];
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 calc(a,b,c,d,29);
}
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<iostream>
#include<cstdint>
#include<algorithm>
int64_t dist(int64_t x, int64_t y) { return std::max(x-y, y-x); }
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
int64_t res = dist(a2, b2);
while(Lb > 0) {
bi_a1 = a1/Lb;
bi_b1 = b1/Lb;
if(bi_a1 != bi_b1) return res + dist(a1,b1);
if(bi_a1 == 1) {
bi_a2 = a2/Lb;
bi_b2 = b2/Lb;
if(dist(bi_a2, bi_b2) > 1) { //
int64_t r1 = dist(a1, Lb-1)+dist(Lb-1, b1);
int64_t r2 = dist(a1, Lb*2)+dist(Lb*2, b1);
return res + std::min(r1, r2);
}
}
a1 %= Lb;
b1 %= Lb;
Lb /= 3;
}
return res;
}
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;
}
|
#ifndef __MATH_UTIL_H__
#define __MATH_UTIL_H__
/* updated: 2020-03-15 */
#include <cstdint>
#include <cassert>
#include <vector>
int64_t strictMod(int64_t a, int64_t b);
int64_t strictDiv(int64_t a, int64_t b);
template <class T>
T power(T base, uint64_t exp);
/* 累乗; 置く場所がないのでここに置いておきます O(logN) */
template <class T>
T power(T base, uint64_t exp) {
T product = T(1);
while (exp > 0) {
if (exp & 1) {
product *= base;
}
base *= base;
exp >>= 1;
}
return product;
}
std::vector<uint64_t> radixEncoding(uint64_t num, uint64_t radix, int length = -1);
uint64_t radixDecoding(const std::vector<uint64_t>& arr, uint64_t radix);
#endif
#ifdef ONLY_MY_ENVIR
#include "MathUtil.h"
#endif
/* 丸め方向を揃えた剰余 */
int64_t strictMod(int64_t a, int64_t b) {
assert(b > 0);
int64_t mod = a % b;
if (mod < 0) mod += b;
return mod;
}
/* 丸め方向を揃えた除算 */
int64_t strictDiv(int64_t a, int64_t b) {
assert(b > 0);
return (a - strictMod(a, b)) / b;
}
/* 基数変換 / uint を radix 進数に変換する */
/* 戻り値は各桁の値を格納した配列(1の位から格納) */
/* 値 0 に対しては空列を返す */
/* length に正の数を指定すると戻り値を固定長にする */
std::vector<uint64_t> radixEncoding(uint64_t num, uint64_t radix, int length) {
std::vector<uint64_t> arr;
while (num > 0) {
arr.push_back(num % radix);
num /= radix;
}
if (length >= 0) {
arr.resize(length, uint64_t());
}
return arr;
}
/* 基数変換 / radix 進数 を uint に変換する */
/* 引数は各桁の値を格納した配列(1の位から格納) */
uint64_t radixDecoding(const std::vector<uint64_t>& arr, uint64_t radix) {
uint64_t num = 0;
uint64_t base = 1;
for (int i = 0; i < arr.size(); ++i) {
num += arr[i] * base;
base *= radix;
}
return num;
}
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include <algorithm>
#include <functional>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <limits>
#include <numeric>
#include <valarray>
#include <fstream>
#include <array>
#include <random>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using uint = uint32_t;
using LL = int64_t;
using ULL = uint64_t;
using PP = pair<LL, LL>;
template <typename T> using PriorityQ = priority_queue<T, vector<T>, greater<T> >;
#define REP(i, a, n) for(LL i = (a), i##_max_ = (n); i < i##_max_; ++i)
#define REM(i, a, n) for(LL i = (LL)(n) - 1, i##_min_ = (a); i >= i##_min_; --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP { cin.tie(NULL); ios::sync_with_stdio(false); }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U> istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
template <class T, class U> ostream& operator<<(ostream& ost, const pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
template <class T, class TCompatible, size_t N> void Fill(T(&dest)[N], const TCompatible& val) { fill(dest, dest + N, val); }
template <class T, class TCompatible, size_t M, size_t N> void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
template <class T> T Next() { T buf; cin >> buf; return buf; }
istream& Ignore(istream& ist) { string s; ist >> s; return ist; }
bool Inside(int i, int j, int h, int w) { return i >= 0 && i < h && j >= 0 && j < w; }
#ifdef ONLY_MY_ENVIR
#include "IntMod.h"
#include "Accumulator.h"
#include "Algebraic.h"
#include "BinaryMatrix.h"
#include "BinaryTree.h"
#include "Bipartite.h"
#include "BIT.h"
#include "Compressor.h"
#include "Decompositions.h"
#include "DynamicMod.h"
#include "Factorization.h"
#include "FFT.h"
#include "FlowSolver.h"
#include "Graph.h"
#include "GraphUtil.h"
#include "LazySegmentTree.h"
#include "LIS.h"
#include "Math.h"
#include "MathUtil.h"
#include "Matrix.h"
#include "MinCostFlowSolver.h"
#include "MinMax.h"
#include "Numbers.h"
#include "Optimize.h"
#include "Permutation.h"
#include "Polynomial.h"
#include "Position.h"
#include "Range.h"
#include "Rational.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "Sets.h"
#include "Shortest.h"
#include "SlidingWindow.h"
#include "SpanningTree.h"
#include "StringSearching.h"
#include "SuffixArray.h"
#include "Tree.h"
#include "UnionFind.h"
#include "VectorUtil.h"
#endif
#ifdef __GNUC__
typedef __int128 LLL;
istream& operator>>(istream& ist, __int128& val) { LL tmp; ist >> tmp; val = tmp; return ist; }
ostream& operator<<(ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; }
#endif
using Num = vector<uint64_t>;
LL cat(LL ax, LL ay, LL bx, LL by) {
Num rax = radixEncoding(ax, 3, 30);
Num ray = radixEncoding(ay, 3, 30);
Num rbx = radixEncoding(bx, 3, 30);
Num rby = radixEncoding(by, 3, 30);
auto cut = [](Num n, int k) -> Num {
n.erase(n.begin(), n.begin() + k);
return n;
};
auto shortest = [](const Num& n, int k) -> LL {
Num a = n;
a[k] = 0;
REP(i, 0, k) {
a[i] = 2;
}
Num b = n;
b[k] = 2;
REP(i, 0, k) {
b[i] = 0;
}
return min(
abs((LL)radixDecoding(b, 3) - (LL)radixDecoding(n, 3)),
abs((LL)radixDecoding(a, 3) - (LL)radixDecoding(n, 3))
);
};
REM(k, 0, 30) {
if (rax[k] != rbx[k]) return 0;
if (rax[k] == 1 && rbx[k] == 1) {
LL p = radixDecoding(cut(ray, k), 3);
LL q = radixDecoding(cut(rby, k), 3);
if (abs(q - p) >= 2) {
return min(shortest(rax, k), shortest(rbx, k));
}
}
}
return 0;
}
LL sub(LL ax, LL ay, LL bx, LL by) {
return abs(bx - ax) + abs(by - ay)
+ 2 * max(
cat(ax, ay, bx, by),
cat(ay, ax, by, bx)
);
}
int main() {
int Q;
cin >> Q;
while (Q--) {
LL ax, ay, bx, by;
cin >> ax >> ay >> bx >> by;
--ax; --ay; --bx; --by;
cout << sub(ax, ay, bx, by) << endl;
}
return 0;
}
|
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<functional>
#include <random>
#include<iostream>
#include<iomanip>
using namespace std;
long long pow3[31];
int main() {
long long a = 1;
for (int i = 0; i < 31; i++) {
pow3[i] = a;
a = a * 3;
}
int Q;
cin >> Q;
for (int i = 0; i < Q; i++) {
long long x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--; y1--; x2--; y2--;
long long x1_copy, x2_copy, y1_copy, y2_copy;
int x1_tri[30], y1_tri[30], x2_tri[30], y2_tri[30];
x1_copy = x1;
y1_copy = y1;
x2_copy = x2;
y2_copy = y2;
for (int i = 0; i < 30; i++) {
x1_tri[29 - i] = x1 % 3; x1 = x1 / 3;
y1_tri[29 - i] = y1 % 3; y1 = y1 / 3;
x2_tri[29 - i] = x2 % 3; x2 = x2 / 3;
y2_tri[29 - i] = y2 % 3; y2 = y2 / 3;
}
x1 = x1_copy;
y1 = y1_copy;
x2 = x2_copy;
y2 = y2_copy;
/*縦に■を横切るか確認*/
int N = 30;
for (int i = 0; i < 30; i++) {
if (x1_tri[i] != x2_tri[i]) { break; }
if (x1_tri[i] == 1 && x2_tri[i] == 1) {
long long y1_r = 0;
long long y2_r = 0;
for (int j = 0; j <= i; j++) {
y1_r *= 3; y1_r += y1_tri[j];
y2_r *= 3; y2_r += y2_tri[j];
}
if (y1_r - y2_r >= 2 || y1_r - y2_r <= -2) {
N = i;
break;
}
}
}
if (N < 30) {
long long x1_er = 0;
long long x2_er = 0;
for (int i = N + 1; i < 30; i++) {
x1_er *= 3; x1_er += x1_tri[i];
x2_er *= 3; x2_er += x2_tri[i];
}
long long ans = 0;
ans = x1_er + x2_er + 2;
if (ans > (pow3[30 - N - 1] - x1_er) + (pow3[30 - N - 1] - x2_er)) {
ans = (pow3[30 - N - 1] - x1_er) + (pow3[30 - N - 1] - x2_er);
}
if (y1 > y2) {
ans += y1 - y2;
}
else {
ans += y2 - y1;
}
cout << ans << "\n";
continue;
}
/*横に■を横切るか確認*/
N = 30;
for (int i = 0; i < 30; i++) {
if (y1_tri[i] != y2_tri[i]) { break; }
if (y1_tri[i] == 1 && y2_tri[i] == 1) {
long long x1_r = 0;
long long x2_r = 0;
for (int j = 0; j <= i; j++) {
x1_r *= 3; x1_r += x1_tri[j];
x2_r *= 3; x2_r += x2_tri[j];
}
if (x1_r - x2_r >= 2 || x1_r - x2_r <= -2) {
N = i;
break;
}
}
}
if (N < 30) {
long long y1_er = 0;
long long y2_er = 0;
for (int i = N + 1; i < 30; i++) {
y1_er *= 3; y1_er += y1_tri[i];
y2_er *= 3; y2_er += y2_tri[i];
}
long long ans = 0;
ans = y1_er + y2_er + 2;
if (ans > (pow3[30 - N - 1] - y1_er) + (pow3[30 - N - 1] - y2_er)) {
ans = (pow3[30 - N - 1] - y1_er) + (pow3[30 - N - 1] - y2_er);
}
if (x1 > x2) {
ans += x1 - x2;
}
else {
ans += x2 - x1;
}
cout << ans << "\n";
continue;
}
/*邪魔がないとき*/
long long x_diff, y_diff;
if (x1 - x2 > 0) { x_diff = x1 - x2; }
else { x_diff = x2 - x1; }
if (y1 - y2 > 0) { y_diff = y1 - y2; }
else { y_diff = y2 - y1; }
cout << x_diff + y_diff << "\n";
}
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll pow3[33];
ll work(ll a,ll b,ll c,ll d,int k)
{
if(k==0)
{
return abs(a-c) + abs(b-d);
}
ll sx = a / pow3[k-1];
ll sy = b / pow3[k-1];
ll tx = c / pow3[k-1];
ll ty = d / pow3[k-1];
if(sx != tx && sy != ty)
{
return abs(a-c)+abs(b-d);
}
if(sx==tx && sy==ty)
{
return work(a%pow3[k-1],b%pow3[k-1],c%pow3[k-1],d%pow3[k-1],k-1);
}
if(sx == tx && sx % 3 == 1 && abs(sy-ty)>1 )
{
return abs(b-d) + min((a/pow3[k-1]+1)*pow3[k-1]-a + (c/pow3[k-1]+1)*pow3[k-1]-c,
a+1-a/pow3[k-1]*pow3[k-1] + c+1-c/pow3[k-1]*pow3[k-1]);
}
if(sy == ty && sy % 3 == 1 && abs(sx-tx)>1)
{
return abs(a-c) + min((b/pow3[k-1]+1)*pow3[k-1]-b + (d/pow3[k-1]+1)*pow3[k-1]-d,
b+1-b/pow3[k-1]*pow3[k-1] + d+1-d/pow3[k-1]*pow3[k-1]);
}
return work(a,b,c,d,k-1);
}
void solve(long long Q, std::vector<long long> a, std::vector<long long> b, std::vector<long long> c, std::vector<long long> d){
for(int i=0;i<Q;++i)
{
cout<<work(a[i]-1,b[i]-1,c[i]-1,d[i]-1,30)<<endl;
}
}
int main(){
pow3[0]=1;
for(int i=1;i<=30;++i)pow3[i]=pow3[i-1]*3;
long long Q;
scanf("%lld",&Q);
std::vector<long long> a(Q);
std::vector<long long> b(Q);
std::vector<long long> c(Q);
std::vector<long long> d(Q);
for(int i = 0 ; i < Q ; i++){
scanf("%lld",&a[i]);
scanf("%lld",&b[i]);
scanf("%lld",&c[i]);
scanf("%lld",&d[i]);
}
solve(Q, std::move(a), std::move(b), std::move(c), std::move(d));
return 0;
}
|
#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 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);
}
cout << solve(a,c,b,d,30) << endl;
}
return 0;
}
|
#include <stdio.h>
using namespace std;
typedef long long ll;
ll mabs(ll a) {
return a > 0 ? a : -a;
}
int main(void) {
ll i, j, k, q, a[4], a3[4][31], now, t;
scanf("%lld", &q);
for(i = 0; i < q; ++i) {
for(j = 0; j < 4; ++j) scanf("%lld", &a[j]), a[j]--;
for(j = 0; j < 4; ++j) {
now = a[j];
for(k = 0; k < 31; ++k) a3[j][k] = now % 3, now /= 3;
}
now = -1, t = 68630377364883;
bool b1 = false, b2 = false;
for(j = 29; j >= 0; --j) {
if(a3[0][j] != a3[2][j]) b1 = true;
if(a3[1][j] != a3[3][j]) b2 = true;
if(b1 && b2) break;
else if((a3[0][j] == 1 && a3[2][j] == 1 && b2 && mabs(a[1] / t - a[3] / t) != 1) || (a3[1][j] == 1 && a3[3][j] == 1 && b1 && mabs(a[0] / t - a[2] / t) != 1)) {
now = j;
break;
}
t /= 3;
}
if(now == -1) printf("%lld\n", mabs(a[0] - a[2]) + mabs(a[1] - a[3]));
else {
t = 1;
for(j = 0; j < now; ++j) t *= 3;
if(a3[0][now] == 1) {
if(a[0] % t + a[2] % t < t - 1) printf("%lld\n", mabs(a[1] - a[3]) + a[0] % t + a[2] % t + 2);
else printf("%lld\n", mabs(a[1] - a[3]) + 2 * t - a[0] % t - a[2] % t);
} else {
if(a[1] % t + a[3] % t < t - 1) printf("%lld\n", mabs(a[0] - a[2]) + a[1] % t + a[3] % t + 2);
else printf("%lld\n", mabs(a[0] - a[2]) + 2 * t - a[1] % t - a[3] % t);
}
}
}
return 0;
} |
#include "bits/stdc++.h"
using namespace std;
int main() {
int Q;
cin >> Q;
vector<long long>P(30);
for (int i = 0; i < 30; ++i) {
if (0 == i) {
P[i] = 1;
continue;
}
P[i] = P[i - 1] * 3;
}
for (int i = 0; i < Q; ++i) {
long long x, y, z, w;
cin >> x >> y >> z >> w;
x--;
y--;
z--;
w--;
long long ret = abs(z - x) + abs(w - y);
for (int j = 29; j >= 0; --j) {
long long x1 = x / P[j];
long long y1 = y / P[j];
long long z1 = z / P[j];
long long w1 = w / P[j];
if ((x1 == z1) && (1 == x1 % 3) && (abs(y1 - w1) >= 2)) {
ret = abs(y - w) + min(x%P[j] + z % P[j] + 2, 2 * P[j] - x % P[j] - z % P[j]);
break;
}
if ((y1 == w1) && (1 == y1 % 3) && (abs(x1 - z1) >= 2)) {
ret = abs(x - z) + min(y%P[j] + w % P[j] + 2, 2 * P[j] - y % P[j] - w % P[j]);
break;
}
}
cout << ret << endl;
}
return 0;
}
|
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
ll n[31];
ll solve(ll a, ll b, ll c, ll d, int k){
assert(0<=a && a<n[k] && 0<=b && b<n[k] && 0<=c && c<n[k] && 0<=d && d<n[k]);
ll x1=a/n[k-1], y1=b/n[k-1], x2=c/n[k-1], y2=d/n[k-1];
if(P(x1, y1)==P(x2, y2)){
a-=n[k-1]*x1, b-=n[k-1]*y1, c-=n[k-1]*x2, d-=n[k-1]*y2;
return solve(a, b, c, d, k-1);
}
if(x1!=x2 && y1!=y2) return abs(a-c)+abs(b-d);
if(y1!=y2){
swap(a, b), swap(c, d), swap(x1, y1), swap(x2, y2);
}
if(y1==1){
ll s=b-n[k-1]+1+d-n[k-1]+1;
return abs(a-c)+min(s, 2*(n[k-1]+1)-s);
}
b-=y1*n[k-1], d-=y1*n[k-1];
if(k==1) return abs(a-c)+abs(b-d);
if(x1>x2){
swap(x1, x2), swap(a, c), swap(b, d);
}
ll x11=a/n[k-2], y11=b/n[k-2], x22=c/n[k-2], y22=d/n[k-2];
if(y11!=y22){
return abs(a-c)+abs(b-d);
}
if(y11==1 && ((x11==2 && x22==3) || (x11==5 && x22==6))){
b-=n[k-2], d-=n[k-2], a-=x11*n[k-2], c-=x11*n[k-2];
return solve(a, b, c, d, k-1);
}else if(y11==1){
c-=n[k-2]*(x22-2);
a-=n[k-2]*x11;
return (x22-x11-2)*n[k-2]+solve(a, b, c, d, k-1);
}else if(x22-x11==1){
c-=n[k-2]*(x22-1);
a-=n[k-2]*x11;
return (x22-x11-1)*n[k-2]+solve(a, b, c, d, k-1);
}else{
c-=n[k-2]*(x22-2);
a-=n[k-2]*x11;
return (x22-x11-2)*n[k-2]+solve(a, b, c, d, k-1);
}
}
int main()
{
n[0]=1;
for(int i=1; i<=30; i++) n[i]=n[i-1]*3;
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<<solve(a, b, c, d, 30)<<endl;
}
return 0;
}
|
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <deque>
using namespace std;
const int MAXD = 35;
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);
}
inline 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<bits/stdc++.h>
int main(){
using namespace std;
unsigned long Q;
cin >> Q;
constexpr unsigned long M{205891132094649UL};
vector<unsigned long> p3{1};
for(unsigned long i{0}; i < 30; ++i)p3.push_back(p3.back() * 3);
const auto& calc = [&p3](unsigned long a, unsigned long b, unsigned long c, unsigned long d) -> unsigned long {
vector<unsigned long> i;
for(const auto& p : p3)if(a / p % 3 == 1 || a / p * p + p * (1 + (a / p % 3 == 2)) <= c)i.push_back(p);
auto ub{b}, lb{b};
for(const auto& p : i){
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>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
using lint=long long;
lint pow3[31]; // pow3[i] = 3^i
lint to_int(vector<int> s){
lint a=0;
rep(i,s.size()) a=a*3+s[i];
return a;
}
lint solve(vector<int> x1,vector<int> y1,vector<int> x2,vector<int> y2){
int n=x1.size();
if(n==0) return 0;
lint dist_x=abs(to_int(x1)-to_int(x2));
lint dist_y=abs(to_int(y1)-to_int(y2));
if(y1[0]==y2[0] && x1[0]==x2[0]){
x1=vector<int>(x1.begin()+1,x1.end());
y1=vector<int>(y1.begin()+1,y1.end());
x2=vector<int>(x2.begin()+1,x2.end());
y2=vector<int>(y2.begin()+1,y2.end());
return solve(x1,y1,x2,y2);
}
else if(y1[0]!=y2[0] && x1[0]!=x2[0]){
return dist_x+dist_y;
}
else if(abs(y1[0]-y2[0])==2 && x1[0]==1 && x2[0]==1) {
vector<int> xl(n,2); xl[0]=0;
vector<int> xr(n,0); xr[0]=2;
lint dist_l=to_int(x1)-to_int(xl)+to_int(x2)-to_int(xl); // 左回り
lint dist_r=to_int(xr)-to_int(x1)+to_int(xr)-to_int(x2); // 右回り
return min(dist_l,dist_r)+dist_y;
}
else if(abs(x1[0]-x2[0])==2 && y1[0]==1 && y2[0]==1) {
return solve(y1,x1,y2,x2);
}
else{
// (x1,y1) が左上のブロックに来るように調整
if(y1[0]==1 || x1[0]==1){
swap(y1,y2);
swap(x1,x2);
}
if(x1[0]==2){
rep(i,n){
x1[i]=2-x1[i];
x2[i]=2-x2[i];
}
}
if(y1[0]==2){
rep(i,n){
y1[i]=2-y1[i];
y2[i]=2-y2[i];
}
}
// (x2,y2) が上段に来るように調整
if(y2[0]!=0){
swap(x1,y1);
swap(x2,y2);
}
if(n==1 || y1[1]!=y2[1]){ // 上段の中で違う高さにある
return dist_x+dist_y;
}
else{ // 上段の中で同じ高さにある
if(x1[1]==2 && x2[0]==1 && x2[1]==0){ // 隣接している
y1[1]=0;
y2[1]=0;
x1[1]=1;
x2[0]=0;
x2[1]=2;
return solve(x1,y1,x2,y2);
}
else{
lint cost=0;
cost-=x1[1]*pow3[n-2];
x1[1]=0;
cost+=x2[0]*pow3[n-1]-(2-x2[1])*pow3[n-2];
x2[0]=0;
x2[1]=2;
return solve(x1,y1,x2,y2)+cost;
}
}
}
}
int main(){
pow3[0]=1;
rep(i,30) pow3[i+1]=pow3[i]*3;
int q; scanf("%d",&q);
rep(_,q){
lint a,b,c,d; scanf("%lld%lld%lld%lld",&a,&b,&c,&d); a--; b--; c--; d--;
auto to_ternary=[](lint a){
vector<int> s;
rep(i,30){
s.emplace_back(a%3);
a/=3;
}
reverse(s.begin(),s.end());
return s;
};
printf("%lld\n",solve(to_ternary(a),to_ternary(b),to_ternary(c),to_ternary(d)));
}
return 0;
}
|
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"
using namespace std;
//constexpr long long int MOD = 1000000007;
constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr long double EPS = 1e-12;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int K;
cin >> K;
vector<long long int>by(32, 1);
for (int i = 1; i <= 31; i++) {
by[i] = by[i - 1] * 3;
}
/*
vector<vector<int>>v(27, vector<int>(27));
for (int i = 1; i <= 8; i += 3) {
for (int j = 1; j <= 8; j += 3)v[i][j] = 1;
}
for (int i = 3; i <= 5; i++) {
for (int j = 3; j <= 5; j++)v[i][j] = 1;
}
for (int i = 0; i < 27; i++) {
for (int j = 0; j < 27; j++) {
v[i][j] = v[i % 9][j % 9];
}
}
for (int i = 9; i < 18; i++) {
for (int j = 9; j < 18; j++)v[i][j] = 1;
}
for (auto i : v) {
for (auto j : i)cout << j;
cout << endl;
}
for (int sy = 0; sy < 27; sy++) {
for (int sx = 0; sx < 27; sx++) {
for (int gy = 0; gy < 27; gy++) {
for (int gx = 0; gx < 27; gx++) {
if (v[sy][sx] || v[gy][gx])continue;
vector<vector<int>>dis(27, vector<int>(27, MOD));
dis[sy][sx] = 0;
queue<pair<int, int>>Q;
Q.push({ sy,sx });
while (!Q.empty()) {
int ny, nx;
tie(ny, nx) = Q.front();
Q.pop();
int dir[] = { 0,-1,0,1,0 };
for (int i = 0; i < 4; i++) {
int y = ny + dir[i];
int x = nx + dir[i + 1];
if (y < 0 || y >= 27 || x < 0 || x >= 27)continue;
if (v[y][x])continue;
if (dis[y][x] > dis[ny][nx] + 1) {
dis[y][x] = dis[ny][nx] + 1;
Q.push({ y,x });
}
}
}
int a = dis[gy][gx];
int b = 0;
long long int ans = abs(sy - gy) + abs(sx - gx);
int num = -1;
for (int i = 29; i >= 0; i--) {
if (abs(sx - gx) < by[i] + 1||(sx+by[i]+by[i+1])/by[i+1]== (gx + by[i]+ by[i + 1]) / by[i + 1])continue;
for (int j = i; j >= 0; j--) {
if (sy%by[j + 1] >= by[j] && sy%by[j + 1] < by[j] * 2 && gy%by[j + 1] >= by[j] && gy%by[j + 1] < by[j] * 2 && abs(sy - gy) < by[j]) {
num = j;
break;
}
}
if (num >= 0) {
long long int add = min(sy%by[num + 1] + 1 + gy % by[num + 1] + 1 - by[num] * 2 - abs(sy - gy), by[num] * 2 - sy % by[num + 1] + by[num] * 2 - gy % by[num + 1] - abs(sy - gy));
b = ans + add;
break;
}
}
if (num == -1) {
for (int i = 29; i >= 0; i--) {
if (abs(sy - gy) < by[i] + 1 || (sy + by[i] + by[i + 1]) / by[i + 1] == (gy + by[i] + by[i + 1]) / by[i + 1])continue;
for (int j = i; j >= 0; j--) {
if (sx%by[j + 1] >= by[j] && sx%by[j + 1] < by[j] * 2 && gx%by[j + 1] >= by[j] && gx%by[j + 1] < by[j] * 2 && abs(sx - gx) < by[j]) {
num = j;
break;
}
}
if (num >= 0) {
long long int add = min(sx%by[num + 1] + 1 + gx % by[num + 1] + 1 - by[num] * 2 - abs(sx - gx), by[num] * 2 - sx % by[num + 1] + by[num] * 2 - gx % by[num + 1] - abs(sx - gx));
b = ans + add;
break;
}
}
if (num == -1) {
b = ans;
}
}
if (a != b) {
cout << sy << " " << sx << " " << gy << " " << gx << endl;
cout << a << " " << b << endl;
cout << num << endl;
}
}
}
}
}
return 0;
*/
while (K--) {
long long int sy, sx, gy, gx;
cin >> sy >> sx >> gy >> gx;
sy--, sx--, gy--, gx--;
long long int ans = abs(sy - gy) + abs(sx - gx);
int num = -1;
for (int i = 29; i >= 0; i--) {
if (abs(sx - gx) < by[i] + 1 || (sx + by[i] + by[i + 1]) / by[i + 1] == (gx + by[i] + by[i + 1]) / by[i + 1])continue;
for (int j = i; j >= 0; j--) {
if (sy%by[j + 1] >= by[j] && sy%by[j + 1] < by[j] * 2 && gy%by[j + 1] >= by[j] && gy%by[j + 1] < by[j] * 2 && abs(sy - gy) < by[j]) {
num = j;
break;
}
}
if (num >= 0) {
long long int add = min(sy%by[num + 1] + 1 + gy % by[num + 1] + 1 - by[num] * 2 - abs(sy - gy), by[num] * 2 - sy % by[num + 1] + by[num] * 2 - gy % by[num + 1] - abs(sy - gy));
cout << ans + add << endl;
break;
}
}
if (num == -1) {
for (int i = 29; i >= 0; i--) {
if (abs(sy - gy) < by[i] + 1 || (sy + by[i] + by[i + 1]) / by[i + 1] == (gy + by[i] + by[i + 1]) / by[i + 1])continue;
for (int j = i; j >= 0; j--) {
if (sx%by[j + 1] >= by[j] && sx%by[j + 1] < by[j] * 2 && gx%by[j + 1] >= by[j] && gx%by[j + 1] < by[j] * 2 && abs(sx - gx) < by[j]) {
// cout << "Num " << num << endl;
num = j;
// cout << "num " << num << endl;
break;
}
}
if (num >= 0) {
long long int add = min(sx%by[num + 1] + 1 + gx % by[num + 1] + 1 - by[num] * 2 - abs(sx - gx), by[num] * 2 - sx % by[num + 1] + by[num] * 2 - gx % by[num + 1] - abs(sx - gx));
cout << ans + add << endl;
break;
}
}
if (num == -1) {
cout << ans << endl;
}
}
//cout << num << endl;
}
} |
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int M = 30;
int main() {
ll p3[M];
for (int i = 0; i < M; i++) {
p3[i] = i == 0 ? 1 : 3 * p3[i-1];
}
int q;
cin >> q;
while (q--) {
ll a, b, c, d;
cin >> a >> b >> c >> d;
a--, b--, c--, d--;
ll ans = 0;
for (int i = M - 1; i >= 0; i--) {
ll a3 = a / p3[i], b3 = b / p3[i];
ll c3 = c / p3[i], d3 = d / p3[i];
if (a3 != c3 && b3 != d3) {
ans += abs(a - c) + abs(b - d);
break;
}
if (a3 == c3 && b3 == d3) {
a = a % p3[i], b = b % p3[i];
c = c % p3[i], d = d % p3[i];
continue;
}
if (b3 == d3) {
swap(a, b);
swap(c, d);
swap(a3, b3);
swap(c3, d3);
}
if (b > d) {
swap(a, c);
swap(b, d);
swap(a3, c3);
swap(b3, d3);
}
if ((a3 == 1 || a3 == 4) && (b3 == 0 || d3 == 5)) {
ans += d - b;
ll dis1 = a % p3[i] + c % p3[i];
ll dis2 = 2 * p3[i] - 2 - dis1;
ans += min(dis1, dis2) + 2;
break;
}
if (i == 0) {
ans += abs(a - c) + abs(b - d);
break;
}
a -= a3 * p3[i], c -= c3 * p3[i];
a3 = c3 = 0;
b -= b3 * p3[i], d -= b3 * p3[i];
d3 -= b3, b3 = 0;
if (d3 <= 1) {
continue;
}
ll d9 = d / p3[i-1];
ans += (d9 - 5) * p3[i-1];
d -= (d9 - 5) * p3[i-1];
d9 = 5;
d3 = 1;
}
cout << ans << endl;
}
return 0;
}
|
#include <cmath>
#include <iostream>
using namespace std;
long long dist(long long a, long long b, long long c, long long d) {
return abs(a - c) + abs(b - d);
}
long long solve(long long a, long long b, long long c, long long d) {
const long long N = 205891132094649LL;
if (c - a > d - b) { swap(a, b); swap(c, d); }
long long s = N/3;
while (s > 0) {
// cerr << a << ' ' << b << ' ' << c << ' ' << d1 << " " << s << endl;
long long a2 = a / s % 3, b2 = b / s % 3, c2 = c / s % 3;
if (a2 == 1 && c2 == 1) {
long long d1 = b2 == 0 ? b / s * s + s : (b / s / 3 + 1) * 3 * s + s;
if (d1 <= d) {
long long h1 = min(a % s * 2 + 2, (s - a % s) * 2),
h2 = min(c % s * 2 + 2, (s - c % s) * 2);
// cerr << h1 << " " << h2 << " " << s << endl;
return dist(a, b, c, d) + min(h1, h2);
}
} else if (a2 != c2) {
break;
}
s /= 3;
}
return dist(a, b, c, d);
}
int main() {
int q; cin >> q;
while (q--) {
long long a, b, c, d; cin >> a >> b >> c >> d;
a--; b--; c--; d--;
cout << solve(min(a, c), min(b, d), max(a, c), max(b, d)) << endl;
}
}
|
#pragma GCC optimize ("Ofast")
#pragma GCC target ("avx")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<random>
#include<functional>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<unordered_map>
#include<climits>
#include<fstream>
#include<complex>
#include<time.h>
#include<cassert>
#include<functional>
#include<numeric>
#include<tuple>
using namespace std;
using ll = long long;
using ld = long double;
#define int long long
#define all(a) (a).begin(),(a).end()
#define fs first
#define sc second
#define xx first
#define yy second.first
#define zz second.second
#define H pair<int, int>
#define P pair<int, pair<int, int>>
#define Q(i,j,k) mkp(i,mkp(j,k))
#define R pair<pair<int, int>, pair<int, int>>
#define S(i,j,k,l) mkp(mkp(i,j),mkp(k,l))
#define rep(i,n) for(int (i) = 0 ; (i) < (n) ; (i)++)
#define rng(i,s,n) for(int (i) = (s) ; (i) < (n) ; (i)++)
#define req(n,i) for(int (i) = (n) - 1 ; (i) >= 0 ; (i)--)
#define range(i,v) for(auto& (i) : v)
#define mkp make_pair
#define mem(x,k) memset(x,k,sizeof(x))
#define vec vector
#define pb emplace_back
#define lb lower_bound
#define ub upper_bound
#define crdcomp(b) sort(all((b)));(b).erase(unique(all((b))),(b).end())
#define getidx(b,i) lower_bound(all(b),(i))-b.begin()
#define ssp(i,n) (i==n-1?"\n":" ")
#define ctoi(c) (int)(c-'0')
#define itoc(c) (char)(c+'0')
#define itn int
#define pri_que priority_queue
#define pp(x,y) pb(H{x,y})
#define ppp(x,y,z) pb(Q(x,y,z))
#define pppp(x,y,z,w) pb(S(x,y,z,w))
#define cyes cout<<"Yes\n"
#define cno cout<<"No\n"
#define endl "\n"
constexpr int mod = 1e9 + 7;
constexpr int Mod = 998244353;
constexpr int MXN = 500000 + 100;
constexpr ll inf = 3 * 1e18;
constexpr int Inf = 15 * 1e8;
const vec<int>dx{ -1,1,0,0 }, dy{ 0,0,-1,1 };
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 (b < a) { a = b; return 1; } return 0; }
ll read() { ll u, k = scanf("%lld", &u); return u; }
string reads() { string s; cin >> s; return s; }
H readh(bool g = 0) { H u; ll k = scanf("%lld %lld", &u.fs, &u.sc); if (g) u.fs--, u.sc--; return u; }
void printh(H t) { printf("%lld %lld\n", t.fs, t.sc); }
bool inarea(H t, int h, int w) { return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w; }
ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; }
ll mod_pow(ll x, ll n, ll p = mod) {
ll res = 1;
while (n > 0) {
if (n & 1) res = res * x % p;
x = x * x % p;
n >>= 1;
}
return res;
}//x^n%p
ll bitcount(ll x) {
int sum = 0; for (int i = 0; i < 60; i++)if ((1ll << i) & x) sum++;
return sum;
}
/*constexpr int fn_ = 1000005;
ll fact_[fn_], comp_[fn_];
ll comb(ll x, ll y, ll Mod = mod) {
if (!fact_[0]) {
fact_[0] = 1; comp_[0] = 1;
for (int i = 1; i < fn_; i++)
fact_[i] = fact_[i - 1] * i % Mod;
comp_[fn_ - 1] = mod_pow(fact_[fn_ - 1], Mod - 2, Mod);
for (int i = fn_ - 2; i > 0; i--)
comp_[i] = comp_[i + 1] * (i + 1) % Mod;
}
if (x < y) return 0;
return fact_[x] * comp_[x - y] % Mod * comp_[y] % Mod;
}*/
//--------------------------------------------------------------
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, 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));
}
signed main() {
int q; cin >> q;
rep(_, q) {
ll a, b, c, d; cin >> a >> b >> c >> d;
a--; b--; c--; d--;
cout << get_dist(a, b, c, d) << endl;
}
} |
#include <iostream>
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;
}
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
const double TL = 1.5 * CLOCKS_PER_SEC;
mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());
#define clr(x, y) memset(x, y, sizeof(x))
#define forn(i, n) for (int i = 0; i < n; i++)
#define LL long long
int n;
LL s[50];
vector<LL> cal(LL x, LL y, int d) {
LL sx = (x - 1) / s[d] + 1;
LL tx = (x - 1) % s[d] + 1;
LL sy = (y - 1) / s[d] + 1;
LL ty = (y - 1) % s[d] + 1;
if(tx > s[d - 1] && tx <= s[d - 1] * 2) {
int p = ty <= s[d - 1] ? 0 : 1;
return {1, sx, sy + p, tx - s[d - 1], s[d - 1] * 2 - tx + 1};
}
if(ty > s[d - 1] && ty <= s[d - 1] * 2) {
int p = tx <= s[d - 1] ? 0 : 1;
return {2, sy, sx + p, ty - s[d - 1], s[d - 1] * 2 - ty + 1};
}
return {0, 0, 0, 0};
}
int main() {
//auto start_clock = clock();
// freopen("in", "r", stdin);
s[0] = 1;
for(int i = 1; i <= 30; i++) {
s[i] = s[i - 1] * 3;
}
scanf("%d", &n);
while(n--) {
LL x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
LL ans = 0;
for(int i = 30; i >= 0; i--) {
if(i == 0) {
ans = abs(x1 - x2) + abs(y1 - y2);
}
else {
auto v1 = cal(x1, y1, i);
auto v2 = cal(x2, y2, i);
if(v1[0] == v2[0] && v1[0] != 0 && v1[1] == v2[1] && v1[2] != v2[2]) {
LL t = min(v1[3] + v2[3], v1[4] + v2[4]);
if(abs(v1[0]) == 1) {
t += abs(y1 - y2);
}
else {
t += abs(x1 - x2);
}
ans = t;
break;
}
}
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll width[35]={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) {
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() {
//freopen("in.txt","r",stdin);
for(int i=1;i<35;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);
printf("%lld\n",cal(a,b,c,d,30));
}
return 0;
} |
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
using namespace std;
//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#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++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acos(-1.0);
//typedef vector<vector<ll>> mat;
typedef vector<int> vec;
ll mod_pow(ll a, ll n, ll m) {
ll res = 1;
while (n) {
if (n & 1)res = res * a%m;
a = a * a%m; n >>= 1;
}
return res;
}
struct modint {
ll n;
modint() :n(0) { ; }
modint(ll m) :n(m) {
if (n >= mod)n %= mod;
else if (n < 0)n = (n%mod + mod) % mod;
}
operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint &a, modint b) { a.n = ((ll)a.n*b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, int n) {
if (n == 0)return modint(1);
modint res = (a*a) ^ (n / 2);
if (n % 2)res = res * a;
return res;
}
ll inv(ll a, ll p) {
return (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
const int max_n = 1 << 18;
modint fact[max_n], factinv[max_n];
void init_f() {
fact[0] = modint(1);
for (int i = 0; i < max_n - 1; i++) {
fact[i + 1] = fact[i] * modint(i + 1);
}
factinv[max_n - 1] = modint(1) / fact[max_n - 1];
for (int i = max_n - 2; i >= 0; i--) {
factinv[i] = factinv[i + 1] * modint(i + 1);
}
}
modint comb(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[b] * factinv[a - b];
}
using mP = pair<modint, modint>;
ll t3[31];
void solve() {
t3[0] = 1;
rep(i, 30) {
t3[i + 1] = t3[i] * 3;
}
ll x[2], y[2];
cin >> x[0] >> y[0] >> x[1] >> y[1];
rep(i, 2) {
x[i]--; y[i]--;
}
if (x[0] > x[1]) {
swap(x[0], x[1]);
swap(y[0], y[1]);
}
ll ans = abs(x[1] - x[0]) + abs(y[1] - y[0]);
for (int t = 29; t >= 0; t--) {
ll dx = x[0] / t3[t+1];
ll gdx = x[1] / t3[t + 1];
ll dy = y[0] / t3[t + 1];
ll gdy = y[1] / t3[t + 1];
if (dx == gdx) {
ll rx = x[0] % t3[t + 1];
ll grx = x[1] % t3[t + 1];
if (t3[t] <= rx && rx < 2 * t3[t] && t3[t] <= grx && grx < 2 * t3[t]) {
bool prob = true;
if (dy == gdy) {
ll ry = y[0] % t3[t + 1];
ll gry = y[1] % t3[t + 1];
prob = false;
if (ry < t3[t] && gry >= 2 * t3[t])prob = true;
if (gry < t3[t] && ry >= 2 * t3[t])prob = true;
}
else {
ll yy = abs(y[1] - y[0]);
if (yy < 2 * t3[t])prob = false;
}
if (prob) {
ll ori = abs(x[1] - x[0]);
ll nexu = rx - t3[t] + 1 + grx - t3[t] + 1;
ll nexd = 2 * t3[t] - rx + 2 * t3[t] - grx;
ll ad = min(nexu, nexd) - ori;
//cout << "izyou x " << t << endl;
cout << ans + ad << endl; return;
}
}
}
if (dy == gdy) {
ll ry = y[0] % t3[t + 1];
ll gry = y[1] % t3[t + 1];
if (t3[t] <= ry && ry < 2 * t3[t] && t3[t] <= gry && gry < 2 * t3[t]) {
bool prob = true;
if (dx == gdx) {
ll rx = x[0] % t3[t + 1];
ll grx = x[1] % t3[t + 1];
prob = false;
if (rx < t3[t] && grx >= 2 * t3[t])prob = true;
if (grx < t3[t] && rx >= 2 * t3[t])prob = true;
}
else {
ll xx = abs(x[1] - x[0]);
if (xx < 2 * t3[t])prob = false;
}
if (prob) {
ll ori = abs(y[1] - y[0]);
ll nexu = ry - t3[t] + 1 + gry - t3[t] + 1;
ll nexd = 2 * t3[t] - ry + 2 * t3[t] - gry;
ll ad = min(nexu, nexd) - ori;
//cout << "izyou y " << t << endl;
cout << ans + ad << endl; return;
}
}
}
}
//cout << "hutuu" << endl;
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(7);
//init_f();
//init();
int t; cin >> t; rep(i, t)solve();
//solve();
stop
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 <= 30; 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 <iostream>
#include <string>
#include <algorithm>
#include <ctime>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cassert>
#include <bitset>
#include <list>
#include <cstdio>
#include <stdio.h>
#include <string.h>
#define rep(i,n) for(int i=0; i<(n); i++)
typedef long long ll;
using namespace std;
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>
using namespace std;
typedef signed long long ll;
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------
int Q;
ll X1,Y1,X2,Y2;
ll p3[32];
void solve() {
int i,j,k,l,r,x,y; string s;
p3[0]=1;
FOR(i,31) p3[i+1]=p3[i]*3;
cin>>Q;
while(Q--) {
cin>>X1>>Y1>>X2>>Y2;
X1--;
Y1--;
X2--;
Y2--;
ll ret=abs(X1-X2)+abs(Y1-Y2);
for(i=29;i>=0;i--) {
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) {
ret=abs(Y2-Y1)+min(X1%p3[i]+X2%p3[i]+2,2*p3[i]-X1%p3[i]-X2%p3[i]);
break;
}
if(y1==y2 && y1%3==1 && abs(x1-x2)>=2) {
ret=abs(X2-X1)+min(Y1%p3[i]+Y2%p3[i]+2,2*p3[i]-Y1%p3[i]-Y2%p3[i]);
break;
}
}
cout<<ret<<endl;
}
}
int main(int argc,char** argv){
string s;int i;
if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
cout.tie(0); solve(); 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;
}
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";
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) solve();
}
|
#include <cstdio>
#include <algorithm>
using namespace std;
int main() {
int Q;
long long a, b, c, d;
long long ttt = 1;
scanf("%d", &Q);
for (int i = 0; i < 30; ++i) ttt *= 3;
for (int i = 0; i < Q; ++i) {
scanf("%lld %lld %lld %lld", &a, &b, &c, &d);
--a, --b, --c, --d;
long long x = ttt;
for (int i = 29; i >= 0; --i) {
x /= 3;
long long pa = a / x, pb = b / x, pc = c / x, pd = d / x;
if (pa == pc && pa % 3 == 1 && pc % 3 == 1 && llabs(pd - pb) > 1) {
long long flex = pa * x - 1, tape = pc * x + x, bad = min(c + a - 2 * flex, 2 * tape - c - a);
printf("%lld\n", bad + llabs(d - b));
goto solve;
} else if (pb == pd && pb % 3 == 1 && pd % 3 == 1 && llabs(pc - pa) > 1) {
long long flex = pb * x - 1, tape = pd * x + x, bad = min(d + b - 2 * flex, 2 * tape - d - b);
printf("%lld\n", bad + llabs(c - a));
goto solve;
}
}
printf("%lld\n", llabs(c - a) + llabs(d - b));
solve:;
}
} |
#include <bits/stdc++.h>
using namespace std;
#define PII pair<int, int>
#define VI vector<int>
#define VPII vector<PII>
#define LL long long
#define LD long double
#define f first
#define s second
#define MP make_pair
#define PB push_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define SIZ(c) (int)(c).size()
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i)
#define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i)
#define sim template<class n
sim, class s> ostream & operator << (ostream &p, pair<n, s> x)
{return p << "<" << x.f << ", " << x.s << ">";}
sim> auto operator << (ostream &p, n y) ->
typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type
{int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";}
void dor() {cerr << endl;}
sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);}
sim, class s> void mini(n &p, s y) {if(p>y) p = y;}
sim, class s> void maxi(n &p, s y) {if(p<y) p = y;}
#ifdef DEB
#define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__)
#else
#define debug(...)
#endif
#define I(x) #x " =", (x), " "
#define A(a, i) #a "[" #i " =", i, "] =", a[i], " "
LL pot3[40];
LL in_seg(LL a, LL b, LL c)
{
return a <= b && b <= c;
}
void solve()
{
LL x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
if(x1 > x2)swap(x1, x2);
if(y1 > y2)swap(y1, y2); // TODO
LL basic_res = x2 - x1 + y2 - y1;
LL all_res = basic_res;
FORD(i, 31, 1)
{
LL pott = pot3[i];
LL seg_poc = pot3[i-1];
LL seg_kon = pot3[i-1]*2-1;
REP(pomm, 2)
{
swap(x1, y1);
swap(x2, y2);
bool is_x_inside = 0;
LL poc_x, kon_x;
if(x1 / pott == x2 / pott && in_seg(seg_poc, x1 % pott, seg_kon) && in_seg(seg_poc, x2 % pott, seg_kon))
{
is_x_inside = 1;
poc_x = (x1 / pott) * pott + seg_poc;
kon_x = (x1 / pott) * pott + seg_kon;
}
else continue;
LL poc_y, kon_y;
if(y1 % pott < seg_poc)poc_y = (y1 / pott) * pott + seg_poc;
else poc_y = (y1 / pott + 1) * pott + seg_poc;
kon_y = poc_y + pot3[i-1]-1;
debug(i, is_x_inside, I(y1), I(poc_y), I(y2), I(kon_y), I(poc_x), I(kon_x));
if(is_x_inside && y1 < poc_y && kon_y < y2)
{
LL add_left = (x1 - poc_x + 1) + (x2 - poc_x + 1) - (x2 - x1);
LL add_right = (kon_x - x1 + 1) + (kon_x - x2 + 1) - (x2 - x1);
maxi(all_res, basic_res + min(add_left, add_right));
debug(basic_res + min(add_left, add_right), add_left, add_right);
// printf("%lld\n", all_res);
// return;
}
}
}
cout << all_res << endl;
}
int main()
{
pot3[0] = 1;
FOR(i, 1, 39)pot3[i] = pot3[i-1] * 3;
int z;
cin >> z;
while(z--)solve();
} |
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for (int i=0; i<(int)(n); ++i)
#define rep1(i,n) for (int i=1; i<(int)(n); ++i)
#define repeq(i,n) for (int i=0; i<=(int)(n); ++i)
#define rep1eq(i,n) for (int i=1; i<=(int)(n); ++i)
#define rrep(i,n) for (int i=(int)(n)-1; i>=0; --i)
#define rrep1(i,n) for (int i=(int)(n)-1; i>0; --i)
#define rrepeq(i,n) for (int i=(int)(n); i>=0; --i)
#define rrep1eq(i,n) for (int i=(int)(n); i>0; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
template<typename T> using Graph = vector<vector<T>>;
template<typename T> using Spacial = vector<vector<vector<T>>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int MOD = 1e9+7;
const int MOD2 = 998244353;
const double EPS = 1e-9;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
template<typename T> struct is_pair : false_type{};
template<typename T1, typename T2> struct is_pair<pair<T1, T2>> : true_type{};
template<typename T> struct is_vector : false_type{};
template<typename T> struct is_vector<vector<T>> : true_type{};
template<typename T1, typename T2>
istream &operator >> (istream &istr, pair<T1, T2> &p) {
istr >> p.first >> p.second;
return istr;
}
template<typename T1, typename T2>
ostream &operator << (ostream &ostr, const pair<T1, T2> &p) {
ostr << p.first << " " << p.second;
return ostr;
}
template<typename T>
istream &operator >> (istream &istr, vector<T> &vec) {
for (auto itr = vec.begin(); itr != vec.end(); ++itr) {
istr >> *itr;
}
return istr;
}
template<typename T>
ostream &operator << (ostream &ostr, const vector<T> &vec) {
if (vec.empty()) return ostr;
bool vp = is_vector<T>() || is_pair<T>();
ostr << vec.front();
for (auto itr = ++vec.begin(); itr != vec.end(); ++itr) {
ostr << (vp ? "\n" : " ") << *itr;
}
return ostr;
}
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
int modpow(int a, long long n, int mod = MOD) {
int ret = 1;
do {
if (n & 1) ret = 1LL * ret * a % mod;
a = 1LL * a * a % mod;
} while (n >>= 1);
return ret;
}
template<typename T>
T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<typename T>
T LCM(T a, T b) { return a / GCD(a, b) * b; }
template<typename T1, typename T2>
bool CompareBySecond(pair<T1, T2> a, pair<T1, T2> b) {
return a.second != b.second ? a.second < b.second : a.first < b.first;
}
template<typename T>
bool CompareBySlope(pair<T, T> a, pair<T, T> b) {
// counterclockwise from 12 o'clock direction
if (a.first * b.first < 0) return a.first < b.first;
if (a.first == 0) return a.second >= 0 || b.first > 0;
if (b.first == 0) return b.second < 0 && a.first < 0;
return a.second * b.first < a.first * b.second;
}
/* -------- <templates end> -------- */
void solve() {
int n = 1;
rep(i,30) n *= 3;
int q; cin >> q;
while (q--) {
int a, b, c, d; cin >> a >> b >> c >> d;
int level = 30;
int lx = 0, rx = n;
int ly = 0, ry = n;
int A, B, C, D;
while (level--) {
int sx = lx + (rx-lx) / 3;
int tx = rx - (rx-lx) / 3;
A = (a<=sx ? 0 : (a<=tx ? 1 : 2));
C = (c<=sx ? 0 : (c<=tx ? 1 : 2));
int sy = ly + (ry-ly) / 3;
int ty = ry - (ry-ly) / 3;
B = (b<=sy ? 0 : (b<=ty ? 1 : 2));
D = (d<=sy ? 0 : (d<=ty ? 1 : 2));
lx += (tx-sx) * A;
rx -= (tx-sx) * (2-A);
ly += (ty-sy) * B;
ry -= (ty-sy) * (2-B);
if (A != C || B != D) break;
}
if (A != C && B != D) {
cout << abs(a-c) + abs(b-d) << endl;
continue;
}
if (B == D) {
swap(a,b); swap(c,d);
swap(A,B); swap(C,D);
swap(lx,ly); swap(rx,ry);
}
if (A == 1) {
cout << min(a-lx + c-lx, rx-a+1 + rx-c+1) + abs(b-d) << endl;
continue;
}
if (level == 0) {
cout << abs(a-c) + abs(b-d) << endl;
continue;
}
int dist = ry - ly;
while (level--) {
int sx = lx + (rx-lx) / 3;
int tx = rx - (rx-lx) / 3;
A = (a<=sx ? 0 : (a<=tx ? 1 : 2));
C = (c<=sx ? 0 : (c<=tx ? 1 : 2));
lx += (tx-sx) * A;
rx -= (tx-sx) * (2-A);
if (A != C) {
cout << abs(a-c) + abs(b-d) << endl;
break;
}
if (A == 1 && abs(b-d) > dist/3*2) {
cout << min(a-lx + c-lx, rx-a+1 + rx-c+1) + abs(b-d) << endl;
break;
}
if (level == 0) {
cout << abs(a-c) + abs(b-d) << endl;
break;
}
dist /= 3;
}
}
}
/* -------- <programs end> -------- */
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
o<<"{";
for(const T& v:vc) o<<v<<",";
o<<"}";
return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }
#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
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 shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {"; \
for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif
int main(){
cin.tie(0);
ios::sync_with_stdio(false); //DON'T USE scanf/printf/puts !!
cout << fixed << setprecision(20);
int Q; cin >> Q;
V<ll> p3(31);
p3[0] = 1; rep(i,30) p3[i+1] = p3[i]*3;
auto Inc = [&](ll x, ll y){
if(x>y) swap(x,y);
rep1(i,3){
ll d = x+i;
if(x<d&&d<y&&d%3==1) return true;
}
return false;
};
auto solve = [&](ll a,ll b,ll c,ll d){
per(k,30){
ll L = p3[k];
ll aa = a/L, bb = b/L, cc = c/L, dd = d/L;
if(aa == cc && aa%3 == 1 && Inc(bb,dd)){
shows(aa,bb,cc,dd);
ll X = a/L*L-1;
ll Y = X+L+1;
return abs(b-d) + min(abs(a-X)+abs(c-X),abs(a-Y)+abs(c-Y));
}
if(bb == dd && bb%3 == 1 && Inc(aa,cc)){
shows(L,aa,bb,cc,dd);
swap(a,b);swap(c,d);
ll X = a/L*L-1;
ll Y = X+L+1;
return abs(b-d) + min(abs(a-X)+abs(c-X),abs(a-Y)+abs(c-Y));
}
if(aa != cc && bb != dd){
return abs(a-c)+abs(b-d);
}
}
return abs(a-c)+abs(b-d);
};
while(Q--){
ll a,b,c,d; cin >> a >> b >> c >> d;
a--,b--,c--,d--;
cout << solve(a,b,c,d) << endl;
}
}
|
#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 u{b}, l{b}, p{M};
while(p /= 3)if((a / p + (4 - a / p % 3) % 3) * p <= c){
if(u / p % 3 == 1)u = u / (3 * p) * 3 * p + 2 * p;
if(l / p % 3 == 1)l = l / (3 * p) * 3 * p + p - 1;
}
return c - a + min(u - b + max(u, d) - min(u, d), b + d - 2 * l);
};
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 <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
long long rev(long long p, long long x1, long long y1, long long x2, long long y2)
{
if (!p) return 0;
if (x1 / p != x2 / p && y1 / p != y2 / p) return 0;
if (x1 / p == x2 / p && y1 / p == y2 / p) return rev(p / 3, x1 % p, y1 % p, x2 % p, y2 % p);
if (x1 / p != x2 / p) return rev(p, y1, x1, y2, x2);
if (x1 / p == 1 && x2 / p == 1){
x1 %= p;
x2 %= p;
if (x1 > x2) swap(x1, x2);
return min(x1 + 1, p - x2);
}
if (y1 > y2) swap(y1, y2);
if (y2 / p - y1 / p == 2) return rev(p / 3, x1 % p, 0, x2 % p, p - 1);
return max(rev(p / 3, x1 % p, 0, x2 % p, y2 % p), rev(p / 3, x1 % p, y1 % p, x2 % p, p - 1));
}
int main()
{
int Q; scanf ("%d", &Q); while (Q--){
long long x1, y1, x2, y2, base;
scanf ("%lld %lld %lld %lld", &x1, &y1, &x2, &y2);
base = abs(x1 - x2) + abs(y1 - y2);
long long p = 1;
for (int k = 0; k < 32; k++) p *= 3;
base += 2 * rev(p, x1 - 1, y1 - 1, x2 - 1, y2 - 1);
printf ("%lld\n", base);
}
return 0;
} |
// 2020-08-07 18:30:38
#include<bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
using P = pair<int, int>;
template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } else return false; }
template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
int q;
ll r1, c1, r2, c2, pow3[40];
void solve() {
REPCM(k, 30, 1) {
int j = k - 1;
bool interrupted = true;
interrupted &= (r1 / pow3[j] + 2 <= r2 / pow3[j]);
interrupted &= (c1 / pow3[j] == c2 / pow3[j]);
interrupted &= ((c1 / pow3[j]) % 3 == 1);
if(interrupted) {
int j = k - 1;
ll l = c1 / pow3[j] * pow3[j];
ll r = l + pow3[j];
ll cd = min(c1-l+c2-l+2, r-c1+r-c2);
cout << r2-r1+cd << '\n';
return;
}
}
cout << abs(r2-r1) + abs(c2-c1) << '\n';
}
void answer() {
cin >> q;
pow3[0] = 1;
rep(i, 35) pow3[i+1] = pow3[i] * 3;
auto normalize = [&]() {
r1--; r2--; c1--; c2--;
if(abs(r1-r2) < abs(c1-c2)) {
swap(r1, c1);
swap(r2, c2);
}
if(r1 > r2) {
swap(r1, r2);
swap(c1, c2);
}
};
rep(qi, q) {
cin >> r1 >> c1 >> r2 >> c2;
normalize();
solve();
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
answer();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define x first
#define y second
#ifdef LOCAL
#include "debug.h"
#endif
const int N = 31;
ll p[N];
int main() {
#ifdef LOCAL
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
p[0] = 1;
for (int i = 1; i < N; i++) p[i] = p[i - 1] * 3LL;
int q;
cin >> q;
while (q--) {
ll x1, y1, x2, y2;
scanf("%lld %lld %lld %lld", &x1, &y1, &x2, &y2);
x1--, y1--, x2--, y2--;
int at_level = 30;
ll ans;
while (true) {
if (at_level == 0) {
ans = abs(x1 - x2) + abs(y1 - y2);
break;
}
ll row1 = x1 / p[at_level - 1];
ll col1 = y1 / p[at_level - 1];
ll row2 = x2 / p[at_level - 1];
ll col2 = y2 / p[at_level - 1];
if (row1 != row2 && col1 != col2) {
ans = abs(x1 - x2) + abs(y1 - y2);
break;
}
if (row1 == row2 && col1 == col2) {
at_level--;
continue;
}
if (row1 == row2 && row1 % 3 == 1 && abs(col1 - col2) > 1) {
ans = abs(y1 - y2);
ll rem_sum = x1 % p[at_level - 1] + x2 % p[at_level - 1];
ll down = rem_sum + 2;
ll up = 2LL * p[at_level - 1] - rem_sum;
ans += min(down, up);
break;
}
if (col1 == col2 && col1 % 3 == 1 && abs(row1 - row2) > 1) {
ans = abs(x1 - x2);
ll rem_sum = y1 % p[at_level - 1] + y2 % p[at_level - 1];
ll down = rem_sum + 2;
ll up = 2LL * p[at_level - 1] - rem_sum;
ans += min(down, up);
break;
}
at_level--;
}
printf("%lld\n", ans);
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
using vvint = vector<vint>;
using vvvint = vector<vvint>;
using vdouble = vector<double>;
using vvdouble = vector<vdouble>;
using vvvdouble = vector<vvdouble>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T> using uset = unordered_set<T>;
template<typename T1, typename T2> using umap = unordered_map<T1, T2>;
constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\n';
constexpr int mod = 1000000007;
template<typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }
template<typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); }
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) { return (a > b && (a = b, true)); }
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) { return (a < b && (a = b, true)); }
template<typename T>
ostream& operator <<(ostream &os, vector<T> &v) {
os << v[0];
for (int i = 1; i < v.size(); i++) os << " " << v[i];
return (os);
}
template<typename T>
istream& operator >>(istream &is, vector<T> &v) {
for (auto &u : v) is >> u; return (is);
}
template<typename T1, typename T2>
istream& operator >>(istream &is, pair<T1, T2> &p) {
return (is >> p.first >> p.second);
}
ll pow3[33];
ll calc(ll a, ll b, ll c, ll d, ll size = 30) {
ll div = pow3[size-1];
ll parta = a / div, partb = b / div;
ll partc = c / div, partd = d / div;
ll spath = abs(d-b) + abs(c-a);
if (parta == partc && partb == partd) {
return (calc(a - parta*div, b - partb*div, c - partc*div, d - partd*div, size-1));
}
//やばいやつら
if (parta == partc || partb == partd) {
if (partb == partd) { swap(a, b); swap(c, d); swap(parta, partb); swap(partc, partd); }
if (parta == 1) {
return (abs(d-b) + min(a-div+1+c-div+1, 2*div-a+2*div-c));
}
a -= parta*div, c -= partc*div;
for (ll sz = size-2; sz >= 0; sz--) {
ll dv = pow3[sz];
ll pta = a/dv, ptc = c/dv;
if (pta != ptc) return (spath);
if (pta == ptc && pta == 1 && abs(d-b) / dv > 1) return (abs(d-b) + min(a-dv+1+c-dv+1, 2*dv-a+2*dv-c));
a -= pta*dv, c -= ptc*dv;
}
}
return (spath);
}
void solve() {
ll a, b, c, d; cin >> a >> b >> c >> d; --a, --b, --c, --d;
cout << calc(a, b, c, d) << endl;
}
void Main() {
//init
pow3[0] = 1;
for (int i = 1; i <= 30; i++) pow3[i] = pow3[i-1] * 3;
int Q; cin >> Q;
while (Q--) solve();
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
Main();
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;
rep(qi,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 <cstdio>
#include <algorithm>
#define LL long long
using namespace std;
LL width[35];
LL cal(LL x1,LL y1,LL x2,LL y2,int level){
LL w;
if(level==0)return abs(y1-y2);//路径上一直未遇到黑色方块
w=width[level-1];//当前级别level,深入到9个level-1级别中进行处理
if(x1/w!=x2/w)return abs(x1-x2)+abs(y1-y2);//处在不同层中,可用曼哈顿距离处理
if(x1/w==1&&abs(y1/w-y2/w)>=2)//x1/w==x2/w 处于同一层,且在456这一层
return abs(y1-y2)+min(x1%w+1+x2%w+1,(w-x1%w)+(w-x2%w));//x1%w+1+x2%w+1向左推进,(w-x1%w)+(w-x2%w)向右推进
return cal(x1%w,y1,x2%w,y2,level-1);//当前处理不了,只能深入到level-级别中进行处理,固定abs(y1-y2),针对x进行处理,因x1/w==x2/w,故可取模
}
int main(){
int i,q;
width[0]=1;
for(i=1;i<=29;i++)width[i]=width[i-1]*3;
LL x1,y1,x2,y2;
scanf("%d",&q);
while(q--){
scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);
x1--,y1--,x2--,y2--;//涉及取模运算,故第1行变成第0行,故第1列变成第0列
if(abs(x1-x2)>abs(y1-y2))swap(x1,y1),swap(x2,y2);//抓住图形具有对称性,避免出现258的情况。
printf("%lld\n",cal(x1,y1,x2,y2,30));
}
return 0;
} |
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#define PB push_back
#define MP make_pair
#define PII pair<int,int>
#define FIR first
#define SEC second
#define ll long long
using namespace std;
template <class T>
inline void rd(T &x) {
x=0; char c=getchar(); int f=1;
while(!isdigit(c)) { if(c=='-') f=-1; c=getchar(); }
while(isdigit(c)) x=x*10-'0'+c,c=getchar(); x*=f;
}
inline ll Abs(ll x) { return x>0?x:-x; }
ll pw[33];
ll sol(ll x1,ll y1,ll x2,ll y2,int d) {
if(d==0) return Abs(y2-y1);
ll w=pw[d-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+x2%w+2,2*w-x1%w-x2%w);
return sol(x1%w,y1,x2%w,y2,d-1);
}
int main() {
pw[0]=1;
for(int i=1;i<=30;++i) pw[i]=pw[i-1]*3;
int T; rd(T);
while(T--) {
ll x1,y1,x2,y2;
rd(x1),rd(y1),rd(x2),rd(y2);
x1--,y1--,x2--,y2--;
if(Abs(x1-x2)>Abs(y1-y2)) swap(x1,y1),swap(x2,y2);
printf("%lld\n",sol(x1,y1,x2,y2,30));
}
return 0;
} |
#include <iostream>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <limits>
#include <algorithm>
#include <functional>
using namespace std;
long long width[30];
long long process(long long x1, long long y1, long long x2, long long y2, int level)
{
if (level == 0) return abs(y1 - y2);
long long w = width[level - 1];
long long x1_id = x1 / w;
long long x2_id = x2 / w;
long long y1_id = y1 / w;
long long y2_id = y2 / w;
if (x1_id != x2_id) return abs(x1 - x2) + abs(y1 - y2);
if (x1_id == 1 && abs(y1_id - y2_id) > 1)
{
return abs(y1 - y2) + min(x1 % w + 1 + x2 % w + 1, (w - x1 % w) + (w - x2 % w));
}
return process(x1 % w, y1, x2 % w, y2, level - 1);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
width[0] = 1;
for (int i = 1; i < 30; i++)
{
width[i] = width[i - 1] * 3;
}
int Q;
cin >> Q;
while (Q--)
{
long long 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 << process(x1, y1, x2, y2, 30) << "\n";
}
return 0;
} |
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<time.h>
#include<random>
#include<array>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i, a, b) for(int i = a; i < b; i++)
#define rrep(i, a, b) for(int i = b - 1; i >= a; i--)
#define repl(i, a, b) for(long long i = a; i < b; i++)
#define rrepl(i, a, b) for(long long i = b - 1; i >= a; i--)
#define ALL(a) a.begin(), a.end()
using pii = pair<int,int>;
using piii = pair<pii,int>;
using pll = pair<long long, long long>;
using plll = pair<pll, long long>;
template<class T>inline bool chmax(T &a, const T &b) {if(a<b){a = b; return 1;} return 0; };
template<class T>inline bool chmin(T &a, const T &b) {if(a>b){a = b; return 1;} return 0; };
void iosetup() {
cin.tie(nullptr);ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
//----------------------- edit from here ---------------------------------
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;
repl(y, y1, y2 + 1) 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;
rep(i, 0, 35){
if(x1 / three == x2 / three and
between(x1 / three, y1 / three, y2 / three)){
ll tmp = min(min(x1 % three, x2 % three) + 1,
three - max(x1 % three, x2 % three));
chmax(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(){
iosetup();
int Q; cin >> Q;
while(Q--){
ll a, b, c, d; cin >> a >> b >> c >> d; a--, b--, c--, d--;
cout << get_dist(a, b, c, d) << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}
ll three[40];
using P = pair<ll, ll>;
ll solve2(int K, ll a, ll b, ll c, ll d) {
if (a == c && b == d) return 0;
const ll L = three[K-1];
vector<ll> ps({0, L-1, 2*L, 3*L-1});
vector<P> checkpoints;
for (ll r : ps) {
for (ll c : ps) {
checkpoints.push_back({r, c});
}
}
// pv(ALL(checkpoints));
// cout << K << " " << a << " " << b << " " << c << " " << d << endl;
ll ans = 1LL << 61;
for (P p : checkpoints) {
ll cur = 0;
cur += abs(a - p.first) + abs(b - p.second);
cur += abs(c - p.first) + abs(d - p.second);
chmin(ans, cur);
}
return ans;
}
ll solve(int K, ll a, ll b, ll c, ll d) {
if (a == c && b == d) return 0;
ll L = three[K-1];
bool same_row = a / L == c / L;
bool same_col = b / L == d / L;
if (same_row && same_col) {
return solve(K-1, a % L, b % L, c % L, d % L);
}
if (!same_row && !same_col) { return solve2(K, a, b, c, d); }
if (same_row && a/L == 1) { return solve2(K, a, b, c, d); }
if (same_col && b/L == 1) { return solve2(K, a, b, c, d); }
if (!same_row) {
swap(a, b);
swap(c, d);
same_row = !same_row;
same_col = !same_col;
}
while (L > 1) {
L /= 3;
if (a / L != c / L) {
return abs(a - c) + abs(b - d);
}
if (abs(b/L - d/L) >= 2 && a / L == c / L && (a / L) % 3 == 1) break;
}
if (L == 1) {
if (a != c) return abs(a-c) + abs(b-d);
if (a % 3 != 1) return abs(a-c) + abs(b-d);
if (abs(b-d) <= 1) return abs(a-c) + abs(b-d);
return abs(b-d) + 2;
} else {
a %= L;
c %= L;
return abs(b-d) + min(a+c+2, L-a+L-c);
}
}
int main2() {
int Q; cin >> Q;
while (Q--) {
ll a, b, c, d;
cin >> a >> b >> c >> d;
ll ans = solve(31, a-1, b-1, c-1, d-1);
cout << ans << endl;
}
return 0;
}
int main() {
three[0] = 1;
for (int i = 1; i < 40; i++) three[i] = three[i-1] * 3;
#ifdef LOCAL
for (;!cin.eof();cin>>ws)
#endif
main2();
return 0;
} |
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <iostream>
#include <numeric>
/* debug macros */
#ifdef WAFDAYO
#define DBG_LINE() {std::cerr<<"\e[2m[L"<<__LINE__<<"]\e[m ";}
#define DBG_PRINT(s,t,u) {std::cerr<<(s)<<" \e[2m=\e[m \e[1m"<<(t)<<"\e[m"<<(u);}
#define SELECT_7TH(x1,x2,x3,x4,x5,x6,x7,...) x7
#define dbg1(x1) DBG_PRINT(#x1,x1,std::endl)
#define dbg2(x1,x2) DBG_PRINT(#x1,x1,", ")dbg1(x2)
#define dbg3(x1,x2,x3) DBG_PRINT(#x1,x1,", ")dbg2(x2,x3)
#define dbg4(x1,x2,x3,x4) DBG_PRINT(#x1,x1,", ")dbg3(x2,x3,x4)
#define dbg5(x1,x2,x3,x4,x5) DBG_PRINT(#x1,x1,", ")dbg4(x2,x3,x4,x5)
#define dbg6(x1,x2,x3,x4,x5,x6) DBG_PRINT(#x1,x1,", ")dbg5(x2,x3,x4,x5,x6)
#define dbg(...) DBG_LINE()\
SELECT_7TH(__VA_ARGS__,dbg6,dbg5,dbg4,dbg3,dbg2,dbg1)(__VA_ARGS__)
#else
#define dbg(...) {}
#endif
/* utility functions */
struct read_item{read_item(){};
template<class T>operator T(){T t;std::cin>>t;return t;}};
char splf(int i,int n){return(i+1<n)?' ':'\n';};
template<bool up>
struct _RI{int i;_RI(int a):i(a){}
int operator*(){return i;}void operator++(){i+=(up?1:-1);}
bool operator!=(const _RI& r){return up?i<r.i:i>=r.i;}};
template<bool up>
struct _RX{_RI<up> x,y;_RI<up> begin(){return x;}_RI<up> end(){return y;}
_RX(int a,int b):x(up?a:(a-1)),y(b){}_RX(int a):_RX(up?0:a,up?a:0){}};
typedef _RX<true> range;
typedef _RX<false> revrange;
/* types and constants */
typedef int64_t i64;
typedef uint64_t u64;
// const i64 inf = (i64)1.05e18;
// const int inf = (int)1.05e9;
using namespace std;
i64 solve(i64 a, i64 b, i64 c, i64 d) {
for(int level = 30; level > 0; level--) {
i64 width = 1;
for(int i = 0; i < level - 1; i++) {
width = width * 3;
}
i64 x1 = a / width;
i64 y1 = b / width;
i64 x2 = c / width;
i64 y2 = d / width;
if(x1 != x2 && y1 != y2) {
return abs(c - a) + abs(d - b);
}
if(x1 == x2 && y1 == y2) {
continue;
}
if(x1 != x2) {
swap(x1, y1);
swap(x2, y2);
swap(a, b);
swap(c, d);
}
if(x1 % 3 != 1) {
continue;
}
if((y1 + 1) / 3 == (y2 + 1) / 3) {
continue;
}
i64 p = a % width;
i64 q = c % width;
return abs(b - d) + min(p + q + 2, 2 * width - p - q);
}
return abs(c - a) + abs(d - b);
}
int main() {
const int Q = read_item();
for(auto i : range(Q)) {
i64 a = read_item();
i64 b = read_item();
i64 c = read_item();
i64 d = read_item();
a -= 1;
b -= 1;
c -= 1;
d -= 1;
i64 ans = solve(a, b, c, d);
printf("%lld\n", ans);
}
return 0;
}
/* wafdayo~~~ */
|
#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 ALL(v) begin(v),end(v)
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//
int main() {
vector<ll> p3(1, 1);
REP(i, 30) p3.emplace_back(p3.back() * 3);
int Q;
cin >> Q;
while (Q--) {
ll y1, x1, y2, x2;
cin >> y1 >> x1 >> y2 >> x2;
--y1; --x1; --y2; --x2;
ll ans = abs(y2 - y1) + abs(x2 - x1);
for (int k = 29; k >= 0; --k) {
bool done = false;
REP(i, 2) {
ll by1 = y1 / p3[k], bx1 = x1 / p3[k], by2 = y2 / p3[k], bx2 = x2 / p3[k];
if (by1 == by2 && by1 % 3 == 1 && abs(bx1 - bx2) >= 2) {
y1 %= p3[k]; y2 %= p3[k];
ans = abs(x2 - x1) + min(y1 + y2 + 2, 2 * p3[k] - (y1 + y2));
done = true;
break;
}
swap(x1, y1); swap(x2, y2);
}
if (done) break;
}
cout << ans << endl;
}
return 0;
}
|
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
#pragma warning (disable: 4996)
long long power[33];
long long Q;
long long A[1 <<18], B[1 << 18], C[1 << 18], D[1 << 18];
long long query(long long lx, long long ly, long long rx, long long ry) {
long long sz = -1;
long long px = 0, py = 0, qx = 0, qy = 0;
for (int i = 29; i >= 0; i--) {
long long V1 = (lx + power[30] + power[i + 1] - power[i]) / power[i + 1]; V1 -= power[30 - (i + 1)];
long long VX = V1 * power[i + 1] + power[i];
if ((lx + power[30] - power[i]) % power[i + 1] < power[i]) {
V1--; VX = lx;
}
long long V2 = (ly + power[30] + power[i + 1] - power[i]) / power[i + 1]; V2 -= power[30 - (i + 1)];
long long VY = V2 * power[i + 1] + power[i];
if ((ly + power[30] - power[i]) % power[i + 1] < power[i]) {
V2--; VY = ly;
}
if (VX <= rx && VY <= ry) {
sz = i;
px = V1 * power[i + 1] + power[i]; qx = V1 * power[i + 1] + 2LL * power[i] - 1LL;
py = V2 * power[i + 1] + power[i]; qy = V2 * power[i + 1] + 2LL * power[i] - 1LL;
break;
}
}
if (sz == -1) {
return abs(lx - rx) + abs(ly - ry);
}
// パターン 1
if (px <= lx && lx <= qx && px <= rx && rx <= qx) {
long long S = abs(ly - ry);
long long T1 = abs(lx - (px - 1LL)) + abs((px - 1LL) - rx);
long long T2 = abs(lx - (qx + 1LL)) + abs((qx + 1LL) - rx);
S += min(T1, T2);
return S;
}
// パターン 2
if (py <= ly && ly <= qy && py <= ry && ry <= qy) {
long long S = abs(lx - rx);
long long T1 = abs(ly - (py - 1LL)) + abs((py - 1LL) - ry);
long long T2 = abs(ly - (qy + 1LL)) + abs((qy + 1LL) - ry);
S += min(T1, T2);
return S;
}
// パターン 3
return abs(lx - rx) + abs(ly - ry);
}
int main() {
// 入力
cin >> Q;
for (int i = 1; i <= Q; i++) cin >> A[i] >> B[i] >> C[i] >> D[i];
power[0] = 1;
for (int i = 1; i <= 32; i++) power[i] = (3LL * power[i - 1]);
// 前準備
for (int i = 1; i <= Q; i++) {
A[i]--; B[i]--; C[i]--; D[i]--;
if (A[i] > C[i]) { swap(A[i], C[i]); swap(B[i], D[i]); }
if (B[i] > D[i]) {
B[i] = power[30] - 1LL - B[i];
D[i] = power[30] - 1LL - D[i];
}
}
// 計算
for (int i = 1; i <= Q; i++) {
cout << query(A[i], B[i], C[i], D[i]) << 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){
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]);
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){
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];
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);
}
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;
}
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);
}
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
ll N=1;
ll a,b,c,d;
ll cost;
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){
ll A=a%sz,C=c%sz;
cost=min(A+1+C+1,(sz-A)+(sz-C))+abs(b-d);
return;
}
if(y1==y2&&y1%3==1&&abs(x1-x2)>1){
ll B=b%sz,D=d%sz;
cost=min(B+1+D+1,(sz-B)+(sz-D))+abs(a-c);
return;
}
}
if(sz>1)solve(sz/3);
}
int main(){
rep(i,30)N*=3;
int q;cin>>q;
rep(i,q){
cin>>a>>b>>c>>d;a--;b--;c--;d--;
cost=abs(a-c)+abs(b-d);
solve(N/3);
printf("%lld\n",cost);
}
}
/*
2
39 24 34 60
*/ |
#include <iostream>
#include <vector>
#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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>
#include <algorithm>
#include <random>
#include <bitset>
#include <queue>
#include <functional>
#include <set>
#include <map>
#include <vector>
#include <chrono>
#include <iostream>
#include <limits>
#include <numeric>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
for (T& x : v)
is >> x;
return is;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
if (!v.empty()) {
os << v.front();
for (int i = 1; i < v.size(); ++i)
os << ' ' << v[i];
}
return os;
}
int main() {
#ifdef LBT
//freopen("test.in", "r", stdin);
int nol_cl = clock();
#endif
ios::sync_with_stdio(false);
cin.tie(nullptr);
int Q;
cin >> Q;
const int L = 30;
vector<ll> pw(L + 1);
pw[0] = 1;
for(int i = 1; i <= L; ++i) pw[i] = pw[i - 1] * 3;
auto ze1 = [&](ll x) {
if (x % 3 == 2) return x - 1;
return x - 2;
};
while (Q--) {
ll x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
--x1; --y1; --x2; --y2;
if (x1 > x2) {
x1 = pw[L] - x1 - 1;
x2 = pw[L] - x2 - 1;
}
if (y1 > y2) {
y1 = pw[L] - y1 - 1;
y2 = pw[L] - y2 - 1;
}
ll ans = abs(x1 - x2) + abs(y1 - y2);
for (int i = L - 1; i >= 0; --i) {
if (x1 / pw[i] == x2 / pw[i] && (x1 / pw[i]) % 3 == 1 && y1 < ze1(y2 / pw[i]) * pw[i]) {
ll v = x1 / pw[i];
ans += 2 * min((v + 1) * pw[i] - x2, x1 - v * pw[i] + 1);
break;
}
if (y1 / pw[i] == y2 / pw[i] && (y1 / pw[i]) % 3 == 1 && x1 < ze1(x2 / pw[i]) * pw[i]) {
ll v = y1 / pw[i];
ans += 2 * min((v + 1) * pw[i] - y2, y1 - v * pw[i] + 1);
break;
}
}
cout << ans << '\n';
}
#ifdef LBT
LOG("Time: %dms\n", int ((clock()
-nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
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 ndarray(vector<T> &vec, int len) { vec.resize(len); }
template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }
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 (q < m) {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 make_pair(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 make_pair(l.first - r.first, l.second - r.second); }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
using namespace __gnu_pbds; // find_by_order(), order_of_key()
template<typename TK> using pbds_set = tree<TK, null_type, less<TK>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename TK, typename TV> using pbds_map = tree<TK, TV, less<TK>, rb_tree_tag, tree_order_statistics_node_update>;
*/
constexpr int DIM = 31;
vector<lint> gen3(lint X)
{
vector<lint> d(DIM);
int i = 0;
while (X) {
d[i] = X % 3;
X /= 3;
i++;
}
return d;
}
lint solve2(lint x1, lint x2, lint y1, lint y2)
{
auto vx1 = gen3(x1);
auto vy1 = gen3(y1);
auto vx2 = gen3(x2);
auto vy2 = gen3(y2);
lint D0 = abs(x1 - x2) + abs(y1 - y2);
bool xs = true, ys = true;
lint R = 1;
REP(d, DIM) R *= 3;
IREP(d, DIM) {
R /= 3;
if (vx1[d] != vx2[d]) xs = false;
if (vy1[d] != vy2[d]) ys = false;
if (ys and !xs and vy1[d] == 1) {
lint xlo = (x1 + R) / (3 * R) * (3 * R) + R;
if (x2 < xlo) continue;
lint z1 = y1 % R;
lint z2 = y2 % R;
lint rrr = 2 + min(z1, z2) * 2;
chmin(rrr, 2 + min((R - 1 - z1), (R - 1 - z2)) * 2);
return rrr + D0;
}
}
return D0;
}
lint solve()
{
lint x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--, y1--, x2--, y2--;
if (x1 >= x2) swap(x1, x2), swap(y1, y2);
lint reta = solve2(x1, x2, y1, y2);
if (y1 >= y2) swap(x1, x2), swap(y1, y2);
lint retb = solve2(y1, y2, x1, x2);
return max(reta, retb);
}
int main()
{
int Q;
cin >> Q;
while (Q--) cout << solve() << endl;
}
|
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <deque>
#include <set>
#include <iomanip>
#include <utility>
typedef long long ll;
typedef long double ld;
using namespace std;
ll beki3[35];
int maxBlack(ll g, ll h, ll i, ll j){
int res=29;
while(res>=0){
ll hr=h%beki3[res+1], jr=j%beki3[res+1];
if(min(hr, jr)<=beki3[res] || beki3[res]*2<max(hr, jr) || abs(j-h)>=beki3[res]){
--res;
continue;
}
if((g+beki3[res]-1)/beki3[res+1]==(i+beki3[res]-1)/beki3[res+1]){
--res;
continue;
}
break;
}
return res;
}
ll solve(ll g, ll h, ll i, ll j){
int x=maxBlack(g, h, i, j), y=maxBlack(h, g, j, i);
if(x>=0){
ll k=(h/beki3[x+1])*beki3[x+1]+beki3[x];
return abs(i-g)+min((h-k)+(j-k), (k+beki3[x]+1-h)+(k+beki3[x]+1-j));
}
if(y>=0){
ll k=(g/beki3[y+1])*beki3[y+1]+beki3[y];
return abs(j-h)+min((g-k)+(i-k), (k+beki3[y]+1-g)+(k+beki3[y]+1-i));
}
return abs(i-g)+abs(j-h);
}
int main() {
int Q;
cin >> Q;
beki3[0]=1;
for(int i=1; i<=30; ++i) beki3[i]=beki3[i-1]*3;
for(int q=0; q<Q; ++q){
ll a, b, c, d;
cin >> a >> b >> c >> d;
cout << solve(a, b, c, d) << endl;
}
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);
int64_t r1 = a1 - (Lb-1) + b1 - (Lb-1);
int64_t r2 = Lb*2 - a1 + Lb*2 - b1;
return std::abs(a2-b2) + std::min(r1, r2);
}
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>
using namespace std;
typedef long long ll;
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 (b<a) { a=b; return 1; } return 0; }
int main() {
int Q;
cin >> Q;
const int D = 30;
auto solve = [&](ll a, ll b, ll c, ll d) {
ll ans = abs(c - a) + abs(d - b);
ll s = 1;
for (int i = 0; i < (int)(D); ++i) s *= 3;
ans = abs(c - a) + abs(d - b);
while (true) {
s /= 3;
if (s <= 0) break;
ll ia = a / s, ic = c / s;
if (ia != ic) break;
ll jb = b / s, jd = d / s;
if (abs(jb - jd) <= 1 || ia % 3 != 1) continue;
ll r = (ia + 1) * s, l = ia * s - 1;
ans += 2 * min(r - max(a, c), min(a, c) - l);
break;
}
return ans;
};
for (int i = 0; i < (int)(Q); ++i) {
ll a, b, c, d;
cin >> a >> b >> c >> d;
--a, --b, --c, --d;
cout << max(solve(a, b, c, d), solve(b, a, d, c)) << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--)
#define IREP(i, n) IFOR(i,0,n)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define all(v) v.begin(),v.end()
#define SZ(v) ((int)v.size())
#define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)
#define bit(n) (1LL<<(n))
#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v) { cout << #v << "=" << endl; REP(i_debug, v.size()){ cout << v[i_debug] << ","; } cout << endl; }
#define mdebug(m) { cout << #m << "=" << endl; REP(i_debug, m.size()){ REP(j_debug, m[i_debug].size()){ cout << m[i_debug][j_debug] << ","; } cout << endl;} }
#define Return(ans) { cout << (ans) << endl; return 0; }
#define pb push_back
#define fi first
#define se second
#define int long long
#define INF 1000000000000000000
template<typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; }
template<typename T> ostream &operator<<(ostream &os, vector<T> &v){ for(int i = 0; i < v.size(); i++) { cout << v[i]; if(i != v.size() - 1) cout << endl; }; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p){ cout << '(' << p.first << ',' << p.second << ')'; return os; }
template<typename T> void Out(T x) { cout << x << endl; }
template<typename T1, typename T2> void Ans(bool f, T1 y, T2 n) { if(f) Out(y); else Out(n); }
using vec = vector<int>;
using mat = vector<vec>;
using Pii = pair<int, int>;
using v_bool = vector<bool>;
using v_Pii = vector<Pii>;
//int dx[4] = {1,0,-1,0};
//int dy[4] = {0,1,0,-1};
//char d[4] = {'D','R','U','L'};
const int mod = 1000000007;
//const int mod = 998244353;
int solve(int a, int b, int c, int d){
if(llabs(c - a) < llabs(d - b)){
swap(a, b); swap(c, d);
}
if(c < a){
swap(a, c); swap(b, d);
}
if(d < b) swap(b, d);
//cout << a << " " << b << " " << c << " " << d << endl;
int ans = llabs(a - c) + llabs(b - d);
int sz = 1;
REP(i, 30){
int A = (a / (3 * sz)) * (3 * sz) + sz;
while(A <= a) A += 3 * sz;
int B = (b / (3 * sz)) * (3 * sz) + sz;
while(B > b) B -= 3 * sz;
//debug(A); debug(B);
if(A + sz - 1 < c && B + sz - 1 >= d){
int t1 = llabs(b - (B - 1)) + llabs(a - c) + llabs(d - (B - 1));
int t2 = llabs(b - (B + sz)) + llabs(a - c) + llabs(d - (B + sz));
Max(ans, min(t1, t2));
}
sz *= 3;
}
return ans;
}
signed main(){
int Q; cin >> Q;
REP(_, Q){
int a, b, c, d; cin >> a >> b >> c >> d;
cout << solve(a - 1, b - 1, c - 1, d - 1) << endl;
}
return 0;
}
|
#ifndef __MATH_UTIL_H__
#define __MATH_UTIL_H__
#include <cstdint>
#include <cassert>
int64_t strictMod(int64_t a, int64_t b);
int64_t strictDiv(int64_t a, int64_t b);
template <class T>
T power(T base, uint64_t exp);
/* 累乗; 置く場所がないのでここに置いておきます O(logN) */
template <class T>
T power(T base, uint64_t exp) {
T product = T(1);
while (exp > 0) {
if (exp & 1) {
product *= base;
}
base *= base;
exp >>= 1;
}
return product;
}
#endif
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include <algorithm>
#include <functional>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <limits>
#include <numeric>
#include <valarray>
#include <fstream>
#include <array>
#include <random>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using uint = uint32_t;
using LL = int64_t;
using ULL = uint64_t;
using PP = pair<LL, LL>;
template <typename T> using PriorityQ = priority_queue<T, vector<T>, greater<T> >;
#define REP(i, a, n) for(LL i = (a), i##_max_ = (n); i < i##_max_; ++i)
#define REM(i, a, n) for(LL i = (LL)(n) - 1, i##_min_ = (a); i >= i##_min_; --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP { cin.tie(NULL); ios::sync_with_stdio(false); }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U> istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
template <class T, class U> ostream& operator<<(ostream& ost, const pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
template <class T, class TCompatible, size_t N> void Fill(T(&dest)[N], const TCompatible& val) { fill(dest, dest + N, val); }
template <class T, class TCompatible, size_t M, size_t N> void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
template <class T> T Next() { T buf; cin >> buf; return buf; }
istream& Ignore(istream& ist) { string s; ist >> s; return ist; }
bool Inside(int i, int j, int h, int w) { return i >= 0 && i < h && j >= 0 && j < w; }
#ifdef ONLY_MY_ENVIR
#include "IntMod.h"
#include "Accumulator.h"
#include "Algebraic.h"
#include "BinaryMatrix.h"
#include "BinaryTree.h"
#include "Bipartite.h"
#include "BIT.h"
#include "Compressor.h"
#include "Decompositions.h"
#include "DynamicMod.h"
#include "Factorization.h"
#include "FFT.h"
#include "FlowSolver.h"
#include "Graph.h"
#include "GraphUtil.h"
#include "LazySegmentTree.h"
#include "LIS.h"
#include "Math.h"
#include "MathUtil.h"
#include "Matrix.h"
#include "MinCostFlowSolver.h"
#include "MinMax.h"
#include "Numbers.h"
#include "Optimize.h"
#include "Permutation.h"
#include "Polynomial.h"
#include "Position.h"
#include "Range.h"
#include "Rational.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "Sets.h"
#include "Shortest.h"
#include "SlidingWindow.h"
#include "SpanningTree.h"
#include "StringSearching.h"
#include "SuffixArray.h"
#include "Tree.h"
#include "UnionFind.h"
#include "VectorUtil.h"
#endif
#ifdef __GNUC__
typedef __int128 LLL;
istream& operator>>(istream& ist, __int128& val) { LL tmp; ist >> tmp; val = tmp; return ist; }
ostream& operator<<(ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; }
#endif
using Num = vector<int>;
Num conv(LL n) {
Num ret;
REP(z, 0, 30) {
ret.push_back(n % 3);
n /= 3;
}
return ret;
}
LL dec(const Num& n) {
LL sum = 0;
LL base = 1;
REP(z, 0, 30) {
sum += base * n[z];
base *= 3;
}
return sum;
}
LL force(Num n, int k) {
LL p = dec(n);
n[k] = 0;
REP(i, 0, k) n[i] = 2;
LL a = dec(n);
n[k] = 2;
REP(i, 0, k) n[i] = 0;
LL b = dec(n);
return min(abs(p - a), abs(p - b));
}
LL cat(const Num& a, const Num& b, const Num& c, const Num& d) {
REM(k, 0, 30) {
if (a[k] != c[k]) return 0;
if (a[k] == 1 && c[k] == 1) {
Num dd = d;
REP(i, 0, k) dd[i] = 0;
Num bb = b;
REP(i, 0, k) bb[i] = 0;
LL diff = abs(dec(dd) - dec(bb));
if (diff >= 2 * power(3LL, k)) {
return min(force(a, k), force(c, k));
}
}
}
return 0;
}
LL sub(LL a, LL b, LL c, LL d) {
return abs(c - a) + abs(d - b)
+ 2 * max(
cat(conv(a), conv(b), conv(c), conv(d)),
cat(conv(b), conv(a), conv(d), conv(c))
);
}
int main() {
int Q;
cin >> Q;
REP(i, 0, Q) {
LL a, b, c, d;
cin >> a >> b >> c >> d;
--a; --b; --c; --d;
cout << sub(a, b, c, d) << endl;
}
return 0;
}
|
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll p,ans,a,b,c,d,x;
int T;
int main() {
p=1;for(int i=1;i<=30;i++)p*=3;
scanf("%d",&T);
while(T--) {
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);
x=p; ans=abs(a-c)+abs(b-d);
while(x) {
ll A=a/x,C=c/x,B=b/x,D=d/x;
if(A^C) {ans=abs(a-c)+abs(b-d);break;}
if(A==1&&abs(B-D)>1) {ans=min(a%x+c%x+2,2*x-a%x-c%x)+abs(b-d);break;}
a%=x;c%=x;x/=3;
}
printf("%lld\n",ans);
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define MOD 998244353
template<typename ty1,typename ty2>
inline int add(ty1 x, ty2 y) {
if(y>=MOD)y%=MOD;
if(x>=MOD)x%=MOD;
x += y; return x < MOD ? x : x - MOD;
}
template<typename ty1,typename ty2>
inline void addto(ty1 &x, ty2 y) {
if(y>=MOD)y%=MOD;
if(x>=MOD)x%=MOD;
x += y; if (x >= MOD) x -= MOD;
}
template<typename ty1,typename ty2>
inline int sub(ty1 x, ty2 y) {
if(y>=MOD)y%=MOD;
if(x>=MOD)x%=MOD;
x -= y; return x < 0 ? x + MOD : x;
}
template<typename ty1,typename ty2>
inline void subto(ty1 &x, ty2 y) {
if(y>=MOD)y%=MOD;
if(x>=MOD)x%=MOD;
x -= y; if (x < 0) x += MOD;
}
template<typename ty1,typename ty2>
inline int mul(ty1 x, ty2 y) {
if(y>=MOD)y%=MOD;
if(x>=MOD)x%=MOD;
return 1ll * x * y % MOD;
}
template<typename ty1,typename ty2>
void multo(ty1 &x, ty2 y) {
if(y>=MOD)y%=MOD;
if(x>=MOD)x%=MOD;
x=1ll * x * y % MOD;
}
long long int gcd(long long int a, long long int b){
if (a > b){
swap(a, b);
}
while (a){
swap(a, b);
a %= b;
}
return b;
}
long long int lcm(long long int a, long long int b){
return a / gcd(a, b)*b;
}
long long int ppow(long long int i, long long int j){
long long int res = 1LL;
while (j){
if ((j & 1LL)){
res *= i;
if (res >= MOD){
res %= MOD;
}
}
j >>= 1;
i *= i;
if (i >= MOD){
i %= MOD;
}
}
return res;
}
class Combination{
public:
vector<long long int> k;
vector<long long int> r;
void resize(int N){
k.resize(N + 2);
r.resize(N + 2);
k[0] = 1;
for (int i = 1; i < N+2; i++){
k[i] = k[i - 1];
k[i] *= i;
if (k[i] >= MOD)k[i] %= MOD;
}
long long int al = k[k.size() - 1];
long long int iv = ppow(k[k.size() - 1],MOD-2);
r[k.size() - 1] = iv;
for (int i = (int)(r.size()) - 2; i >= 0; i--){
r[i] = r[i + 1] * (i + 1);
if (r[i] >= MOD){
r[i] %= MOD;
}
}
}
long long int C(int a, int b){
if (a < b)return 0;
long long int up = k[a];
long long int dw = r[b] * r[a - b];
dw %= MOD;
up *= dw;
up %= MOD;
return up;
}
long long int H(int a, int b){
return C(a + b - 1, b);
}
long long int catalan_number(int n){
return (C(2 * n, n) + MOD - C(2 * n, n - 1)) % MOD;
}
};
Combination C;
long long int ex(long long int pos1,long long int pos2,long long int len){
long long int c1=pos1+pos2+2;
long long int c2=len-pos1+len-pos2;
return min(c1,c2);
}
int main(){
int q;
cin>>q;
while(q--){
long long int a,b,c,d;
scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
a--;
b--;
c--;
d--;
long long int ds=abs(a-c)+abs(d-b);
long long int p=pow(3LL,30LL);
for(int i=30;i>=0;i--,p/=3LL){
long long int ax=a/p;
long long int bx=c/p;
long long int ay=b/p;
long long int by=d/p;
if(ax==bx&&ay==by){
continue;
}
if(ax==bx&&ax%3==1&&abs(ay-by)!=1){
if(p>=3)ds=ex(a%p,c%p,p)+abs(b-d);
else ds=ex(0,0,1)+abs(b-d);
break;
}
if(ax==bx){
continue;
}
if(ay==by&&by%3==1&&abs(ax-bx)!=1){
if(p>=3)ds=ex(b%p,d%p,p)+abs(a-c);
else ds=ex(0,0,1)+abs(a-c);
break;
}
if(ay==by){
continue;
}
break;
}
printf("%lld\n",ds);
}
return 0;
}
|
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int, int>
#define f first
#define s second
using namespace std;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
vector<LL> pw(35);
pw[0] = 1;
for (int i = 1; i < 35; i++) {
pw[i] = pw[i - 1] * 3;
}
int tc;
cin >> tc;
while (tc--) {
LL x1, y1, x2, y2, ok = 1, ans;
cin >> x1 >> y1 >> x2 >> y2;
x1--, y1--, x2--, y2--;
for (int k = 30; k >= 0; k--) {
LL sx = x1 / pw[k], sy = y1 / pw[k];
LL tx = x2 / pw[k], ty = y2 / pw[k];
if (sx != tx && sy != ty) {
break;
}
if (sx == tx && sx % 3 == 1 && abs(sy - ty) > 1) {
LL xl = x1 / pw[k] * pw[k] - 1, xr = xl + pw[k] + 1;
LL dy = abs(y2 - y1), dx = min(abs(xl - x1) + abs(x2 - xl), abs(xr - x1) + abs(x2 - xr));
ans = dx + dy;
ok = 0;
break;
// cout << "Atx " << k << ' ' << pw[k] << ' ' << xl << ' ' << xr << ' ' << dx << ' ' << y1 << ' ' << y2 << ' ' << dy << '\n';
}
if (sy == ty && sy % 3 == 1 && abs(sx - tx) > 1) {
LL yl = y1 / pw[k] * pw[k] - 1, yr = yl + pw[k] + 1;
LL dx = abs(x2 - x1), dy = min(abs(yl - y1) + abs(y2 - yl), abs(yr - y1) + abs(y2 - yr));
ans = dx + dy;
ok = 0;
break;
}
}
if (ok) {
cout << abs(x2 - x1) + abs(y2 - y1) << '\n';
}
else {
cout << ans << '\n';
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
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;
}
const long long INF = 1e18;
//const ll mod = 1000000007;
vector<ll> f(ll a) {
vector<ll> ret(30);
for(int i = 0; i < 30; i++) {
ret[i] = a % 3;
a /= 3;
//cerr << ret[i] << " ";
}
//cerr << endl;
return ret;
}
vector<ll> v[4];
ll beki[35];
ll val(vector<ll> v) {
ll ret = 0;
for(int i = 0; i < 30; i++) {
ret += beki[i] * v[i];
}
return ret;
}
bool contain(vector<ll> v, vector<ll> w, int idx) {
if(val(v) > val(w)) {
return contain(w, v, idx);
}
for(int i = 0; i < idx; i++) {
v[i] = 2;
}
if(v[idx] == 2) {
v[idx] = 3;
}
ll delta = 0;
for(int i = 0; i < 30; i++) {
delta += (w[i] - v[i]) * beki[i];
}
return delta >= beki[idx];
}
void solve() {
ll a[4];
for(int i = 0; i < 4; i++) {
cin >> a[i];
a[i]--;
v[i] = f(a[i]);
}
bool Hsame = true;
bool Wsame = true;
ll ans = abs(a[0] - a[2]) + abs(a[1] - a[3]);
for(ll idx = 29; idx >= 0; idx--) {
if(v[0][idx] != v[2][idx]) Hsame = false;
if(v[1][idx] != v[3][idx]) Wsame = false;
if(!Hsame and !Wsame) {
cout << ans << endl;
return;
}
if(Hsame and Wsame) continue;
cerr << idx << " " << Hsame << " " << Wsame << endl;
if(Hsame) {
ll tmp = 1e18;
//cerr << v[0][idx-1] << endl;
if(v[0][idx] != 1) continue;
if(!contain(v[1], v[3], idx)) continue;
ll bone = a[0] % beki[idx];
ll btwo = a[2] % beki[idx];
cerr << bone << " " << btwo << endl;
chmin(tmp, (bone + 1) * 2);
chmin(tmp, (btwo + 1) * 2);
chmin(tmp, (beki[idx] - bone) * 2);
chmin(tmp, (beki[idx] - btwo) * 2);
cout << ans + tmp << endl;
return;
}
if(Wsame) {
ll tmp = 1e18;
if(v[1][idx] != 1) continue;
if(!contain(v[0], v[2], idx)) continue;
ll bone = a[1] % beki[idx];
ll btwo = a[3] % beki[idx];
chmin(tmp, (bone + 1) * 2);
chmin(tmp, (btwo + 1) * 2);
chmin(tmp, (beki[idx] - bone) * 2);
chmin(tmp, (beki[idx] - btwo) * 2);
cout << ans + tmp << endl;
return;
}
}
cout << ans << endl;
}
int main() {
//cout.precision(10);
cin.tie(0);
ios::sync_with_stdio(false);
int Q;
cin >> Q;
beki[0] = 1;
for(ll i = 1; i <= 30; i++) {
beki[i] = beki[i-1] * 3;
}
while(Q--) {
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i,n) for (ll i = 0; i < (n); ++i)
const ll MAX_LEVEL = 30;
vector<ll> pow3;
void init_pow3(){
pow3.resize(MAX_LEVEL + 1);
pow3.at(0) = 1;
for(ll i = 1; i <= MAX_LEVEL; ++i){
pow3.at(i) = 3 * pow3.at(i - 1);
}
}
ll solve(ll y1, ll x1, ll y2, ll x2, ll l){
if(l == 0){
return abs(y1 - y2) + abs(x1 - x2);
}
const ll w = pow3.at(l - 1);
const ll yi1 = y1 / w, yi2 = y2 / w;
const ll xi1 = x1 / w, xi2 = x2 / w;
if(yi1 == yi2 && xi1 == xi2){
return solve(y1, x1, y2, x2, l - 1);
}else if(yi1 != yi2 && xi1 != xi2){
return abs(y1 - y2) + abs(x1 - x2);
}else if(yi1 == yi2){
assert(xi1 != xi2);
if(yi1 % 3 != 1 || abs(xi1 - xi2) <= 1){
return solve(y1, x1, y2, x2, l - 1);
}else{
ll ret = abs(x1 - x2);
ret += min(y1 % w + y2 % w + 2, 2 * w - y1 % w - y2 % w);
return ret;
}
}else{
assert(xi1 == xi2 && yi1 != yi2);
return solve(x1, y1, x2, y2, l);
}
}
int main(){
init_pow3();
ll q;
cin >> q;
REP(i, q){
ll ai, bi, ci, di;
cin >> ai >> bi >> ci >> di;
ai--; bi--; ci--; di--;
cout << solve(ai, bi, ci, di, MAX_LEVEL) << endl;
}
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.