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

some 模板

时间:2019-10-18 21:57:39      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:void   har   uil   print   struct   adc   git   using   bsp   

注:此博客部分模板(也有可能是全部)来源于其它大佬的bolg

1.高精度

技术图片
  1 #include<bits/stdc++.h>
  2 #define MAXN 501
  3 const int Big_B = 10; const int Big_L = 1;
  4 inline int intcmp_ (int a, int b) { if (a > b) return 1; return a < b ? -1 : 0; }
  5 struct Int 
  6 {
  7 #define rg register
  8     inline int max (int a, int b) { return a > b ? a : b; }
  9     inline int min (int a, int b) { return a < b ? a : b; }
 10     std :: vector <int> c; Int () {} typedef long long LL; 
 11     Int (int x) { for (; x > 0; c.push_back (x % Big_B), x /= Big_B); }
 12     Int (LL x) { for (; x > 0; c.push_back (x % Big_B), x /= Big_B); }
 13     inline void CrZ () { for (; !c.empty () && c.back () == 0; c.pop_back ()); }
 14     inline Int &operator += (const Int &rhs){
 15         c.resize (max (c.size (), rhs.c.size ())); rg int i, t = 0, S;
 16         for (i = 0, S = rhs.c.size (); i < S; ++ i)
 17             c[i] += rhs.c[i] + t, t = c[i] >= Big_B, c[i] -= Big_B & (-t);
 18         for (i = rhs.c.size (), S = c.size (); t && i < S; ++ i)
 19             c[i] += t, t = c[i] >= Big_B, c[i] -= Big_B & (-t);
 20         if (t) c.push_back (t); return *this;
 21     }
 22     inline Int &operator -= (const Int &rhs){
 23         c.resize (max (c.size (), rhs.c.size ())); rg int i, t = 0, S;
 24         for (i = 0, S = rhs.c.size (); i < S; ++ i)
 25             c[i] -= rhs.c[i] + t, t = c[i] < 0, c[i] += Big_B & (-t);
 26         for (i = rhs.c.size (), S = c.size (); t && i < S; ++ i)
 27             c[i] -= t, t = c[i] < 0, c[i] += Big_B & (-t);
 28         CrZ (); return *this;
 29     }
 30     inline Int &operator *= (const Int &rhs){
 31         rg int na = c.size (), i, j, S, ai; 
 32         c.resize (na + rhs.c.size ()); LL t;
 33         for (i = na - 1; i >= 0; -- i){
 34             ai = c[i], t = 0, c[i] = 0;
 35             for (j = 0, S = rhs.c.size (); j < S; ++ j){
 36                 t += c[i + j] + (LL) ai * rhs.c[j];
 37                 c[i + j] = t % Big_B, t /= Big_B;
 38             }
 39             for (j = rhs.c.size (), S = c.size (); t != 0 && i + j < S; ++ j)
 40                 t += c[i + j], c[i + j] = t % Big_B, t /= Big_B;
 41             assert (t == 0);
 42         }
 43         CrZ (); return *this;
 44     }
 45     inline Int &operator /= (const Int &rhs) { return *this = div (rhs); }
 46     inline Int &operator %= (const Int &rhs) { return div (rhs), *this; }
 47     inline Int &shlb (int l = 1){
 48         if (c.empty ()) return *this; c.resize (c.size () + l);rg int i;
 49         for (i = c.size () - 1; i >= l; -- i) c[i] = c[i - l];
 50         for (i = 0; i < l; ++ i) c[i] = 0;
 51         return *this;
 52     }
 53     inline Int &shrb (int l = 1){
 54         for (rg int i = 0; i < c.size () - l; ++ i) c[i] = c[i + l];
 55         c.resize (max (c.size () - l, 0)); return *this;
 56     }
 57     inline Int div (const Int &rhs){
 58         assert (!rhs.c.empty ()); Int q, r; rg int i; if (rhs > *this) return 0;
 59         q.c.resize (c.size () - rhs.c.size () + 1); rg int _l, _r, mid;
 60         for (i = c.size () - 1; i > c.size () - rhs.c.size (); -- i) r.shlb (), r += c[i];
 61         for (i = c.size () - rhs.c.size (); i >= 0; -- i){
 62             r.shlb (); r += c[i]; 
 63             if (r.Comp (rhs) < 0) q.c[i] = 0;
 64             else {
 65                 _l = 0, _r = Big_B;
 66                 for (; _l != _r; ){
 67                     mid = _l + _r >> 1;
 68                     if ((rhs * mid).Comp (r) <= 0) _l = mid + 1; else _r = mid;
 69                 }
 70                 q.c[i] = _l - 1, r -= rhs * q.c[i];
 71             }
 72         }
 73         q.CrZ (), *this = r; return q;
 74     }
 75     inline int Comp (const Int &rhs) const {
 76         if (c.size () != rhs.c.size ()) return intcmp_ (c.size (), rhs.c.size ());
 77         for (rg int i = c.size () - 1; i >= 0; -- i) 
 78             if (c[i] != rhs.c[i]) return intcmp_ (c[i], rhs.c[i]);
 79         return 0;
 80     }
 81     friend inline Int operator + (const Int &lhs, const Int &rhs)
 82     { Int res = lhs; return res += rhs; }
 83     inline friend Int operator - (const Int &lhs, const Int &rhs){ 
 84         if (lhs < rhs){
 85             putchar (-); 
 86             Int res = rhs; return res -= lhs;
 87         }
 88         else { Int res = lhs; return res -= rhs; }
 89     }
 90     friend inline Int operator * (const Int &lhs, const Int &rhs)
 91     { Int res = lhs; return res *= rhs; }
 92     friend inline Int operator / (const Int &lhs, const Int &rhs)
 93     { Int res = lhs; return res.div (rhs); }
 94     friend inline Int operator % (const Int &lhs, const Int &rhs)
 95     { Int res = lhs; return res.div (rhs), res; }
 96     friend inline std :: ostream &operator << (std :: ostream &out, const Int &rhs){ 
 97         if (rhs.c.size () == 0) out << "0";
 98         else {
 99             out << rhs.c.back ();
100             for (rg int i = rhs.c.size () - 2; i >= 0; -- i)
101                 out << std :: setfill (0) << std :: setw (Big_L) << rhs.c[i];
102         }
103         return out;
104     } 
105     friend inline std :: istream &operator >> (std :: istream &in, Int &rhs){
106         static char s[100000];
107         in >> s + 1; int Len = strlen (s + 1);
108         int v = 0; LL r = 0, p = 1;
109         for (rg int i = Len; i >= 1; -- i){
110             ++ v; r = r + (s[i] - 0) * p, p *= 10;
111             if (v == Big_L) rhs.c.push_back (r), r = 0, v = 0, p = 1;
112         }
113         if (v != 0) rhs.c.push_back (r); return in;
114     }
115     friend inline bool operator < (const Int &lhs, const Int &rhs)
116     { return lhs.Comp (rhs) < 0; }
117     friend inline bool operator <= (const Int &lhs, const Int &rhs)
118     { return lhs.Comp (rhs) <= 0; }
119     friend inline bool operator > (const Int &lhs, const Int &rhs)
120     { return lhs.Comp (rhs) > 0; }
121     friend inline bool operator >= (const Int &lhs, const Int &rhs)
122     { return lhs.Comp (rhs) >= 0; }
123     friend inline bool operator == (const Int &lhs, const Int &rhs)
124     { return lhs.Comp (rhs) == 0; }
125     friend inline bool operator != (const Int &lhs, const Int &rhs)
126     { return lhs.Comp (rhs) != 0; }
127 #undef rg    
128 };
129 int Main (){
130     return 0;
131 }
132 int ZlycerQan = Main ();
133 int main (int argc, char *argv[]) {;}
高精度

