local mfl, mce = math.floor, math.ceil local mmi, mma = math.min, math.max local bls, brs = bit.lshift, bit.rshift local mod = 1000000007 local function bmul(x, y) local x1, y1 = mfl(x / 31623), mfl(y / 31623) local x0, y0 = x - x1 * 31623, y - y1 * 31623 return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod end local function badd(x, y) return (x + y) % mod end local function bsub(x, y) return x < y and x - y + mod or x - y end local SegTree = {} SegTree.updateAll = function(self) for i = self.stagenum - 1, 1, -1 do local cnt = bls(1, i - 1) for j = 1, cnt do self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2]) end end end SegTree.create = function(self, n, func, emptyvalue) self.func, self.emptyvalue = func, emptyvalue local stagenum, mul = 1, 1 self.stage = {{}} while mul < n do mul, stagenum = mul * 2, stagenum + 1 self.stage[stagenum] = {} end self.stagenum = stagenum self.left_stage = {} for i = 1, n do local sp, sz = 1, bls(1, stagenum - 1) 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 for i = 1, mul do self.stage[stagenum][i] = emptyvalue end self:updateAll() end SegTree.getRange = function(self, left, right) if left == right then return self.stage[self.stagenum][left] end local stagenum = self.stagenum local ret = self.emptyvalue while left <= right do local stage = mma(self.left_stage[left], self.sz_stage[right - left + 1]) local sz = bls(1, stagenum - stage) ret = self.func(ret, self.stage[stage][1 + brs(left - 1, stagenum - stage)]) left = left + sz end return ret end SegTree.update = function(self, idx) for i = self.stagenum - 1, 1, -1 do local dst = brs(idx + 1, 1) local rem = dst * 4 - 1 - idx self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem]) idx = dst end end SegTree.setValue = function(self, idx, value, silent) self.stage[self.stagenum][idx] = value if not silent then self:update(idx) end end SegTree.left_bound = function(self, val, left, right) local ret, retpos = self.emptyvalue, right + 1 local stage, l, r = 1, left, right local stagenum = self.stagenum while true do local sz = bls(1, stagenum - stage) while r % sz ~= 0 or r + 1 - l < sz do stage = stage + 1 sz = bls(1, stagenum - stage) end local tmp = self.func(ret, self.stage[stage][mfl(r / sz)]) if tmp < val then ret, retpos = tmp, r - sz + 1 if l + sz <= r then stage, l, r = 1, l, r - sz else break end else if sz ~= 1 then stage, l, r = stage + 1, r - sz + 2, r else break end end end return retpos - 1 end SegTree.right_bound = function(self, val, left, right) local ret, retpos = self.emptyvalue, left - 1 local l, r = left, right local stage = mma(self.left_stage[left], self.sz_stage[right - left + 1]) local stagenum = self.stagenum while true do local sz = bls(1, stagenum - stage) local tmp = self.func(ret, self.stage[stage][mce(l / sz)]) if tmp < val then ret, retpos = tmp, l + sz - 1 if retpos == right then break end if l + sz <= r then l = l + sz stage = mma(self.left_stage[l], self.sz_stage[r - l + 1]) else break end else if sz ~= 1 then stage, r = stage + 1, l + sz - 2 else break end end end return retpos + 1 end SegTree.new = function(n, func, emptyvalue) local obj = {} setmetatable(obj, {__index = SegTree}) obj:create(n, func, emptyvalue) return obj end local nn = io.read("*n", "*l") local n = 1 + mfl(nn / 2) local stmul = SegTree.new(n, bmul, 1) local s = io.read() for i = 1, n do local a = s:sub(4 * i - 3, 4 * i - 3) a = tonumber(a) stmul:setValue(i, a, true) end stmul:updateAll() local stsum = SegTree.new(n, badd, 0) local stflag = SegTree.new(n + 1, mma, 0) stflag:setValue(n + 1, 1) local curleft = 1 for i = 2, n do local b = s:sub(i * 4 - 5, i * 4 - 5) == "+" if b then local v = stmul:getRange(curleft, i - 1) stsum:setValue(curleft, v) stflag:setValue(curleft, 1) curleft = i end end do local v = stmul:getRange(curleft, n) stsum:setValue(curleft, v) stflag:setValue(curleft, 1) end local function setValue(pos, val) stmul:setValue(pos, val) local left = stflag:left_bound(1, 1, pos) local right = stflag:right_bound(1, pos + 1, n + 1) - 1 stsum:setValue(left, stmul:getRange(left, right)) end local function change(x) local curflag = stflag:getRange(x, x) local left = stflag:left_bound(1, 1, x - 1) local right = stflag:right_bound(1, x + 1, n + 1) - 1 if curflag == 0 then stsum:setValue(left, stmul:getRange(left, x - 1)) stsum:setValue(x, stmul:getRange(x, right)) stflag:setValue(x, 1) else stsum:setValue(left, stmul:getRange(left, right)) stsum:setValue(x, 0) stflag:setValue(x, 0) end end local q = io.read("*n", "*l") for iq = 1, q do local tp, x, y = io.read():match("(%g) (%d+) (%d+)") x = tonumber(x) y = tonumber(y) if tp == "!" then if x % 2 == 1 then x = mfl(x / 2) + 1 y = mfl(y / 2) + 1 local xv = stmul:getRange(x, x) local yv = stmul:getRange(y, y) setValue(x, yv) setValue(y, xv) else x = mfl(x / 2) y = mfl(y / 2) x = x + 1 y = y + 1 if stflag:getRange(x, x) ~= stflag:getRange(y, y) then change(x) change(y) end end else x = mfl(x / 2) + 1 y = mfl(y / 2) + 1 local xright = stflag:right_bound(1, x + 1, n + 1) - 1 if y <= xright then print(stmul:getRange(x, y)) else local v = stmul:getRange(x, xright) local yleft = stflag:left_bound(1, 1, y) v = badd(v, stmul:getRange(yleft, y)) if xright + 1 <= yleft - 1 then v = badd(v, stsum:getRange(xright + 1, yleft - 1)) end print(v) end end end