結果
問題 | No.1036 Make One With GCD 2 |
ユーザー |
👑 |
提出日時 | 2020-04-25 10:50:19 |
言語 | Lua (LuaJit 2.1.1734355927) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 2,703 bytes |
コンパイル時間 | 247 ms |
コンパイル使用メモリ | 6,820 KB |
実行使用メモリ | 84,828 KB |
最終ジャッジ日時 | 2024-11-07 04:08:34 |
合計ジャッジ時間 | 34,534 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 40 TLE * 1 |
ソースコード
local mfl, mce = math.floor, math.ceillocal mmi, mma = math.min, math.maxlocal bls, brs = bit.lshift, bit.rshiftlocal ffi = require("ffi")local C = ffi.Clocal function getgcd(x, y)while 0LL < x dox, y = y % x, xendreturn yendlocal SegTree = {}SegTree.updateAll = function(self)for i = self.stagenum - 1, 1, -1 dolocal cnt = bls(1, i - 1)local child = self.stage[i + 1]local parent = self.stage[i]for j = 1, cnt doparent[j] = self.func(child[j * 2 - 1], child[j * 2])endendendSegTree.create = function(self, n, func, emptyvalue)self.func, self.emptyvalue = func, emptyvaluelocal stagenum, mul = 1, 1self.stage = {}self.stage[1] = ffi.new("int64_t[?]", 2)while mul < n domul, stagenum = mul * 2, stagenum + 1self.stage[stagenum] = ffi.new("int64_t[?]", mul + 1)endself.stagenum = stagenum-- for i = 1, mul do self.stage[stagenum][i] = emptyvalue endself.spos = {}for i = 1, n dolocal sp, sz = 1, bls(1, stagenum - 1)while(i - 1) % sz ~= 0 dosp = sp + 1sz = brs(sz, 1)endself.spos[i] = spendself.sz_stage = {}local tmp, sp = 1, stagenumfor i = 1, n doif tmp * 2 == i then tmp, sp = tmp * 2, sp - 1 endself.sz_stage[i] = spendendSegTree.setSilent = function(self, idx, value)self.stage[self.stagenum][idx] = valueendSegTree.right_bound = function(self, left, right)local ret, retpos = self.emptyvalue, left - 1local l, r = left, rightlocal stage = mma(self.spos[left], self.sz_stage[right - left + 1])local stagenum = self.stagenumwhile true dolocal sz = bls(1, stagenum - stage)local tmp = self.func(ret, self.stage[stage][mce(l / sz)])if 1LL < tmp thenret, retpos = tmp, l + sz - 1if retpos == right then break endif l + sz <= r thenl, r = l + sz, rstage = mma(self.spos[l], self.sz_stage[r - l + 1])else break endelseif sz ~= 1 then stage, l, r = stage + 1, l, l + sz - 2else break endendendreturn retpos + 1endSegTree.new = function(n, func, emptyvalue)local obj = {}setmetatable(obj, {__index = SegTree})obj:create(n, func, emptyvalue)return objendlocal n = io.read("*n", "*l")local st = SegTree.new(n, getgcd, 0LL)ffi.cdef[[long long atoll(const char*);]]local s = io.read()dolocal i = 1for w in s:gmatch("%d+") dost.stage[st.stagenum][i] = C.atoll(w)i = i + 1endend-- print(os.clock())st:updateAll()-- print(os.clock())local ret = 0local z = nfor i = n, 1, -1 dolocal p = st:right_bound(i, z)ret = ret + n + 1 - pz = mmi(p, n)endprint(ret)-- print(os.clock())