2.KMP算法

技术图片
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 int next[2333333];
 6 int la, lb, j;
 7 char a[2333333], b[2333333];
 8 int main () {
 9     cin >> a + 1;
10     cin >> b + 1;
11     la = strlen (a + 1);
12     lb = strlen (b + 1);
13     for (int i = 2; i <= lb; i++) {
14         while (j && b[i] != b[j + 1])
15             j = next[j];
16         if (b[j + 1] == b[i])j++;
17         next[i] = j;
18     }
19     j = 0;
20     for (int i = 1; i <= la; i++) {
21         while (j && b[j + 1] != a[i]) 
22             j = next[j];
23         if (b[j + 1] == a[i])j++;
24         if (j == lb) {
25             printf ("%d\n", i - lb + 1);
26             j = next[j];
27         }
28     }
29     for (int i = 1; i <= lb; i++) 
30         printf ("%d ", next[i]);
31     return 0;
32 }
KMP

求nxt数组是自己匹配自己从2开始

求最大匹配是从1开始遍历被匹配的串

3.加入堆优化的Dijkstra

技术图片
 1 #include <queue>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 using namespace std;
 6 const int N = 1010001;
 7 int n, m, s, cnt, head[N], dis[N];
 8 bool vis[N];
 9 struct node{
10     int next, to, w;
11 }e[N];
12 int read() {
13     int s = 0, w = 1;
14     char ch = getchar();
15     while(!isdigit(ch)){if(ch == -) w = -1;ch = getchar();}
16     while(isdigit(ch)){s = s * 10 + ch - 0;ch = getchar();}
17     return s * w;
18 }
19 void add(int x, int y, int z) {
20     e[++cnt].next = head[x];
21     e[cnt].to = y;
22     e[cnt].w = z;
23     head[x] = cnt;
24 }
25 struct Node {
26     int u, v;
27     bool operator<(const Node &b) const {
28         return u > b.u;
29     }
30 };
31 void dijikstra(int s) {
32     priority_queue <Node> q;
33     memset(dis, 0x3f3f3f3f, sizeof(dis));
34     dis[s] = 0;
35     Node o;
36     o.u = 0;
37     o.v = s;
38     q.push(o);
39     while(!q.empty()) {
40         int u = q.top().v;
41         int d = q.top().u;
42         q.pop();
43         if(d != dis[u])continue;
44         for(int i = head[u]; i; i = e[i].next) {
45             int v = e[i].to;
46             int w = e[i].w;
47             if(dis[v] > dis[u] + w) {
48                 dis[v] = dis[u] + w;
49                 Node p;
50                 p.u = dis[v], p.v = v;
51                 q.push(p);
52             }
53         }
54     }
55 }
56 int main () {
57     n = read();
58     m = read();
59     s = read();
60     while(m--) {
61         int x, y, z;
62         x = read();
63         y = read();
64         z = read();
65         add (x, y, z);
66     }
67     dijikstra(s);
68     for(int i = 1; i <= n; i++)
69         printf("%d ", dis[i]);
70     return 0; 
71 }
72 
73 堆优化 迪杰
Dijkstra

