码迷,mamicode.com
首页 > 其他好文 > 详细

各种模板

时间:2019-01-02 14:59:47      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:read   names   fine   visit   dep   scanf   puts   cin   begin   

2018-12-29起,将各类模板都保存至此处,各位可以对着这里背板子(有些代码可能借鉴了其他blog,请见谅)

目前已借鉴的博客:(大为感谢)

1. https://www.cnblogs.com/TheRoadToTheGold/p/6254255.html

2. https://www.cnblogs.com/chinhhh/p/7965433.html

3. https://www.luogu.org/blog/41868/soutionp2142

(有错的话请各路神犇指教)

1. 平衡树treap:

  1 #include <cstdio>
  2 #include <cstdlib>
  3 const int maxn = 100005;
  4 struct node
  5 {
  6     node *s[2];
  7     int k, f, siz;
  8     node() {}
  9     node(int w, node *son)
 10     {
 11         s[0] = s[1] = son;
 12         k = w;
 13         f = rand();
 14         siz = 1;
 15     }
 16     void pushup()
 17     {
 18         siz = s[0]->siz + s[1]->siz + 1;
 19     }
 20 } nil, base[maxn];
 21 typedef node *P_node;
 22 P_node null, len, Root;
 23 void treap_init()
 24 {
 25     nil = node(0, NULL);
 26     null = &nil;
 27     null->s[0] = null->s[1] = null;
 28     null->siz = 0;
 29     len = base;
 30     Root = null;
 31 }
 32 P_node newnode(int x)
 33 {
 34     *len = node(x, null);
 35     return len++;
 36 }
 37 P_node merge(P_node a, P_node b)
 38 {
 39     if (a == null)
 40         return b;
 41     if (b == null)
 42         return a;
 43     if (a->f > b->f) {
 44         a->s[1] = merge(a->s[1], b);
 45         a->pushup();
 46         return a;
 47     }
 48     b->s[0] = merge(a, b->s[0]);
 49     b->pushup();
 50     return b;
 51 }
 52 void split(P_node x, int k, P_node &l, P_node &r)
 53 {
 54     if (x == null)
 55     {
 56         l = r = null;
 57         return;
 58     }
 59     if (x->k <= k)
 60     {
 61         l = x;
 62         split(l->s[1], k, l->s[1], r);
 63     }
 64     else
 65     {
 66         r = x;
 67         split(r->s[0], k, l, r->s[0]);
 68     }
 69     x->pushup();
 70 }
 71 void Insert(int k)
 72 {
 73     P_node l, r;
 74     split(Root, k, l, r);
 75     Root = merge(merge(l, newnode(k)), r);
 76 }
 77 void Erase(int k)
 78 {
 79     P_node l, mid, r;
 80     split(Root, k - 1, l, r);
 81     split(r, k, mid, r);
 82     Root = merge(l, merge(merge(mid->s[0], mid->s[1]), r));
 83 }
 84 int Kth(P_node x, int k)
 85 {
 86     if (k <= x->s[0]->siz)
 87         return Kth(x->s[0], k);
 88     else if (k > x->s[0]->siz + 1)
 89         return Kth(x->s[1], k - x->s[0]->siz - 1);
 90     return x->k;
 91 }
 92 int get_edge(P_node x, int d)
 93 {
 94     while (x->s[d] != null) x = x->s[d];
 95     return x->k;
 96 }
 97 int q;
 98 inline int red()
 99 {
100     int tot = 0, f = 1;
101     char ch = getchar();
102     while (ch < 0 || 9 < ch)
103     {
104         if (ch == -)
105             f = -f;
106         ch = getchar();
107     }
108     while (0 <= ch && ch <= 9) tot = tot * 10 + ch - 48, ch = getchar();
109     return tot * f;
110 }
111 int main()
112 {
113     treap_init();
114     q = red();
115     while (q--)
116     {
117         int c = red();
118         if (c == 1)
119             Insert(red());
120         else if (c == 2)
121             Erase(red());
122         else if (c == 3)
123         {
124             int k = red();
125             P_node l, r;
126             split(Root, k - 1, l, r);
127             printf("%d\n", l->siz + 1);
128             Root = merge(l, r);
129         }
130         else if (c == 4)
131             printf("%d\n", Kth(Root, red()));
132         else if (c == 5)
133         {
134             int k = red();
135             P_node l, r;
136             split(Root, k - 1, l, r);
137             printf("%d\n", get_edge(l, 1));
138             Root = merge(l, r);
139         }
140         else
141         {
142             int k = red();
143             P_node l, r;
144             split(Root, k, l, r);
145             printf("%d\n", get_edge(r, 0));
146             Root = merge(l, r);
147         }
148     }
149     return 0;
150 }

