結果

問題 No.2161 Black Market
ユーザー vwxyzvwxyz
提出日時 2023-11-30 18:38:16
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 7,027 bytes
コンパイル時間 384 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 270,172 KB
最終ジャッジ日時 2024-09-26 14:21:22
合計ジャッジ時間 42,814 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 146 ms
89,088 KB
testcase_01 AC 148 ms
88,960 KB
testcase_02 AC 224 ms
90,880 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 AC 232 ms
91,992 KB
testcase_07 AC 240 ms
91,860 KB
testcase_08 AC 240 ms
91,984 KB
testcase_09 AC 239 ms
91,468 KB
testcase_10 AC 248 ms
91,336 KB
testcase_11 AC 240 ms
91,684 KB
testcase_12 AC 224 ms
91,008 KB
testcase_13 AC 150 ms
89,600 KB
testcase_14 AC 165 ms
89,600 KB
testcase_15 AC 229 ms
91,140 KB
testcase_16 AC 212 ms
90,752 KB
testcase_17 AC 147 ms
89,088 KB
testcase_18 RE -
testcase_19 AC 216 ms
90,880 KB
testcase_20 AC 2,972 ms
262,412 KB
testcase_21 AC 2,968 ms
262,408 KB
testcase_22 AC 3,625 ms
262,008 KB
testcase_23 AC 4,845 ms
262,164 KB
testcase_24 AC 5,446 ms
270,172 KB
testcase_25 AC 2,122 ms
173,784 KB
testcase_26 AC 4,642 ms
265,788 KB
testcase_27 AC 1,016 ms
94,164 KB
testcase_28 AC 1,057 ms
97,168 KB
testcase_29 AC 630 ms
96,352 KB
testcase_30 AC 487 ms
96,008 KB
testcase_31 AC 2,607 ms
198,696 KB
testcase_32 AC 961 ms
94,004 KB
testcase_33 AC 601 ms
102,944 KB
testcase_34 AC 553 ms
93,368 KB
testcase_35 AC 529 ms
92,912 KB
testcase_36 AC 420 ms
93,248 KB
testcase_37 AC 388 ms
93,160 KB
testcase_38 AC 634 ms
93,648 KB
testcase_39 AC 415 ms
93,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
import copy
import decimal
import fractions
import heapq
import itertools
import math
import random
import sys
import time
from collections import Counter,deque,defaultdict
from functools import lru_cache,reduce
from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max
def _heappush_max(heap,item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappushpop_max(heap, item):
    if heap and item < heap[0]:
        item, heap[0] = heap[0], item
        heapq._siftup_max(heap, 0)
    return item
from math import gcd as GCD
read=sys.stdin.read
readline=sys.stdin.readline
readlines=sys.stdin.readlines
write=sys.stdout.write
#import pypyjit
#pypyjit.set_param('max_unroll_recursion=-1')
#sys.set_int_max_str_digits(10**9)

class Segment_Tree:
    def __init__(self,N,f,e,lst=None,dynamic=False):
        self.f=f
        self.e=e
        self.N=N
        if dynamic:
            self.segment_tree=defaultdict(lambda:self.e)
        else:
            if lst==None:
                self.segment_tree=[self.e]*2*self.N
            else:
                assert len(lst)<=self.N
                self.segment_tree=[self.e]*self.N+[x for x in lst]+[self.e]*(N-len(lst))
                for i in range(self.N-1,0,-1):
                    self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])

    def __getitem__(self,i):
        if type(i)==int:
            if -self.N<=i<0:
                return self.segment_tree[i+self.N*2]
            elif 0<=i<self.N:
                return self.segment_tree[i+self.N]
            else:
                raise IndexError("list index out of range")
        else:
            a,b,c=i.start,i.stop,i.step
            if a==None:
                a=self.N
            else:
                a+=self.N
            if b==None:
                b=self.N*2
            else:
                b+=self.N
            return self.segment_tree[slice(a,b,c)]

    def __setitem__(self,i,x):
        if -self.N<=i<0:
            i+=self.N*2
        elif 0<=i<self.N:
            i+=self.N
        else:
            raise IndexError("list index out of range")
        self.segment_tree[i]=x
        while i>1:
            i>>= 1
            self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])

    def Build(self,lst):
        for i,x in enumerate(lst,self.N):
            self.segment_tree[i]=x
        for i in range(self.N-1,0,-1):
            self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])

    def Fold(self,L=None,R=None):
        if L==None:
            L=self.N
        else:
            L+=self.N
        if R==None:
            R=self.N*2
        else:
            R+=self.N
        vL=self.e
        vR=self.e
        while L<R:
            if L&1:
                vL=self.f(vL,self.segment_tree[L])
                L+=1
            if R&1:
                R-=1
                vR=self.f(self.segment_tree[R],vR)
            L>>=1
            R>>=1
        return self.f(vL,vR)

    def Fold_Index(self,L=None,R=None):
        if L==None:
            L=self.N
        else:
            L+=self.N
        if R==None:
            R=self.N*2
        else:
            R+=self.N
        if L==R:
            return None
        x=self.Fold(L-self.N,R-self.N)
        while L<R:
            if L&1:
                if self.segment_tree[L]==x:
                    i=L
                    break
                L+=1
            if R&1:
                R-=1
                if self.segment_tree[R]==x:
                    i=R
                    break
            L>>=1
            R>>=1
        while i<self.N:
            if self.segment_tree[i]==self.segment_tree[i<<1]:
                i<<=1
            else:
                i<<=1
                i|=1
        i-=self.N
        return i

    def Bisect_Right(self,L=None,f=None):
        if L==self.N:
            return self.N
        if L==None:
            L=0
        L+=self.N
        vl=self.e
        vr=self.e
        l,r=L,self.N*2
        while l<r:
            if l&1:
                vl=self.f(vl,self.segment_tree[l])
                l+=1
            if r&1:
                r-=1
                vr=self.f(self.segment_tree[r],vr)
            l>>=1
            r>>=1
        if f(self.f(vl,vr)):
            return self.N
        v=self.e
        while True:
            while L%2==0:
                L>>=1
            vv=self.f(v,self.segment_tree[L])
            if f(vv):
                v=vv
                L+=1
            else:
                while L<self.N:
                    L<<=1
                    vv=self.f(v,self.segment_tree[L])
                    if f(vv):
                        v=vv
                        L+=1
                return L-self.N

    def Bisect_Left(self,R=None,f=None):
        if R==0:
            return 0
        if R==None:
            R=self.N
        R+=self.N
        vl=self.e
        vr=self.e
        l,r=self.N,R
        while l<r:
            if l&1:
                vl=self.f(vl,self.segment_tree[l])
                l+=1
            if r&1:
                r-=1
                vr=self.f(self.segment_tree[r],vr)
            l>>=1
            r>>=1
        if f(self.f(vl,vr)):
            return 0
        v=self.e
        while True:
            R-=1
            while R>1 and R%2:
                R>>=1
            vv=self.f(self.segment_tree[R],v)
            if f(vv):
                v=vv
            else:
                while R<self.N:
                    R=2*R+1
                    vv=self.f(self.segment_tree[R],v)
                    if f(vv):
                        v=vv
                        R-=1
                return R+1-self.N

    def __str__(self):
        return "["+", ".join(map(str,self.segment_tree[self.N:]))+"]"