4.未加入堆优化的Dijkstra

技术图片
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 const int N = 500007;
 6 int head[N], cnt, n, m, s;
 7 long long dis[N];
 8 bool vis[N];
 9 struct node {
10     int next, to;
11     long long w;
12 }e[N];
13 void add(int x, int y, long long z) {
14     e[++cnt].next = head[x];
15     e[cnt].to = y;
16     e[cnt].w = z;
17     head[x] = cnt;
18 } 
19 void dijkstra(int s) {
20     for(int i = 1; i <= n; i++) dis[i] = 2147483647; 
21     dis[s] = 0;
22     for(int i = 1; i <= n; i++) {
23         int k = 1, maxn = 2147483647;
24         for(int j = 1; j <= n; j++)
25             if(!vis[j] && dis[j] <= maxn)
26                 k = j, maxn = dis[j];
27         vis[k] = 1;
28         for(int j = head[k]; j; j = e[j].next) 
29             if(dis[e[j].to] > dis[k] + e[j].w)
30                 dis[e[j].to] = dis[k] + e[j].w;
31     }
32 }
33 int main () {
34     scanf("%d%d%d", &n, &m, &s);
35     for(int i = 1; i <= m; i++) {
36         int x, y;
37         long long z;
38         scanf("%d%d%lld", &x, &y, &z);
39         add(x, y, z);
40     }
41     dijkstra(s);
42     for(int i = 1; i <= n; i++)
43         printf("%lld ", dis[i]);
44     return 0;
45 }
46 
47 朴素 迪杰
Dijkstra

5.SPFA

