Submission #69877779
Source Code Expand
import sys
input=sys.stdin.readline
n=int(input())
WV=[tuple(map(int,input().split())) for _ in range(n)]
q=int(input())
Q=[tuple(map(int,input().split())) for _ in range(q)]
CMAX=500
B=max(1,int(n**0.5))
nb=(n+B-1)//B
box=[[0]*(CMAX+1) for _ in range(nb)]
for b in range(nb):
l=b*B
r=min(n,(b+1)*B)
dp=box[b]
for i in range(l,r):
w,v=WV[i]
for c in range(CMAX-w,-1,-1):
nv=dp[c]+v
if nv>dp[c+w]:
dp[c+w]=nv
def add_item(now,w,v,C):
for c in range(C-w,-1,-1):
nv=now[c]+v
if nv>now[c+w]:
now[c+w]=nv
def merge_block(now,blk,C,tmp):
tmp[:C+1]=[0]*(C+1)
for i in range(C+1):
bi=now[i]
if bi==0 and i!=0: continue
rem=C-i
for j in range(rem+1):
nv=bi+blk[j]
if nv>tmp[i+j]:
tmp[i+j]=nv
now[:C+1]=tmp[:C+1]
out=[]
now=[0]*(CMAX+1)
tmp=[0]*(CMAX+1)
for L,R,C in Q:
L-=1; R-=1
now[:C+1]=[0]*(C+1)
bL=L//B; bR=R//B
lEnd=min(R+1,(bL+1)*B)
for i in range(L,lEnd):
w,v=WV[i]
if w<=C: add_item(now,w,v,C)
if bL!=bR:
rBeg=max(L,bR*B)
for i in range(rBeg,R+1):
w,v=WV[i]
if w<=C: add_item(now,w,v,C)
for b in range(bL+1,bR):
merge_block(now,box[b],C,tmp)
out.append(str(now[C]))
print("\n".join(out))
Submission Info
| Submission Time | |
|---|---|
| Task | G - Range Knapsack Query |
| User | uparupaaa |
| Language | Python (PyPy 3.10-v7.3.12) |
| Score | 0 |
| Code Size | 1302 Byte |
| Status | TLE |
| Exec Time | 2214 ms |
| Memory | 128988 KiB |
Judge Result
| Set Name | Sample | All | ||||||
|---|---|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 0 / 575 | ||||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00_sample_00.txt, 00_sample_01.txt |
| All | 00_sample_00.txt, 00_sample_01.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 01_small_03.txt, 01_small_04.txt, 01_small_05.txt, 01_small_06.txt, 01_small_07.txt, 02_large_00.txt, 02_large_01.txt, 02_large_02.txt, 02_large_03.txt, 02_large_04.txt, 02_large_05.txt, 02_large_06.txt, 02_large_07.txt, 02_large_08.txt, 02_large_09.txt, 02_large_10.txt, 02_large_11.txt, 02_large_12.txt, 02_large_13.txt, 02_large_14.txt, 02_large_15.txt, 02_large_16.txt, 02_large_17.txt, 02_large_18.txt, 02_large_19.txt, 02_large_20.txt, 02_large_21.txt, 02_large_22.txt, 02_large_23.txt, 02_large_24.txt, 02_large_25.txt, 02_large_26.txt, 02_large_27.txt, 02_large_28.txt, 02_large_29.txt, 02_large_30.txt, 02_large_31.txt, 02_large_32.txt, 02_large_33.txt, 02_large_34.txt, 02_large_35.txt, 03_handmade_00.txt, 03_handmade_01.txt, 03_handmade_02.txt, 03_handmade_03.txt, 03_handmade_04.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00_sample_00.txt | AC | 71 ms | 81012 KiB |
| 00_sample_01.txt | AC | 76 ms | 81908 KiB |
| 01_small_00.txt | TLE | 2212 ms | 97540 KiB |
| 01_small_01.txt | TLE | 2212 ms | 100740 KiB |
| 01_small_02.txt | TLE | 2213 ms | 106252 KiB |
| 01_small_03.txt | AC | 235 ms | 84176 KiB |
| 01_small_04.txt | TLE | 2212 ms | 100676 KiB |
| 01_small_05.txt | TLE | 2211 ms | 85712 KiB |
| 01_small_06.txt | TLE | 2212 ms | 101060 KiB |
| 01_small_07.txt | TLE | 2212 ms | 100900 KiB |
| 02_large_00.txt | TLE | 2213 ms | 108020 KiB |
| 02_large_01.txt | TLE | 2213 ms | 108316 KiB |
| 02_large_02.txt | TLE | 2213 ms | 108124 KiB |
| 02_large_03.txt | TLE | 2213 ms | 108456 KiB |
| 02_large_04.txt | TLE | 2213 ms | 108964 KiB |
| 02_large_05.txt | TLE | 2213 ms | 108084 KiB |
| 02_large_06.txt | TLE | 2213 ms | 108360 KiB |
| 02_large_07.txt | TLE | 2212 ms | 108424 KiB |
| 02_large_08.txt | TLE | 2213 ms | 108720 KiB |
| 02_large_09.txt | TLE | 2212 ms | 107936 KiB |
| 02_large_10.txt | TLE | 2213 ms | 108360 KiB |
| 02_large_11.txt | TLE | 2213 ms | 108112 KiB |
| 02_large_12.txt | TLE | 2213 ms | 108280 KiB |
| 02_large_13.txt | TLE | 2213 ms | 108164 KiB |
| 02_large_14.txt | TLE | 2213 ms | 108084 KiB |
| 02_large_15.txt | TLE | 2213 ms | 108268 KiB |
| 02_large_16.txt | TLE | 2213 ms | 108068 KiB |
| 02_large_17.txt | TLE | 2213 ms | 107920 KiB |
| 02_large_18.txt | TLE | 2213 ms | 108164 KiB |
| 02_large_19.txt | TLE | 2212 ms | 108352 KiB |
| 02_large_20.txt | TLE | 2213 ms | 108872 KiB |
| 02_large_21.txt | TLE | 2213 ms | 108408 KiB |
| 02_large_22.txt | TLE | 2213 ms | 108436 KiB |
| 02_large_23.txt | TLE | 2212 ms | 108628 KiB |
| 02_large_24.txt | TLE | 2214 ms | 108412 KiB |
| 02_large_25.txt | TLE | 2212 ms | 108352 KiB |
| 02_large_26.txt | TLE | 2212 ms | 108344 KiB |
| 02_large_27.txt | TLE | 2212 ms | 108504 KiB |
| 02_large_28.txt | TLE | 2212 ms | 108288 KiB |
| 02_large_29.txt | TLE | 2212 ms | 108180 KiB |
| 02_large_30.txt | TLE | 2213 ms | 108588 KiB |
| 02_large_31.txt | TLE | 2213 ms | 108752 KiB |
| 02_large_32.txt | TLE | 2212 ms | 108172 KiB |
| 02_large_33.txt | TLE | 2212 ms | 108808 KiB |
| 02_large_34.txt | TLE | 2212 ms | 108332 KiB |
| 02_large_35.txt | TLE | 2212 ms | 108164 KiB |
| 03_handmade_00.txt | AC | 316 ms | 128704 KiB |
| 03_handmade_01.txt | AC | 312 ms | 128988 KiB |
| 03_handmade_02.txt | TLE | 2212 ms | 108376 KiB |
| 03_handmade_03.txt | TLE | 2212 ms | 111324 KiB |
| 03_handmade_04.txt | TLE | 2212 ms | 108424 KiB |