2. 线段树(带lazy标记)

 1 #include<iostream>
 2 #include<cstdio>
 3 #define MAXN 1000001
 4 #define ll long long
 5 using namespace std;
 6 unsigned ll n, m, a[MAXN], ans[MAXN << 2], tag[MAXN << 2];
 7 inline ll ls(ll x)
 8 {
 9     return x << 1;
10 }
11 inline ll rs(ll x)
12 {
13     return x << 1 | 1;
14 }
15 void scan()
16 {
17     cin >> n >> m;
18     for (ll i = 1; i <= n; i++)
19         scanf("%lld", &a[i]);
20 }
21 inline void push_up(ll p)
22 {
23     ans[p] = ans[ls(p)] + ans[rs(p)];
24 }
25 void build(ll p, ll l, ll r)
26 {
27     tag[p] = 0;
28     if (l == r) { ans[p] = a[l]; return; }
29     ll mid = (l + r) >> 1;
30     build(ls(p), l, mid);
31     build(rs(p), mid + 1, r);
32     push_up(p);
33 }
34 inline void f(ll p, ll l, ll r, ll k)
35 {
36     tag[p] = tag[p] + k;
37     ans[p] = ans[p] + k * (r - l + 1);
38 }
39 inline void push_down(ll p, ll l, ll r)
40 {
41     ll mid = (l + r) >> 1;
42     f(ls(p), l, mid, tag[p]);
43     f(rs(p), mid + 1, r, tag[p]);
44     tag[p] = 0;
45 }
46 inline void update(ll nl, ll nr, ll l, ll r, ll p, ll k)
47 {
48     if (nl <= l && r <= nr)
49     {
50         ans[p] += k * (r - l + 1);
51         tag[p] += k;
52         return;
53     }
54     push_down(p, l, r);
55     ll mid = (l + r) >> 1;
56     if (nl <= mid)update(nl, nr, l, mid, ls(p), k);
57     if (nr > mid) update(nl, nr, mid + 1, r, rs(p), k);
58     push_up(p);
59 }
60 ll query(ll q_x, ll q_y, ll l, ll r, ll p)
61 {
62     ll res = 0;
63     if (q_x <= l && r <= q_y)return ans[p];
64     ll mid = (l + r) >> 1;
65     push_down(p, l, r);
66     if (q_x <= mid)res += query(q_x, q_y, l, mid, ls(p));
67     if (q_y > mid) res += query(q_x, q_y, mid + 1, r, rs(p));
68     return res;
69 }
70 int main()
71 {
72     ll a1, b, c, d, e, f;
73     scan();
74     build(1, 1, n);
75     while (m--)
76     {
77         scanf("%lld", &a1);
78         switch (a1)
79         {
80         case 1: {
81             scanf("%lld%lld%lld", &b, &c, &d);
82             update(b, c, 1, n, 1, d);
83             break;
84         }
85         case 2: {
86             scanf("%lld%lld", &e, &f);
87             printf("%lld\n", query(e, f, 1, n, 1));
88             break;
89         }
90         }
91     }
92     return 0;
93 }

 

3. 并查集

#include<iostream>
using namespace std;
const int N = 10000;
int n, m;
int fa[N], size[N];
inline void init()
{
    for (int i = 1; i <= n; i++)fa[i] = i, size[i] = 1;
}
inline int find(int x)
{
    return x == fa[x] ? x : fa[x] = find(fa[x]);
}
inline void merge(int x, int y)
{
    int xx = find(x), yy = find(y);
    if (size[xx] < size[yy])swap(xx, yy);
    fa[yy] = xx; size[xx] += size[yy];
}
inline bool both(int x, int y)
{
    if (find(x) == find(y))
        return 1;
    else return 0;
}
int main()
{
    cin >> n >> m;
    init();
    for (int i = 1; i <= m; i++)
    {
        int a, b, tmp;
        cin >> tmp;
        if (tmp == 1)
        {
            cin >> a >> b;
            merge(a, b);
        }
        if (tmp == 2)
        {
            cin >> a >> b;
            if (both(a, b))
                cout << Y << endl;
            else cout << N << endl;
        }
    }
    return 0;
}

4. 快速排序(sort也不错)