技术图片
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <queue>
 4 #define N 500005
 5 #define inf 2147483647
 6 using namespace std;
 7 int n, m, s, cnt;
 8 int dis[N], vis[N], head[N];
 9 struct node {
10     int next, to, w;
11 }tr[N];
12 void add (int x, int y, int z) {
13     tr[++cnt].to = y;
14     tr[cnt].next = head[x];
15     tr[cnt].w = z;
16     head[x] = cnt;
17 }
18 void spfa () {
19     queue<int> q;
20     for (int i = 1; i <= n; i++) 
21         dis[i] = inf;
22     vis[s] = 1;
23     q.push(s);
24     dis[s] = 0;
25     while (!q.empty()) {
26         int he = q.front();
27         q.pop();
28         vis[he] = 0;
29         for (int i = head[he]; i ;i = tr[i].next) {
30             if (dis[tr[i].to] > dis[he] + tr[i].w) {
31                 dis[tr[i].to] = dis[he] + tr[i].w;
32                 if (!vis[tr[i].to]) {
33                     vis[tr[i].to] = 1;
34                     q.push(tr[i].to);
35                 }
36             }
37         }
38     }
39 }
40 int main () {
41     scanf ("%d%d%d", &n, &m, &s);
42     for (int i = 1; i <= m; i++) {
43         int a, b, c;
44         scanf ("%d%d%d", &a, &b, &c);
45         add (a, b, c);
46     }
47     spfa ();
48     for (int i = 1; i <= n; i++) 
49         if (s == i) printf ("0 ");
50         else printf ("%d ", dis[i]);
51     return 0;
52 }
53 
54 spfa
SPFA

6.Tire树

技术图片
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 char ch[800000];
 6 int n, m, tr[8000000][26], s, root, tot, f[8000000];
 7 int main () {
 8     scanf ("%d", &n);
 9     for (int i = 1; i <= n; i++) {
10         root = 0;
11         cin >> ch;
12         int len = strlen (ch);
13         for (int j = 0; j < len; j++) {
14             int nu = ch[j] - a;
15             if (!tr[root][nu]) tr[root][nu] = ++tot;
16             root = tr[root][nu];
17         }
18         f[root] = 1;
19     }
20     scanf ("%d", &m);
21     for (int i = 1; i <= m; i++) {
22         root = 0;
23         cin >> ch;
24         int len = strlen (ch);
25         for (int j = 0; j < len; j++) {
26             int nu = ch[j] - a;
27             if (!tr[root][nu]) {
28                 printf ("WRONG\n");
29                 break;
30             }
31             root = tr[root][nu];
32             if (j == len - 1 && f[root] == 2) printf ("REPEAT\n");
33             if (j == len - 1 && f[root] == 1) f[root] = 2,printf ("OK\n"); 
34         }
35     }
36     return 0;
37 }
Tire树

7.树状数组

技术图片
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 int n, m, tr[1000000];
 5 int lowbit (int x) {
 6     return x & (-x);
 7 }
 8 void add (int x, int k) {
 9     while (x <= n) {
10         tr[x] += k;
11         x += lowbit (x);
12     }
13 }
14 int sum (int x) {
15     int ans = 0;
16     while (x !=0) {
17         ans += tr[x];
18         x -= lowbit (x);
19     }
20     return ans;
21 }
22 int main () {
23     scanf ("%d%d", &n, &m);
24     for (int i = 1; i <= n; i++) {
25         int a;
26         scanf ("%d", &a);
27         add (i, a);
28     }
29     for (int i = 1; i <= m; i++) {
30         int a, b, c;
31         scanf ("%d%d%d", &a, &b, &c);
32         if (a == 1) add (b, c);
33         if (a == 2) cout << sum(c) - sum (b - 1) << endl;
34     }
35     return 0;
36 }
单点修改+区间查询
技术图片
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 int n, m, now, x, c[500001];
 5 int lowbit (int x) {
 6     return x & -x;
 7 }
 8 void build (int x, int y) {
 9     while (x <= n) {
10         c[x] += y;
11         x += lowbit (x);
12     }
13 }
14 int sum (int x) {
15     int ans = 0;
16     while (x) {
17         ans += c[x];
18         x -= lowbit (x);
19     }
20     return ans;
21 }
22 int main () {
23     scanf ("%d%d", &n, &m);
24     for (int i = 1; i <= n; i++) {
25         scanf ("%d", &x);
26         build (i, x - now);
27         now = x;
28     }
29     while (m--) {
30         scanf ("%d", &x);
31         if (x == 1) {
32             int l, r, w;
33             scanf ("%d%d%d", &l, &r, &w);
34             build (l, w);
35             build (r + 1, -w);
36         }
37         else if (x == 2) {
38             scanf ("%d", &x);
39             printf ("%d\n", sum (x));
40         }
41     }
42     return 0;
43 }
区间修改+单点查询

 

就先整理到这里趴你们的博主太菜了

some 模板

标签:void   har   uil   print   struct   adc   git   using   bsp   

原文地址:https://www.cnblogs.com/yanxiujie/p/11700833.html

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