結果

問題 No.2758 RDQ
ユーザー vwxyzvwxyz
提出日時 2024-08-09 04:33:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,155 ms / 2,000 ms
コード長 6,306 bytes
コンパイル時間 341 ms
コンパイル使用メモリ 82,136 KB
実行使用メモリ 174,420 KB
最終ジャッジ日時 2024-08-09 04:33:25
合計ジャッジ時間 16,440 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 483 ms
110,904 KB
testcase_01 AC 46 ms
55,168 KB
testcase_02 AC 47 ms
55,680 KB
testcase_03 AC 47 ms
55,148 KB
testcase_04 AC 48 ms
55,624 KB
testcase_05 AC 64 ms
67,584 KB
testcase_06 AC 1,142 ms
173,060 KB
testcase_07 AC 1,152 ms
172,988 KB
testcase_08 AC 1,127 ms
174,420 KB
testcase_09 AC 1,155 ms
174,160 KB
testcase_10 AC 1,113 ms
172,832 KB
testcase_11 AC 802 ms
128,784 KB
testcase_12 AC 776 ms
128,656 KB
testcase_13 AC 772 ms
129,248 KB
testcase_14 AC 793 ms
128,512 KB
testcase_15 AC 758 ms
128,856 KB
testcase_16 AC 763 ms
128,768 KB
testcase_17 AC 826 ms
128,588 KB
testcase_18 AC 759 ms
129,152 KB
testcase_19 AC 780 ms
128,768 KB
testcase_20 AC 788 ms
129,280 KB
testcase_21 AC 46 ms
55,296 KB
testcase_22 AC 47 ms
55,168 KB
testcase_23 AC 54 ms
63,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class Segment_Tree:
    def __init__(self,N,f,e,lst=None,dynamic=False,bisect_search=True):
        self.f=f
        self.e=e
        self.N=N
        self.bisect_search=bisect_search
        if self.bisect_search:
            self.le=1
            while self.le<self.N:
                self.le*=2
        else:
            self.le=self.N
        if dynamic:
            self.segment_tree=defaultdict(lambda:self.e)
        else:
            if lst==None:
                self.segment_tree=[self.e]*2*self.le
            else:
                assert len(lst)<=self.le
                self.segment_tree=[self.e]*self.le+[x for x in lst]+[self.e]*(self.le-len(lst))
                for i in range(self.le-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.le<=i<0:
                return self.segment_tree[i+self.le*2]
            elif 0<=i<self.le:
                return self.segment_tree[i+self.le]
            else:
                raise IndexError("list index out of range")
        else:
            a,b,c=i.start,i.stop,i.step
            if a==None:
                a=self.le
            else:
                a+=self.le
            if b==None:
                b=self.le*2
            else:
                b+=self.le
            return self.segment_tree[slice(a,b,c)]

    def __setitem__(self,i,x):
        if -self.le<=i<0:
            i+=self.le*2
        elif 0<=i<self.le:
            i+=self.le
        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.le):
            self.segment_tree[i]=x
        for i in range(self.le-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.le
        else:
            assert 0<=L<=self.le
            L+=self.le
        if R==None:
            R=self.le*2
        else:
            assert 0<=R<=self.le
            R+=self.le
        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.le
        else:
            assert 0<=L<=self.le
            L+=self.le
        if R==None:
            R=self.le*2
        else:
            assert 0<=R<=self.le
            R+=self.le
        if L==R:
            return None
        x=self.Fold(L-self.le,R-self.le)
        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.le:
            if self.segment_tree[i]==self.segment_tree[i<<1]:
                i<<=1
            else:
                i<<=1
                i|=1
        i-=self.le
        return i

    def Bisect_Right(self,L=None,f=None):
        assert self.bisect_search
        if L==self.le:
            return self.le
        if L==None:
            L=0
        assert 0<=L<=self.le
        L+=self.le
        vl=self.e
        vr=self.e
        l,r=L,self.le*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.le:
                    L<<=1
                    vv=self.f(v,self.segment_tree[L])
                    if f(vv):
                        v=vv
                        L+=1
                return L-self.le

    def Bisect_Left(self,R=None,f=None):
        assert self.bisect_search
        if R==0:
            return 0
        if R==None:
            R=self.le
        assert 0<=R<=self.le
        R+=self.le
        vl=self.e
        vr=self.e
        l,r=self.le,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.le:
                    R=2*R+1
                    vv=self.f(self.segment_tree[R],v)
                    if f(vv):
                        v=vv
                        R-=1
                return R+1-self.le

    def __str__(self):
        return "["+", ".join(map(str,[self.segment_tree[i] for i in range(self.le,self.le+self.N)]))+"]"

N,Q=map(int,input().split())
A=list(map(int,input().split()))
M=max(A)
D=[[] for a in range(M+1)]
for d in range(1,M+1):
    for a in range(d,M+1,d):
        D[a].append(d)
idx=[[] for a in range(M+1)]
for i in range(N):
    for d in D[A[i]]:
        idx[d].append(i)
ans_lst=[None]*Q
query=[[] for a in range(M+1)]
for q in range(Q):
    L,R,K=map(int,input().split())
    L-=1
    if M<K:
        ans_lst[q]=0
    else:
        query[K].append((L,R,q))
ST=Segment_Tree(N,lambda x,y:x+y,0)
for d in range(1,M+1):
    for i in idx[d]:
        ST[i]+=1
    for l,r,q in query[d]:
        ans_lst[q]=ST.Fold(l,r)
    for i in idx[d]:
        ST[i]-=1
print(*ans_lst,sep="\n")
0