-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManaual.tex
More file actions
4388 lines (3999 loc) · 110 KB
/
Manaual.tex
File metadata and controls
4388 lines (3999 loc) · 110 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass[twoside]{article}
\usepackage[colorlinks,linkcolor=black]{hyperref}
\usepackage{xeCJK,xunicode}
\usepackage{fancyhdr}
\usepackage{amsmath,amsthm}
\usepackage{listings,xcolor}
\usepackage{geometry}
\usepackage{fontspec}
\setCJKmainfont[BoldFont=STYuanti-SC-Regular,ItalicFont=STYuanti-SC-Regular]{STYuanti-SC-Regular}
\setCJKmonofont[BoldFont=STYuanti-SC-Regular]{STYuanti-SC-Regular}
\setCJKsansfont{STYuanti-SC-Regular}
\setsansfont{Monaco}
\setmonofont[Mapping={}]{Monaco}
\newcommand{\HRule}{\rule{\linewidth}{0.5mm}}
\geometry{left=2.5cm,right=2.5cm,top=2.5cm,bottom=2.5cm}
\lstset{
language = c++,
breaklines = true,
captionpos = b,
tabsize = 4,
numbers = left,
columns = fullflexible,
keepspaces = true,
commentstyle = \color[RGB]{0,128,0},
keywordstyle = \color[RGB]{0,0,255},
basicstyle = \small\ttfamily,
rulesepcolor = \color{red!20!green!20!blue!20},
showstringspaces = false,
}
\title{ACM/ICPC Template Manaual}
\author{肥宅很快乐}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[C]{ACM/ICPC Template Manaual, 肥宅很快乐}
\begin{document}\small
\begin{titlepage}
\begin{center}
\vspace*{0.5cm}\includegraphics[width=0.75\textwidth]{logo.jpg} \\ [2cm]
\HRule \\ [1cm]
\textbf{\Huge{ACM/ICPC Template Manaual}} \\ [0.5cm]
\HRule \\ [4cm]
\textbf{\Huge{浙江工商大学}} \\ [1cm]
\LARGE{肥宅很快乐}
\vfill
\Large{\today}
\end{center}
\clearpage
\end{titlepage}
\tableofcontents\clearpage
\pagestyle{fancy}
\lfoot{}
\cfoot{\thepage}\rfoot{}
\setcounter{section}{-1}
\setcounter{page}{1}
\clearpage\section{头文件}
\begin{lstlisting}
// 巨菜的ACMer-Happy233
#include <bits/stdc++.h>
using namespace std;
//-----
typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define fi first
#define se second
#define pw(x) (1ll << (x))
#define bt(x, k) (((x) >> k) & 1)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rep(i, l, r) for(int i=(l);i<(r);++i)
#define per(i, l, r) for(int i=(r)-1;i>=(l);--i)
#define mst(t, v, n) memset(t, v, sizeof(decltype(*(t))) * (n))
#define sf(x) scanf("%d", &(x))
#ifndef ACM_LOCAL
#define endl '\n'
#endif
int main() {
#ifdef ACM_LOCAL
freopen("./data/std.in", "r", stdin);
// freopen("./data/std.out", "w", stdout);
#else
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#endif
#ifdef ACM_LOCAL
auto start = clock();
#endif
int t = 1;
// cin >> t;
while (t--)
solve();
#ifdef ACM_LOCAL
auto end = clock();
cerr << "Run Time: " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
#endif
return 0;
}\end{lstlisting}
\clearpage\section{字串符}
\subsection{KMP}
\begin{lstlisting}
template<class elemType>
inline void kmp_nxt(elemType &T, vector<int> &nxt) {
nxt[0] = -1;
for (int i = 1; i < T.size(); i++) {
int j = nxt[i - 1];
while (j >= 0 && T[i - 1] != T[j]) j = nxt[j];
if (j >= 0 && T[i - 1] == T[j]) nxt[i] = j + 1;
else nxt[i] = 0;
}
}
template<class elemType>
inline int kmp_count(elemType &S, elemType &T) {
vector<int> nxt(T.size());
kmp_nxt(T, nxt);
int index, count = 0;
for (index = 0; index < S.size(); ++index) {
int pos = 0;
int iter = index;
while (pos < T.size() && iter < S.size()) {
if (S[iter] == T[pos]) {
++iter;
++pos;
} else {
if (pos == 0) ++iter;
else pos = nxt[pos - 1] + 1;
}
}
if (pos == T.size() && (iter - index) == T.size()) ++count;
}
return count;
}
template<class elemType>
inline void kmp_next(elemType T[], int count, vector<int> &nxt) {
nxt[0] = -1;
for (int i = 1; i < count; i++) {
int j = nxt[i - 1];
while (j >= 0 && T[i - 1] != T[j]) j = nxt[j];
if (j >= 0 && T[i - 1] == T[j]) nxt[i] = j + 1;
else nxt[i] = 0;
}
}
template<class elemType>
inline int kmp_count(elemType S[], int c1, elemType T[], int c2) {
vector<int> nxt(c2);
kmp_nxt(T, c2, nxt);
int index, count = 0;
for (index = 0; index < c1; ++index) {
int pos = 0;
int iter = index;
while (pos < c2 && iter < c1) {
if (S[iter] == T[pos]) {
++iter;
++pos;
}
else {
if (pos == 0) ++iter;
else pos = nxt[pos - 1] + 1;
}
}
if (pos == c2 && (iter - index) == c2) ++count;
}
return count;
}\end{lstlisting}
\clearpage\section{动态规划}
\subsection{01Bag}
\begin{lstlisting}
void dp(int n, int m) {
// n=物品个数
for (int i = 0; i < n; i++) {
// m=背包最大容量
for (int j = m; j >= wei[i]; j--)
// wei=大小 val=价值
f[j] = max(f[j], f[j - wei[i]] + val[i]);
}
}
\end{lstlisting}
\subsection{BagProblem}
\begin{lstlisting}
#define N 1000
// val=价值 wei=重量 num=数量
int val[N], wei[N], num[N], f[N];
// n=种类个数 m=背包最大值
// 01背包
void dp1(int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = m; j >= wei[i]; j--)
f[j] = max(f[j], f[j - wei[i]] + val[i]);
}
}
// 完全背包
void dp2(int n, int m) {
//初始化看要求
for (int i = 0; i <= m; i++) {
f[i] = INF;
}
f[0] = 0;
//若要求恰好装满背包,那在初始化时除了f[0]=0其它f[1..V]均=-∞
//若没要求背包装满,只希望价格大,初始化时应将f[0..V]=0)
for (int i = 0; i < n; i++)
for (int j = wei[i]; j <= m; j++)
f[j] = max(f[j], f[j - wei[i]] + val[i]);
}
// 多重背包
void dp3(int n, int m) {
for (int i = 0; i < n; i++)
for (int k = 0; k < num[i]; k++)
for (int j = m; j >= wei[i]; j--)
f[j] = max(f[j], f[j - wei[i]] + val[i]);
}
\end{lstlisting}
\subsection{FullBag}
\begin{lstlisting}
/*
完全背包问题的特点是,每种物品可以无限制的重复使用,可以选择放或不放。
完全背包问题描述:
有N物品和一个容量为V的背包。第i件物品的重量是wei[i],价值是val[i]。
*/
#include <cstdio>
#define INF 0x3fffffff
#define N 10047
int f[N],val[N],wei[N];
int min(int a,int b)
{
return x<y?x:y;
}
int main()
{
int t,i,j,k,E,F,m,n;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&E,&F);
int c = F-E;
for(i = 0 ; i <= c ; i++)
f[i]=INF;
scanf("%d",&n);
for(i = 0 ; i < n ; i++)
{
scanf("%d%d",&val[i],&wei[i]);//val[i]为面额,wei[i]为重量
}
f[0]=0;//因为此处假设的是小猪储钱罐 恰好装满 的情况
//注意初始化(要求恰好装满背包,那么在初始化时除了f[0]为0其它f[1..V]均设为-∞,
//这样就可以保证最终得到的f[N]是一种恰好装满背包的最优解。
//如果并没有要求必须把背包装满,而是只希望价格尽量大,初始化时应该将f[0..V]全部设为0)
for(i =0 ; i < n ; i++)
{
for(j = wei[i] ; j <= c ; j++)
{
f[j] = min(f[j],f[j-wei[i]]+val[i]);//此处求的是最坏的情况所以用min,确定最少的钱,当然最后就用max了,HEHE
}
}
if(f[c] == INF)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n",f[c]);
}
return 0;
}
//此代码为HDU1114; \end{lstlisting}
\subsection{MultiBag}
\begin{lstlisting}
//多重背包(MultiplePack): 有N种物品和一个容量为V的背包。
//第i种物品最多有n[i]件可用,每件费用是c[i],价值是w[i]。
//求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,
//且价值总和最大。
//HDU 2191
#include <cstdio>
#include <cstring>
#define N 247
int max(int x,int y){
return x>y?x:y;
}
int main() {
int t,n,m,i,j,k;
int w[N],pri[N],num[N],f[N];
while(~scanf("%d",&t)){
while(t--){
memset(f,0,sizeof(f));
scanf("%d%d",&n,&m);//n为总金额,m为大米种类
for(i = 0 ; i < m ; i++){
scanf("%d%d%d",&pri[i],&w[i],&num[i]);//num[i]为每种大米的袋数
}
for(i = 0 ; i < m ; i++){
for(k = 0 ; k < num[i] ; k++){
for(j = n ; j >= pri[i]; j--){
f[j] = max(f[j],f[j-pri[i]]+w[i]);
}
}
}
printf("%d\n",f[n]);
}
}
return 0;
} \end{lstlisting}
\subsection{Maze01}
\begin{lstlisting}
struct Maze01 {
// 原始01矩阵 1-n 1-m
bool a[N][N];
// 以(i,j)向上最大高度的最大子矩阵的左右高
int l[N][N], r[N][N], h[N][N];
// 最大连续前缀1的左端位置, 如果(i,j)为0则ml=j+1
int ml[N][N];
// 矩阵大小
int n, m;
void prework() {
for (int i = 1; i <= m; i++) {
l[0][i] = 0;
r[0][i] = m;
}
for (int i = 1; i <= n; i++) {
int maxl = 1;
int maxr = m;
for (int j = 1; j <= m; j++) {
if (a[i][j] == 0) {
maxl = j + 1;
h[i][j] = l[i][j] = 0;
} else {
h[i][j] = h[i - 1][j] + 1;
l[i][j] = max(maxl, l[i - 1][j]);
}
ml[i - 1][j] = maxl;
ml[i][j] = j + 1;
}
for (int j = m; j >= 1; --j) {
if (a[i][j] == 0) {
maxr = j - 1;
r[i][j] = m;
} else {
r[i][j] = min(maxr, r[i - 1][j]);
}
}
}
}
// 单调栈
pii st[N];
int calc() {
prework();
int ans = 0;
for (int i = 1; i <= n; i++) {
int top = 0;
for (int j = 1; j <= m; j++) {
if (h[i][j] == 0) continue;
if (top == 0 || st[top] != make_pair(l[i][j], r[i][j])) {
st[++top] = make_pair(l[i][j], r[i][j]);
}
while (top && st[top].second == j) {
int pos = st[top--].first;
if (pos < ml[i][j]) {
// l[i][j]-r[i][j]为底, h[i][j]为高
// 可以得到所有的唯一子矩阵, 不存在maze1完全属于maze2
ans++;
}
}
}
}
return ans;
}
} dp;\end{lstlisting}
\clearpage\section{数据结构}
\subsection{BTree}
\begin{lstlisting}
template<class T>
struct TreeNode {
T value;
TreeNode *left;
TreeNode *right;
};
template<class T>
TreeNode<T> *createTree(const T *pre, const T *in, const int len) {
TreeNode<T> *t = NULL;
if (len > 0) {
t = new TreeNode<T>;
t->value = pre[0];
int index;
for (index = 0; index < len; index++) {
if (in[index] == pre[0]) {
break;
}
}
if (index == len) {
index = -1;
}
t->left = createTree(pre + 1, in, index);
t->right = createTree(pre + index + 1, in + index + 1, len - index - 1);
}
return t;
}
template<class T>
int preOrder(TreeNode<T> *root, queue<T> &out) {
if (root) {
int count = 1;
out.push(root->value);
count += preOrder(root->left, out);
count += preOrder(root->right, out);
return count;
} else {
return 0;
}
}
template<class T>
int inOrder(TreeNode<T> *root, queue<T> &out) {
if (root) {
int count = 1;
count += inOrder(root->left, out);
out.push(root->value);
count += inOrder(root->right, out);
return count;
} else {
return 0;
}
}
template<class T>
void postOrder(TreeNode<T> *root, queue<T> &out) {
if (root) {
postOrder(root->left, out);
postOrder(root->right, out);
out.push(root->value);
} else {
return;
}
}
template<class T>
T *convertQueueToArray(queue<T> &out, int len) {
T *list = new T[len];
int now = 0;
while (!out.empty() && now < len) {
list[now] = out.front();
out.pop();
now++;
}
return list;
}
template<class T>
void destroyTree(TreeNode<T> *root) {
if (root) {
destroyTree(root->left);
destroyTree(root->right);
delete root;
} else return;
}
template<class T>
void insertIntoBSTree(TreeNode<T> *root, const T &value) {
if (!root) {
return;
}
if (value < root->value) {
if (root->left) {
insertIntoTree(root->left, value);
} else {
root->left = new TreeNode<T>;
root->left->value = value;
root->left->left = NULL;
root->left->right = NULL;
}
} else if (value > root->value) {
if (root->right) {
insertIntoTree(root->right, value);
} else {
root->right = new TreeNode<T>;
root->right->value = value;
root->right->left = NULL;
root->right->right = NULL;
}
}
}
template<class T>
TreeNode<T> *createBSTree(T *list, int len) {
if (len < 1) {
return NULL;
}
TreeNode<T> *root = new TreeNode<char>;
root->value = list[0];
root->left = NULL;
root->right = NULL;
for (int i = 1; i < len; i++) {
insertIntoBSTree(root, list[i]);
}
return root;
}\end{lstlisting}
\subsection{pbds-bbtree}
\begin{lstlisting}
// RBTree 红黑树
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
// 红黑树
__gnu_pbds::tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> t;
// null_type无映射(低版本g++为null_mapped_type)
// 类似multiset
__gnu_pbds::tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> t;
find_by_order(size_t order);
// 结点更新
tree_order_statistics_node_update
insert(p);
erase(it);
// 求k在树中是第几大:
order_of_key(p);
// 找到第order小的迭代器
find_by_order(order);
// 前驱
lower_bound(p);
// 后驱
upper_bound(p);
// 合并
a.join(b);
// 分割 key小于等于v的元素属于a,其余的属于b
a.split(v, b);
// 优先队列
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/assoc_container.hpp>
// 操作类似于stl的优先队列
typedef __gnu_pbds::priority_queue<node, greater<node>, __gnu_pbds::thin_heap_tag> heap;
heap::point_iterator; // 指向元素的指针\end{lstlisting}
\subsection{树状数组}
\begin{lstlisting}
const int N = 1000005;
struct BITree {
int n;
ll c[N];
void init(int _n) {
n = _n + 1;
memset(c, 0, sizeof(ll) * n);
}
void change(int pos, ll v) {
for (int i = pos; i < n; i += i & (-i))
c[i] += v;
}
ll query(int x) {
ll ans = 0;
for (int i = x; i > 0; i -= i & (-i))
ans += c[i];
return ans;
}
void update(int l, int r, ll v) {
change(l, v);
change(r + 1, -v);
}
} tree;
// 区间更新区间查询
struct BITree {
int n;
ll c[N], d[N];
void init(int _n) {
n = _n + 1;
memset(c, 0, sizeof(ll) * n);
memset(d, 0, sizeof(ll) * n);
}
void change(int pos, ll v) {
for (int i = pos; i < n; i += i & (-i))
c[i] += v, d[i] += v * (pos - 1);
}
ll query(int x) {
ll ans = 0;
for (int i = x; i > 0; i -= i & (-i))
ans += x * c[i] - d[i];
return ans;
}
void update(int l, int r, ll v) {
change(l, v);
change(r + 1, -v);
}
ll sum(int l, int r) {
return query(r) - query(l - 1);
}
} tree;\end{lstlisting}
\subsection{二维树状数组}
\begin{lstlisting}
const int N = 2005;
inline int lowbit(const int &x) {
return x & -x;
}
struct TdBITree {
int n, m;
ll c[N][N];
void init(int n, int m) {
this->n = n;
this->m = m;
memset(c, 0, sizeof(c))
}
void init(int n, int m, ll v) {
this->n = n;
this->m = m;
rep(x, 1, N) {
rep(y, 1, N) {
c[x][y] = (x * y + (x - lowbit(x)) * (y - lowbit(y)) - x * (y - lowbit(y)) - (x - lowbit(x)) * y) * v;
}
}
}
void change(int x, int y, ll v) {
for (int i = x; i <= n; i += lowbit(i))
for (int j = y; j <= m; j += lowbit(j))
c[i][j] += v;
}
ll query(int x, int y) {
ll ans = 0;
for (int i = x; i >= 1; i -= lowbit(i))
for (int j = y; j >= 1; j -= lowbit(j))
ans += c[i][j];
return ans;
}
ll solve(int x1, int y1, int x2, int y2) {
return query(x2, y2) - query(x1 - 1, y2) - query(x2, y1 - 1) + query(x1 - 1, y1 - 1);
}
};\end{lstlisting}
\subsection{线段树}
\begin{lstlisting}
// hdu 6562
struct TreeNode {
int l, r;
int lson, rson;
ll sum;
ll len;
ll laz1, laz2, laz3;
inline void init(int a, int b, int ls, int rs) {
lson = ls;
rson = rs;
l = a, r = b;
sum = 0;
len = 1;
laz1 = laz2 = 0;
laz3 = 1;
}
inline int mid() {
return (l + r) >> 1;
}
inline int width() {
return r - l + 1;
}
inline void add(ll val) {
sum = (sum * 10 + val * len * 10 + val * width()) % MOD;
len = (len * 100) % MOD;
// 右懒惰
laz1 = (laz1 * 10 + val) % MOD;
// 左懒惰
laz2 = (laz2 + val * laz3) % MOD;
// 长度懒惰
laz3 = (laz3 * 10) % MOD;
}
};
struct SegTree {
int tot;
TreeNode node[N << 1];
inline void init() {
tot = 0;
}
inline void up(int k) {
TreeNode &nd = node[k];
nd.sum = (node[nd.lson].sum + node[nd.rson].sum) % MOD;
nd.len = (node[nd.lson].len + node[nd.rson].len) % MOD;
}
inline void push(int k) {
TreeNode &nd = node[k];
if (nd.laz3 == 1) return;
TreeNode &lson = node[nd.lson];
TreeNode &rson = node[nd.rson];
lson.sum = (nd.laz1 * lson.width() + lson.sum * nd.laz3 + nd.laz2 * lson.len % MOD * nd.laz3) % MOD;
lson.len = (lson.len * nd.laz3 % MOD * nd.laz3) % MOD;
lson.laz1 = (lson.laz1 * nd.laz3 + nd.laz1) % MOD;
lson.laz2 = (nd.laz2 * lson.laz3 + lson.laz2) % MOD;
lson.laz3 = (lson.laz3 * nd.laz3) % MOD;
rson.sum = (nd.laz1 * rson.width() + rson.sum * nd.laz3 + nd.laz2 * rson.len % MOD * nd.laz3) % MOD;
rson.len = (rson.len * nd.laz3 % MOD * nd.laz3) % MOD;
rson.laz1 = (rson.laz1 * nd.laz3 + nd.laz1) % MOD;
rson.laz2 = (nd.laz2 * rson.laz3 + rson.laz2) % MOD;
rson.laz3 = (rson.laz3 * nd.laz3) % MOD;
nd.laz1 = nd.laz2 = 0;
nd.laz3 = 1;
}
void build(int k, int l, int r) {
TreeNode &nd = node[k];
nd.init(l, r, tot + 1, tot + 2);
tot += 2;
if (l == r) {
return;
}
int mid = nd.mid();
build(nd.lson, nd.l, mid);
build(nd.rson, mid + 1, nd.r);
up(k);
}
void change(int k, int l, int r, ll val) {
TreeNode &nd = node[k];
if (nd.l == l && nd.r == r) {
nd.add(val);
return;
}
push(k);
int mid = nd.mid();
if (r <= mid) {
change(nd.lson, l, r, val);
} else if (l > mid) {
change(nd.rson, l, r, val);
} else {
change(nd.lson, l, mid, val);
change(nd.rson, mid + 1, r, val);
}
up(k);
}
ll query(int k, int l, int r) {
TreeNode &nd = node[k];
if (nd.l == l && nd.r == r) {
return nd.sum;
}
push(k);
int mid = nd.mid();
ll ans = 0;
if (r <= mid) {
ans += query(nd.lson, l, r);
} else if (l > mid) {
ans += query(nd.rson, l, r);
} else {
ans += query(nd.lson, l, mid);
ans += query(nd.rson, mid + 1, r);
}
return ans % MOD;
}
} tree;\end{lstlisting}
\subsection{二维线段树}
\begin{lstlisting}
const int N = int(2e3 + 48);
ll p[N][N];
struct SegTree {
inline int static get(int l, int r) {
return (l + r) | (l != r);
}
struct node {
int l, r;
inline int mid() { return (l + r) >> 1; }
inline bool point() { return l == r; }
inline node operator--(int) { return {l, mid()}; }
inline node operator++(int) { return {mid() + 1, r}; }
bool operator==(node t) const { return l == t.l && r == t.r; }
operator int() { return get(l, r); }
};
ll s[N * 2][N * 2];
inline void upX(node x, node y) {
s[x][y] = max(s[x--][y], s[x++][y]);
}
inline void upY(node x, node y) {
s[x][y] = max(s[x][y--], s[x][y++]);
}
void buildY(node x, node y) {
if (y.point()) {
s[x][y] = p[x.l][y.l];
return;
}
buildY(x, y--);
buildY(x, y++);
upY(x, y);
return;
}
void mergeY(node x, node y) {
if (y.point()) {
upX(x, y);
return;
}
mergeY(x, y--);
mergeY(x, y++);
upX(x, y);
return;
}
void build(node x, node y) {
if (x.point()) {
buildY(x, y);
return;
}
build(x--, y);
build(x++, y);
mergeY(x, y);
return;
}
ll queryY(node x, node y, node a, node b) {
if (y == b) {
return s[x][y];
}
int mid = y.mid();
if (b.r <= mid) {
return queryY(x, y--, a, b);
} else if (b.l > mid) {
return queryY(x, y++, a, b);
} else {
return max(queryY(x, y--, a, {b.l, mid}),
queryY(x, y++, a, {mid + 1, b.r}));
}
}
ll query(node x, node y, node a, node b) {
if (x == a) {
return queryY(x, y, a, b);
}
int mid = x.mid();
if (a.r <= mid) {
return query(x--, y, a, b);
} else if (a.l > mid) {
return query(x++, y, a, b);
} else {
return max(query(x--, y, {a.l, mid}, b),
query(x++, y, {mid + 1, a.r}, b));
}
}
} tree;\end{lstlisting}
\subsection{树状数组求逆序对}
\begin{lstlisting}
BITree t;
int n;
pii a[N];
void solve() {
t.init(n);
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
a[i] = make_pair(x, i);
}
sort(a + 1, a + n + 1);
ll ans = 0;
for (int i = 1; i <= n; i++) {
t.change(a[i].second, 1);
ans += (i - t.query(a[i].second));
}
cout << ans << endl;
}
\end{lstlisting}
\subsection{ST}
\begin{lstlisting}
// 只需要取值
struct ST {
int ck[N];
int dp[20][N];
void init(int n, int squ[]) {
++n;
ck[0] = -1;
for (int i = 1; i <= n; i++) {
ck[i] = ck[i - 1] + ((i & (i - 1)) == 0 ? 1 : 0);
}
for (int i = 0; i < n; i++) {
dp[0][i] = squ[i];
}
for (int k = 1; k <= ck[n]; k++) {
int dk = k - 1;
for (int i = 0; i + (1 << k) - 1 < n; i++) {
dp[k][i] = max(dp[dk][i], dp[dk][i + (1 << dk)]);
}
}
}
int query(int l, int r) {
if (l > r) swap(l, r);
int k = ck[r - l + 1];
return max(dp[k][l], dp[k][r - (1 << k) + 1]);
}
};
// 可得到下标
struct ST {
int ck[N];
int rmq[N];
int dp[20][N];
void init(int n, int squ[]) {
++n;
ck[0] = -1;
for (int i = 1; i <= n; i++) {
ck[i] = ck[i - 1] + ((i & (i - 1)) == 0 ? 1 : 0);
}
memcpy(rmq, squ, sizeof(int) * n);
for (int i = 0; i < n; i++) {
dp[0][i] = i;
}
for (int k = 1; k <= ck[n]; k++) {
int dk = k - 1;
for (int i = 0; i + (1 << k) - 1 < n; i++) {
int a = dp[dk][i];
int b = dp[dk][i + (1 << dk)];
dp[k][i] = rmq[a] < rmq[b] ? a : b;
}
}
}
int query(int l, int r) {
if (l > r) swap(l, r);
int k = ck[r - l + 1];
int a = dp[k][l];
int b = dp[k][r - (1 << k) + 1];
return rmq[a] < rmq[b] ? a : b;
}
};\end{lstlisting}
\subsection{笛卡尔树}
\begin{lstlisting}
// 笛卡尔树,静态建树,区间最值跳转
struct CartesianTree {
int rt; // 根节点
pii ch[N]; // 左右儿子
int st[N]; // 单调栈
void build(int n, int p[]) {
rt = 0;
int t = 0;
for (int i = 1; i <= n; i++) {
ch[i] = {0, 0};
// 决定了大于还是小于
while (t && p[st[t]] > p[i]) --t;
if (t) {
// 上一个点的右儿子作为自己的左儿子
// 成为上一个点的右儿子
ch[i].first = ch[st[t]].second;
ch[st[t]].second = i;
} else { // 自己作为根节点
ch[i].first = rt;
rt = i;
}
st[++t] = i;
}
}
} dika;
\end{lstlisting}
\subsection{DancingLinks}
\begin{lstlisting}
// Dancing Links