結果

問題 No.1649 Manhattan Square
ユーザー mkawa2mkawa2
提出日時 2024-01-06 12:15:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,635 ms / 3,000 ms
コード長 2,481 bytes
コンパイル時間 359 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 181,620 KB
最終ジャッジ日時 2024-01-06 12:16:30
合計ジャッジ時間 55,972 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,460 KB
testcase_01 AC 35 ms
53,460 KB
testcase_02 AC 95 ms
77,592 KB
testcase_03 AC 98 ms
77,592 KB
testcase_04 AC 94 ms
77,592 KB
testcase_05 AC 93 ms
77,592 KB
testcase_06 AC 100 ms
77,588 KB
testcase_07 AC 1,635 ms
181,620 KB
testcase_08 AC 1,478 ms
180,816 KB
testcase_09 AC 1,450 ms
181,400 KB
testcase_10 AC 1,445 ms
181,344 KB
testcase_11 AC 1,426 ms
181,424 KB
testcase_12 AC 1,205 ms
111,320 KB
testcase_13 AC 1,336 ms
132,068 KB
testcase_14 AC 1,265 ms
117,848 KB
testcase_15 AC 1,285 ms
120,280 KB
testcase_16 AC 1,164 ms
108,316 KB
testcase_17 AC 1,191 ms
116,440 KB
testcase_18 AC 1,112 ms
109,784 KB
testcase_19 AC 1,258 ms
124,852 KB
testcase_20 AC 1,207 ms
113,580 KB
testcase_21 AC 1,343 ms
127,576 KB
testcase_22 AC 1,423 ms
181,108 KB
testcase_23 AC 1,475 ms
180,920 KB
testcase_24 AC 1,426 ms
181,096 KB
testcase_25 AC 1,409 ms
181,164 KB
testcase_26 AC 1,414 ms
181,556 KB
testcase_27 AC 1,424 ms
181,048 KB
testcase_28 AC 1,419 ms
181,576 KB
testcase_29 AC 1,417 ms
181,188 KB
testcase_30 AC 1,467 ms
180,904 KB
testcase_31 AC 1,413 ms
180,648 KB
testcase_32 AC 1,432 ms
181,036 KB
testcase_33 AC 1,411 ms
180,864 KB
testcase_34 AC 1,426 ms
181,060 KB
testcase_35 AC 1,430 ms
180,472 KB
testcase_36 AC 1,482 ms
181,612 KB
testcase_37 AC 1,419 ms
181,560 KB
testcase_38 AC 1,426 ms
181,044 KB
testcase_39 AC 1,433 ms
180,932 KB
testcase_40 AC 1,422 ms
180,824 KB
testcase_41 AC 1,415 ms
180,900 KB
testcase_42 AC 1,238 ms
171,864 KB
testcase_43 AC 35 ms
53,460 KB
testcase_44 AC 35 ms
53,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(1000005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = -1-(-1 << 31)
inf = -1-(-1 << 62)

# md = 10**9+7
md = 998244353

class BitSum:
    def __init__(self, n):
        self._n = n+1
        self._table = [0]*self._n
        self._origin = [0]*n

    def __getitem__(self, item):
        return self._origin[item]

    def __setitem__(self, index, value):
        self.add(index, value-self._origin[index])
        self._origin[index] = value

    def add(self, i, x):
        if x == 0: return
        self._origin[i] += x
        i += 1
        while i < self._n:
            self._table[i] += x
            self._table[i] %= md
            i += i & -i

    def sum(self, r):
        res = 0
        while r > 0:
            res += self._table[r]
            res %= md
            r -= r & -r
        return res

n = II()
xy = LLI(n)
xy.sort()

dec = sorted(set(y for x, y in xy))
enc = {a: i for i, a in enumerate(dec)}

s = t = s2 = t2 = u = 0
cbit = BitSum(len(dec))
xbit = BitSum(len(dec))
ybit = BitSum(len(dec))
xybit = BitSum(len(dec))
ans = 0
for p, (x, y) in enumerate(xy):
    # (x-X+y-Y)2
    # x2+X2+y2+Y2-2xX+2xy-2xY-2yX+2XY-2yY
    # x2+y2+X2+Y2-2(xX+yY)+2(xy-xY-yX+XY) (Y<y)
    # x2+y2+X2+Y2-2(xX+yY)-2(xy-xY-yX+XY) (Y>=y)
    ans += p*(x*x%md+y*y%md)%md+s2+t2-2*(x*s%md+y*t%md)
    i = enc[y]
    c = cbit.sum(i)
    xb = xbit.sum(i)
    yb = ybit.sum(i)
    xyb = xybit.sum(i)
    # ans += 2*(c*x*y%md-x*yb%md-y*xb%md+xyb)-2*((p-c)*x*y%md-x*(t-yb)%md-y*(s-xb)%md+(u-xyb))
    ans += 2*((2*c-p)*x*y%md-x*(2*yb-t)%md-y*(2*xb-s)%md+2*xyb-u)
    ans %= md

    x2 = x*x%md
    y2 = y*y%md
    xy = x*y%md
    s += x
    s2 += x2
    t += y
    t2 += y2
    u += xy
    s %= md
    s2 %= md
    t %= md
    t2 %= md
    u %= md
    cbit.add(i, 1)
    xbit.add(i, x)
    ybit.add(i, y)
    xybit.add(i, xy)

print(ans)
0