Templates-ZackofZHOU
ZackofZHOU · · 个人记录
C++の常用模板
神秘头文件
#include<iostream>
using namespace std;
signed main()
{
return 0;
}
数据结构Data Structure
::::success[Segment Tree 线段树]
template <typename typ>
struct ST_Dat
{
int l,r;
typ size,lazy;
void change(typ k)
{
size += (r - l + 1) * k;
lazy += k;
}
ST_Dat (int L = 0,int R = 0,typ Size = 0,typ Lazy = 0)
{
l = L,r = R,size = Size,lazy = Lazy;
}
};
template <typename typ>
struct SegTree
{
ST_Dat <typ> Tree[N << 2];
void update(int p)
{
Tree[p].size = Tree[p << 1].size + Tree[p << 1 | 1].size;
}
void build(int left,int right,typ *arr,int p = 1)
{
Tree[p] = ST_Dat <typ> (left,right);
if(left == right)
{
Tree[p].size = arr[left];
return;
}
int mid = (left + right) >> 1;
build(left,mid,arr,p << 1);
build(mid + 1,right,arr,p << 1 | 1);
update(p);
}
void pushdown(int p)
{
if(Tree[p].lazy)
{
Tree[p << 1].change(Tree[p].lazy);
Tree[p << 1 | 1].change(Tree[p].lazy);
Tree[p].lazy = 0;
}
}
void change(typ value,int left,int right,int p = 1)
{
if(left <= Tree[p].l && Tree[p].r <= right)
{
Tree[p].change(value);
return;
}
pushdown(p);
int mid = (Tree[p].l + Tree[p].r) >> 1;
if(left <= mid)
change(value,left,right,p << 1);
if(mid < right)
change(value,left,right,p << 1 | 1);
upd(p);
}
typ query(int left,int right,int p = 1)
{
if(left <= Tree[p].l && Tree[p].r <= right)
return Tree[p].size;
pushdown(p);
int mid = (Tree[p].l + Tree[p].r) >> 1;
typ ans = 0;
if(left <= mid)
ans += query(left,right,p << 1);
if(mid < right)
ans += query(left,right,p << 1 | 1);
return ans;
}
};
:::: ::::success[BIT 树状数组] 这个东西名字贼多,有 Binary Index Tree ( 简称BIT,也称作01字典树 ),Frenwick Tree 等,但核心是一样的。
int __lowbit_for_BIT__(int x)
{
return x & -x;
}
template <typename typ>
struct BinIndTree
{
int n;
typ Tree[N];
void change(typ value,int left,bool only_give_one = false)
{
int tmp = left + 1;
for(;left <= n;left += __lowbit_for_BIT__(left))
Tree[left] += value;
if(!only_give_one)
return;
for(;tmp <= n;tmp += __lowbit_for_BIT__(tmp))
Tree[tmp] -= value;
}
void build(typ *arr,int left,int right,bool only_give_one = false)
{
n = right - left + 1;
for(int i = left;i <= right;i++)
change(arr[i],i - left + 1,only_give_one);
}
typ query(int right)
{
typ sum = 0;
for(;right;right -= __lowbit_for_BIT__(right))
sum += Tree[right];
return sum;
}
};
:::: ::::success[Hash Table 哈希表] 开放选址法(也称作散列表的线性探查法)哈希表
const int HASHMAX = 1e7 + 10;
template <typename KEY,typename VALUE>
struct HashTable
{
VALUE val[HASHMAX];
KEY id[HASHMAX];
bool vis[HASHMAX];
Hash ()
{
memset(val,0,sizeof(val));
memset(id,0,sizeof(id));
memset(vis,0,sizeof(vis));
}
VALUE& operator [] (KEY ky)
{
int t = ky % HASHMAX;
while(vis[t])
{
if(id[t] == ky)
return val[t];
t++;
if(t >= HASHMAX)
t = 0;
}
return val[t];
}
bool find(KEY ky)
{
int t = ky % HASHMAX;
while(vis[t])
{
if(id[t] == ky)
return true;
t++;
if(t >= HASHMAX)
t = 0;
}
return false;
}
void ins(KEY ky,VALUE va)
{
int t = ky % HASHMAX;
while(vis[t])
{
if(id[t] == ky)
break;
t++;
if(t >= HASHMAX)
t = 0;
}
id[t] = ky,val[t] = va;
vis[t] = true;
}
};
:::: ::::success[BST 二叉搜索树]
template <typename typ>
struct BSTDat
{
int l,r,siz,num,cnt,fa;
BSTDat (int _l = 0,int _r = 0)
{
l = _l,r = _r;
siz = num = cnt = 0;
}
void build(int x)
{
num = x;
cnt = siz = 1;
}
};
int BST_ROOT__ = 1;
template <typename typ>
struct BST
{
BSTDat <typ> tre[N];
int len;
BST ()
{
len = 1;
}
void upd(int p)
{
if(!p)
return;
tre[p].siz = tre[tre[p].l].siz + tre[tre[p].r].siz + tre[p].cnt;
}
void ins(typ x,int p = BST_ROOT__)
{
if(!tre[p].cnt)
{
tre[p].build(x);
return;
}
if(x < tre[p].num)
{
if(!tre[p].l)
{
tre[p].l = ++len;
tre[len].fa = p;
}
ins(x,tre[p].l);
}
else if(x > tre[p].num)
{
if(!tre[p].r)
{
tre[p].r = ++len;
tre[len].fa = p;
}
ins(x,tre[p].r);
}
else
tre[p].cnt++;
upd(p);
}
int queryrnk(typ x,int p = BST_ROOT__)
{
if(!tre[p].cnt)
return 0;
if(x == tre[p].num)
return tre[tre[p].l].siz;
else if(x < tre[p].num)
return queryrnk(x,tre[p].l);
return queryrnk(x,tre[p].r) + tre[p].cnt + tre[tre[p].l].siz;
}
int queryrnknum(int rnk,int p = BST_ROOT__)
{
if(!tre[p].cnt)
return 0;
if(tre[tre[p].l].siz >= rnk)
return queryrnknum(rnk,tre[p].l);
else if(tre[tre[p].l].siz + tre[p].cnt >= rnk)
return tre[p].num;
return queryrnknum(rnk - tre[tre[p].l].siz - tre[p].cnt,tre[p].r);
}
int querypre(typ x,int p = BST_ROOT__,int ans = -inf)
{
if(!tre[p].cnt)
return ans;
if(tre[p].num >= x)
return tre[p].l ? querypre(x,tre[p].l,ans) : ans;
if(!tre[p].r)
return tre[p].num;
return querypre(x,tre[p].r,tre[p].num);
}
int querynxt(typ x,int p = BST_ROOT__,int ans = inf)
{
if(!tre[p].cnt)
return ans;
if(tre[p].num <= x)
return tre[p].r ? querynxt(x,tre[p].r,ans) : ans;
if(!tre[p].l)
return tre[p].num;
return querynxt(x,tre[p].l,tre[p].num);
}
}t;
:::: ::::success[DSU 启发式合并的可删除并查集]
struct DSU
{
int tot,f[(N << 1) + Q],siz[(N << 1) + Q];
void clr(int MAX = ((N - 10) << 1) + Q)
{
tot = N - 10;
for(int i = 1;i <= tot;i++)
{
f[i] = i + tot;
siz[i] = 1;
}
for(int i = tot;i <= (tot << 1);i++)
{
f[i] = i;
siz[i] = 1;
}
}
DSU (int MAX = ((N - 10) << 1) + Q)
{
clr(MAX);
}
int fa(int p)
{
return p == f[p] ? p : f[p] = fa(f[p]);
}
bool uni(int p1,int p2)
{
int f1 = fa(p1),f2 = fa(p2);
if(f1 == f2)
return false;
if(siz[f1] > siz[f2])
swap(f1,f2);
f[f1] = f2;
siz[f2] += siz[f1];
return true;
}
bool isuni(int p1,int p2)
{
return fa(p1) == fa(p2);
}
void del(int p)
{
int f1 = fa(p);
siz[f1]--;
f[p] = ++tot;
}
};
:::: ::::success[可撤销并查集] 通过按秩合并进行并查集合并,因为路径压缩会改变树的形态,不便于撤销。
struct DSU
{
int tot,stl,st[N],f[N],siz[N];
DSU (int len = 0)
{
tot = len;
stl = 0;
}
void ini(int len)
{
for(int i = 1;i <= tot;i++)
{
f[i] = i;
siz[i] = 1;
}
}
int fa(int p)
{
return p == f[p] ? p : fa(fa[p]);
}
bool uni(int p1,int p2)
{
p1 = fa(p1),p2 = fa(p2);
if(p1 == p2)
return false;
if(siz[p1] < siz[p2])
swap(p1,p2);
st[++stl] = p2;
f[p2] = p1,siz[p1] += siz[p2];
return true;
}
void back(int len)
{
if(stl < len)
len = stl;
while(len--)
{
siz[f[st[stl]]] -= siz[st[stl]];
f[st[stl]] = st[stl];
stl--;
}
}
};
:::: ::::success[平衡树] 提示:写平衡树的时候,建议有旋转操作的使用指针数据完成,或者合理运用取地址符。 :::success[ScapTree 替罪羊树] 常数还行,比较好写
const double ScapAlpha = 0.75;
int root;
struct ScapTree
{
int tot,tot_ac,len,arr[N],siz[N],ls[N],rs[N],val[N],cnt[N],scap[N];
ScapTree ()
{
tot = tot_ac = 0;
}
void upd(int p)
{
siz[p] = cnt[p] + siz[ls[p]] + siz[rs[p]];
scap[p] = 1 + scap[ls[p]] + scap[rs[p]];
}
void flatten(int p)
{
if(!p)
return;
flatten(ls[p]);
arr[++len] = p;
flatten(rs[p]);
}
int build(int l,int r)
{
if(l > r)
return 0;
int mid = (l + r) >> 1,p = arr[mid];
ls[p] = build(l,mid - 1),rs[p] = build(mid + 1,r);
upd(p);
return p;
}
void rebuild(int& p)
{
len = 0;
flatten(p);
p = build(1,len);
}
bool ins(int k,int &p = root,int dep = 0)
{
bool flag = false;
if(!p)
{
p = ++tot;
val[p] = k;
flag = dep > (1.0 * log(scap[p] + 1) / log(1.0 / ScapAlpha));
}
if(val[p] == k)
{
if(!cnt[p])
tot_ac++;
cnt[p]++;
}
else if(k < val[p])
flag = ins(k,ls[p],dep + 1);
else
flag = ins(k,rs[p],dep + 1);
upd(p);
if(flag && ((1.0 * scap[ls[p]] > ScapAlpha * scap[p]) || (1.0 * scap[rs[p]] > ScapAlpha * scap[p])))
{
rebuild(p);
return false;
}
return flag;
}
bool rem(int k,int p = root)
{
if(!p)
return false;
bool flag = true;
if(k < val[p])
flag = rem(k,ls[p]);
else if(k > val[p])
flag = rem(k,rs[p]);
else if(cnt[p])
{
cnt[p]--;
if(!cnt[p])
tot_ac--;
}
else
flag = false;
upd(p);
return flag;
}
void del(int k)
{
bool flag = rem(k);
if(!tot_ac)
root = 0;
else if(flag && tot_ac < scap[root] * ScapAlpha)
rebuild(root);
}
int queryrnk(int k,int p = root)
{
int res = 0;
while(p && val[p] != k)
{
if(k < val[p])
p = ls[p];
else
{
res += siz[ls[p]] + cnt[p];
p = rs[p];
}
}
return res + siz[ls[p]] + 1;
}
int queryrnknum(int rnk,int p = root)
{
if(rnk < 1 || rnk > siz[p])
return -1;
while(true)
{
if(rnk <= siz[ls[p]])
p = ls[p];
else if(rnk <= siz[ls[p]] + cnt[p])
return val[p];
else
{
rnk -= siz[ls[p]] + cnt[p];
p = rs[p];
}
}
return -1;
}
int querypre(int k)
{
return queryrnknum(queryrnk(k) - 1);
}
int querynxt(int k)
{
return queryrnknum(queryrnk(k + 1));
}
};
::: :::success[带旋Treap] 指针Treap, 查询使用迭代优化
typedef long long ll;
const ll inf = 0x3f3f3f3f3f3f3f3f;
mt19937 rd(time(0));
struct TreapDat
{
struct TreapDat *ch[2];
int cnt,rnk,siz;
ll val;
TreapDat (ll Val = 0)
{
val = Val;
siz = cnt = 1;
rnk = rd();
ch[0] = ch[1] = nullptr;
}
void upd()
{
siz = cnt;
if(ch[0] != nullptr)
siz += (ch[0] -> siz);
if(ch[1] != nullptr)
siz += (ch[1] -> siz);
}
};
struct Treap
{
TreapDat* root;
Treap ()
{
root = nullptr;
}
void Rotate(TreapDat* &p,int face)
{
TreapDat* tmp = p -> ch[face];
p -> ch[face] = tmp -> ch[!face];
tmp -> ch[!face] = p;
p -> upd(),tmp -> upd();
p = tmp;
}
void ins(ll Val,TreapDat* &p)
{
if(p == nullptr)
{
p = new TreapDat (Val);
return;
}
if(Val < (p -> val))
{
ins(Val,p -> ch[0]);
if((p -> ch[0] -> rnk) < (p -> rnk))
Rotate(p,0);
}
else if(Val > (p -> val))
{
ins(Val,p -> ch[1]);
if((p -> ch[1] -> rnk) < (p -> rnk))
Rotate(p,1);
}
else
p -> cnt++;
p -> upd();
}
void del(ll Val,TreapDat* &p)
{
if(p == nullptr)
return;
if(Val < (p -> val))
del(Val,p -> ch[0]);
else if(Val > (p -> val))
del(Val,p -> ch[1]);
else
{
if(p -> cnt > 1)
{
p -> cnt--,p -> upd();
return;
}
bool f1 = (p -> ch[0] != nullptr),f2 = (p -> ch[1] != nullptr);
if(!f1)
{
if(!f2)
{
delete p;
p = nullptr;
}
else
{
TreapDat* tmp = (p -> ch[1]);
delete p;
p = tmp;
}
}
else
{
if(!f2)
{
TreapDat* tmp = (p -> ch[0]);
delete p;
p = tmp;
}
else
{
int tmp = ((p -> ch[0] -> rnk) < (p -> ch[1] -> rnk)) ? 0 : 1;
Rotate(p,tmp);
del(Val,p -> ch[!tmp]);
p -> upd();
}
}
return;
}
if(p != nullptr)
p -> upd();
}
int queryrnk(ll Val,TreapDat* p)
{
int res = 1;
while(p != nullptr)
{
if(Val <= p -> val)
p = p -> ch[0];
else
{
int lsiz = (p -> ch[0] != nullptr ? p -> ch[0] -> siz : 0);
res += lsiz + (p -> cnt);
p = p->ch[1];
}
}
return res;
}
ll queryrnknum(int rnk,TreapDat* p)
{
while(p != nullptr)
{
int lsiz = (p -> ch[0] != nullptr ? p -> ch[0] -> siz : 0);
if(rnk <= lsiz)
p = p -> ch[0];
else if(rnk <= lsiz + (p -> cnt))
return p -> val;
else
{
rnk -= lsiz + p->cnt;
p = p -> ch[1];
}
}
return inf;
}
ll querypre(ll Val,TreapDat* p)
{
ll res = -inf;
while(p != nullptr)
{
if(p -> val < Val)
{
res = p -> val;
p = p -> ch[1];
}
else
p = p->ch[0];
}
return res;
}
ll querynxt(ll val,TreapDat* p)
{
ll res = inf;
while(p != nullptr)
{
if (p -> val > val)
{
res = p -> val;
p = p -> ch[0];
}
else
p = p -> ch[1];
}
return res;
}
};
:::
:::success[FHQTreap 范浩强Treap]
重点就在 split , split_rank 和 merge 三大函数。
mt19937 rd(time(0));
#define tdat TreapDat*
#define pTT pair <tdat,tdat>
#define tTTT tuple <tdat,tdat,tdat>
struct TreapDat
{
int val,siz,cnt,rnk;
TreapDat* ch[2];
TreapDat ()
{
val = 0;
siz = cnt = 1;
rnk = rd();
ch[0] = ch[1] = nullptr;
}
void upd()
{
siz = cnt;
if(ch[0] != nullptr)
siz += (ch[0] -> siz);
if(ch[1] != nullptr)
siz += (ch[1] -> siz);
}
};
struct FHQTreap
{
tdat root;
FHQTreap ()
{
root = nullptr;
}
pTT split(tdat p,int k)
{
if(p == nullptr)
return make_pair(nullptr,nullptr);
if((p -> val) <= k)
{
pTT tmp = split((p -> ch[1]),k);
p -> ch[1] = tmp.first;
p -> upd();
return make_pair(p,tmp.second);
}
else
{
pTT tmp = split((p -> ch[0]),k);
p -> ch[0] = tmp.second;
p -> upd();
return make_pair(tmp.first,p);
}
}
tTTT split_rank(tdat p,int rnk)
{
if(p == nullptr)
return make_tuple(nullptr,nullptr,nullptr);
int lsiz = ((p -> ch[0]) == nullptr ? 0 : (p -> ch[0] -> siz));
tdat l; tdat mid; tdat r;
if(rnk <= lsiz)
{
tTTT tmp = split_rank((p -> ch[0]),rnk);
tie(l,mid,r) = tmp;
p -> ch[0] = r;
p -> upd();
return make_tuple(l,mid,p);
}
else if(rnk <= lsiz + (p -> cnt))
{
l = p -> ch[0],r = p -> ch[1];
p -> ch[0] = nullptr,p -> ch[1] = nullptr;
return make_tuple(l,p,r);
}
else
{
tTTT tmp = split_rank((p -> ch[1]),rnk - lsiz - (p -> cnt));
tie(l,mid,r) = tmp;
p -> ch[1] = l;
p -> upd();
return make_tuple(p,mid,r);
}
}
tdat merge(tdat l,tdat r)
{
if(l == nullptr && r == nullptr)
return nullptr;
if(l == nullptr && r != nullptr)
return r;
if(l != nullptr && r == nullptr)
return l;
if((l -> rnk) < (r -> rnk))
{
l -> ch[1] = merge(l -> ch[1],r);
l -> upd();
return l;
}
else
{
r -> ch[0] = merge(l,r -> ch[0]);
r -> upd();
return r;
}
}
void ins(int k)
{
pTT tmp = split(root,k),ltmp = split(tmp.first,k - 1);
tdat tool = new TreapDat ();
if(ltmp.second == nullptr)
{
tool -> val = k;
ltmp.second = tool;
}
else
{
ltmp.second -> cnt++;
ltmp.second -> upd();
}
tdat lmer = merge(ltmp.first,ltmp.second);
root = merge(lmer,tmp.second);
}
void del(int k)
{
pTT tmp = split(root,k),ltmp = split(tmp.first,k - 1);
if((ltmp.second -> cnt) > 1)
{
ltmp.second -> cnt--;
ltmp.second -> upd();
ltmp.first = merge(ltmp.first,ltmp.second);
}
else
{
if(tmp.first == ltmp.second)
tmp.first = nullptr;
delete ltmp.second;
ltmp.second = nullptr;
}
root = merge(ltmp.first,tmp.second);
}
int Rank(int k)
{
pTT tmp = split(root,k - 1);
int res = ((tmp.first == nullptr) ? 0 : (tmp.first -> siz)) + 1;
root = merge(tmp.first,tmp.second);
return res;
}
int queryrnknum(tdat p,int rnk)
{
tdat l; tdat mid; tdat r;
tie(l,mid,r) = split_rank(p,rnk);
int res = (mid -> val);
root = merge(merge(l,mid),r);
return res;
}
int kth(int rnk)
{
return queryrnknum(root,rnk);
}
int pre(int k)
{
pTT tmp = split(root,k - 1);
int res = queryrnknum(tmp.first,(tmp.first -> siz));
root = merge(tmp.first,tmp.second);
return res;
}
int nxt(int k)
{
pTT tmp = split(root,k);
int res = queryrnknum(tmp.second,1);
root = merge(tmp.first,tmp.second);
return res;
}
};
::: :::success[AVLTree] 之前用数组写调试了 114514 年,于是一气之下直接用指针写了一个。
struct Dat
{
int val,cnt,siz,mxdep;
Dat* ch[2];
Dat (int _val = 0)
{
val = _val;
cnt = siz = mxdep = 1;
ch[0] = ch[1] = nullptr;
}
void upd()
{
siz = cnt;
mxdep = 1;
for(int i = 0;i < 2;i++)
{
if(ch[i] != nullptr)
{
siz += ch[i] -> siz;
mxdep = max(mxdep,ch[i] -> mxdep + 1);
}
}
}
int dep()
{
int dee[2];
for(int i = 0;i < 2;i++)
dee[i] = (ch[i] == nullptr ? 0 : ch[i] -> mxdep);
return dee[0] - dee[1];
}
};
struct AVLTree
{
Dat* root;
AVLTree ()
{
root = nullptr;
}
void rotate(Dat* &p,bool d)
{
Dat* son = p -> ch[d];
p -> ch[d] = son -> ch[!d];
son -> ch[!d] = p;
p -> upd(),son -> upd();
p = son;
}
void check(Dat* &p)
{
Dat* son;
int tmp,tmpp;
tmp = p -> dep();
if(tmp == 2)
{
son = p -> ch[0];
tmpp = son -> dep();
if(tmpp == 1)
rotate(p,0);
else if(tmpp == -1)
{
rotate(p -> ch[0],1);
rotate(p,0);
}
}
else if(tmp == -2)
{
son = p -> ch[1];
tmpp = son -> dep();
if(tmpp == 1)
{
rotate(p -> ch[1],0);
rotate(p,1);
}
else if(tmpp == -1)
rotate(p,1);
}
}
void insert(int k,Dat* &p)
{
if(p == nullptr)
{
p = new Dat(k);
return;
}
if(k < p -> val)
insert(k,p -> ch[0]);
else if(k > p -> val)
insert(k,p -> ch[1]);
else
{
p -> cnt++;
p -> siz++;
}
p -> upd();
check(p);
}
void remove(int k,Dat* &p)
{
if(k < p -> val)
remove(k,p -> ch[0]);
else if(k > p -> val)
remove(k,p -> ch[1]);
else
{
if(p -> cnt > 1)
{
p -> cnt--;
p -> siz--;
return;
}
bool f1 = (p -> ch[0] != nullptr),f2 = (p -> ch[1] != nullptr);
if(!f1)
{
if(!f2)
{
delete p;
p = nullptr;
}
else
{
Dat* tmp = (p -> ch[1]);
delete p;
p = tmp;
}
}
else
{
if(!f2)
{
Dat* tmp = (p -> ch[0]);
delete p;
p = tmp;
}
else
{
if(p -> ch[0] -> mxdep < p -> ch[1] -> mxdep)
{
rotate(p,1);
remove(k,p);
}
else
{
rotate(p,0);
remove(k,p);
}
}
}
return;
}
if(p != nullptr)
{
p -> upd();
check(p);
}
}
int queryrnk(int k,Dat* p)
{
int res = 0;
while(p != nullptr)
{
if(k <= p -> val)
p = p -> ch[0];
else
{
int lsiz = (p -> ch[0] != nullptr ? p -> ch[0] -> siz : 0);
res += lsiz + (p -> cnt);
p = p -> ch[1];
}
}
return res;
}
int queryrnknum(int rnk,Dat* p)
{
while(p != nullptr)
{
int lsiz = (p -> ch[0] != nullptr ? p -> ch[0] -> siz : 0);
if(rnk <= lsiz)
p = p -> ch[0];
else if(rnk <= lsiz + (p -> cnt))
return p -> val;
else
{
rnk -= lsiz + p->cnt;
p = p -> ch[1];
}
}
return (int)(1e9);
}
void ins(int k)
{
insert(k,root);
}
void del(int k)
{
remove(k,root);
}
int Rank(int k)
{
return queryrnk(k,root) + 1;
}
int kth(int k)
{
return queryrnknum(k,root);
}
int pre(int k)
{
return kth(Rank(k) - 1);
}
int nxt(int k)
{
return kth(Rank(k + 1));
}
};
| ::: |
|---|
| ::::success[矩阵加乘法及矩阵快速幂] |
| ```cpp |
| typedef unsigned long long ull; |
| const int N = 110,M = N; // 这里不能开太大,要不然memset会爆炸 |
| #define tem template <typename typ> |
| #define matt Matrix <typ> |
| tem struct Matrix |
| { |
| int n,m; |
| typ t[N][M]; |
| Matrix () |
| { |
| memset(t,0,sizeof(t)); |
| } |
| typ* operator [] (int p) |
| { |
| return t[p]; |
| } |
| void build(int len1,int len2 = 0) |
| { |
| if(len2 <= 0) |
| len2 = len1; |
| n = len1,m = len2; |
| } |
| void prt() |
| { |
| cout << "(" << n << ',' << m << "): \n"; |
| for(int i = 1;i <= n;i++) |
| for(int j = 1;j <= m;j++) |
| cout << t[i][j] << " \n"[j == m]; |
| } |
| }; |
| tem matt operator + (matt A,matt B) |
| { |
| if(A.n != B.n |
| return matt (); |
| for(int i = 1;i <= A.n;i++) |
| for(int j = 1;j <= A.m;j++) |
| A[i][j] += B[i][j]; |
| return A; |
| } |
| tem matt operator * (matt A,matt B) |
| { |
| matt res; |
| if(A.m != B.n) |
| return res; |
| res.build(max(A.n,B.m)); |
| res.n = A.n,res.m = B.m; |
| for(int i = 1;i <= A.n;i++) |
| for(int k = 1;k <= A.m;k++) |
| for(int j = 1;j <= B.m;j++) |
| res[i][j] += A[i][k] * B[k][j]; |
| return res; |
| } |
| tem matt operator += (matt &A,matt &B) |
| { |
| return A = A + B; |
| } |
| tem matt operator *= (matt &A,matt &B) |
| { |
| return A = A * B; |
| } |
| tem matt Mat_qp(matt A,ull B) |
| { |
| matt res = A; |
| if(!B) |
| return res; |
| B--; |
| while(B) |
| { |
| if(B & 1) |
| res *= A; |
| A *= A; |
| B >>= 1; |
| } |
| return res; |
| } |
| ``` |
| :::: |
| ::::error[高精度] |
| 还没写完... |
| ```cpp |
| #include<iostream> |
| using namespace std; |
| const int BigIntMaxLen = 2e4 + 10; |
| struct BigInt |
| { |
| short num[BigIntMaxLen]; |
| int len; |
| BigInt () |
| { |
| len = 0; |
| } |
| int size() |
| { |
| return len; |
| } |
| short& operator [] (int p) |
| { |
| return num[p]; |
| } |
| void prt() |
| { |
| for(int i = len;i > 0;i--) |
| cout << num[i]; |
| } |
| void flat() |
| { |
| for(int i = 1;i <= len;i++) |
| { |
| num[i + 1] += num[i] / 10; |
| num[i] %= 10; |
| } |
| if(num[len + 1]) |
| len++; |
| while(!num[len]) |
| len--; |
| } |
| }; |
| BigInt operator + (BigInt A,BigInt B) |
| { |
| BigInt ans; |
| ans.len = max(A.len,B.len); |
| for(int i = 1;i <= ans.len;i++) |
| ans[i] = A[i] + B[i]; |
| ans.flat(); |
| return ans; |
| } |
| BigInt operator += (BigInt &A,BigInt B) |
| { |
| return A = A + B; |
| } |
| ``` |
| :::: |
| ::::success[自动取模的Int] |
| ```cpp |
| #include<iostream> |
| using namespace std; |
| typedef long long ll; |
| #define tem template <typename typ> |
| #define mdit ModInter <typ> |
| void exgcd(ll a,ll b,ll &x,ll &y) |
| { |
| if(!b) |
| { |
| x = 1,y = 0; |
| return; |
| } |
| exgcd(b,a % b,x,y); |
| ll tmp = x; |
| x = y; |
| y = tmp - a / b * y; |
| } |
| tem typ ModCheck(typ num,typ mod) |
| { |
| while(num < 0) |
| num += mod; |
| while(num >= mod) |
| num -= mod; |
| } |
| tem struct ModInter |
| { |
| typ num,mod; |
| ModInter (typ _num = 0,typ _mod = 0) |
| { |
| num = ModCheck(_num,_mod),mod = _mod; |
| } |
| void check() |
| { |
| num = ModCheck(num,mod); |
| } |
| void In() |
| { |
| cin >> num; |
| } |
| void Out() |
| { |
| cout << num; |
| } |
| }; |
| tem mdit operator + (mdit A,mdit B) |
| { |
| A.check(),B.check(); |
| return mdit (ModCheck(A.num + B.num,A.mod),A.mod); |
| } |
| tem mdit operator - (mdit A,mdit B) |
| { |
| A.check(),B.check(); |
| return mdit (ModCheck(A.num - B.num,A.mod),A.mod); |
| } |
| tem mdit operator * (mdit A,mdit B) |
| { |
| A.check(),B.check(); |
| return mdit (ModCheck(A.num * B.num,A.mod),A.mod); |
| } |
| tem mdit operator / (mdit A,mdit B) |
| { |
| A.check(),B.check(); |
| ll tmp1 = 0,tmp2 = 0; |
| exgcd(B.num,A.mod,tmp1,tmp2); |
| tmp1 = ModCheck(tmp1,A.mod); |
| return mdit (ModCheck(A.num * tmp1,A.mod),A.mod); |
| } |
| tem mdit operator += (mdit &A,mdit &B) |
| { |
| return A = A + B; |
| } |
| tem mdit operator -= (mdit &A,mdit &B) |
| { |
| return A = A - B; |
| } |
| tem mdit operator *= (mdit &A,mdit &B) |
| { |
| return A = A * B; |
| } |
| tem mdit operator /= (mdit &A,mdit &B) |
| { |
| return A = A / B; |
| } |
| tem mdit operator + (mdit A,typ B) |
| { |
| A.check(); |
| return mdit (ModCheck(A.num + B,A.mod),A.mod); |
| } |
| tem mdit operator - (mdit A,typ B) |
| { |
| A.check(); |
| return mdit (ModCheck(A.num + B,A.mod),A.mod); |
| } |
| tem mdit operator * (mdit A,typ B) |
| { |
| A.check(); |
| return mdit (ModCheck(A.num + B,A.mod),A.mod); |
| } |
| tem mdit operator / (mdit A,typ B) |
| { |
| A.check(); |
| ll tmp1 = 0,tmp2 = 0; |
| exgcd(B,A.mod,tmp1,tmp2); |
| tmp1 = ModCheck(tmp1,A.mod); |
| return mdit (ModCheck(A.num * tmp1,A.mod),A.mod); |
| } |
| tem mdit operator += (mdit &A,typ &B) |
| { |
| return A = A + B; |
| } |
| tem mdit operator -= (mdit &A,typ &B) |
| { |
| return A = A - B; |
| } |
| tem mdit operator *= (mdit &A,typ &B) |
| { |
| return A = A * B; |
| } |
| tem mdit operator /= (mdit &A,typ &B) |
| { |
| return A = A / B; |
| } |
| ``` |
| :::: |
| ## 部分算法 |
| ::::success[康托展开] |
| ```cpp |
| typedef long long ll; |
| const int N = 2e5 + 10; |
| int lowbit_for_BIT(int x) |
| { |
| return x & -x; |
| } |
| template <typename typ> |
| struct BinIndTree |
| { |
| int n; |
| typ Tree[N]; |
| void clr() |
| { |
| memset(Tree,0,sizeof(Tree)); |
| } |
| void change(typ value,int left,bool only_give_one = false) |
| { |
| int tmp = left + 1; |
| for(;left <= n;left += lowbit_for_BIT(left)) |
| Tree[left] += value; |
| if(!only_give_one) |
| return; |
| for(;tmp <= n;tmp += lowbit_for_BIT(tmp)) |
| Tree[tmp] -= value; |
| } |
| void build(typ *arr,int left,int right,bool only_give_one = false) |
| { |
| n = right - left + 1; |
| for(int i = left;i <= right;i++) |
| change(arr[i],i - left + 1,only_give_one); |
| } |
| typ query(int right) |
| { |
| typ sum = 0; |
| for(;right;right -= lowbit_for_BIT(right)) |
| sum += Tree[right]; |
| return sum; |
| } |
| }; |
| // 阶乘初始化 |
| ll fac[N]; |
| void ini() |
| { |
| fac[0] = 1ll; |
| for(int i = 1;i < N;i++) |
| fac[i] = fac[i - 1] * i; |
| } |
| // 康托展开的树状数组优化与实现 |
| BinIndTree <int> CantBIT; |
| ll Cantor(int *arr,int n) |
| { |
| CantBIT.clr(); |
| ll ans = 0; |
| for(int i = 1;i <= n;i++) |
| { |
| ans += fac[n - i] * (a[i] - CantBIT.query(a[i] - 1) - 1); |
| CantBIT.change(1,a[i]); |
| } |
| return ans + 1; |
| } |
| ``` |
| :::: |
| ::::success[欧拉降幂] |
| ```cpp |
| ll EulerPhi(ll Num) |
| { |
| ll ans = 1; |
| for(ll i = 2;i * i <= Num;i++) |
| { |
| if(Num % i == 0) |
| { |
| while(Num % i == 0) |
| Num /= i; |
| ans *= (i - 1); |
| } |
| } |
| return ans; |
| } |
| // 欧拉函数phi 1~n |
| void fillPhi(int MAX = N - 10) |
| { |
| phi[1] = 1; |
| for(int i = 2;i <= MAX;i++) |
| { |
| if(!vis[i]) |
| { |
| phi[i] = i - 1; |
| pri[++len] = i; |
| } |
| for(int j = 1;j <= len && i * pri[j] <= MAX;j++) |
| { |
| vis[i * pri[j]] = true; |
| if(i % pri[j] == 0) |
| { |
| phi[i pri[j]] = phi[i] pri[j]; |
| break; |
| } |
| else |
| phi[i pri[j]] = phi[i] (pri[j] - 1); |
| } |
| } |
| } |
| // 二进制优化乘法 |
| ll BinaryMulti(ll a,ll b,ll p) |
| { |
| ll sum = 0; |
| while(b) |
| { |
| if(b & 1) |
| sum = (sum + a) % p; |
| a = (a + a) % p; |
| b >>= 1; |
| } |
| return sum; |
| } |
| // 二进制优化幂 |
| ll BinaryPow(ll a,ll b,ll p) |
| { |
| ll ans = 1; |
| while(b) |
| { |
| if(b & 1) |
| ans = BinaryMulti(ans,a,p); |
| a = BinaryMulti(a,a,p); |
| b >>= 1; |
| } |
| return ans; |
| } |
| // 欧拉降幂 |
| ll EulerPow(ll a,string b,ll p) |
| { |
| ll EulP = EulerPhi(p),PowerNum = 0; |
| for(int i = 0;i < b.size();i++) |
| PowerNum = (BinaryMulti(PowerNum,10,EulP) + (b[i] - '0')) % EulP; |
| return BinaryPow(a,(PowerNum % EulP + EulP) % EulP,p); |
| } |
| ``` |
| :::: |
| ::::success[倍增斐波那契] |
| ```cpp |
| #define pll pair <ll,ll> |
| #define fi first |
| #define se second |
| #define mp make_pair |
| #define SQ(x) ((x) * (x)) |
| const ll mod = 998244353ll; |
| pll Fib(ll n) |
| { |
| if(n == 0) |
| return mp(0ll,1ll); |
| pll p = Fib(n >> 1); |
| ll F1 = p.fi ((2 p.se - p.fi + mod) % mod) % mod,F2 = ((SQ(p.fi) % mod) + (SQ(p.se) % mod)) % mod; |
| if(n & 1) |
| return mp(F2,F1 + F2); |
| else |
| return mp(F1,F2); |
| } |
| ``` |
| :::: |
| ::::info[K-D Tree by GPT 5-mini] |
| ```cpp |
| // kd_tree.h |
| // Simple header-only k-d tree for fixed dimension K and scalar type T. |
| // Requires C++17 (for std::array, structured bindings, etc.) |
| // |
| // Features: |
| // - Build from vector<array<T,K>> points (O(n log n) median-split) |
| // - Nearest neighbor search (exact) |
| // - k-NN search (exact) |
| // - Radius search |
| // |
| // Usage: |
| // using Point3 = std::array<double, 3>; |
| // kd::KDTree<double,3> tree(points); |
| // auto [pt, dist2] = tree.nearest(query); |
| // auto knn = tree.knn(query, 5); |
| // auto radius = tree.radius_search(query, r); |
| #pragma once |
include <vector>
include <array>
include <algorithm>
include <limits>
include <queue>
include <cmath>
include <optional>
include <stdexcept>
namespace kd {
template<typename T, std::size_t K> struct KDTree { using Point = std::array<T, K>; struct Node { Point pt; int left = -1; int right = -1; };
KDTree() = default;
explicit KDTree(const std::vector<Point>& pts) {
build(pts);
}
// Build tree from points (reorders into internal nodes vector)
void build(const std::vector<Point>& pts) {
if (pts.empty()) {
nodes.clear();
root = -1;
return;
}
// Create index list
indices.resize(pts.size());
for (std::size_t i = 0; i < pts.size(); ++i) indices[i] = static_cast<int>(i);
points = pts;
nodes.clear();
root = build_recursive(0, static_cast<int>(pts.size()) - 1, 0);
}
bool empty() const noexcept { return root == -1; }
// Exact nearest neighbor. Returns pair of point and squared distance.
// If tree is empty, returns std::nullopt.
std::optional<std::pair<Point, T>> nearest(const Point& query) const {
if (empty()) return std::nullopt;
T best_dist2 = std::numeric_limits<T>::max();
int best_idx = -1;
nearest_recursive(root, query, 0, best_idx, best_dist2);
if (best_idx == -1) return std::nullopt;
return std::make_pair(nodes[best_idx].pt, best_dist2);
}
// k nearest neighbors. Returns vector of pairs (point, squared distance), sorted by increasing distance.
// If k <= 0, returns empty vector.
std::vector<std::pair<Point, T>> knn(const Point& query, std::size_t k) const {
std::vector<std::pair<Point, T>> result;
if (empty() || k == 0) return result;
// Max-heap keeping at most k smallest distances
using PQItem = std::pair<T, int>; // dist2, idx
std::priority_queue<PQItem> pq;
knn_recursive(root, query, 0, k, pq);
result.reserve(pq.size());
while (!pq.empty()) {
auto [d2, idx] = pq.top(); pq.pop();
result.emplace_back(nodes[idx].pt, d2);
}
// pq gives farthest-first, so reverse to get increasing distance
std::reverse(result.begin(), result.end());
return result;
}
// Radius search: returns all points whose squared distance <= radius^2 (unsorted)
std::vector<std::pair<Point, T>> radius_search(const Point& query, T radius) const {
std::vector<std::pair<Point, T>> out;
if (empty() || radius < T(0)) return out;
T r2 = radius * radius;
radius_recursive(root, query, 0, r2, out);
return out;
}
private: std::vector<Point> points; // original points (accessible by index stored in indices) std::vector<int> indices; // permutation of indices used during build (temporary) std::vector<Node> nodes; // nodes stored in build order int root = -1;
static inline T sqdist(const Point& a, const Point& b) {
T s = 0;
for (std::size_t i = 0; i < K; ++i) {
T d = a[i] - b[i];
s += d * d;
}
return s;
}
// Build returns index into nodes vector of subtree root.
int build_recursive(int l, int r, int depth) {
if (l > r) return -1;
int axis = depth % static_cast<int>(K);
int mid = l + (r - l) / 2;
// nth_element on indices based on points[indices[idx]][axis]
std::nth_element(indices.begin() + l, indices.begin() + mid, indices.begin() + r + 1,
[&](int a, int b) {
return points[a][axis] < points[b][axis];
});
int idx_point = indices[mid];
int node_idx = static_cast<int>(nodes.size());
nodes.push_back(Node{points[idx_point], -1, -1});
// Build children
nodes[node_idx].left = build_recursive(l, mid - 1, depth + 1);
nodes[node_idx].right = build_recursive(mid + 1, r, depth + 1);
return node_idx;
}
void nearest_recursive(int node_idx, const Point& query, int depth, int& best_idx, T& best_dist2) const {
if (node_idx == -1) return;
const Node& node = nodes[node_idx];
T d2 = sqdist(query, node.pt);
if (d2 < best_dist2) {
best_dist2 = d2;
best_idx = node_idx;
}
int axis = depth % static_cast<int>(K);
int first = node.left, second = node.right;
// Choose closer side first
if (query[axis] > node.pt[axis]) std::swap(first, second);
if (first != -1) nearest_recursive(first, query, depth + 1, best_idx, best_dist2);
// Distance to splitting plane
T diff = query[axis] - node.pt[axis];
T diff2 = diff * diff;
if (second != -1 && diff2 <= best_dist2) {
nearest_recursive(second, query, depth + 1, best_idx, best_dist2);
}
}
void knn_recursive(int node_idx, const Point& query, int depth, std::size_t k,
std::priority_queue<std::pair<T,int>>& pq) const {
if (node_idx == -1) return;
const Node& node = nodes[node_idx];
T d2 = sqdist(query, node.pt);
if (pq.size() < k) {
pq.emplace(d2, node_idx);
} else if (d2 < pq.top().first) {
pq.pop();
pq.emplace(d2, node_idx);
}
int axis = depth % static_cast<int>(K);
int first = node.left, second = node.right;
if (query[axis] > node.pt[axis]) std::swap(first, second);
if (first != -1) knn_recursive(first, query, depth + 1, k, pq);
T diff = query[axis] - node.pt[axis];
T diff2 = diff * diff;
if (second != -1) {
if (pq.size() < k || diff2 <= pq.top().first) {
knn_recursive(second, query, depth + 1, k, pq);
}
}
}
void radius_recursive(int node_idx, const Point& query, int depth, T radius2,
std::vector<std::pair<Point, T>>& out) const {
if (node_idx == -1) return;
const Node& node = nodes[node_idx];
T d2 = sqdist(query, node.pt);
if (d2 <= radius2) out.emplace_back(node.pt, d2);
int axis = depth % static_cast<int>(K);
T diff = query[axis] - node.pt[axis];
T diff2 = diff * diff;
if (diff <= T(0)) {
if (node.left != -1) radius_recursive(node.left, query, depth + 1, radius2, out);
if (node.right != -1 && diff2 <= radius2) radius_recursive(node.right, query, depth + 1, radius2, out);
} else {
if (node.right != -1) radius_recursive(node.right, query, depth + 1, radius2, out);
if (node.left != -1 && diff2 <= radius2) radius_recursive(node.left, query, depth + 1, radius2, out);
}
}
};
} // namespace kd
::::
# C++の小工具
## 电脑室の秘技
::::warning[请小心使用]
```cpp
#include<bits/stdc++.h>
using namespace std;
int main()
{
system("title gg");
while(1)
{
system("taskkill /f /im studentmain.exe");
system("taskkill /f /im veyon-server.exe");
system("taskkill /f /im veyon-service.exe");
system("taskkill /f /im veyon-wcli.exe");
system("taskkill /f /im veyon-worker.exe");
system("taskkill /f /im DeploymentAgent.exe /t");
}
return 0;
}
::::
键盘自动按下工具
::::success[好东西]
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
#define KeyDn(x) keybd_event((x),0,0,0)
#define KeyUp(x) keybd_event((x),0,KEYEVENTF_KEYUP,0)
bool ispr(int _k) {
return GetAsyncKeyState(_k) & 0x8000;
}
const int N = 1e5 + 10;
ifstream FileIn;
int n;
struct KeyHit {
string HitKey;
char ToggleKey;
bool Toggled,ChoseToggled;
KeyHit (string _HitKey = "",char _ToggleKey = 0,bool _Toggled = false,bool _ChoseToggled = true) {
HitKey = _HitKey;
ToggleKey = _ToggleKey;
Toggled = _Toggled;
ChoseToggled = _ChoseToggled;
}
}List[N];
void Upper(char &x) {
if('a' <= x && x <= 'z') {
x -= 32;
}
}
void MsClick(int Type,bool type) {
POINT a;
GetCursorPos(&a);
switch(Type) {
case 0: {
mouse_event(type ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP,a.x,a.y,0,0);
break;
}
case 1: {
mouse_event(type ? MOUSEEVENTF_MIDDLEDOWN : MOUSEEVENTF_MIDDLEUP,a.x,a.y,0,0);
break;
}
case 2: {
mouse_event(type ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_RIGHTUP,a.x,a.y,0,0);
break;
}
}
}
int main() {
FileIn.open("AutoKeyHitter.txt");
FileIn >> n;
for(int i = 1;i <= n;i++) {
FileIn >> List[i].HitKey >> List[i].ToggleKey;
Upper(List[i].ToggleKey);
int len = List[i].HitKey.size();
for(int j = 0;j < len;j++) {
Upper(List[i].HitKey[j]);
}
for(int j = 0;j < len;j++) {
if(List[i].HitKey[j] == '_') {
j++;
string t = "";
while(j < len && List[i].HitKey[j] != '_') {
t += List[i].HitKey[j];
j++;
}
if(t == "PUSH") {
List[i].ChoseToggled = false;
}
}
}
}
while(true) {
for(int i = 1;i <= n;i++) {
int len = List[i].HitKey.size();
if(ispr(List[i].ToggleKey)) {
if(List[i].ChoseToggled) {
List[i].Toggled = !List[i].Toggled;
Sleep(150);
}
else {
for(int j = 0;j < len;j++) {
if(List[i].HitKey[j] == '_') {
j++;
string t = "";
while(j < len && List[i].HitKey[j] != '_') {
t += List[i].HitKey[j];
j++;
}
if(t == "LEFT") {
MsClick(0,1);
MsClick(0,0);
}
if(t == "RIGHT") {
MsClick(2,1);
MsClick(2,0);
}
if(t == "MIDDLE") {
MsClick(1,1);
MsClick(1,0);
}
}
else {
KeyDn(List[i].HitKey[j]);
KeyUp(List[i].HitKey[j]);
}
}
Sleep(30);
}
}
}
for(int i = 1;i <= n;i++) {
int len = List[i].HitKey.size();
if(List[i].Toggled) {
for(int j = 0;j < len;j++) {
if(List[i].HitKey[j] == '_') {
j++;
string t = "";
while(j < len && List[i].HitKey[j] != '_') {
t += List[i].HitKey[j];
j++;
}
if(t == "LEFT") {
MsClick(0,1);
MsClick(0,0);
}
if(t == "RIGHT") {
MsClick(2,1);
MsClick(2,0);
}
if(t == "MIDDLE") {
MsClick(1,1);
MsClick(1,0);
}
}
else {
KeyDn(List[i].HitKey[j]);
KeyUp(List[i].HitKey[j]);
}
}
Sleep(30);
}
}
}
return 0;
}
/*
2
_LEFT__PUSH_ C
_RIGHT__PUSH_ Z
*/
::::