#include<bits/stdc++.h>
using namespace std;
void qsort(int, int);
int a[100001], sum;
int main()
{
    int n, i;
    cin >> n;
    for (i = 1; i <= n; i++)
        cin >> a[i];
    qsort(1, n);
    for (i = 1; i <= n; i++)
        cout << a[i] << " ";
    cout << endl;
}
void qsort(int l, int r)
{
    int i, j, mid, p;
    i = l; j = r;
    mid = a[(l + r) / 2];
    do
    {
        while (a[i] < mid) i++;
        while (a[j] > mid) j--;
        if (i <= j)
        {
            p = a[i]; a[i] = a[j]; a[j] = p;
            sum++;
            i++; j--;
        }
    } while (i <= j);
    if (l < j) qsort(l, j);
    if (i < r) qsort(i, r);
}

5. 树剖

#include <cstdio>
#include <algorithm>
#include <cctype>
using namespace std;
#define rint register int
#define mem(a,b) memset(a,(b),sizeof(a))
#define temp template<typename T>
typedef long long LL;
temp inline void read(T &x)
{
    x = 0;
    T w = 1, ch = getchar();
    while (!isdigit(ch) && ch != -) ch = getchar();
    if (ch == -) w = -1, ch = getchar();
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch^0);
        ch = getchar();
    }
    x = x * w;
}
#define mid ((l+r)>>1)
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define len (r-l+1)
const int maxn = 200000 + 10;
int n, m, r, mod;
int e, beg[maxn], nex[maxn], to[maxn], w[maxn], wt[maxn];
int a[maxn << 2], laz[maxn << 2];
int son[maxn], id[maxn], fa[maxn], cnt, dep[maxn], siz[maxn], top[maxn];
int res = 0;
inline void add(int x, int y)
{
    to[++e] = y;
    nex[e] = beg[x];
    beg[x] = e;
}
inline void pushdown(int rt, int lenn)
{
    laz[rt << 1] += laz[rt];
    laz[rt << 1 | 1] += laz[rt];
    a[rt << 1] += laz[rt] * (lenn - (lenn >> 1));
    a[rt << 1 | 1] += laz[rt] * (lenn >> 1);
    a[rt << 1] %= mod;
    a[rt << 1 | 1] %= mod;
    laz[rt] = 0;
}
inline void build(int rt, int l, int r)
{
    if (l == r)
    {
        a[rt] = wt[l];
        if (a[rt] > mod) a[rt] %= mod;
        return;
    }
    build(lson);
    build(rson);
    a[rt] = (a[rt << 1] + a[rt << 1 | 1]) % mod;
}
inline void query(int rt, int l, int r, int L, int R)
{
    if (L <= l && r <= R)
    {
        res += a[rt];
        res %= mod;
        return;
    }
    else
    {
        if (laz[rt]) pushdown(rt, len);
        if (L <= mid) query(lson, L, R);
        if (R > mid) query(rson, L, R);
    }
}
inline void update(int rt, int l, int r, int L, int R, int k)
{
    if (L <= l && r <= R)
    {
        laz[rt] += k;
        a[rt] += k * len;
    }
    else
    {
        if (laz[rt]) pushdown(rt, len);
        if (L <= mid) update(lson, L, R, k);
        if (R > mid) update(rson, L, R, k);
        a[rt] = (a[rt << 1] + a[rt << 1 | 1]) % mod;
    }
}
inline int qrange(int x, int y)
{
    int ans = 0;
    while (top[x] != top[y])
    {
        if (dep[top[x]] < dep[top[y]]) swap(x, y);
        res = 0;
        query(1, 1, n, id[top[x]], id[x]);
        ans += res;
        ans %= mod;
        x = fa[top[x]];
    }
    if (dep[x] > dep[y]) swap(x, y);
    res = 0;
    query(1, 1, n, id[x], id[y]);
    ans += res;
    return ans %= mod;
}
inline void updrange(int x, int y, int k)
{
    k %= mod;
    while (top[x] != top[y])
    {
        if (dep[top[x]] < dep[top[y]]) swap(x, y);
        update(1, 1, n, id[top[x]], id[x], k);
        x = fa[top[x]];
    }
    if (dep[x] > dep[y]) swap(x, y);
    update(1, 1, n, id[x], id[y], k);
}
inline int qson(int x)
{
    res = 0;
    query(1, 1, n, id[x], id[x] + siz[x] - 1);
    return res;
}
inline void updson(int x, int k)
{
    update(1, 1, n, id[x], id[x] + siz[x] - 1, k);
}
inline void dfs1(int x, int f, int deep)
{
    dep[x] = deep;
    fa[x] = f;
    siz[x] = 1;
    int maxson = -1;
    for (rint i = beg[x]; i; i = nex[i])
    {
        int y = to[i];
        if (y == f) continue;
        dfs1(y, x, deep + 1);
        siz[x] += siz[y];
        if (siz[x] > maxson) son[x] = y, maxson = siz[y];
    }
}
inline void dfs2(int x, int topf)
{
    id[x] = ++cnt;
    wt[cnt] = w[x];
    top[x] = topf;
    if (!son[x]) return;
    dfs2(son[x], topf);
    for (rint i = beg[x]; i; i = nex[i])
    {
        int y = to[i];
        if (y == fa[x] || y == son[x]) continue;
        dfs2(y, y);
    }
}
int main()
{
    read(n); read(m); read(r); read(mod);
    for (rint i = 1; i <= n; i++)
        read(w[i]);
    for (rint i = 1; i < n; i++)
    {
        int a, b;
        read(a); read(b);
        add(a, b); add(b, a);
    }
    dfs1(r, 0, 1);
    dfs2(r, r);
    build(1, 1, n);
    while (m--)
    {
        int k, x, y, z;
        read(k);
        if (k == 1)
        {
            read(x);
            read(y);
            read(z);
            updrange(x, y, z);
        }
        else if (k == 2)
        {
            read(x);
            read(y);
            printf("%d\n", qrange(x, y));
        }
        else if (k == 3)
        {
            read(x);
            read(y);
            updson(x, y);
        }
        else
        {
            read(x);
            printf("%d\n", qson(x));
        }
    }
    //    getchar();
    //    getchar();
}

 6. tarjan强联通分量

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <iostream>
 5 using namespace std;
 6 struct node
 7 {
 8     int v, next;
 9 }edge[1001];
