-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tex
9552 lines (8291 loc) · 228 KB
/
main.tex
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
% 新添加内容
% 编译命令
% bitset操作
% 圆交多边形模板
% Powerful Number Sieve
% Min25 筛
% FWT
% Pohlig Hellman
% 拉格朗日插值
% 三四元环计数
% 杜教筛
% ISAP, HLPP
% 决策单调性分治
% 预处理离散对数
% Coppersmith & LLL
% LGV引理
% 圆反演
% 待添加内容
% 斯特林数(working)
% 群论(working)
% 正则表达式转前缀表达式...
% 闵科夫斯基和
% 线性筛完全体
% 带负权的dij版MCMF
% 基于值域预处理的快速 GCD
% Min_Max容斥
% 类欧几里得算法详解
% 多边形面积
% 模拟退火
% 大质数表
% 拟阵
% 舞蹈链
\documentclass{ctexart}
\usepackage{xeCJK}
\usepackage{multicol} % 分栏
\usepackage[a4paper, inner=2cm, outer=2cm, top=2cm, bottom=2cm, bindingoffset=0cm]{geometry}
\usepackage{fancyhdr}
\usepackage[colorlinks=true]{hyperref}
% \usepackage[colorlinks=true]{}
\usepackage{zhnumber} % 中文日期
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{appendix}
\lstset{
basicstyle = \zihao{-5}\ttfamily, % 基本代码风格
keywordstyle = \zihao{-5}\bfseries, % 关键字风格
commentstyle = \zihao{-5}\rmfamily\slshape, % 注释的风格,斜体
stringstyle = \zihao{-5}\ttfamily, % 字符串风格
flexiblecolumns, % 别问为什么,加上这个
numbers = left, % 行号的位置在左边
showspaces = false, % 是否显示空格,显示了有点乱,所以不现实了
numberstyle = \zihao{-6}\ttfamily, % 行号的样式,小五号,tt等宽字体
showstringspaces = false,
captionpos = t, % 这段代码的名字所呈现的位置,t指的是top上面
frame = lrtb, % 显示边框
breaklines = true,
language = c++
}
\lstdefinestyle{Python}{
language = Python, % 语言选Python
basicstyle = \zihao{-5}\ttfamily,
numberstyle = \zihao{-5}\ttfamily,
keywordstyle = \color{blue},
keywordstyle = [2] \color{teal},
stringstyle = \color{magenta},
commentstyle = \color{red}\ttfamily,
breaklines = true, % 自动换行,建议不要写太长的行
columns = fixed, % 如果不加这一句,字间距就不固定,很丑,必须加
basewidth = 0.5em,
}
\newtheorem{example}{例}
\newtheorem{solution}{解}
\newtheorem{algorithm}{算法}
\newtheorem{theorem}{定理}[section] % 按 section 编号
\newtheorem{definition}{定义}
\newtheorem{axiom}{公理}
\newtheorem{property}{性质}
\newtheorem{proposition}{命题}
\newtheorem{lemma}{引理}
\newtheorem{corollary}{推论}
\newtheorem{remark}{注解}
\newtheorem{condition}{条件}
\newtheorem{conclusion}{结论}
\newtheorem{assumption}{假设}
\title{ACM Templates}
\date{\today}
\author{NamelessOIer}
\begin{document}
\maketitle
\setcounter{page}{0}
\thispagestyle{empty}
\newpage
\pagenumbering{roman}
\tableofcontents
\newpage
\pagenumbering{arabic}
\pagestyle{fancy}
\section{杂项}
\subsection{注意事项}
\begin{itemize}
\item 最好不要乱改板子,除非卡常/题目需求
\end{itemize}
\subsection{读入优化}
\begin{lstlisting}
#define gc c=getchar()
#define r(x) read(x)
#define getchar() (frS==frT&&(frT=(frS=frBB)+fread(frBB,1,1<<12,stdin),frS==frT)?EOF:*frS++)
char frBB[1<<12],*frS=frBB,*frT=frBB;
// fread, may do not need.
template<typename T>
inline void read(T &x){
x=0;T k=1;char gc;
while(!isdigit(c)){if(c=='-')k=-1;gc;}
while(isdigit(c)){x=x*10+c-'0';gc;}x*=k;
}
\end{lstlisting}
\subsection{输出优化}
\begin{lstlisting}
template<typename T>
inline void write(const T &x){
if(x>9)write(x/10);
putchar(x%10+'0');
}
\end{lstlisting}
\subsection{关闭流同步}
\begin{lstlisting}
std::ios::sync_with_stdio(false);
std::cin.tie(0);
// 如果编译开启了 C++11 或更高版本,建议使用 std::cin.tie(nullptr);
\end{lstlisting}
\subsection{文件读写}
\begin{lstlisting}
freopen(".in","r",stdin);
freopen(".out","w",stdout);
\end{lstlisting}
\subsection{fflush}
To flush the output buffer, you need to do the following immediately after the query output and the end-of-line character:
\begin{itemize}
\item fflush(stdout) or cout.flush() in C ++;
\item System.out.flush() in Java;
\item flush(output) in Pascal;
\item stdout.flush() in Python;
\item see the documentation for other languages.
\end{itemize}
\section{数据结构}
\subsection{哈希表}
\begin{lstlisting}
const int N=999983;
struct Hash{
int tot;
int fir[N],nex[N],key[N],val[N];
vector<int>vis;
inline void clear(){
for(int i=0;i<vis.size();++i){
fir[vis[i]]=0;
}
vis.clear();
tot=0;
}
inline void insert(int x,int v){
int k=x%N;
nex[++tot]=fir[k];
key[tot]=x;
val[tot]=v;
if(!fir[k])(vis.push_back(k));
fir[k]=tot;
}
inline int find(int x){
int k=x%N;
for(int i=fir[k];i;i=nex[i]){
if(key[i]==x){
return val[i];
}
}
return -1;
}
}mp;
\end{lstlisting}
\subsection{ST表}
\begin{lstlisting}
inline int STMin(const int &l,const int &r){
int k=lg[r-l+1];
return min(st[l][k],st[r-(1<<k)+1][k]);
}
inline void STinit(){
for(int i=2;i<=n;++i){
lg[i]=lg[i>>1]+1;
}
for(int j=1;j<=lg[n];++j){
for(int i=1;i+(1<<(j-1))-1<=n;++i){
st[i][j]=min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
}
}
}
\end{lstlisting}
\subsection{虚树}
\begin{lstlisting}
int n,m;
struct Graph1{
vector<int>G[N];
inline void addedge(int u,int v){
G[u].emplace_back(v);
G[v].emplace_back(u);
}
int dfs_clock;
int dfn[N];
int dep[N];
int ac[N][20];
void dfs(int x,int f){
dfn[x]=++dfs_clock;
dep[x]=dep[f]+1;
ac[x][0]=f;
for(int i=1;(ac[x][i]=ac[ac[x][i-1]][i-1]);++i);
for(auto v:G[x]){
if(v==f)continue;
dfs(v,x);
}
}
inline int LCA(int u,int v){
if(dep[u]<dep[v])swap(u,v);
for(int i=19;~i;--i){
if(dep[ac[u][i]]>=dep[v]){
u=ac[u][i];
}
}
if(u==v)return u;
for(int i=19;~i;--i){
if(ac[u][i]!=ac[v][i]){
u=ac[u][i];
v=ac[v][i];
}
}
return ac[u][0];
}
inline void clear(){
dfs_clock=0;
for(int i=1;i<=n;++i){
G[i].clear();
for(int j=0;ac[i][j];++j)ac[i][j]=0;
}
}
}G1;
struct Graph2{
vector<int>G[N];
int tot;
bool mark[N];
int a[N];
int sta[N];
void dfs(int x,int f){
a[++tot]=x;
for(auto v:G[x]){
dfs(v,x);
}
}
inline void build(){
G1.dfs(1,0);
tot=0;
mark[1]=1;
for(int i=1;i<=n;++i){
if(mark[i]){
a[++tot]=i;
}
}
sort(a+1,a+tot+1,[](const int &x,const int &y){return G1.dfn[x]<G1.dfn[y];});
int top=1;
sta[top]=1;
for(int i=2;i<=tot;++i){
int lca=G1.LCA(a[i],sta[top]);
if(lca!=sta[top]){
while(G1.dfn[lca]<G1.dfn[sta[top-1]]){
G[sta[top-1]].emplace_back(sta[top]);
--top;
}
if(lca==sta[top-1]){
G[lca].emplace_back(sta[top]);
--top;
}
else {
G[lca].emplace_back(sta[top]);
sta[top]=lca;
}
}
sta[++top]=a[i];
}
for(int i=1;i<top;++i){
G[sta[i]].emplace_back(sta[i+1]);
}
tot=0;
dfs(1,0);
}
inline void clear(){
for(int i=1;i<=tot;++i){
G[a[i]].clear();
mark[a[i]]=0;
}
}
}G2;
\end{lstlisting}
\subsection{树状数组}
\subsubsection{二维树状数组}
矩阵加,矩阵求和
\begin{lstlisting}
const int N=1005;
ll c1[N][N];
ll c2[N][N];
ll c3[N][N];
ll c4[N][N];
ll a[N][N];
int n,m;
inline void add(int x,int y,int d){
for(int i=x;i<=n;i+=(i&(-i))){
for(int j=y;j<=m;j+=(j&(-j))){
c1[i][j]+=d;
c2[i][j]+=d*x;
c3[i][j]+=d*y;
c4[i][j]+=d*x*y;
}
}
}
inline ll sum(int x,int y){
ll ret=a[x][y];
for(int i=x;i;i^=(i&(-i))){
for(int j=y;j;j^=(j&(-j))){
ret+=(x+1)*(y+1)*c1[i][j]-(y+1)*c2[i][j]-(x+1)*c3[i][j]+c4[i][j];
}
}
return ret;
}
inline void ADD(int x0,int y0,int x1,int y1,int k){
add(x0,y0,k);
add(x1+1,y1+1,k);
add(x0,y1+1,-k);
add(x1+1,y0,-k);
}
inline ll SUM(int x0,int y0,int x1,int y1){
return sum(x1,y1)-sum(x0-1,y1)-sum(x1,y0-1)+sum(x0-1,y0-1);
}
\end{lstlisting}
\subsubsection{树状数组平衡树操作}
P3369 【模板】普通平衡树
\begin{lstlisting}
const int N=2e5+7;
int opt[N],Q[N],c[N];
vector<int>Tmp;
inline int Hash(int x){
return lower_bound(Tmp.begin(),Tmp.end(),x)-Tmp.begin()+1;
}
inline void Add(int x,int d){
for(;x<=N;x+=x&(-x))c[x]+=d;
}
inline void Insert(int x){
Add(x,1);
}
inline int Sum(int x){
int ret=0;
for(;x;x-=x&(-x))ret+=c[x];
return ret;
}
inline void Erase(int x){
Add(x,-1);
}
inline int Rank(int x){
return Sum(x-1)+1;
}
inline int Kth(int k){
int res=0;
for(int i=17;~i;--i){
res+=(1<<i);
if(c[res]>=k)res-=(1<<i);
else k-=c[res];
}
return Tmp[res];
}
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
int n;r(n);
for(int i=1;i<=n;++i){
r(opt[i]),r(Q[i]);
if(opt[i]!=4)Tmp.push_back(Q[i]);
}
sort(Tmp.begin(),Tmp.end());
unique(Tmp.begin(),Tmp.end())-Tmp.begin();
for(int i=1;i<=n;++i){
int x;
if(opt[i]!=4)x=Hash(Q[i]);
else x=Q[i];
switch(opt[i]){
case 1:Insert(x);break;
case 2:Erase(x);break;
case 3:printf("%d\n",Rank(x));break;
case 4:printf("%d\n",Kth(x));break;
case 5:printf("%d\n",Kth(Sum(x-1)));break;
case 6:printf("%d\n",Kth(Sum(x)+1));break;
}
}
return 0;
}
\end{lstlisting}
\subsubsection{树状数组可持久化平衡树操作}
P3835 【模板】可持久化平衡树
\begin{lstlisting}
const int N=5e5+7;
int opt[N],Q[N],c[N];
vector<int>Tmp,G[N];
inline int Hash(int x){
return lower_bound(Tmp.begin(),Tmp.end(),x)-Tmp.begin()+1;
}
inline void Add(int x,int d){
for(;x<=N;x+=x&(-x))c[x]+=d;
}
inline void Insert(int x){
Add(x,1);
}
inline int Sum(int x){
int ret=0;
for(;x;x-=x&(-x))ret+=c[x];
return ret;
}
inline void Erase(int x){
Add(x,-1);
}
inline int Rank(int x){
return Sum(x-1)+1;
}
inline int Kth(int k){
int res=0;
for(int i=18;~i;--i){
res+=(1<<i);
if(c[res]>=k)res-=(1<<i);
else k-=c[res];
}
return Tmp[res];
}
int ans[N];
void dfs(int u){
int x;
bool flag;
if(opt[u]!=4)x=Hash(Q[u]);
else x=Q[u];
switch(opt[u]){
case 1:Insert(x);break;
case 2:{
flag=(Sum(x)!=Sum(x-1));
if(flag)Erase(x);
break;
}
case 3:ans[u]=Rank(x);break;
case 4:ans[u]=Kth(x);break;
case 5:ans[u]=Kth(Sum(x-1));break;
case 6:ans[u]=Kth(Sum(x)+1);break;
}
for(int i=0;i<G[u].size();++i){
dfs(G[u][i]);
}
switch(opt[u]){
case 1:Erase(x);break;
case 2:if(flag)Insert(x);break;
}
}
int main(){
int n;r(n);
for(int i=1;i<=n;++i){
int ver;r(ver),r(opt[i]),r(Q[i]);
if(opt[i]!=4)Tmp.push_back(Q[i]);
G[ver].push_back(i);
}
sort(Tmp.begin(),Tmp.end());
unique(Tmp.begin(),Tmp.end())-Tmp.begin();
dfs(0);
for(int i=1;i<=n;++i){
if(opt[i]>2)printf("%d\n",ans[i]);
}
return 0;
}
\end{lstlisting}
\subsection{线段树}
\subsubsection{主席树}
P3402 【模板】可持久化并查集
\begin{lstlisting}
// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
#define gc c=getchar()
#define r(x) read(x)
template<typename T>
inline void read(T&x){
x=0;T k=1;char gc;
while(!isdigit(c)){if(c=='-')k=-1;gc;}
while(isdigit(c)){x=x*10+c-'0';gc;}x*=k;
}
const int N=2e5+7;
int rt[N];
int id;
struct seg{int fa,dep,ls,rs;}tr[N<<5];
#define mid ((l+r)>>1)
inline int build(int l,int r){
int rt=++id;
if(l<r)tr[rt].ls=build(l,mid),tr[rt].rs=build(mid+1,r);
else tr[rt].fa=l;
return rt;
}
inline int update(int pre,int l,int r,int x,int v){
int rt=++id;
tr[rt]=tr[pre];
if(l<r)x<=mid?tr[rt].ls=update(tr[pre].ls,l,mid,x,v):tr[rt].rs=update(tr[pre].rs,mid+1,r,x,v);
else tr[rt].fa=v;
return rt;
}
inline int query(int rt,int l,int r,int x){
if(l<r)return x<=mid?query(tr[rt].ls,l,mid,x):query(tr[rt].rs,mid+1,r,x);
else return rt;
}
inline void add(int rt,int l,int r,int x){
if(l<r)x<=mid?add(tr[rt].ls,l,mid,x):add(tr[rt].rs,mid+1,r,x);
else ++tr[rt].dep;
}
int n,m;
inline int find(int ver,int x){
int f=query(rt[ver],1,n,x);
return x==tr[f].fa?f:find(ver,tr[f].fa);
}
inline void uni(int ver,int a,int b){
rt[ver]=rt[ver-1];
int f1=find(ver,a);
int f2=find(ver,b);
if(tr[f1].fa==tr[f2].fa)return;
if(tr[f1].dep<tr[f2].dep)swap(f1,f2);
rt[ver]=update(rt[ver-1],1,n,tr[f2].fa,tr[f1].fa);
if(tr[f1].dep==tr[f2].dep)add(rt[ver],1,n,tr[f1].fa);
}
inline void query(int ver,int a,int b){
printf("%d\n",find(ver,a)==find(ver,b));
}
int main(){
r(n);r(m);
rt[0]=build(1,n);
for(int i=1;i<=m;++i){
int opt,a,b;
r(opt);r(a);
switch(opt){
case 1:{
r(b);
uni(i,a,b);
break;
}
case 2:{
rt[i]=rt[a];
break;
}
case 3:{
r(b);
rt[i]=rt[i-1];
query(i,a,b);
break;
}
}
}
}
\end{lstlisting}
\subsubsection{线段树合并}
P3521 [POI2011]ROT-Tree Rotations
\begin{lstlisting}
#include<bits/stdc++.h>
using namespace std;
#define gc c=getchar()
#define r(x) read(x)
#define ll long long
template<typename T>
inline void read(T&x){
x=0;T k=1;char gc;
while(!isdigit(c)){if(c=='-')k=-1;gc;}
while(isdigit(c)){x=x*10+c-'0';gc;}x*=k;
}
const int N=200005;
int ls[N*30];
int rs[N*30];
int siz[N*30];
int n,tot;
void insert(int &rt,int l,int r,int x){
rt=++tot;
++siz[rt];
if(l==r)return;
int mid=(l+r)>>1;
if(x<=mid)insert(ls[rt],l,mid,x);
else insert(rs[rt],mid+1,r,x);
}
ll ans1,ans2,ans;
int merge(int x,int y){
if(!x||!y)return x|y;
ans1+=(ll)siz[ls[x]]*siz[rs[y]];
ans2+=(ll)siz[ls[y]]*siz[rs[x]];
ls[x]=merge(ls[x],ls[y]);
rs[x]=merge(rs[x],rs[y]);
siz[x]+=siz[y];
return x;
}
void solve(int &rt){
int x;r(x);
if(!x){
int ls,rs;
solve(ls);
solve(rs);
ans1=ans2=0;
rt=merge(ls,rs);
ans+=min(ans1,ans2);
}
else insert(rt,1,n,x);
}
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
r(n);
int t=0;
solve(t);
printf("%lld\n",ans);
return 0;
}
\end{lstlisting}
\subsubsection{最值线段树}
\begin{lstlisting}
#include<bits/stdc++.h>
using namespace std;
#define gc c=getchar()
#define r(x) read(x)
#define ll long long
template<typename T>
inline void read(T&x){
x=0;T k=1;char gc;
while(!isdigit(c)){if(c=='-')k=-1;gc;}
while(isdigit(c)){x=x*10+c-'0';gc;}x*=k;
}
const int INF=1e9;
const int N=5e5+7;
int a[N];
struct MaxInfo{
int fir,sec,cnt,tag;
inline MaxInfo(int _fir=-INF,int _sec=-INF,int _cnt=0,int _tag=0):fir(_fir),sec(_sec),cnt(_cnt),tag(_tag){}
inline void operator += (int v){
fir+=v;
sec+=v;
}
};
inline MaxInfo operator + (const MaxInfo &A,const MaxInfo &B){
if(A.fir==B.fir)return MaxInfo(A.fir,max(A.sec,B.sec),A.cnt+B.cnt);
if(A.fir<B.fir)return MaxInfo(B.fir,max(A.fir,B.sec),B.cnt);
return MaxInfo(A.fir,max(B.fir,A.sec),A.cnt);
}
struct MinInfo{
int fir,sec,cnt,tag;
inline MinInfo(int _fir=INF,int _sec=INF,int _cnt=0,int _tag=0):fir(_fir),sec(_sec),cnt(_cnt),tag(_tag){}
inline void operator += (int v){
fir+=v;
sec+=v;
}
};
inline MinInfo operator + (const MinInfo &A,const MinInfo &B){
if(A.fir==B.fir)return MinInfo(A.fir,min(A.sec,B.sec),A.cnt+B.cnt);
if(A.fir>B.fir)return MinInfo(B.fir,min(A.fir,B.sec),B.cnt);
return MinInfo(A.fir,min(B.fir,A.sec),A.cnt);
}
struct Node{
MaxInfo mx;
MinInfo mi;
ll sum;
int siz;
int tag;
inline Node (){
tag=sum=0;
}
inline Node (int x){
mx=MaxInfo(x,-INF,1);
mi=MinInfo(x,INF,1);
sum=x;
siz=1;
}
inline Node (const MaxInfo &_mx,const MinInfo &_mi,ll _sum,int _siz,int _tag=0):mx(_mx),mi(_mi),sum(_sum),siz(_siz),tag(_tag){}
inline void AddMax(int v){
if(mx.fir==mi.fir){
mi.fir+=v;
tag+=v;
}
else {
if(mx.fir==mi.sec){
mi.sec+=v;
}
mx.tag+=v;
}
mx.fir+=v;
sum+=(ll)mx.cnt*v;
}
inline void AddMin(int v){
if(mi.fir==mx.fir){
mx.fir+=v;
tag+=v;
}
else {
if(mi.fir==mx.sec){
mx.sec+=v;
}
mi.tag+=v;
}
mi.fir+=v;
sum+=(ll)mi.cnt*v;
}
inline void AddSeg(int v){
mx+=v;
mi+=v;
tag+=v;
sum+=(ll)siz*v;
}
}tr[N<<2];
inline void print(int rt){
puts("-------------------------------");
printf("ID=%d\n",rt);
printf("Sum=%d\n",tr[rt].sum);
printf("Tag=%d\n",tr[rt].tag);
printf("MaxInfo=%d %d %d %d\n",tr[rt].mx.fir,tr[rt].mx.sec,tr[rt].mx.cnt,tr[rt].mx.tag);
printf("MinInfo=%d %d %d %d\n",tr[rt].mi.fir,tr[rt].mi.sec,tr[rt].mi.cnt,tr[rt].mi.tag);
puts("-------------------------------");
}
inline Node operator + (const Node &A,const Node &B){
return Node(A.mx+B.mx,A.mi+B.mi,A.sum+B.sum,A.siz+B.siz);
}
#define ls (rt<<1)
#define rs (rt<<1|1)
inline void update(int rt){
tr[rt]=tr[ls]+tr[rs];
}
inline void pushdown(int rt){
Node &x=tr[rt];
Node &l=tr[ls];
Node &r=tr[rs];
if(x.mx.tag){
int mx=max(l.mx.fir,r.mx.fir);
if(l.mx.fir==mx)l.AddMax(x.mx.tag);
if(r.mx.fir==mx)r.AddMax(x.mx.tag);
x.mx.tag=0;
}
if(x.mi.tag){
int mi=min(l.mi.fir,r.mi.fir);
if(l.mi.fir==mi)l.AddMin(x.mi.tag);
if(r.mi.fir==mi)r.AddMin(x.mi.tag);
x.mi.tag=0;
}
if(x.tag){
l.AddSeg(x.tag);
r.AddSeg(x.tag);
x.tag=0;
}
}
void build(int rt,int l,int r){
if(l==r){
tr[rt]=Node(a[l]);
return ;
}
int mid=(l+r)>>1;
build(ls,l,mid);
build(rs,mid+1,r);
update(rt);
}
void Min(int rt,int l,int r,int v){
if(tr[rt].mx.fir<=v)return ;
if(tr[rt].mx.sec<v){
tr[rt].AddMax(v-tr[rt].mx.fir);
return ;
}
int mid=(l+r)>>1;
pushdown(rt);
Min(ls,l,mid,v);
Min(rs,mid+1,r,v);
update(rt);
}
void Max(int rt,int l,int r,int v){
if(tr[rt].mi.fir>=v)return ;
if(tr[rt].mi.sec>v){
tr[rt].AddMin(v-tr[rt].mi.fir);
return ;
}
int mid=(l+r)>>1;
pushdown(rt);
Max(ls,l,mid,v);
Max(rs,mid+1,r,v);
update(rt);
}
void Work(int rt,int l,int r,int x,int y,int opt,int v){
if(x<=l&&r<=y){
switch(opt){
case 1:return tr[rt].AddSeg(v);
case 2:return Max(rt,l,r,v);
case 3:return Min(rt,l,r,v);
}
}
int mid=(l+r)>>1;
pushdown(rt);
if(x<=mid)Work(ls,l,mid,x,y,opt,v);
if(y>mid)Work(rs,mid+1,r,x,y,opt,v);
update(rt);
}
int QMax(int rt,int l,int r,int x,int y){
if(x<=l&&r<=y)return tr[rt].mx.fir;
int mid=(l+r)>>1;
pushdown(rt);
if(y<=mid)return QMax(ls,l,mid,x,y);
else if(x>mid)return QMax(rs,mid+1,r,x,y);
else return max(QMax(ls,l,mid,x,y),QMax(rs,mid+1,r,x,y));
}
int QMin(int rt,int l,int r,int x,int y){
if(x<=l&&r<=y)return tr[rt].mi.fir;
int mid=(l+r)>>1;
pushdown(rt);
if(y<=mid)return QMin(ls,l,mid,x,y);
else if(x>mid)return QMin(rs,mid+1,r,x,y);
else return min(QMin(ls,l,mid,x,y),QMin(rs,mid+1,r,x,y));
}
ll QSum(int rt,int l,int r,int x,int y){
if(x<=l&&r<=y)return tr[rt].sum;
int mid=(l+r)>>1;
pushdown(rt);
if(y<=mid)return QSum(ls,l,mid,x,y);
else if(x>mid)return QSum(rs,mid+1,r,x,y);
else return QSum(ls,l,mid,x,y)+QSum(rs,mid+1,r,x,y);
}
int main(){
int n,m;r(n);
for(int i=1;i<=n;++i)r(a[i]);
build(1,1,n);
for(r(m);m;--m){
int opt,x,y,t;
r(opt),r(x),r(y);
if(opt<=3)r(t),Work(1,1,n,x,y,opt,t);
else switch(opt){
case 4:printf("%lld\n",QSum(1,1,n,x,y));break;
case 5:printf("%d\n",QMax(1,1,n,x,y));break;
case 6:printf("%d\n",QMin(1,1,n,x,y));break;
}
}
return 0;
}
\end{lstlisting}
\subsubsection{历史线段树}
P4314 CPU监控
\begin{lstlisting}
#include<bits/stdc++.h>
using namespace std;
#define gc c=getchar()
#define r(x) read(x)
#define ll long long
template<typename T>
inline void read(T&x){
x=0;T k=1;char gc;
while(!isdigit(c)){if(c=='-')k=-1;gc;}
while(isdigit(c)){x=x*10+c-'0';gc;}x*=k;
}
const int INF=INT_MAX/2;
const int N=1e5+7;
struct Tag{
int a,b;
inline Tag(int _a=0,int _b=-INF):a(_a),b(_b){}
}null;
inline bool operator != (const Tag &A,const Tag &B){
return A.a!=B.a||A.b!=B.b;
}
inline Tag operator + (const Tag &A,const Tag &B){
return Tag(max(-INF,A.a+B.a),max(A.b+B.a,B.b));
}