2014年11月19日01:37:51 广州赛区模板
ACM
增广路SAP
1. Const
2. Fin = '';
3. Fout = '';
4. Const
5. Maxm = 10000;
6. Maxn = 10000;
7. Var
8. t, nt, c, p: Array[1..Maxm]Of Longint;
9. Ind, h: Array[1..Maxn]Of Longint;
10. hv: Array[0..Maxn]Of Longint;
11. i, j, k, x: Longint;
12. n, m, cnt: Longint;
13. st, ed: Longint;
14. Function Min(i, j: Longint): Longint;
15. Begin
16. If i < j Then MIn := i Else Min := j;
17. End;
18.
19. Procedure Addedge(i, j, k: Longint);
20. Begin
21. Inc(cnt);
22. t[cnt] := j;
23. c[cnt] := k;
24. nt[cnt] := Ind[i];
25. Ind[i] := cnt;
26.
27. Inc(cnt);
28. t[cnt] := i;
29. c[cnt] := 0;
30. nt[cnt] := Ind[j];
31. Ind[j] := cnt;
32.
33. p[cnt] := cnt - 1;
34. p[cnt - 1] := cnt;
35. End;
36.
37. Function Aug(x, Lim: Longint): Longint;
38. Var
39. k, l, d, Minh: Longint;
40. Begin
41. If x = Ed Then Begin
42. Aug := LIm; Exit;
43. End;
44. k := Ind[x];
45. l := Lim;
46. MInh := n;
47. While k <> -1 Do
48. Begin
49. If c[k] > 0 Then
50. Begin
51. If h[t[k]] = h[x] - 1 Then
52. Begin
53. d := Aug(t[k], Min(c[k], L));
54. Dec(c[k], d);
55. Inc(c[p[k]], d);
56. Dec(l, d);
57. If l = 0 Then Break;
58. If h[st] > n Then Begin Aug := Lim - l; Exit; End;
59. End;
60. If Minh > h[t[k]] Then Minh := h[t[k]];
61. End;
62. k := nt[k];
63. End;
64.
65. If l = LIm Then
66. Begin
67. Dec(hv[h[x]]);
68. If hv[h[x]] = 0 Then Begin h[st] := n + 1; Aug := LIm - l; Exit; End;
69. h[x] := Minh + 1;
70. Inc(hv[h[x]]);
71. End;
72. Aug := LIm - l;
73. End;
74.
75. Function Sap(): Longint;
76. Begin
77. Fillchar(h, Sizeof(h), 0);
78. Fillchar(hv, Sizeof(hv), 0);
79. hv[0] := n;
80. Sap := 0;
81. While h[st] < n + 1 Do
82. Sap := Sap + Aug(st, MaxLongint);
83. End;
84.
85. Begin
86. Assign(Input, Fin);
87. Assign(Output, Fout);
88. Reset(Input);
89. Rewrite(Output);
90. While Not Eof(Input) Do
91. Begin
92. ReadLn(m, n);
93. If (m = 0) And (n = 0) Then Break;
94. Fillchar(Ind, Sizeof(Ind), $FF);
95. cnt := 0;
96. For i:=1 To m Do
97. Begin
98. Read(j, k, x);
99. Addedge(j, k, x);
100. End;
101.
102. st := 1;
103. ed := n;
104. WriteLn(Sap);
105. End;
106. Close(Output);
107. End.
108.
109. Dinic模板:
110. Const
111. Fin = '';
112. Fout = '';
113. Const
114. Maxm = 10000;
115. Maxn = 10000;
116. Var
117. t, nt, c, p: Array[1..Maxm]Of Longint;
118. Ind, h: Array[1..Maxn]Of Longint;
119. Vis: Array[1..Maxn]Of Boolean;
120. q: Array[0..Maxn]Of Longint;
121. i, j, k, x: Longint;
122. n, m, cnt: Longint;
123. st, ed: Longint;
124. Function Min(i, j: Longint): Longint;
125. Begin
126. If i < j Then MIn := i Else Min := j;
127. End;
128.
129. Procedure Addedge(i, j, k: Longint);
130. Begin
131. Inc(cnt);
132. t[cnt] := j;
133. c[cnt] := k;
134. nt[cnt] := Ind[i];
135. Ind[i] := cnt;
136.
137. Inc(cnt);
138. t[cnt] := i;
139. c[cnt] := 0;
140. nt[cnt] := Ind[j];
141. Ind[j] := cnt;
142.
143. p[cnt] := cnt - 1;
144. p[cnt - 1] := cnt;
145. End;
146.
147. Function BFS(): Boolean;
148. Var
149. l, r, k, x: Longint;
150. Begin
151. Fillchar(Vis, Sizeof(Vis), False);
152. Fillchar(h, Sizeof(h), 0);
153. l := 0;
154. r := 0;
155. q[l] := st;
156. Vis[st] := True;
157. While l <= r Do
158. Begin
159. x := q[l];
160. k := Ind[q[l]];
161. Inc(l);
162. While k <> -1 Do
163. Begin
164. If (c[k] > 0) And (Not Vis[t[k]]) Then
165. Begin
166. h[t[k]] := h[x] + 1;
167. Inc(r);
168. q[r] := t[k];
169. Vis[t[k]] := True;
170. End;
171. k := nt[k];
172. End;
173. End;
174. If h[ed] = 0 Then BFS := False Else BFS := True;
175. End;
176.
177. Function DFS(x, lim: Longint): Longint;
178. Var
179. k, l, d: Longint;
180. Begin
181. If x = ed Then Begin DFS := Lim; Exit; End;
182. k := Ind[x];
183. L := Lim;
184. dFS := 0;
185. While k <> -1 Do
186. Begin
187. If h[t[k]] = h[x] + 1 Then
188. Begin
189. d := DFS(t[k], Min(l, c[k]));
190. Dec(c[k], d);
191. Inc(c[p[k]], d);
192. Dec(l, d);
193. If l = 0 Then Break;
194. End;
195. k := nt[k];
196. End;
197. DFS := Lim - l;
198. End;
199.
200. Function Dinic(): Longint;
201. Begin
202. Dinic := 0;
203. While BFS Do Dinic := Dinic + DFS(st, MaxLongint);
204. End;
205.
206. Begin
207. Assign(Input, Fin);
208. Assign(Output, Fout);
209. Reset(Input);
210. Rewrite(Output);
211. While Not Eof(Input) Do
212. Begin
213. ReadLn(m, n);
214. If (m = 0) And (n = 0) Then Break;
215. Fillchar(Ind, Sizeof(Ind), $FF);
216. cnt := 0;
217. For i:=1 To m Do
218. Begin
219. Read(j, k, x);
220. Addedge(j, k, x);
221. End;
222.
223. st := 1;
224. ed := n;
225. WriteLn(Dinic);
226. End;
227. Close(Output);
228. End.
线段树扫描线面积并
1. #include <iostream>
2. #include <cstdio>
3. #include <cstring>
4. #include <algorithm>
5. #define rep(i, j) for (int i = 0; i < j; i++)
6. #define N 100000
7. #define LL long long
8.
9. using namespace std;
10.
11. struct obj
12. {
13. int a, b;
14. obj *l, *r;
15. int s;
16. int add;
17. }f[N];
18.
19. struct obj1
20. {
21. int a, b;
22. int c;
23. bool flg;
24. char col;
25. }l[N], lx[N];
26.
27. int cnt;
28. int x[N];
29. int tmp[N];
30. obj* top;
31. int cnt1;
32. int n;
33.
34. obj* make(int a, int b)
35. {
36. cnt++;
37. int tmp = cnt;
38. f[cnt].a = a;
39. f[cnt].b = b;
40. f[cnt].l = NULL;
41. f[cnt].r = NULL;
42. f[cnt].s = 0;
43. f[cnt].add = 0;
44. if (a < b)
45. {
46. int mid = (a + b) >> 1;
47. f[tmp].l = make(a, mid);
48. f[tmp].r = make(mid + 1, b);
49. }
50. return f + tmp;
51. }
52.
53. void update(obj* a)
54. {
55. if (a->add > 0) a->s = x[a->b + 1] - x[a->a];
56. else if (a->a == a->b) a->s = 0;
57. else a->s = a->l->s + a->r->s;
58. }
59.
60. void insert(obj* t, int a, int b)
61. {
62. if (a <= t->a && t->b <= b)
63. {
64. (t->add)++;
65. update(t);
66. }
67. else
68. {
69. int mid = (t->a + t->b) >> 1;
70. if (a <= mid) insert(t->l, a, b);
71. if (b > mid) insert(t->r, a, b);
72. update(t);
73. }
74. }
75.
76. void del(obj* t, int a, int b)
77. {
78. if (a <= t->a && t->b <= b)
79. {
80. (t->add)--;
81. update(t);
82. }
83. else
84. {
85. int mid = (t->a + t->b) >> 1;
86. if (a <= mid) del(t->l, a, b);
87. if (b > mid) del(t->r, a, b);
88. update(t);
89. }
90. }
91.
92. int cmp(obj1 a, obj1 b)
93. {
94. return a.c < b.c;
95. }
96.
97.
98. int find(int xx)
99. {
100. int l = 0, r = cnt1 - 1;
101. while(l < r - 1)
102. {
103. int mid = (l + r) >> 1;
104. if (xx < x[mid]) r = mid; else l = mid;
105. }
106. if (x[l] == xx) return l;
107. else return r;
108. }
109.
110. LL test(int n)
111. {
112. long long ans = 0;
113. cnt = 0;
114. top = make(0, cnt1 - 1);
115. rep(i, n)
116. {
117. if(l[i].flg) insert(top, find(l[i].a), find(l[i].b) - 1);
118. else del(top, find(l[i].a), find(l[i].b) - 1);
119. ans += (LL)top->s * (LL)(l[i + 1].c - l[i].c);
120. }
121. return ans;
122. }
123.
124.
125.
126. int main()
127. {
128. //freopen("test.in", "r", stdin);
129. int tt, ii;
130. char ss[3];
131. scanf("%d", &tt);
132. for(int ii = 0; ii < tt; ii++)
133. {
134. scanf("%d", &n);
135. for(int i = 0; i < n; i++)
136. {
137. int a, b, c, d;
138. scanf("%s", ss);
139. scanf("%d%d%d%d", &a, &b, &c, &d);
140. lx[i].col = ss[0];
141. lx[i].a = a;
142. lx[i].b = c;
143. lx[i].c = b;
144. lx[i].flg = true;
145.
146. lx[i + n].col = ss[0];
147. lx[i + n].a = a;
148. lx[i + n].b = c;
149. lx[i + n].c = d;
150. lx[i + n].flg = false;
151.
152. x[i] = a;
153. x[i + n] = c;
154. }
155. cnt1 = 0;
156. sort(x, x + 2 * n);
157. rep(i, 2 * n) if (i == 0 || x[i] != x[i - 1]) tmp[cnt1++] = x[i];
158. rep(i, cnt1) x[i] = tmp[i];
159.
160.
161. int cc = 0;
162. for(int i = 0; i < 2 * n; i++)
163. {
164. if(lx[i].col == 'R') l[cc++] =lx[i];
165. }
166. sort(l, l + cc, cmp);
167. LL r = test(cc);
168.
169. cc = 0;
170. for(int i = 0; i < 2 * n; i++)
171. {
172. if(lx[i].col == 'G') l[cc++] =lx[i];
173. }
174. sort(l, l + cc, cmp);
175. LL g = test(cc);
176.
177. cc = 0;
178. for(int i = 0; i < 2 * n; i++)
179. {
180. if(lx[i].col == 'B') l[cc++] =lx[i];
181. }
182. sort(l, l + cc, cmp);
183. LL b = test(cc);
184.
185. cc = 0;
186. for(int i = 0; i < 2 * n; i++)
187. {
188. if(lx[i].col == 'R' || lx[i].col == 'B') l[cc++] =lx[i];
189. }
190. sort(l, l + cc, cmp);
191. LL rb = test(cc);
192.
193. cc = 0;
194. for(int i = 0; i < 2 * n; i++)
195. {
196. if(lx[i].col == 'R' || lx[i].col == 'G') l[cc++] =lx[i];
197. }
198. sort(l, l + cc, cmp);
199. LL rg = test(cc);
200.
201. cc = 0;
202. for(int i = 0; i < 2 * n; i++)
203. {
204. if(lx[i].col == 'G' || lx[i].col == 'B') l[cc++] =lx[i];
205. }
206. sort(l, l + cc, cmp);
207. LL gb = test(cc);
208.
209. cc = 0;
210. for(int i = 0; i < 2 * n; i++)
211. {
212. l[cc++] =lx[i];
213. }
214. sort(l, l + cc, cmp);
215. LL rgb = test(cc);
216.
217. LL RB = gb - rgb - g + rg;
218. LL RGB =r + b - rb - RB;
219. LL GB = g + b - gb - RGB;
220. LL RG = r + g - rg - RGB;
221. LL R = r - RG - RB - RGB;
222. LL G = g - RG - GB - RGB;
223. LL B = b - RB - GB - RGB;
224.
225. /*
226. cout << r << " " << g << " " << b << endl;
227. cout << rg << " " << rb << " " << gb << endl;
228. */
229. printf("Case %d:\n", ii + 1);
230.
231. printf("%I64d\n", R);
232. printf("%I64d\n", G);
233. printf("%I64d\n", B);
234. printf("%I64d\n", RG);
235. printf("%I64d\n", RB);
236. printf("%I64d\n", GB);
237. printf("%I64d\n", RGB);
238.
239. }
240. return 0;
241. }
卡特兰数
h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0)
h(n)=C(2n,n)/(n+1) (n=0,1,2,...)
容斥原理公式
乘法逆元
(a * x) % p = 1, a * x + b * p = 1
用拓展欧几里得解
拓展GCD
ax+by=1
1. __int64 gcd(__int64 a, __int64 b)
2. {
3. if (a == 0) return b;
4. return gcd(b % a, a);
5. }
6.
7. void extend(__int64 a, __int64 b, __int64 &x, __int64 &y)
8. {
9. if (b == 0) {x = 1; y = 0;return;}
10. extend(b, a % b, x, y);
11.
12. int t = x;
13. x = y;
14. y = t - (a / b) * y;
15. }
莫比乌斯反演
若,则
若d=1 则μ(d) = 1
若d=a * b * c...a,b,c为不同的素数,则μ(d) = (-1)^k
稀疏矩阵乘法
1. #define MOD 10000
2. struct Matrix
3. {
4. int nRow, nCol;
5. int **numbers;
6. Matrix(int r = 0, int c = 0)
7. {
8. nRow = r; nCol = c;
9. numbers = new int*[r];
10. for(int i = 0; i < r; i++)
11. {
12. numbers[i] = new int[c];
13. for(int j = 0; j < c; j++) (numbers[i])[j] = 0;
14. }
15. }
16. Matrix operator + (Matrix add)
17. {
18. Matrix res(nRow, nCol);
19. for(int i = 0; i < nRow; i++)
20. for(int j = 0; j < nCol; j++)
21. (res.numbers[i])[j] = (numbers[i])[j] + (add.numbers[i])[j];
22. return res;
23. }
24. Matrix operator * (Matrix mul)
25. {
26. Matrix res(nRow, mul.nCol);
27. for(int i = 0; i < nRow; i++)
28. for(int j = 0; j < nCol; j++)
29. if((numbers[i])[j])
30. for(int k = 0; k < mul.nCol; k++)
31. (res.numbers[i])[k] += ((numbers[i])[j] * (mul.numbers[j])[k]) % MOD;
32. return res;
33. }
34. };