結果

問題 No.3680 セグメント釣り
コンテスト
ユーザー Rapca1256
提出日時 2026-09-05 15:36:25
言語 Python3
(3.14.7 + numpy 2.5.2 + scipy 1.18.0)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
TLE  
実行時間 -
コード長 2,953 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 541 ms
コンパイル使用メモリ 21,212 KB
実行使用メモリ 16,044 KB
最終ジャッジ日時 2026-09-05 15:36:48
合計ジャッジ時間 19,846 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge5_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 12 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
import math

# 入力高速化
#input = sys.stdin.readline
# 再帰回数上限
sys.setrecursionlimit(10**9)

# よくあるmod
#MOD = 1000000007
MOD = 998244353

def is_inside(y, x, h, w):
  return (0 <= y < h and 0 <= x < w)

def solve():
    sx, sy, tx, ty = map(int, input().split(' '))
    if sx > tx:
        sx, tx = tx, sx
        sy, ty = ty, sy
    nsx = sx
    nsy = sy
    ntx = tx
    nty = ty
    k = sy
    cost = 10**20
    for _ in range(k):
        if nsx == 0: break
        nsx //= 2
    for _ in range(k):
        if nsy == 0: break
        nsy //= 2
    for _ in range(k):
        if ntx == 0: break
        ntx //= 2
    for _ in range(k):
        if nty == 0: break
        nty //= 2
    
    if sy <= ty:
        while (ntx - nsx) > 0:
            if cost >= (k - sy) + abs(k - ty) + (ntx - nsx):
                cost = (k - sy) + abs(k - ty) + (ntx - nsx)
            k += 1
            nsx //= 2
            nsy //= 2
            ntx //= 2
            nty //= 2
        cost = min((k - sy) + abs(k - ty) + ntx - nsx, cost)
    else:
        while (ntx - nsx) > 0:
            if cost >= 2*(k - sy) + ntx - nsx:
                cost = 2*(k - sy) + ntx - nsx
            k += 1
            nsx //= 2
            nsy //= 2
            ntx //= 2
            nty //= 2
        cost = min(cost, 2*(k - sy) + ntx - nsx)
        cost += abs(sy - ty)
        
    print(cost)

def check(sx, sy, tx, ty):
    # 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7
    if sx > tx:
        sx, tx = tx, sx
        sy, ty = ty, sy
    nsx = sx
    nsy = sy
    ntx = tx
    nty = ty
    k = sy
    cost = 10**20
    for _ in range(k):
        if nsx == 0: break
        nsx //= 2
    for _ in range(k):
        if nsy == 0: break
        nsy //= 2
    for _ in range(k):
        if ntx == 0: break
        ntx //= 2
    for _ in range(k):
        if nty == 0: break
        nty //= 2
    
    if sy <= ty:
        while (ntx - nsx) > 0:
            if cost >= (k - sy) + abs(k - ty) + (ntx - nsx):
                cost = (k - sy) + abs(k - ty) + (ntx - nsx)
            k += 1
            cur *= 2
            nsx //= 2
            nsy //= 2
            ntx //= 2
            nty //= 2
        cost = min((k - sy) + abs(k - ty) + ntx - nsx, cost)
    else:
        while (ntx - nsx) > 0:
            if cost >= 2*(k - sy) + ntx - nsx:
                cost = 2*(k - sy) + ntx - nsx
            k += 1
            cur *= 2
            nsx //= 2
            nsy //= 2
            ntx //= 2
            nty //= 2
        cost = min(cost, 2*(k - sy) + ntx - nsx)
        cost += abs(sy - ty)
        
    print(cost)

import random
def test():
    sy = random.randint(0, 10**18)
    sx = random.randint(0, 10**18)
    tx = random.randint(0, 10**18)
    ty = random.randint(0, 10**18)
    print(sx, sy, tx, ty)
    check(sx, sy, tx, ty)
    
if __name__ == '__main__':
    T = int(input())
    for _ in range(T):
        solve()
0