def Compress(lst):
    decomp=sorted(list(set(lst)))
    comp={x:i for i,x in enumerate(decomp)}
    return comp,decomp

N,K,L,P=map(int,readline().split())
A,B=[],[]
for i in range(N):
    a,b=map(int,readline().split())
    A.append(a)
    B.append(b)
n=N//2-1
AB0=[[] for k in range(K+1)]
for bit in range(1<<n):
    c=sum(1 for i in range(n) if bit&1<<i)
    a=sum(A[i] for i in range(n) if bit&1<<i)
    b=sum(B[i] for i in range(n) if bit&1<<i)
    if a<=L:
        for k in range(K+1):
            if c<=k:
                AB0[k].append((a,b))
AB1=[[] for k in range(K+1)]
for bit in range(1<<N-n):
    c=sum(1 for i in range(N-n) if bit&1<<i)
    a=sum(A[n+i] for i in range(N-n) if bit&1<<i)
    b=sum(B[n+i] for i in range(N-n) if bit&1<<i)
    if c<=K and a<=L:
        AB1[c].append((a,b))
ans=0
for k in range(K+1):
    AB0[K-k].sort()
    AB1[k].sort(reverse=True)
    comp,decomp=Compress([b for a,b in AB0[K-k]]+[P-b for a,b in AB1[k]])
    le=len(comp)
    ST=Segment_Tree(le,lambda x,y:x+y,0)
    i=0
    for a,b in AB1[k]:
        while i<len(AB0[K-k]) and AB0[K-k][i][0]+a<=L:
            ST[comp[AB0[K-k][i][1]]]+=1
            i+=1
        ans+=ST.Fold(comp[P-b],le)
print(ans)
0