10 int DFN[1001], LOW[1001];
11 int stack[1001], heads[1001], visit[1001], cnt, tot, index;
12 void add(int x, int y)
13 {
14     edge[++cnt].next = heads[x];
15     edge[cnt].v = y;
16     heads[x] = cnt;
17     return;
18 }
19 void tarjan(int x)
20 {
21     DFN[x] = LOW[x] = ++tot;
22     stack[++index] = x;
23     visit[x] = 1;
24     for (int i = heads[x]; i != -1; i = edge[i].next)
25     {
26         if (!DFN[edge[i].v])
27         {
28             tarjan(edge[i].v);
29             LOW[x] = min(LOW[x], LOW[edge[i].v]);
30         }
31         else if (visit[edge[i].v])
32         {
33             LOW[x] = min(LOW[x], DFN[edge[i].v]);
34         }
35     }
36     if (LOW[x] == DFN[x])
37     {
38         do
39         {
40             printf("%d ", stack[index]);
41             visit[stack[index]] = 0;
42             index--;
43         } while (x != stack[index + 1]);
44         printf("\n");
45     }
46     return;
47 }
48 int main()
49 {
50     memset(heads, -1, sizeof(heads));
51     int n, m;
52     cin >> n >> m;
53     int x, y;
54     for (int i = 1; i <= m; i++)
55     {
56         cin >> x >> y;
57         add(x, y);
58     }
59     for (int i = 1; i <= n; i++)
60         if (!DFN[i])  tarjan(i);
61     //getchar();
62     //getchar();
63     //getchar();
64     return 0;
65 }

