This commit is contained in:
subcrip 2024-05-22 11:50:35 +08:00
parent 6478be4f2d
commit 2fd9869a11
Signed by: subcrip
SSH Key Fingerprint: SHA256:dFPFi68d8C87YkFkEBU4TkcrYRySWpekRR1hbnDWUCw
7 changed files with 207 additions and 1 deletions

View File

@ -22,6 +22,7 @@
"eslint.nvim": { "branch": "main", "commit": "158f2289907d377018752fcfe0d61b746dc11767" }, "eslint.nvim": { "branch": "main", "commit": "158f2289907d377018752fcfe0d61b746dc11767" },
"feline.nvim": { "branch": "master", "commit": "3587f57480b88e8009df7b36dc84e9c7ff8f2c49" }, "feline.nvim": { "branch": "master", "commit": "3587f57480b88e8009df7b36dc84e9c7ff8f2c49" },
"fidget.nvim": { "branch": "main", "commit": "0ba1e16d07627532b6cae915cc992ecac249fb97" }, "fidget.nvim": { "branch": "main", "commit": "0ba1e16d07627532b6cae915cc992ecac249fb97" },
"floating-input.nvim": { "branch": "main", "commit": "8480827466a51d7baac56ddec4ccfb355fcef43a" },
"fzf-lua": { "branch": "main", "commit": "53698d7ff093592228d14a509933ce403b3b8f9f" }, "fzf-lua": { "branch": "main", "commit": "53698d7ff093592228d14a509933ce403b3b8f9f" },
"git-conflict.nvim": { "branch": "main", "commit": "4c8e252b87d54d944c1e56bfb477f78b6fdaf661" }, "git-conflict.nvim": { "branch": "main", "commit": "4c8e252b87d54d944c1e56bfb477f78b6fdaf661" },
"gitsigns.nvim": { "branch": "main", "commit": "805610a9393fa231f2c2b49cb521bfa413fadb3d" }, "gitsigns.nvim": { "branch": "main", "commit": "805610a9393fa231f2c2b49cb521bfa413fadb3d" },

View File

