結果

問題 No.1919 Many Monster Battles
ユーザー googol_S0googol_S0
提出日時 2022-04-29 23:16:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,533 ms / 2,000 ms
コード長 2,229 bytes
コンパイル時間 366 ms
コンパイル使用メモリ 86,996 KB
実行使用メモリ 249,208 KB
最終ジャッジ日時 2023-09-11 14:50:53
合計ジャッジ時間 29,142 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,316 KB
testcase_01 AC 78 ms
71,304 KB
testcase_02 AC 76 ms
71,220 KB
testcase_03 AC 116 ms
78,424 KB
testcase_04 AC 113 ms
78,584 KB
testcase_05 AC 115 ms
78,612 KB
testcase_06 AC 114 ms
78,560 KB
testcase_07 AC 114 ms
78,324 KB
testcase_08 AC 112 ms
78,276 KB
testcase_09 AC 111 ms
78,512 KB
testcase_10 AC 111 ms
78,648 KB
testcase_11 AC 113 ms
78,644 KB
testcase_12 AC 114 ms
78,200 KB
testcase_13 AC 1,528 ms
248,644 KB
testcase_14 AC 1,480 ms
248,312 KB
testcase_15 AC 1,424 ms
248,712 KB
testcase_16 AC 1,517 ms
248,420 KB
testcase_17 AC 1,533 ms
249,208 KB
testcase_18 AC 1,203 ms
193,976 KB
testcase_19 AC 1,178 ms
193,896 KB
testcase_20 AC 1,272 ms
194,876 KB
testcase_21 AC 1,255 ms
194,684 KB
testcase_22 AC 1,187 ms
193,636 KB
testcase_23 AC 1,052 ms
240,740 KB
testcase_24 AC 1,106 ms
240,976 KB
testcase_25 AC 1,067 ms
239,168 KB
testcase_26 AC 1,025 ms
240,428 KB
testcase_27 AC 803 ms
196,152 KB
testcase_28 AC 822 ms
195,796 KB
testcase_29 AC 889 ms
196,000 KB
testcase_30 AC 899 ms
195,940 KB
testcase_31 AC 1,313 ms
245,292 KB
testcase_32 AC 1,246 ms
245,712 KB
testcase_33 AC 744 ms
156,272 KB
testcase_34 AC 764 ms
156,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def Binit(B,siz):
  while len(B)<siz+1:
    B.append(0)
  while len(B)>siz+1:
    del B[-1]
  for i in range(siz+1):
    B[i]=0
  B.append(siz)

def Badd(B,a,x):
  z=a
  while z<=B[-1]:
    B[z]+=x
    z+=(z&(-z))

def Bsum(B,a):
  r=0
  z=a
  while z>0:
    r+=B[z]
    z-=(z&(-z))
  return r

from copy import *
def init(N,node,A,unit,func):
  while len(node):
    del node[-1]
  n=1
  while n<N:
    n<<=1
  for i in range(n*2-1):
    if len(node)<=i:
      node.append(unit)
    else:
      node[i]=unit
  for i in range(len(A)):
    node[n-1+i]=A[i]
  for i in range(n-2,-1,-1):
    node[i]=node[(i<<1)+1]+node[(i<<1)+2]
  node.append(func)
  node.append(unit)
  node.append(n)

def upd(node,x,a):
  y=node[-1]+x
  node[y-1]+=a
  while y>1:
    y=y>>1
    node[y-1]=node[(y<<1)-1]+node[y<<1]

def query(node,l,r):
  x,y=l,r
  z=node[-1]-1
  rr=node[-2]
  rl=node[-2]
  while True:
    if x==y:
      return rl+rr
    if x&1:
      rl=rl+node[x+z]
      x+=1
    if y&1:
      rr=node[y+z-1]+rr
    if z==0:
      return rl+rr
    x>>=1
    y>>=1
    z>>=1

mod=10**9+7
def comp(x):
  #y=sorted(set(x))
  y=sorted(x)
  d=dict()
  for i in range(len(y)):
    d[y[i]]=i
  return d

def solve(A,B):
  N=len(A)
  X=[(A[i]<<31)+B[i] for i in range(N)]
  X.sort()
  X=[(X[i]>>31,X[i]&((1<<31)-1)) for i in range(N)]
  Y=[X[i][0]-X[i][1] for i in range(N)]
  Z=[X[i][0]+X[i][1] for i in range(N)]
  y,z=comp(Y),comp(Z)
  ANS=0
  segy=[]
  segz=[]
  Binit(segy,N+3)
  Binit(segz,N+3)
  c=0
  for i in range(N):
    if i and X[i]==X[i-1]:
      c+=1
    else:
      c=0
    a,b=y[Y[i]],z[Z[i]]
    ANS=(ANS+X[i][0]*(Bsum(segy,a)+Bsum(segz,b)-i+c))%mod
    Badd(segy,a+1,1)
    Badd(segz,b+1,1)
  Binit(segy,N+3)
  Binit(segz,N+3)
  c=0
  for i in range(N-1,-1,-1):
    if i+1<N and X[i]==X[i+1]:
      c+=1
    else:
      c=0
    a,b=y[Y[i]],z[Z[i]]
    ANS=(ANS-X[i][0]*(-Bsum(segy,a+1)-Bsum(segz,b+1)+(N-1-i)+c))%mod
    Badd(segy,a+1,1)
    Badd(segz,b+1,1)
  return ANS

N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
if 0:
  N=200000
  from random import *
  A=[randrange(0,1000000000) for i in range(N)]
  B=[randraange(0,1000000000) for i in range(N)]
print(solve(A,B)*2%mod,solve(B,A)*2%mod)
0