7. 最大流(Ford,EK,Dinic,ISAP)

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <vector>
  5 #include <queue>
  6 using namespace std;
  7 struct edge
  8 {
  9     int to, cap, rev;
 10 };
 11 const int maxn = 10001, INF = 0x7F7F7F7F;
 12 int n, m;
 13 vector <edge> G[maxn + 1];
 14 edge make_edge(int to, int cap, int rev)
 15 {
 16     edge x;
 17     x.to = to, x.cap = cap, x.rev = rev;
 18     return x;
 19 }
 20 void add_edge(int from, int to, int cap)
 21 {
 22     G[from].push_back(make_edge(to, cap, G[to].size()));
 23     G[to].push_back(make_edge(from, 0, G[from].size() - 1));
 24 }
 25 void init()
 26 {
 27     for (int i = 1; i <= m; i++)
 28     {
 29         int u, v, w;
 30         scanf("%d%d%d", &u, &v, &w);
 31         add_edge(u, v, w);
 32     }
 33 }
 34 namespace Ford_Fulkerson
 35 {
 36     bool used[maxn + 1];
 37     int dfs(int x, int t, int f)
 38     {
 39         if (x == t) return f;
 40         used[x] = true;
 41         for (int i = 0; i < G[x].size(); i++)
 42         {
 43             edge &e = G[x][i];
 44             if (!used[e.to] && e.cap > 0)
 45             {
 46                 int d = dfs(e.to, t, min(f, e.cap));
 47                 if (d > 0)
 48                 {
 49                     e.cap -= d;
 50                     G[e.to][e.rev].cap += d;
 51                     return d;
 52                 }
 53             }
 54         }
 55         return 0;
 56     }
 57     int max_flow(int s, int t)
 58     {
 59         int flow = 0;
 60         for (;;)
 61         {
 62             memset(used, 0, sizeof(used));
 63             int f = dfs(s, t, INF);
 64             if (f == 0) return flow;
 65             flow += f;
 66         }
 67     }
 68 }
 69 namespace Edmond_Karp
 70 {
 71     bool vis[maxn + 1];
 72     int prev[maxn + 1];
 73     int pree[maxn + 1];
 74     void bfs(int s)
 75     {
 76         memset(vis, 0, sizeof(vis));
 77         memset(prev, -1, sizeof(prev));
 78         memset(pree, -1, sizeof(prev));
 79         queue <int> q;
 80         vis[s] = true;
 81         q.push(s);
 82         while (!q.empty())
 83         {
 84             int x = q.front();
 85             q.pop();
 86             for (int i = 0; i < G[x].size(); i++)
 87             {
 88                 edge &e = G[x][i];
 89                 if (e.cap > 0 && !vis[e.to])
 90                 {
 91                     prev[e.to] = x;
 92                     pree[e.to] = i;
 93                     vis[e.to] = true;
 94                     q.push(e.to);
 95                 }
 96             }
 97         }
 98     }
 99 
100     int max_flow(int s, int t)
101     {
102         int flow = 0;
103         for (;;)
104         {
105             bfs(s);
106             if (!vis[t]) return flow;
107             int d = INF;
108             for (int i = t; prev[i] != -1; i = prev[i])
109                 d = min(d, G[prev[i]][pree[i]].cap);
110             for (int i = t; prev[i] != -1; i = prev[i])
111             {
112                 edge &e = G[prev[i]][pree[i]];
113                 e.cap -= d;
114                 G[e.to][e.rev].cap += d;
115             }
116             flow += d;
117         }
118     }
119 }
120 namespace Dinic
121 {
122     int level[maxn + 1];
123     int iter[maxn + 1];
124     void bfs(int s)
125     {
126         memset(level, -1, sizeof(level));
127         queue <int> q;
128         level[s] = 0;
129         q.push(s);
130         while (!q.empty())
131         {
132             int x = q.front();
133             q.pop();
134             for (int i = 0; i < G[x].size(); i++)
135             {
136                 edge &e = G[x][i];
137                 if (e.cap > 0 && level[e.to] < 0)
138                 {
139                     level[e.to] = level[x] + 1;
140                     q.push(e.to);
141                 }
142             }
143         }
144     }
145     int dfs(int x, int t, int f)
146     {
147         if (x == t) return f;
148         for (int &i = iter[x]; i < G[x].size(); i++)
149         {
150             edge &e = G[x][i];
151             if (e.cap > 0 && level[e.to] > level[x])
152             {
153                 int d = dfs(e.to, t, min(f, e.cap));
154                 if (d > 0)
155                 {
156                     e.cap -= d;
157                     G[e.to][e.rev].cap += d;
158                     return d;
159                 }
160             }
161         }
162         return 0;
163     }
164     int max_flow(int s, int t)
165     {
166         int flow = 0;
167         for (;;)
168         {
169             bfs(s);
170             if (level[t] < 0) return flow;
171             memset(iter, 0, sizeof(iter));
172             int f;
173             while ((f = dfs(s, t, INF)) > 0)
174                 flow += f;
175         }
176     }
177 }
178 namespace ISAP
179 {
180     int gap[maxn + 1];
181     int iter[maxn + 1];
182     int level[maxn + 1];
183     void bfs(int t)
184     {
185         memset(gap, 0, sizeof(gap));
186         memset(level, -1, sizeof(level));
187         queue <int> q;
188         level[t] = 1;
189         gap[level[t]] = 1;
190         q.push(t);
191         while (!q.empty())
192         {
193             int x = q.front();
194             q.pop();
195             for (int i = 0; i < G[x].size(); i++)
196             {
197                 edge &e = G[x][i];
198                 if (level[e.to] < 0) {
199                     level[e.to] = level[x] + 1;
200                     gap[level[e.to]]++;
201                     q.push(e.to);
202                 }
203             }
204         }
205     }
206 
207     int dfs(int x, int s, int t, int f)
208     {
209         if (x == t) return f;
210         int flow = 0;
211         for (int &i = iter[x]; i < G[x].size(); i++)
212         {
213             edge &e = G[x][i];
214             if (e.cap > 0 && level[x] == level[e.to] + 1)
215             {
216                 int d = dfs(e.to, s, t, min(f - flow, e.cap));
217                 e.cap -= d;
218                 G[e.to][e.rev].cap += d;
219                 flow += d;
220                 if (f == flow) return f;
221             }
222         }
223 
224         gap[level[x]]--;
225         if (gap[level[x]] == 0)
226             level[s] = n + 1;
227         iter[x] = 0;
228         gap[++level[x]]++;
229         return flow;
230     }
231     int max_flow(int s, int t)
232     {
233         int flow = 0;
234         bfs(t);
235         memset(iter, 0, sizeof(iter));
236         while (level[s] <= n)
237             flow += dfs(s, s, t, INF);
238         return flow;
239     }
240 }
241 int main()
242 {
243     int s, t;
244     scanf("%d%d%d%d", &n, &m, &s, &t);
245     init();
246     printf("%d\n", ISAP::max_flow(s, t));
247     return 0;
248 }

