結果

問題 No.1099 Range Square Sum
ユーザー vwxyzvwxyz
提出日時 2021-09-01 11:00:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,859 ms / 2,000 ms
コード長 6,892 bytes
コンパイル時間 383 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 213,212 KB
最終ジャッジ日時 2024-11-27 11:04:30
合計ジャッジ時間 21,512 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 149 ms
88,960 KB
testcase_01 AC 149 ms
89,216 KB
testcase_02 AC 151 ms
89,088 KB
testcase_03 AC 153 ms
88,960 KB
testcase_04 AC 148 ms
89,088 KB
testcase_05 AC 139 ms
88,960 KB
testcase_06 AC 140 ms
89,088 KB
testcase_07 AC 137 ms
89,088 KB
testcase_08 AC 136 ms
89,088 KB
testcase_09 AC 135 ms
89,088 KB
testcase_10 AC 142 ms
89,216 KB
testcase_11 AC 236 ms
91,904 KB
testcase_12 AC 199 ms
91,520 KB
testcase_13 AC 210 ms
92,032 KB
testcase_14 AC 204 ms
91,520 KB
testcase_15 AC 217 ms
92,032 KB
testcase_16 AC 206 ms
91,776 KB
testcase_17 AC 209 ms
91,648 KB
testcase_18 AC 202 ms
91,776 KB
testcase_19 AC 208 ms
91,776 KB
testcase_20 AC 208 ms
91,648 KB
testcase_21 AC 1,809 ms
213,052 KB
testcase_22 AC 1,859 ms
212,788 KB
testcase_23 AC 1,798 ms
210,740 KB
testcase_24 AC 1,818 ms
212,400 KB
testcase_25 AC 1,840 ms
213,212 KB
testcase_26 AC 1,131 ms
133,420 KB
testcase_27 AC 1,136 ms
133,044 KB
testcase_28 AC 1,142 ms
133,164 KB
testcase_29 AC 1,138 ms
132,128 KB
testcase_30 AC 1,183 ms
132,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
import copy
import decimal
import fractions
import functools
import heapq
import itertools
import math
import random
import sys
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

class Lazy_Segment_Tree:
    def __init__(self,N,f,e,f_act,e_act,operate):
        self.N=N
        self.f=f
        self.e=e
        self.f_act=f_act
        self.e_act=e_act
        self.operate=operate
        self.segment_tree=[self.e]*(self.N+self.N)
        self.segment_tree_act=[self.e_act]*(self.N+self.N)

    def __getitem__(self,i):
        if type(i) is int:
            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.Propagate_Above(i)
            self.Recalculate_Above(i)
            return self.Operate_At(i)
        else:
            a,b,c=i.start,i.stop,i.step
            if a==None or a<-self.N:
                a=self.N
            elif self.N<=a:
                a=self.N*2
            elif a<0:
                a+=self.N*2
            else:
                a+=self.N
            if b==None or self.N<=b:
                b=self.N*2
            elif b<-self.N:
                b=self.N
            elif b<0:
                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.Propagate_Above(i)
        self.segment_tree[i]=x
        self.segment_tree_act[i]=self.e_act
        self.Recalculate_Above(i)

    def Operate_At(self,i):
        return self.operate(self.segment_tree[i],self.segment_tree_act[i])

    def Propagate_At(self,i):
        self.segment_tree[i]=self.Operate_At(i)
        self.segment_tree_act[i<<1]=self.f_act(self.segment_tree_act[i<<1],self.segment_tree_act[i])
        self.segment_tree_act[i<<1|1]=self.f_act(self.segment_tree_act[i<<1|1],self.segment_tree_act[i])
        self.segment_tree_act[i]=self.e_act

    def Propagate_Above(self,i):
        H=i.bit_length()-1
        for h in range(H,0,-1):
            self.Propagate_At(i>>h)

    def Recalculate_Above(self,i):
        while i>1:
            i>>=1
            self.segment_tree[i]=self.f(self.Operate_At(i<<1),self.Operate_At(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])
        self.segment_tree_act=[self.e_act]*(self.N+self.N)

    def Fold(self,L=None,R=None):
        if L==None or L<-self.N:
            L=self.N
        elif self.N<=L:
            L=self.N*2
        elif L<0:
            L+=self.N*2
        else:
            L+=self.N
        if R==None or self.N<=R:
            R=self.N*2
        elif R<-self.N:
            R=self.N
        elif R<0:
            R+=self.N*2
        else:
            R+=self.N
        self.Propagate_Above(L//(L&-L))
        self.Propagate_Above(R//(R&-R)-1)
        vL=self.e
        vR=self.e
        while L<R:
            if L&1:
                vL=self.f(vL,self.Operate_At(L))
                L+=1
            if R&1:
                R-=1
                vR=self.f(self.Operate_At(R),vR)
            L>>=1
            R>>=1
        return self.f(vL,vR)

    def Fold_Index(self,L=None,R=None):
        if L==None or L<-self.N:
            L=self.N
        elif self.N<=L:
            L=self.N*2
        elif L<0:
            L+=self.N*2
        else:
            L+=self.N
        if R==None or self.N<=R:
            R=self.N*2
        elif R<-self.N:
            R=self.N
        elif R<0:
            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 Operate_Range(self,a,L=None,R=None):
        if L==None or L<-self.N:
            L=self.N
        elif self.N<=L:
            L=self.N*2
        elif L<0:
            L+=self.N*2
        else:
            L+=self.N
        if R==None or self.N<=R:
            R=self.N*2
        elif R<-self.N:
            R=self.N
        elif R<0:
            R+=self.N*2
        else:
            R+=self.N
        L0=L//(L&-L)
        R0=R//(R&-R)-1
        self.Propagate_Above(L0)
        self.Propagate_Above(R0)
        while L<R:
            if L&1:
                self.segment_tree_act[L]=self.f_act(self.segment_tree_act[L],a)
                L+=1
            if R&1:
                R-=1
                self.segment_tree_act[R]=self.f_act(self.segment_tree_act[R],a)
            L>>=1
            R>>=1
        self.Recalculate_Above(L0)
        self.Recalculate_Above(R0)

    def Update(self):
        for i in range(1,self.N):
            self.Propagate_At(i)
        for i in range(self.N,self.N*2):
            self.segment_tree[i]=self.Operate_At(i)
            self.segment_tree_act[i]=self.e_act
        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])

N=int(readline())
def f(tpl0,tpl1):
    x0,y0,z0=tpl0
    x1,y1,z1=tpl1
    return (x0+x1,y0+y1,z0+z1)
e=(0,0,1)
def f_act(a,b):
    return a+b
e_act=0
def operate(tpl,a):
    x,y,z=tpl
    return (x+z*a,y+2*x*a+z*a**2,z)

LST=Lazy_Segment_Tree(N,f,e,f_act,e_act,operate)
A=list(map(int,readline().split()))
for i,a in enumerate(A):
    LST.Operate_Range(a,i,i+1)
Q=int(readline())
for _ in range(Q):
    query=tuple(map(int,readline().split()))
    if query[0]==1:
        _,l,r,x=query
        l-=1;r-=1
        LST.Operate_Range(x,l,r+1)
    else:
        _,l,r=query
        l-=1;r-=1
        ans=LST.Fold(l,r+1)[1]
        print(ans)
0