模板栏
冰茶姬
int fa[maxn] , n , m ;
int find(int x) {
if(fa[x] != x) fa[x] = find(fa[x]) ;
return fa[x] ;
}
void unionn(int x ,int y) {
x = find(x) ;
y = find(y) ;
fa[x] = y ;
}
int check(int x,int y) {
x = find(x) , y = find(y) ;
if(x == y) return 1 ;
return 0 ;
}
int main () {
n = read() , m = read() ;
for(int i = 0 ; i <= n ; i ++) fa[i] = i ;
rep(i,1,m) {
int opt = read() , x = read() , y = read() ;
if(opt == 1) {
unionn(x,y) ;
}else {
if(check(x,y)) puts("Y") ;
else puts("N") ;
}
}
return 0 ;
}
蛤戏
/*
ps:本代码经过360,腾讯电脑管家,金山毒霸等权威软件严格杀毒,未携带任何病毒,请评测姬放心评测!!
*/
unsigned long long power[maxn] ;
vector <unsigned long long> v ;
signed main () {
n = read() ;
power[0] = 1 ;
for(signed i = 1 ; i <= 10001 ; i ++ ) {
power[i] = power[i-1] * base ;
}
for(signed i = 1 ; i <= n ; i ++) {
string s ;
cin >> s ;unsigned long long ans = 0 ;
for(signed i = s.size()-1 , cnt = 0; i >= 0 ; i --) {
ans += int(s[i])*power[cnt] ;
cnt ++ ;
}
// cout << ans << endl ;
v.push_back(ans) ;
}sort(v.begin(),v.end()) ;
unsigned long long ans = 1 ;
for(signed i = 1 ; i < v.size() ; i ++ ) {
if(v[i] != v[i-1]) ans ++ ;
}
cout << ans << endl ;
return 0 ;
}
快速幂
int n , m , ha ;
int quick_pow(int x ,int p) {
int res = 1 ;
for(; p ; p >>= 1 , x = x * x % ha) {
if(p & 1) res = res * x % ha ;
}return res ;
}
signed main () {
n = read() , m = read() , ha = read() ;
printf("%lld^%lld mod %lld=%lld",n,m,ha,quick_pow(n,m)) ;
return 0 ;
}
线性筛素数
int prime[maxn] , vis[maxn] , tot , n , m ;
int main () {
vis[1] = 1 ;
n = read() , m = read() ;
for(int i = 2 ; i <= n ; i ++) {
if(!vis[i]) prime[++tot] = i ;
for(int j = 1 ; j <= n && i * prime[j] <= n ; j ++) {
vis[i*prime[j]] = 1 ;
if(i % prime[j] == 0) break ;
}
}
while(m --) {
cout << (vis[read()] == 0 ? "Yes" : "No") << endl ;
}
return 0 ;
}
堆
priority_queue<int,vector<int>,greater<int> >q ;
int n , m ;
int main () {
m = read() ;
while(m --) {
int opt = read() ,x ;
if(opt == 1) {
x = read() ;
q.push(x) ;
}else if(opt == 2) {
cout << q.top() << endl ;
}else {
q.pop() ;
}
}
return 0 ;
}
最小生成树
struct dy {
int x , y , z ;
int operator < (const dy rr) const {
return z < rr.z ;
}
}a[maxm] ;
int fa[maxn] ;
int find(int x) {
if(fa[x] != x) fa[x] = find(fa[x]) ;
return fa[x] ;
}
void unionn(int x ,int y) {
x = find(x) , y = find(y) ;
fa[y] = x ;
}
int n , m ;
int main () {
n = read() , m = read() ;
for(int i = 0 ; i <= n ; i ++) fa[i] = i ;
for(int i = 1 ; i <= m ; i ++) {
a[i].x = read() ;
a[i].y = read() ;
a[i].z = read() ;
}
sort(a+1,a+1+m) ;int ans = 0 ;
for(int i = 1 , k = 0 ; i <= m ; i ++ ) {
if(find(a[i].x) != find(a[i].y)) {
unionn(a[i].x,a[i].y) ;
k ++ ;
ans += a[i].z ;
}if(k == n-1) break ;
}
cout << ans <<endl ;
return 0 ;
}
裴蜀定理
int main() {
n = read() ;int ans=0;
for(int i = 1 ; i <= n ; i ++){
x = read() ;
ans = gcd(ans,x) ;
}
cout << abs(ans) << endl ;
return 0 ;
}
int gcd(int a,int b) {
return b==0? a:gcd(b,a%b) ;
}
spfa
struct dy {
int x , y , z , next ;
}a[maxm] ;
int head[maxn] , dis[maxn] , vis[maxn] ;
void add(int x ,int y ,int z) {
a[++t].x = x ;
a[t].y = y ;
a[t].z = z ;
a[t].next = head[x] ;
head[x] = t ;
}
queue<int>q ;
void spfa(int s) {
while(q.size()) q.pop() ;
for(int i = 0 ; i <= n ; i ++) dis[i] = 2147483647 ;
// cout << dis[s] << "*\n" ;
dis[s] = 0 ;
vis[s] = 1 ;
q.push(s) ;
while(!q.empty()) {
int u = q.front() ;
q.pop() ;
vis[u] = 0 ;
for(int i = head[u] ; i ; i = a[i].next) {
int v = a[i].y ;
if(dis[v] > dis[u] + a[i].z) {
dis[v] = dis[u] + a[i].z ;
if(!vis[v]) {
vis[v] = 1 ;
q.push(v) ;
}
}
}
}
}
int main () {
n = read() , m = read() , s = read() ;
for(int i = 1 ; i <= m ; i ++) {
int x = read() , y = read() , z = read() ;
add(x,y,z) ;
}
spfa(s) ;
return 0 ;
}
树状数组---单点修改区间查询
int n , m , tree[maxn] ;
int lowbit(int x) {
return x & (-x) ;
}
void update(int x ,int k) {
for(int i = x ; i <= n ; i += lowbit(i)) {
tree[i] += k ;
}
}
int query(int x) {
int res = 0 ;
for(int i = x ; i ; i -= lowbit(i)) {
res += tree[i] ;
}return res ;
}
int main () {
n = read() , m = read() ;
for(int i = 1 ; i <= n ; i ++) {
int x = read() ;
update(i,x) ;
}
while(m --) {
int opt = read() , x = read() , y = read() ;
if(opt == 1) {
update(x,y) ;
}else {
printf("%d\n",query(y)-query(x-1)) ;
}
}
return 0 ;
}
树状数组2
int n , m , tree[maxn] ;
int lowbit(int x) {
return x & (-x) ;
}
void update(int x,int k) {
for(int i = x ; i <= n ; i += lowbit(i)) {
tree[i] += k ;
}
}
int query(int x) {
int res = 0 ;
for(int i = x ; i ; i -= lowbit(i)) {
res += tree[i] ;
}return res ;
}
int a[maxn] ;
int main () {
n = read() , m = read() ;
for(int i = 1 ; i <= n ; i ++) {
a[i] = read() ;
update(i,a[i]-a[i-1]) ;
}
while(m --) {
int opt = read() ;
if(opt == 1) {
int l = read() , r = read() , k = read() ;
update(l,k) ;
update(r+1,-k) ;
}else {
int x = read() ;
cout << query(x) << endl ;
}
}
return 0 ;
}
ST表
const int maxn = 1e5+100 ;
int read() {
int x = 0 , f = 1 ; char s = getchar() ;
while(s > '9' || s < '0') {if(s == '-') f = -1 ; s = getchar() ;}
while(s <='9' && s >='0') {x = x * 10 + (s-'0'); s = getchar() ;}
return x*f ;
}
int n , m , a[maxn] , f[maxn][30] ;
signed main () {
n = read() , m = read() ;
for(int i = 1 ; i <= n ; i ++) {
f[i][0] = read() ;
}
for(int j = 1 ; j <= 23 ; j ++) {
for(int i = 1 ; i+(1<<j)-1 <= n ; i ++) {
f[i][j] = max(f[i][j-1],f[i+(1<<(j-1))][j-1]) ;
}
}
while(m --) {
int l = read() , r = read() ;
int t = log(r-l+1)/log(2) ;
printf("%lld\n",max(f[l][t],f[r+1-(1<<t)][t]));
}
return 0 ;
}
看猫片
char s[maxn] , t[maxn] ;
int p[maxn] ;
int main () {
scanf("%s%s",s+1,t+1) ;
int j = 0 ;
p[0] = 0 ;
int n = strlen(s+1) , m = strlen(t+1) ;
for(int i = 1 ; i < m ; i ++) {
while(j > 0 && t[i+1] != t[j+1]) j = p[j] ;
if(t[i+1] == t[j+1]) j ++ ;
p[i+1] = j ;
}
j = 0 ;
for(int i = 0 ; i < n ; i ++) {
while(j > 0 && s[i+1] != t[j+1]) j = p[j] ;
if(t[j+1] == s[i+1]) j ++ ;
if(j == m) {
cout << i - m + 2 << endl ;
j = p[j] ;
}
}
for(int i = 1 ; i <= m ; i ++) {
cout << p[i] << " " ;
}
return 0 ;
}
线性求逆元
signed main () {
n = read() , p = read() ;
fac[1] = 1 ;
puts("1") ;
for(int i = 2 ; i <= n ; i ++) {
fac[i] = (p-p/i) * fac[p%i] % p ;
printf("%lld\n",fac[i]) ;
}
return 0 ;
}
线段树1
struct dy {
int lc , rc , sum ,tag ;
}tree[maxn*2+100] ;
int a[maxn] , n , m , t = 1 ;
void pushup(int u ) {
tree[u].sum = tree[tree[u].lc].sum + tree[tree[u].rc].sum ;
return ;
}
void pushdown(int u ,int l ,int r) {
int mid = (l+r) >> 1 ;
tree[tree[u].lc].sum += tree[u].tag * (mid-l+1) ;
tree[tree[u].lc].tag += tree[u].tag ;
tree[tree[u].rc].sum += tree[u].tag * (r-mid) ;
tree[tree[u].rc].tag += tree[u].tag ;
tree[u].tag = 0 ;
}
void build(int u ,int l ,int r) {
if(l == r) {
tree[u].sum = a[l] ;
tree[u].tag = 0 ;
return ;
}
int mid = (l+r) >> 1 ;
++ t ;
tree[u].lc = t ;
build(tree[u].lc,l,mid) ;
++t ;
tree[u].rc = t ;
build(tree[u].rc,mid+1,r) ;
pushup(u) ;
}
void update(int u ,int l ,int r ,int ll ,int rr ,int w) {
if(l == ll && r == rr) {
tree[u].tag += w ;
tree[u].sum += w * (r-l+1) ;
return ;
}
int mid = (l+r)/2 ;
pushdown(u,l,r) ;
if(rr <= mid) update(tree[u].lc,l,mid,ll,rr,w) ;
else if(ll > mid) update(tree[u].rc,mid+1,r,ll,rr,w) ;
else {
update(tree[u].lc,l,mid,ll,mid,w) ;
update(tree[u].rc,mid+1,r,mid+1,rr,w) ;
}
pushup(u) ;
}
int query(int u ,int l ,int r ,int ll ,int rr ) {
if(l == ll && r == rr) {
return tree[u].sum ;
}
int mid = (l+r) / 2 ;
pushdown(u,l,r) ;
if(rr <= mid) return query(tree[u].lc,l,mid,ll,rr) ;
else if(ll > mid) return query(tree[u].rc,mid+1,r,ll,rr) ;
else {
return query(tree[u].lc,l,mid,ll,mid) + query(tree[u].rc,mid+1,r,mid+1,rr);
}
pushup(u) ;
}
signed main () {
n = read() , m = read() ;
for(int i = 1 ; i <= n ; i ++) {
a[i] = read() ;
}
build(1,1,n) ;
while(m --) {
int opt = read() , x = read() , y = read() ;
if(opt == 1) {
int k = read() ;
update(1,1,n,x,y,k) ;
}else {
cout << query(1,1,n,x,y) << endl;
}
}
return 0 ;
}
LCA
/*
ps:本代码经过360,腾讯电脑管家,金山毒霸等权威软件严格杀毒,未携带任何病毒,请评测姬放心评测!!
*/
int n , m , s , f[maxn][30] , dep[maxn] , vis[maxn] , head[maxn] ;
struct dy {
int x , y , next ;
}a[maxn*2] ;int t ;
void dfs(int u ,int fa) {
dep[u] = dep[fa] + 1 ;
f[u][0] = fa ;
for(int i = 1 ; i <= 23 ; i ++) {
f[u][i] = f[f[u][i-1]][i-1] ;
}
for(int i = head[u] ; i ; i = a[i].next) {
int v = a[i].y ;
if(v != fa) {
dfs(v,u) ;
}
}
}
void add(int x ,int y) {
a[++t].x = x ;
a[t].y = y ;
a[t].next = head[x] ;
head[x] = t ;
}
int lca(int x ,int y) {
if(dep[x] < dep[y]) swap(x,y) ;
for(int i = 21 ; i >= 0 ; i --) {
if(dep[f[x][i]] >= dep[y]) {
x = f[x][i] ;
}
}
for(int i = 21 ; i >= 0 ; i --) {
if(f[x][i] != f[y][i]) {
x = f[x][i] ;
y = f[y][i] ;
}
}
if(x == y) return x ;
return f[x][0] ;
}
int main () {
n = read() , m = read() , s = read() ;
for(int i = 1 ; i < n ; i ++ ) {
int x = read() , y = read() ;
add(x,y) ;
add(y,x) ;
}
dfs(s,0);
while(m --) {
int x = read() , y = read() ;
printf("%d\n",lca(x,y)) ;
}
return 0 ;
}