8. 筛法(伪筛)

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstdlib>
 4 #include <algorithm>
 5 #include <cstring>
 6 using namespace std;
 7 #define maxn 1000000+10
 8 bool a[maxn];
 9 int n;
10 inline void cal_prime()
11 {
12     memset(a, true, sizeof(a));
13     for (int i = 2; i <= n; i++)
14     {
15         if (!a[i]) continue;
16         for (int j = i * 2; j <= n; j += i)
17             a[j] = false;
18     }
19 }
20 int main()
21 {
22     scanf("%d", &n);
23     int num;
24     scanf("%d", &num);
25     cal_prime();
26     a[0] = false;
27     a[1] = false;
28     for (int i = 1; i <= num; i++)
29     {
30         int cmp;
31         scanf("%d", &cmp);
32         if (a[cmp]) printf("Yes\n");
33         else printf("No\n");
34     }
35     printf("no");
36     return 0;
37 }

9. 可并堆

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 const int maxn = 100000 + 5;
 5 int read()
 6 {
 7     int x = 0, f = 1;
 8     char ch = getchar();
 9     while ((ch < 0) || (ch > 9))
10     {
11         if (ch == -)
12             f = -f;
13         ch = getchar();
14     }
15     while ((ch >= 0) && (ch <= 9))
16     {
17         x = x * 10 + ch - 0;
18         ch = getchar();
19     }
20     return x * f;
21 }
22 struct node
23 {
24     node* son[2];
25     node* fa;
26     int val, dist, pos;
27 };
28 node tnode[maxn + 10];
29 node *p[maxn + 10];
30 int n, m;
31 inline int upd_dist(node* now)
32 {
33     if (now->son[1] != NULL)
34         now->dist = now->son[1]->dist + 1;
35     else
36         now->dist = 0;
37     return 0;
38 }
39 node *merge(node *a, node *b)
40 {
41     if (a == NULL) return b;
42     if (b == NULL) return a;
43     if ((a->val >= b->val) && (a->pos > b->pos))
44         swap(a, b);
45     a->son[1] = merge(a->son[1], b);
46     a->son[1]->fa = a;
47     if ((a->son[0] != NULL) && (a->son[1] != NULL) && (a->son[0]->dist < a->son[1]->dist))
48         swap(a->son[0], a->son[1]);
49     else if ((a->son[0] == NULL) && (a->son[1] != NULL))
50     {
51         a->son[0] = a->son[1];
52         a->son[1] = NULL;
53     }
54     upd_dist(a);
55     return a;
56 }
57 node* getf(node *x)
58 {
59     if (x->fa == NULL) return x;
60     return getf(x->fa);
61 }
62 int getmin_pop(node *x)
63 {
64     node *f = getf(x);
65     int v = f->val;
66     node* s = merge(f->son[0], f->son[1]);
67     if (s != NULL) s->fa = NULL;
68     f->pos = -1;
69     return v;
70 }
71 int main()
72 {
73     n = read(); m = read();
74     for (register int i = 1; i <= n; i++)
75     {
76         p[i] = &tnode[i];
77         p[i]->son[0] = p[i]->son[1] = p[i]->fa = NULL;
78         p[i]->val = read();
79         p[i]->dist = 0;
80         p[i]->pos = i;
81     }
82     while (m--)
83     {
84         int op = read(), a = read();
85         if (op == 1)
86         {
87             int b = read();
88             if (p[a]->pos == -1 || p[b]->pos == -1) continue;
89             node *fa = getf(p[a]);
90             node *fb = getf(p[b]);
91             if (fa == fb) continue;
92             merge(fa, fb);
93         }
94         else
95             if (p[a]->pos == -1) puts("-1");
96             else printf("%d\n", getmin_pop(p[a]));
97     }
98     return 0;
99 }

