local mfl, mce = math.floor, math.ceil local bls, brs = bit.lshift, bit.rshift local function mma(a, b) return a < b and b or a end local p2 = {1} for i = 2, 28 do p2[i] = p2[i - 1] * 2 end local LazyRangeSeg = {} LazyRangeSeg.updateAll = function(self) local lin, sq = self.lin, self.sq for i = self.stagenum - 1, 1, -1 do local cnt = p2[i] for j = 1, cnt do lin[i][j] = lin[i + 1][j * 2 - 1] + lin[i + 1][j * 2] sq[i][j] = sq[i + 1][j * 2 - 1] + sq[i + 1][j * 2] end end end LazyRangeSeg.create = function(self, n) local stagenum, mul = 1, 1 self.lin = {{0}} self.sq = {{0LL}} self.lazy = {{0}} while mul < n do mul, stagenum = mul * 2, stagenum + 1 self.lin[stagenum] = {} self.sq[stagenum] = {} self.lazy[stagenum] = {} for i = 1, mul do self.lin[stagenum][i] = 0 self.sq[stagenum][i] = 0LL self.lazy[stagenum][i] = 0 end end self.left_stage = {} for i = 1, n do local sp, sz = 1, p2[stagenum] while(i - 1) % sz ~= 0 do sp, sz = sp + 1, brs(sz, 1) end self.left_stage[i] = sp end self.sz_stage = {} local tmp, sp = 1, stagenum for i = 1, n do if tmp * 2 == i then tmp, sp = tmp * 2, sp - 1 end self.sz_stage[i] = sp end self.stagenum = stagenum local z = io.read() local i = 0 for a in z:gmatch("%-?%d+") do i = i + 1 a = tonumber(a) self.lin[stagenum][i] = a self.sq[stagenum][i] = 1LL * a * a end self:updateAll() end LazyRangeSeg.resolve = function(self, right) local stagenum = self.stagenum local offset = 0 local lazy = self.lazy for i = 1, stagenum - 1 do local p = offset + p2[stagenum - i + 1] if p < right then offset = p p = p + p2[stagenum - i + 1] end if right < p then local curidx = brs(p, stagenum - i) local lzval = lazy[i][curidx] if lzval ~= 0 then local sz = p2[stagenum - i] self:resolveRange(i + 1, curidx * 2 - 1, lzval, true) self:resolveRange(i + 1, curidx * 2, lzval, true) lazy[i + 1][curidx * 2 - 1] = lazy[i + 1][curidx * 2 - 1] + lzval lazy[i + 1][curidx * 2] = lazy[i + 1][curidx * 2] + lzval lazy[i][curidx] = 0 end elseif p == right then break else assert(false) end end end LazyRangeSeg.resolveRange = function(self, stagepos, idx, value, shallow) local linadd = p2[self.stagenum - stagepos + 1] * value local lin, sq = self.lin, self.sq sq[stagepos][idx] = sq[stagepos][idx] + 1LL * linadd * value + 2LL * lin[stagepos][idx] * value lin[stagepos][idx] = lin[stagepos][idx] + linadd if shallow then return end for i = stagepos - 1, 1, -1 do local dst = brs(idx + 1, 1) local rem = dst * 4 - 1 - idx lin[i][dst] = lin[i + 1][idx] + lin[i + 1][rem] sq[i][dst] = sq[i + 1][idx] + sq[i + 1][rem] idx = dst end end LazyRangeSeg.getRange = function(self, left, right) if 1 < left then self:resolve(left - 1) end self:resolve(right) local stagenum = self.stagenum local ret = 0LL local sq, ls, szs = self.sq, self.left_stage, self.sz_stage while left <= right do local stage = mma(ls[left], szs[right - left + 1]) ret = ret + sq[stage][1 + brs(left - 1, stagenum - stage)] left = left + p2[stagenum - stage + 1] end return ret end LazyRangeSeg.setRange = function(self, left, right, value) if 1 < left then self:resolve(left - 1) end self:resolve(right) local stagenum = self.stagenum local lazy, ls, szs = self.lazy, self.left_stage, self.sz_stage while left <= right do local stage = mma(ls[left], szs[right - left + 1]) local idx = 1 + brs(left - 1, stagenum - stage) self:resolveRange(stage, idx, value) lazy[stage][idx] = lazy[stage][idx] + value left = left + p2[stagenum - stage + 1] end end local n = io.read("*n", "*l") local lzst = LazyRangeSeg lzst:create(n) local q = io.read("*n") for iq = 1, q do local tp = io.read("*n") if tp == 1 then local l, r, x = io.read("*n", "*n", "*n") lzst:setRange(l, r, x) else local l, r = io.read("*n", "*n") local val = lzst:getRange(l, r) val = tostring(val):gsub("LL", "") print(val) end end -- print(os.clock())