@ -618,6 +618,27 @@ vim.keymap.set('n', '<leader>r', '<Cmd>TroubleToggle lsp_references<CR>');
vim.keymap.set('n', '<A-t>', '<Cmd>BufferPick<CR>', {noremap=true}); vim.keymap.set('n', '<A-t>', '<Cmd>BufferPick<CR>', {noremap=true});
vim.keymap.set('n', '<C-g>', '<Cmd>Neogit kind=split_above<CR>', {noremap=true}); vim.keymap.set('n', '<C-g>', '<Cmd>Neogit kind=split_above<CR>', {noremap=true});
vim.keymap.set({'v', 'x'}, '<leader>cc', '<Cmd>CodeSnap<CR>', {noremap=true}); vim.keymap.set({'v', 'x'}, '<leader>cc', '<Cmd>CodeSnap<CR>', {noremap=true});
vim.keymap.set('n', '<C-s>', '<Cmd>PopupSaveas<CR>', {noremap=true});
vim.api.nvim_create_user_command('PopupSaveas', function()
vim.ui.input({ prompt = 'Save As: ' }, function(input)
if input ~= nil then
if vim.fn.filereadable(input) == 1 then
local choice = vim.fn.input('File exists. Overwrite? ([y]/n): ')
if choice ~= 'n' then
vim.cmd('saveas! ' .. input)
print('File overwritten: ' .. input)
else
print('Cancelled')
end
else
vim.cmd('saveas ' .. input)
print('File saved as: ' .. input)
end
else
print('Save As cancelled')
end
end)end, {})
require("nvim-treesitter.configs").setup { require("nvim-treesitter.configs").setup {
incremental_selection = { incremental_selection = {

View File

@ -0,0 +1,31 @@
return [=[
vector<int> get_phi(int n) {
vector<bool> not_prime(n + 1);
vector<int> res;
vector<int> phi(n + 1);
phi[1] = 1;
for (int i = 2; i <= n; ++i) {
if (not not_prime[i]) {
res.emplace_back(i);
phi[i] = i - 1;
}
for (auto&& x : res) {
if (i * x > n) break;
not_prime[i * x] = 1;
if (i % x == 0) {
// phi(n) = n * prod((p - 1) / p)
// => phi(i * x) = i * x * prod((p - 1) / p) = (i * prod((p - 1) / p)) * x = phi(i) * x,
// since `i` covers all factors of i * x
phi[i * x] = phi[i] * x;
break;
} else {
// i coprimes x
// phi(i * x) = phi(i) * phi(x) = phi(i) * (x - 1)
phi[i * x] = phi[i] * (x - 1);
}
}
}
return phi;
}
]=]

99
nvim/lua/snippets/hld.lua Normal file
View File

@ -0,0 +1,99 @@
return [=[
template <typename Info, typename Tag>
struct HLD {
struct node_info {
int father, depth, hson, size, head, dfn = -1;
};
int n;
vector<int> seq;
vector<node_info> info;
segtree<Info, Tag> tr;
// returns: (dfs sequence, node info)
// node numbering starts from `1`
// if `dfn(v) == -1`, then node `v` is never accessed.
HLD(const vector<vector<int>>& ch, const vector<Info>& init, int root = 0) : n(ch.size() - 1), seq(), info(n + 1), tr(n + 1) {
vector<node_info> res(n + 1);
auto dfs1 = [&] (auto dfs1, int v, int pa) -> void {
res[v].father = pa;
res[v].depth = res[pa].depth + 1;
res[v].size = 1;
int mx = 0;
for (auto&& u : ch[v]) {
if (u == pa) continue;
dfs1(dfs1, u, v);
res[v].size += res[u].size;
if (res[u].size > mx) {
mx = res[u].size;
res[v].hson = u;
}
}
};
dfs1(dfs1, root, root);
int tm = 0;
auto dfs2 = [&] (auto dfs2, int v, int head) -> void {
res[v].dfn = tm++;
seq.emplace_back(v);
res[v].head = head;
if (not res[v].hson) return;
dfs2(dfs2, res[v].hson, head);
for (auto&& u : ch[v]) {
if (u == res[v].father or u == res[v].hson) continue;
dfs2(dfs2, u, u);
}
};
dfs2(dfs2, root, root);
info = res;
for (int i = 1; i <= n; ++i) {
tr.set(info[i].dfn, init[i]);
}
}
void set(int v, const Info& t) {
tr.set(info[v].dfn, t);
}
void apply(int v, const Tag& t) {
tr.apply(info[v].dfn, t);
}
Info query(int v) {
return tr.query(info[v].dfn);
}
void path_apply(int u, int v, const Tag& t) {
while (info[u].head != info[v].head) {
if (info[info[u].head].depth < info[info[v].head].depth) {
swap(u, v);
}
tr.range_apply(info[info[u].head].dfn, info[u].dfn, t);
}
if (info[u].depth < info[v].depth) swap(u, v);
tr.range_apply(info[v].dfn, info[u].dfn);
}
Info path_query(int u, int v) {
Info res;
while (info[u].head != info[v].head) {
if (info[info[u].head].depth < info[info[v].head].depth) {
swap(u, v);
}
res = res + tr.range_query(info[info[u].head].dfn, info[u].dfn);
}
if (info[u].depth < info[v].depth) swap(u, v);
res = res + tr.range_query(info[v].dfn, info[u].dfn);
return res;
}
void subtree_apply(int v, const Tag& t) {
tr.range_apply(info[v].dfn, info[v].dfn + info[v].size - 1, t);
}
Info subtree_query(int v) {
return tr.range_query(info[v].dfn, info[v].dfn + info[v].size - 1);
}
};
]=]

View File

@ -239,3 +239,39 @@ ls.add_snippets(nil, {
}) })
} }
}) })
local soe = require('snippets.soe')
ls.add_snippets(nil, {
cpp = {
snip({
trig = 'soe',
namr = 'soe',
dscr = 'Sieve of Euler',
},{
text(lines(soe))
})
}
})
local get_phi = require('snippets.get-phi')
ls.add_snippets(nil, {
cpp = {
snip({
trig = 'get_phi',
namr = 'get_phi',
dscr = 'Sieve of Euler',
},{
text(lines(get_phi))
})
}
})
local hld = require('snippets.hld')
ls.add_snippets(nil, {
cpp = {
snip({
trig = 'hld',
namr = 'hld',
dscr = 'Heavy-light decomposition',
},{
text(lines(hld))
})
}
})

18
nvim/lua/snippets/soe.lua Normal file
View File

@ -0,0 +1,18 @@
return [=[
vector<int> soe(int n) {
vector<bool> not_prime(n + 1);
vector<int> res;
for (int i = 2; i <= n; ++i) {
if (not not_prime[i]) {
res.emplace_back(i);
}
for (auto&& x : res) {
if (i * x > n) break;
not_prime[i * x] = 1;
if (i % x == 0) break;
}
}
return res;
}
]=]

File diff suppressed because one or more lines are too long