10. 快速幂

 1 #include<cstdio>
 2 int b,k;
 3 int f(int p){
 4     if(p==0) return 1%k;
 5     int tmp=f(p>>1);
 6     tmp=tmp*tmp%k;
 7     if(p&1) tmp=(tmp*b)%k;
 8     return tmp;
 9 }
10 int main(){
11     int p;
12     scanf("%d%d%d",&b,&p,&k);
13     printf("%d",b);
14     b%=k;
15     printf("^%d mod %d=%d\n",p,k,f(p));
16     return 0;
17 }

11. spfa最短路

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int inf = 2147483647;
 4 const int maxn = 10005;
 5 int n, m, b, e = 0, i, j;
 6 int dis[maxn], head[500005];
 7 bool vis[maxn];
 8 struct node
 9 {
10     int next, to, dis;
11 }edge[500005];
12 
13 queue<int> q;
14 void addedge(int from, int to, int dis)
15 {
16     edge[++e].next = head[from];
17     edge[e].dis = dis;
18     edge[e].to = to;
19     head[from] = e;
20 }
21 
22 void spfa()
23 {
24     for (i = 1; i <= n; i++) dis[i] = inf;
25     dis[b] = 0;
26     q.push(b), vis[b] = 1;
27     while (!q.empty())
28     {
29         int begin = q.front();
30         q.pop();
31         for (i = head[begin]; i; i = edge[i].next)
32         {
33             if (dis[edge[i].to] > dis[begin] + edge[i].dis)
34             {
35                 dis[edge[i].to] = dis[begin] + edge[i].dis;
36                 if (!vis[edge[i].to])
37                 {
38                     vis[edge[i].to] = 1;
39                     q.push(edge[i].to);
40                 }
41             }
42         }
43         vis[begin] = 0;
44     }
45 }
46 int main()
47 {
48     cin >> n >> m >> b;
49     for (int i = 1; i <= m; i++)
50     {
51         int f, t, d;
52         cin >> f >> t >> d;
53         addedge(f, t, d);
54     }
55     spfa();
56     for (int i = 1; i <= n; i++)
57         if (b == i) cout << 0 <<  ;
58         else cout << dis[i] <<  ;
59     return 0;
60 }

