模板合集
yeshubo_qwq · · 个人记录
会陆续更新一些模板。
欢迎嘲讽/cf
鸣谢:@a_grain @LuoYiHang 无偿提供的模板,%%%!
IO:
namespace IO {
template <typename type>
void read(type &x) {
char c = getchar();
int tag = 1;
while (c != '-' && (c < '0' || c > '9'))
c = getchar();
if (c == '-')
tag = -1, x = 0;
else
x = (c ^ 48);
c = getchar();
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
x *= tag;
}
template <typename type, typename ... types>
void read(type &x, types &... y) {
read(x);
read(y...);
}
template <typename type>
void write(type x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
};
Treap:
const int N = 1e5 + 5, INF = 1e9;
struct Treap {
int siz[N], son[N][2], num[N], val[N], rnd[N], sum, root;
void init() {
memset(siz, 0, sizeof(siz));
memset(son, 0, sizeof(son));
memset(num, 0, sizeof(num));
memset(val, 0, sizeof(val));
memset(rnd, 0, sizeof(rnd));
sum = 0, root = 0;
}
void push_up(int p) {
siz[p] = siz[son[p][0]] + siz[son[p][1]] + num[p];
}
void rotate(int &p, int tag) {
int k = son[p][tag ^ 1];
son[p][tag ^ 1] = son[k][tag];
son[k][tag] = p;
push_up(p), push_up(k);
p = k;
}
void ins(int &p, int x) {
if (!p) {
p = ++sum;
siz[p] = num[p] = 1, val[p] = x;
rnd[p] = rand();
return ;
}
if (val[p] == x) {
siz[p]++, num[p]++;
return ;
}
int tag = (val[p] < x);
ins(son[p][tag], x);
if (rnd[p] < rnd[son[p][tag]])
rotate(p, tag ^ 1);
push_up(p);
}
void del(int &p, int x) {
if (!p)
return ;
if (x < val[p])
del(son[p][0], x);
else if (x > val[p])
del(son[p][1], x);
else { /*x==val[p]*/
if (!son[p][0] && !son[p][1]) {
siz[p]--, num[p]--;
if (!num[p])
p = 0;
} else if (son[p][0] && !son[p][1]) {
rotate(p, 1), del(son[p][1], x);
} else if (!son[p][0] && son[p][1]) {
rotate(p, 0), del(son[p][0], x);
} else { /*son[p][0] && son[p][1]*/
int tag = (rnd[son[p][0]] > rnd[son[p][1]]);
rotate(p, tag);
del(son[p][tag], x);
}
}
push_up(p);
}
int rank(int p, int x) {
if (!p)
return 0;
if (x < val[p])
return rank(son[p][0], x);
if (x == val[p])
return siz[son[p][0]] + 1;
return siz[son[p][0]] + num[p] + rank(son[p][1], x);
}
int find(int p, int x) {
if (!p)
return 0;
if (siz[son[p][0]] >= x)
return find(son[p][0], x);
else if (siz[son[p][0]] + num[p] < x)
return find(son[p][1], x - num[p] - siz[son[p][0]]);
return val[p];
}
int pre(int p, int x) {
if (!p)
return -INF;
if (val[p] >= x)
return pre(son[p][0], x);
else
return max(val[p], pre(son[p][1], x));
}
int nxt(int p, int x) {
if (!p)
return INF;
if (val[p] <= x)
return nxt(son[p][1], x);
else
return min(val[p], nxt(son[p][0], x));
}
};
Treap tree;
FHQ-Treap:
struct FHQ {
int x, y, z, val[N], siz[N], rnd[N], son[N][2], cnt, root;
void init() {
cnt = 0, root = 0;
}
void push_up(int p) {
siz[p] = siz[son[p][0]] + siz[son[p][1]] + 1;
}
void split(int p, int k, int &x, int &y) {
if (!p) {
x = y = 0;
return ;
}
if (val[p] <= k)
x = p, split(son[p][1], k, son[p][1], y);
else
y = p, split(son[p][0], k, x, son[p][0]);
push_up(p);
}
int merge(int x, int y) {
if (!x || !y)
return x | y;
if (rnd[x] < rnd[y]) {
son[x][1] = merge(son[x][1], y);
push_up(x);
return x;
} else {
son[y][0] = merge(x, son[y][0]);
push_up(y);
return y;
}
}
int new_node(int x) {
++cnt;
val[cnt] = x, siz[cnt] = 1, rnd[cnt] = rand();
son[cnt][0] = son[cnt][1] = 0;
return cnt;
}
void ins(int k) {
split(root, k, x, y);
root = merge(merge(x, new_node(k)), y);
}
void del_n(int k) {
split(root, k, x, z);
split(x, k - 1, x, y);
root = merge(x, z);
}
void del_1(int k) {
split(root, k, x, z);
split(x, k - 1, x, y);
y = merge(son[y][0], son[y][1]);
root = merge(merge(x, y), z);
}
int find(int p, int x) {
while (1)
if (x <= siz[son[p][0]])
p = son[p][0];
else if (x == siz[son[p][0]] + 1)
return p;
else
x -= (siz[son[p][0]] + 1), p = son[p][1];
}
int rank(int k) {
split(root, k - 1, x, y);
k = siz[x] + 1;
root = merge(x, y);
return k;
}
int pre(int k) {
split(root, k - 1, x, y);
k = val[find(x, siz[x])];
root = merge(x, y);
return k;
}
int nxt(int k) {
split(root, k, x, y);
k = val[find(y, 1)];
root = merge(x, y);
return k;
}
} tree;
FHQ-Treap 超快 insert+delete(By a_grain):
void insert(int &now, int c) {
if (!now) {
now = c;
return;
}
if (tr[c].rd < tr[now].rd) {
int x, y;
split(now, tr[c].v, x, y);
tr[c].ls = x;
tr[c].rs = y;
now = c;
pu(c);
return;
} else {
if (tr[c].v <= tr[now].v) {
insert(tr[now].ls, c);
pu(now);
return;
} else {
insert(tr[now].rs, c);
pu(now);
return;
}
}
}
void del(int &now, int c) {
if (c == tr[now].v) {
now = merge(tr[now].ls, tr[now].rs);
} else if (c < tr[now].v)
del(tr[now].ls, c), pu(now);
else
del(tr[now].rs, c), pu(now);
}
Splay(By LuoYiHang)
void clear(int x) {
son[x][0] = son[x][1] = fa[x] = who[x] = num[x] = len[x] = 0;
}
bool which(int x) {
return x == son[fa[x]][1];
}
inline void pushup(int x) {
if (x) {
len[x] = num[x];
if (son[x][0])
len[x] += len[son[x][0]];
if (son[x][1])
len[x] += len[son[x][1]];
}
return ;
}
inline void rotate(int x) {
int y = fa[x], z = fa[y], fl = which(x);
son[y][fl] = son[x][fl ^ 1];
fa[son[y][fl]] = y;
son[x][fl ^ 1] = y;
fa[y] = x;
fa[x] = z;
if (z)
son[z][son[z][1] == y] = x;
pushup(y);
pushup(x);
}
inline void splay(int x) {
for (int i; i = fa[x]; rotate(x))
if (fa[i])
rotate((which(x) == which(i)) ? i : x);
root = x;
}
inline void insert(int x) {
if (!root) {
cnt++;
son[cnt][0] = son[cnt][1] = fa[cnt] = 0;
root = cnt;
len[cnt] = num[cnt]++;
who[cnt] = x;
return ;
}
int now = root, Dad = 0;
while (1) {
if (x == who[now]) {
num[now]++;
pushup(now);
pushup(Dad);
splay(now);
break;
}
Dad = now;
now = son[now][who[now] < x];
if (!now) {
++cnt;
son[cnt][0] = son[cnt][1] = 0;
fa[cnt] = Dad;
who[cnt] = x;
num[cnt] = len[cnt] = 1;
son[Dad][who[Dad] < x] = cnt;
pushup(Dad);
splay(cnt);
break;
}
}
}
inline int Kpath(int x) {
int now = root;
while (1) {
if (son[now][0] && x <= len[son[now][0]])
now = son[now][0];
else {
int s = (son[now][0] ? len[son[now][0]] : 0) + num[now];
if (x <= s) {
splay(now);
return who[now];
}
x -= s;
now = son[now][1];
}
}
}
inline int Rank(int x) {
if (!root)
return 1;
int now = root, s = 0;
while (1) {
if (x < who[now])
now = son[now][0];
else {
s += (son[now][0] ? len[son[now][0]] : 0);
if (x == who[now]) {
splay(now);
return s + 1;
}
s += num[now];
if (son[now][1])
now = son[now][1];
else
return s + 1;
}
}
}
inline int front() {
int now = son[root][0];
while (son[now][1])
now = son[now][1];
splay(now);
return now;
}
inline int hind() {
int now = son[root][1];
while (son[now][0])
now = son[now][0];
splay(now);
return now;
}
inline void remove(int x) {
Rank(x);
if (num[root] > 1) {
num[root]--;
pushup(root);
return ;
}
if (!son[root][0] && !son[root][1]) {
clear(root);
root = 0;
return;
}
if (!son[root][0]) {
int old = root;
root = son[root][1];
fa[root] = 0;
clear(old);
return;
} else if (!son[root][1]) {
int old = root;
root = son[root][0];
fa[root] = 0;
clear(old);
return;
}
int old = root, pre = front();
son[root][1] = son[old][1];
fa[son[old][1]] = root;
clear(old);
pushup(root);
return ;
}
BIT:
struct BIT {
type tr[N];
int n;
void init(int x) {
n = x;
go(i, 0, x) tr[i] = 0;
}
int lowbit(int x) {
return x & (-x);
}
void add(int x, type y) {
while (x <= n)
tr[x] += y, x += lowbit(x);
}
type query(int x) {
type res = 0;
while (x > 0)
res += tr[x], x -= lowbit(x);
return res;
}
type query(int x, int y) {
return query(y) - query(x - 1);
}
};
template <class type>
struct EXBIT {
BIT <type> a, b;
int n;
void init(int x) {
n = x, a.init(n), b.init(n);
}
void add(int x, int y, type z) {
a.add(x, z), a.add(y + 1, -z);
b.add(x, z * x), b.add(y + 1, -z * (y + 1));
}
type query(int x) {
return a.query(x) * (x + 1) - b.query(x);
}
type query(int x, int y) {
return query(y) - query(x - 1);
}
};
EXBIT <int> c;
线段树:
void push_up(int p) {
tr[p] = (tr[p << 1] + tr[p << 1 | 1]) % mod;
}
void push_down(int p, int pl, int pr) {
int mid = (pl + pr) >> 1;
tr[p << 1] = (tr[p << 1] * mul[p] + (mid - pl + 1) * tag[p]) % mod;
tr[p << 1 | 1] = (tr[p << 1 | 1] * mul[p] + (pr - mid) * tag[p]) % mod;
tag[p << 1] = (tag[p << 1] * mul[p] + tag[p]) % mod;
tag[p << 1 | 1] = (tag[p << 1 | 1] * mul[p] + tag[p]) % mod;
mul[p << 1] = (mul[p << 1] * mul[p]) % mod;
mul[p << 1 | 1] = (mul[p << 1 | 1] * mul[p]) % mod;
tag[p] = 0, mul[p] = 1;
}
void build(int p, int pl, int pr) {
tag[p] = 0, mul[p] = 1;
if (pl == pr) {
tr[p] = a[pl] % mod;
return ;
}
int mid = (pl + pr) >> 1;
build(p << 1, pl, mid);
build(p << 1 | 1, mid + 1, pr);
push_up(p);
}
void update1(int p, int pl, int pr, int l, int r, ll z) {
if (l <= pl && pr <= r) {
tr[p] = (tr[p] + (pr - pl + 1) * z) % mod;
tag[p] = (tag[p] + z) % mod;
return ;
}
push_down(p, pl, pr);
int mid = (pl + pr) >> 1;
if (l <= mid)
update1(p << 1, pl, mid, l, r, z);
if (mid < r)
update1(p << 1 | 1, mid + 1, pr, l, r, z);
push_up(p);
}
void update2(int p, int pl, int pr, int l, int r, ll z) {
if (l <= pl && pr <= r) {
tr[p] = (tr[p] * z) % mod;
tag[p] = (tag[p] * z) % mod;
mul[p] = (mul[p] * z) % mod;
return ;
}
push_down(p, pl, pr);
int mid = (pl + pr) >> 1;
if (l <= mid)
update2(p << 1, pl, mid, l, r, z);
if (mid < r)
update2(p << 1 | 1, mid + 1, pr, l, r, z);
push_up(p);
}
ll query(int p, int pl, int pr, int l, int r) {
if (l <= pl && pr <= r)
return tr[p] % mod;
push_down(p, pl, pr);
int mid = (pl + pr) >> 1;
ll ans = 0;
if (l <= mid)
ans += query(p << 1, pl, mid, l, r), ans %= mod;
if (mid < r)
ans += query(p << 1 | 1, mid + 1, pr, l, r);
return ans % mod;
}
zkw(By a_grain):
#include <bits/stdc++.h>
using namespace std;
const int NN = 5e5;
int n, tr[NN << 2 + 5], T, fl, i, a[NN + 5], bj, x, y;
void read(int &x) {
char c = getchar();
int f = 1;
x = 0;
while (c < '0' || c > '9')
(c == '-' ? f = -1 : f = 1), c = getchar();
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
x = x * f;
}
inline void write(int x) {
if (x < 0)
putchar('-'), x = -x;
if (x >= 10)
write(x / 10);
putchar(x % 10 + 48);
}
void inc(int p, int x) {
tr[p + fl] += x;
for (p = (p + fl) >> 1; p >= 1; p >>= 1)
tr[p] = tr[p << 1] + tr[p << 1 | 1];
}
int ask(int l, int r) {
int ans = 0;
for (l = l + fl - 1, r = r + fl + 1; l ^ r ^ 1; l >>= 1, r >>= 1) {
if (~l & 1)
ans += tr[l ^ 1];
if (r & 1)
ans += tr[r ^ 1];
}
return ans;
}
int main() {
read(n);
read(T);
for (fl = 1; fl <= n; fl <<= 1);
for (i = 1; i <= n; i++)
read(a[i]), tr[i + fl] = a[i];
for (i = fl; i >= 1; i--)
tr[i] = tr[i << 1] + tr[i << 1 | 1];
while (T--) {
read(bj);
read(x);
read(y);
if (bj == 1) {
inc(x, y);
} else
write(ask(x, y)), putchar('\n');
}
return 0;
}
线段树二分(By a_grain):
int ask1(int p,int c){
if(tr[p].l==tr[p].r){
return tr[p].r;
}
int mid=(tr[p].l+tr[p].r)>>1;
if(tr[tr[p].rs].sum<c){
return ask1(tr[p].ls,c-tr[tr[p].rs].sum);
}
else{
return ask1(tr[p].rs,c);
}
}
int ask2(int p,int c){
if(tr[p].l==tr[p].r){
return tr[p].r;
}
int mid=(tr[p].l+tr[p].r)>>1;
if(tr[tr[p].ls].sum<c){
return ask2(tr[p].rs,c-tr[tr[p].ls].sum);
}
else{
return ask2(tr[p].ls,c);
}
}
KMP:
void KMP() {
int j = 0;
go(i, 2, n) {
while (j > 0 && a[j + 1] != a[i])
j = fail[j];
if (a[j + 1] == a[i])
j++;
fail[i] = j;
}
}
void query() {
int j = 0;
go(i, 1, m) {
while (j > 0 && a[j + 1] != b[i])
j = fail[j];
if (a[j + 1] == b[i])
j++;
if (j == n)
cout << i - n + 1 << '\n';
}
}
主席树(By a_grain):
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int T,i,n,cntt,l,r,k,a[N],b[N],f[N],old[N],rot[N];
struct tree{
int l,r,sum,ll,rr;
}tr[N*25];
void build(int &p,int l,int r){
p=++cntt;
tr[p]=(tree){l,r,0,0,0};
if(l==r){
tr[p].sum=1;
return;
}
int mid=tr[p].l+tr[p].r>>1;
build(tr[p].ll,l,mid);build(tr[p].rr,mid+1,r);
tr[p].sum=tr[tr[p].ll].sum+tr[tr[p].rr].sum;
}
void ins(int &p,int pr,int x,int c){
p=++cntt;
tr[p]=tr[pr];
if(tr[p].l==tr[p].r){
tr[p].sum+=c;
return;
}
int mid=tr[p].l+tr[p].r>>1;
if(x<=mid)ins(tr[p].ll,tr[pr].ll,x,c);
else ins(tr[p].rr,tr[pr].rr,x,c);
tr[p].sum=tr[tr[p].ll].sum+tr[tr[p].rr].sum;
}
int ask(int lp,int rp,int x){
if(tr[lp].l==tr[lp].r)return tr[lp].l;
int kkk=tr[tr[rp].ll].sum-tr[tr[lp].ll].sum;
if(kkk<x)return ask(tr[lp].rr,tr[rp].rr,x-kkk);
else return ask(tr[lp].ll,tr[rp].ll,x);
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>T;int TT=T;
for(i=1;i<=n;i++)cin>>a[i],b[i]=a[i];
sort(b+1,b+1+n);
int kk=unique(b+1,b+1+n)-b-1;
for(i=1;i<=n;i++){
int s1=a[i];
a[i]=lower_bound(b+1,b+1+kk,a[i])-b;
old[a[i]]=s1;
}
build(rot[0],1,kk);
for(i=1;i<=n;i++)
ins(rot[i],rot[i-1],a[i],1);
while(T--){
cin>>l>>r>>k;
cout<<old[ask(rot[l-1],rot[r],k)]<<'\n';
}
return 0;
}
Trie:
struct trie {
int nex[N][M], cnt[N], id;
void init() {
go(i, 0, id) {
cnt[i] = 0;
go(j, 0, M - 1) nex[i][j] = 0;
}
id = 0;
}
int get(char x) {
if (x >= 'a' && x <= 'z')
return x - 97;
if (x >= 'A' && x <= 'Z')
return x - 39;
if (x >= '0' && x <= '9')
return x + 4;
}
void insert(string s) {
int p = 0;
go(i, 0, s.size() - 1) {
int x = get(s[i]);
if (!nex[p][x])
nex[p][x] = ++id;
p = nex[p][x];
cnt[p]++;
}
}
int query(string s) {
int p = 0;
go(i, 0, s.size() - 1) {
int x = get(s[i]);
if (!nex[p][x])
return 0;
p = nex[p][x];
}
return cnt[p];
}
} trie;
ACAM:
#define go(i,x,y) for (int i=x;i<=y;i++)
const int N=1e6+5,M=26;
struct ACAM{
int nex[N][M],cnt,end[N],fail[N];
int vis[N];
queue <int> q;
void clear(){
go(i,0,cnt){
go(j,0,25) nex[i][j]=0;
end[i]=fail[i]=0;
}
go(i,1,n) vis[i]=0;
cnt=0;
while (!q.empty()) q.pop();
}
int idx(char c){ return c-97;}
void insert(char *s,int num){
int p=0;
for (int i=0;s[i]!='\0';i++){
int x=idx(s[i]);
if (!nex[p][x]) nex[p][x]=++cnt;
p=nex[p][x];
}
// ++end[p];
end[p]=num;
}
void build(){
int p=0;
go(i,0,25)
if (nex[p][i]) q.push(nex[p][i]);
while (!q.empty()){
p=q.front(),q.pop();
go(i,0,25)
if (nex[p][i])
fail[nex[p][i]]=nex[fail[p]][i],
q.push(nex[p][i]);
else
nex[p][i]=nex[fail[p]][i];
}
}
int query(char *s){
int p=0,ans=0;
for (int i=0;s[i]!='\0';i++){
p=nex[p][idx(s[i])];
for (int j=p; j /*&& ~end[j]*/; j=fail[j])
if (end[j]) vis[end[j]]++,ans=max(vis[end[j]],ans);
/*ans+=end[j],end[j]=-1;*/
}
return ans;
}
} ACAM;
Quick pow:
namespace qpow {
LL mul(LL x, LL y, LL mod) {
x %= mod, y %= mod;
if (mod <= 1000000000)
return x * y % mod;
if (mod <= 1000000000000LL)
return (((x * (y >> 20) % mod) << 20) + x * (y & ((1 << 20) - 1))) % mod;
LL tmp = (LL)floor(x * (long double)y / mod + 0.5);
LL res = (x * y - tmp * mod) % mod;
if (res < 0)
res += mod;
return res;
}
LL qpow(LL x, LL y, LL mod) {
LL res = 1;
for (; y; y >>= 1) {
if (y & 1)
res = mul(res, x, mod);
x = mul(x, x, mod);
}
return res;
}
}
manacher:
int manacher(char *str, int n) {
int ans = 0, pos = 0, r = 0;
go(i, 1, n) {
p[i] = (i < r ? min(p[(pos << 1) - i], r - i) : 1);
while (i - p[i] >= 1 && i + p[i] <= n && str[i - p[i]] == str[i + p[i]])
p[i]++;
if (i + p[i] > r)
pos = i, r = i + p[i];
ans = max(ans, p[i] - 1);
}
return ans;
}
乘法逆元:
inv[0]=0,inv[1]=1;
go(i,2,n) inv[i]=((-mod/i+mod)%mod*inv[mod%i])%mod;
康托展开:
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, i, a[1000005], c[1000005], fac[1000005], ans;
const int mod = 998244353;
int lowbit(int x) {
return x & - x;
}
void add(int x, int v) {
while (x <= n)
c[x] += v, x += lowbit(x);
}
int sum(int x) {
int res = 0;
while (x > 0)
res += c[x], x -= lowbit(x);
return res;
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (i = 1; i <= n; i++)
cin >> a[i];
fac[0] = 1;
for (i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % mod;
for (i = 1; i <= n; i++)
add(a[i], 1);
for (i = 1; i <= n; i++) {
ans = (ans + ((sum(a[i] - 1) * fac[n - i]) % mod)) % mod;
add(a[i], -1);
}
return cout << (ans + 1), 0;
}
差分约束:
const int M = 5e3 + 5, N = 5e3 + 5;
struct edge {
int to, nex, w;
} e[M + N];
int h[N], hcnt;
void add_edge(int x, int y, int z) {
e[++hcnt] = (edge) {
y, h[x], z
};
h[x] = hcnt;
}
int n, m, x, y, z, dis[N], cnt[N], vis[N];
queue <int> q;
bool SPFA(int s) {
memset(dis, 0x3f, sizeof(dis));
// memset(vis,0,sizeof(vis));
// memset(cnt,0,sizeof(cnt));
while (!q.empty())
q.pop();
dis[s] = 0, vis[s] = 1, cnt[s] = 1;
q.push(s);
while (!q.empty()) {
int x = q.front();
q.pop();
vis[x] = 0;
for (int i = h[x]; i; i = e[i].nex) {
int y = e[i].to;
if (dis[x] + e[i].w < dis[y]) {
dis[y] = dis[x] + e[i].w;
if (!vis[y]) {
if (++cnt[y] >= n + 1)
return false;
vis[y] = 1, q.push(y);
}
}
}
}
return true;
}
int main() {
read(n), read(m);
go(i, 1, m) read(x), read(y), read(z), add_edge(y, x, z);
go(i, 1, n) add_edge(0, i, 0);
if (SPFA(0)) {
go(i, 1, n) write(dis[i]), putchar(' ');
} else {
puts("NO");
}
return 0;
}
哈希:
const int N = 1e6 + 5, mod = 99989397, base = 233;
void pre() {
B[0] = 1, B1[n + 1] = 1;
for (int i = 1; i <= m; i++)
res[i] = (res[i - 1] * base % mod + S[i]) % mod, B[i] = (B[i - 1] * base) % mod;
for (int i = m; i >= 1; i--)
res1[i] = (res1[i + 1] * base % mod + S[i]) % mod;
}
int query(int l, int r) {
return (l > r ? 0 : (res[r] - res[l - 1] * B[r - l + 1] % mod + mod) % mod); //正
}
int query1(int l, int r) {
return (l > r ? 0 : (res1[l] - res1[r + 1] * B[r - l + 1] % mod + mod) % mod); //反
}
卡哈希(自然溢出):
int popcount(int x){
int cnt=0;
while (x>0) cnt+=(x&1),x>>=1;
return cnt;
}
signed main(){
scanf("%c %c",&x,&y);
n=1024;
go(i,1,n)
if (popcount(i-1)&1) str1[i]=x,str2[i]=y;
else str1[i]=y,str2[i]=x;
printf("%s\n%s",str1+1,str2+1);
return 0;
}
ST:
namespace ST {
void init(int *a) {
for (int i = 1; i <= n; i++)
st[i][0] = a[i];
for (int j = 1; (1 << j) <= n; j++)
for (int i = 1; i + (1 << j) - 1 <= n; i++)
st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
int query(int l, int r) {
int k = log2(r - l + 1);
return min(st[l][k], st[r - (1 << k) + 1][k]);
}
}
LCA:
namespace LCA {
void dfs(int x, int F) {
for (auto y : G[x])
if (y != F) {
fa[y] = x;
dep[y] = dep[x] + 1;
dfs(y, x);
}
}
void init() {
fa[root] = -1, dep[root] = 1;
dfs(root, 0);
memset(f, -1, sizeof f);
for (int i = 1; i <= n; i++)
f[i][0] = fa[i];
for (int j = 1; (1 << j) <= n; j++)
for (int i = 1; i <= n; i++)
if (f[i][j - 1] != -1)
f[i][j] = f[f[i][j - 1]][j - 1];
}
int lca(int u, int v) {
if (dep[u] < dep[v])
swap(u, v);
int c = dep[u] - dep[v];
for (int i = 20; i >= 0; i--)
if ((c >> i) & 1)
u = f[u][i];
if (u == v)
return u;
for (int i = 20; i >= 0; i--)
if (f[u][i] != f[v][i] && f[u][i] != -1 && f[v][i] != -1)
u = f[u][i], v = f[v][i];
return f[u][0];
}
}