結果

問題 No.5010 Better Mo's Algorithm is Needed!! (Unweighted)
ユーザー titia
提出日時 2022-12-17 03:27:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 4,928 ms / 5,000 ms
コード長 1,830 bytes
コンパイル時間 974 ms
実行使用メモリ 187,872 KB
スコア 16,985,670,009
最終ジャッジ日時 2022-12-17 03:38:14
合計ジャッジ時間 632,507 ms
ジャッジサーバーID
(参考情報)
judge16 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 120
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from operator import itemgetter
import time
from random import randint

time0=time.time()

N,Q,WT,ST=map(int,input().split())
W=list(map(int,input().split()))
LR=[list(map(int,input().split()))+[i+1] for i in range(Q)]

LR2=sorted(LR,key=itemgetter(0))

LIST=[[] for i in range(1000)]

for x,y,ind in LR2:
    LIST[x//500].append((y,ind))

ANS=[]

count=0
for i in range(1000):
    if LIST[i]==[]:
        continue
    if count%2==0:
        LIST[i].sort(key=itemgetter(0))
    else:
        LIST[i].sort(key=itemgetter(0),reverse=True)
        
    for _,ind in LIST[i]:
        ANS.append(ind)

    count+=1

SUM=[0]

for w in W:
    SUM.append(SUM[-1]+w)

def score(x,y):
    if x==-1:
        a,b,_=LR[y-1]
        return SUM[b]-SUM[a-1]
    else:
        a,b,_=LR[y-1]
        c,d,_=LR[x-1]

        a2,b2=min(a,c),max(a,c)
        c2,d2=min(b,d),max(b,d)

        return SUM[b2-1]-SUM[a2-1]+SUM[d2]-SUM[c2]
    
def score_reverse(x,y):
    sc=0

    if x==0:
        a=ANS[x]
        b=ANS[y]
        c=ANS[y+1]

        sc-=score(-1,a)+score(a,b)+score(b,c)
        sc+=score(-1,b)+score(b,a)+score(a,c)

        return sc

    if y==len(ANS)-1:
        a=ANS[x-1]
        b=ANS[x]
        c=ANS[y]

        sc-=score(a,b)+score(b,c)
        sc+=score(a,c)+score(c,b)

        return sc

    a=ANS[x-1]
    b=ANS[x]
    c=ANS[y]
    d=ANS[y+1]

    sc-=score(a,b)+score(b,c)+score(c,d)
    sc+=score(a,c)+score(c,b)+score(b,d)

    return sc

def last_score(ANS):
    sc=0
    for i in range(len(ANS)):
        if i==0:
            sc+=score(-1,ANS[0])
        else:
            sc+=score(ANS[i-1],ANS[i])

    return sc
    
while time.time()-time0<4.7:
    k=randint(0,len(ANS)-2)

    if score_reverse(k,k+1)<0:
        ANS[k],ANS[k+1]=ANS[k+1],ANS[k]

print(*ANS)



        
    
0