12. Floyd最短路

 1 #include <cstdio>
 2 int e[1005][1005], k, i, j, n, m, t1, t2, t3;
 3 int main()
 4 {
 5     int inf = 99999999;
 6     scanf("%d %d", &n, &m);
 7     for (i = 1; i <= n; i++)
 8         for (j = 1; j <= n; j++)
 9             if (i == j) e[i][j] = 0;
10             else e[i][j] = inf;
11     for (i = 1; i <= m; i++)
12     {
13         scanf("%d %d %d", &t1, &t2, &t3);
14         e[t1][t2] = t3;
15     }
16     for (k = 1; k <= n; k++)
17         for (i = 1; i <= n; i++)
18             for (j = 1; j <= n; j++)
19                 if (e[i][j] > e[i][k] + e[k][j])
20                     e[i][j] = e[i][k] + e[k][j];
21     for (i = 1; i <= n; i++)
22     {
23         for (j = 1; j <= n; j++)
24             printf("%10d", e[i][j]);
25     }
26         printf("\n");
27 }

 13. 高精加减乘除

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 const int M=10,P=1; 
  4 
  5 struct BigNum
  6 {
  7        int n[100000],l,Y;
  8        BigNum(){l=1,memset(n,0,sizeof(n));}
  9        //----------------------------------
 10        void init()
 11        {
 12             string s;
 13             cin>>s;
 14             int now=0,ct=0,c1=1;
 15             for(int i=s.length()-1;i>=0;i--)
 16             {
 17              n[now]+=(s[i]-0)*c1;
 18              c1*=10;
 19              ct++;
 20              if(ct==P&&i!=0)
 21              {
 22               now++;
 23               c1=1;
 24               ct=0;
 25              }        
 26             }
 27             l=now+1;     
 28        }
 29        //-----------------------------------
 30        void print()
 31        {
 32             printf("%d",n[l-1]);
 33             for(int i=l-2;i>=0;i--)
 34             printf("%0*d",P,n[i]);
 35             printf("\n");
 36        }
 37        //------------------------------------
 38        BigNum operator +(BigNum x)const
 39        {
 40               BigNum t=*this;
 41               if(x.l>t.l)t.l=x.l;
 42               for(int i=0;i<t.l;i++)
 43               {
 44                t.n[i]+=x.n[i];
 45                if(t.n[i]>=M)
 46                {
 47                    t.n[i+1]+=t.n[i]/M;
 48                    t.n[i]%=M;             
 49                }        
 50               }
 51               while(t.n[t.l])
 52               {
 53                   t.n[t.l+1]+=t.n[t.l]/M;
 54                   t.n[t.l++]%=M;               
 55               }     
 56               return t;  
 57        }
 58        //--------------------------------------
 59        bool operator < (BigNum x) const
 60        {
 61            BigNum t=*this;
 62            if(t.l!=x.l)return t.l<x.l;
 63            for(int i=t.l-1;i>=0;i--)
 64            {
 65               if(t.n[i]!=x.n[i])return t.n[i]<x.n[i];        
 66            }
 67            return 0;       
 68        }
 69        BigNum operator -(BigNum x)const
 70        {
 71            BigNum t=*this;
 72            if(t<x){printf("-");swap(t,x);} 
 73            for(int i=0;i<t.l;i++)
 74            {
 75             t.n[i]-=x.n[i];
 76             if(t.n[i]<0)
 77             {
 78                 t.n[i]+=M;
 79                 --t.n[i+1];            
 80             }        
 81            } 
 82            while(!t.n[t.l-1]&&t.l>1)t.l--;
 83            return t;     
 84        }
 85        //--------------------------------------------
 86        BigNum operator * (BigNum x) const//提供了2种*运算
 87        {
 88               BigNum c,t=*this;
 89               c.l=t.l+x.l-1;
 90               for(int i=0;i<t.l;i++)
 91               for(int j=0;j<x.l;j++)
 92               {
 93                   c.n[i+j]+=t.n[i]*x.n[j];
 94                   if(c.n[i+j]>=M)
 95                   {
 96                       c.n[i+j+1]+=c.n[i+j]/M;
 97                       c.n[i+j]%=M;               
 98                   }        
 99               }
100               while(c.n[c.l])
101               {
102                    c.n[c.l+1]+=c.n[c.l]/M;
103                    c.n[c.l++]%=M;               
104               }       
105               return c;
106        }
107        BigNum operator * (int x) const
108        {
109           BigNum t=*this,c;
110           c.l=t.l;
111           for(int i=0;i<t.l;i++)
112           {
113               c.n[i]+=t.n[i]*x;
114               if(c.n[i]>=M)
115               {
116                    c.n[i+1]+=c.n[i]/M;
117                    c.n[i]%=M;             
118               }        
119           }       
120           while(c.n[c.l])
121           {
122                  c.n[c.l+1]+=c.n[c.l]/M;
123                  c.n[c.l++]%=M;               
124           }
125           return c;
126        }
127        //--------------------------------------------
128     void Add(int x){if(x||l)n[l++]=x;}
129     void Re(){reverse(n,n+l);}
130     BigNum operator /(const BigNum &x)const//提供了2种/运算
131     {
132         BigNum t=*this,r,y;
133         y.l=0,r.l=t.l;
134         for(int i=t.l-1;i>=0;--i)
135         {
136             y.Add(t.n[i]);
137             y.Re();
138             while(!(y<x))y=y-x,r.n[i]++;
139             while(!y.n[y.l-1] && y.l>1)--y.l;
140             y.Re();
141         }
142         while(!r.n[r.l-1] && r.l>1)--r.l;
143         return r;
144     }
145     BigNum operator /(const int &x)const
146     {
147         BigNum t=*this,r;
148         r.l=t.l;
149         int tmp=0;
150         for(int i=t.l-1;i>=0;--i)
151         {
152             tmp+=t.n[i];
153             if(tmp>=x)
154                 r.n[i]+=tmp/x,tmp%=x;
155             tmp*=M;
156         }
157         while(!r.n[r.l-1] && r.l>1)--r.l;
158         return r;
159     }
160        //---------------------------------------
161 
162 }a,b,c;
163 
164 int main()
165 {
166     a.init();
167     b.init();
168 /*   
169     c=a+b;
170     c.print();
171 */ 
172     c=a-b;
173     c.print();
174 /*
175     c=a*b;
176     c.print();
177 
178     c=a/b;
179     c.print();
180 */
181     return 0;        
182 }

 

各种模板

标签:read   names   fine   visit   dep   scanf   puts   cin   begin   

原文地址:https://www.cnblogs.com/tangwuling/p/10195472.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!