-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint.cpp
More file actions
3858 lines (3446 loc) · 79.9 KB
/
print.cpp
File metadata and controls
3858 lines (3446 loc) · 79.9 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
#include <bits/stdc++.h>
#include <iomanip>
#define DEBUG 1
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<LD, LD> PLDLD;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<char> VB;
#define FOR(i,a,b) for(int i=(a);i<(int)(b);++i)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(int)(b);--i)
#define RREP(i,n) RFOR(i,n,0)
#define CLR(a) memset((a), 0 ,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define UNQ(a) a.erase(std::unique(ALL(a)),a.end());
#define endl "\n"
#define BEGIN_STACK(size) \
void *dummy = malloc(size); \
void *org_stack; \
char *my = (char *)alloca((1 + (int)(((long long)dummy) & 127)) * 16); \
*my = 0; \
asm volatile("mov %%rsp, %%rbx\n" \
"mov %%rax, %%rsp" \
: "=b"(org_stack) \
: "a"((char *)dummy + (size)-1024));
#define END_STACK \
asm volatile("mov %%rax, %%rsp" ::"a"(org_stack)); \
free(dummy);
const LD EPS=1e-10;
const long long INFLL=(LL)(1e9)*(LL)(1e9);
const int INF=1e9+7;
template<class T>
void chmin(T& a, const T b)
{
if(a>b)
a=b;
}
template<class T>
void chmax(T& a, const T b)
{
if(a<b)
a=b;
}
const LL powLL(const LL p, const LL q)
{
LL t=1;
for(int i=0;i<q;i++)
t*=p;
return t;
}
template <typename T>
struct has_iter
{
private:
template <typename U>
static constexpr true_type check(typename U::iterator*);
template <typename U>
static constexpr false_type check(...);
public:
static constexpr bool value = decltype(check<T>(nullptr))::value;
};
template<typename T, typename U = typename T::iterator>
void print(const T& container)
{
auto&& first=begin(container), last=end(container);
auto&& back=prev(last);
for(auto e=first; e!=last; e=next(e))
cout<<*e<<" \n"[e==back];
}
extern void* enabler;
template<typename Head, typename enable_if<!has_iter<Head>::value>::type*& = enabler>
void print(const Head& head)
{
cout<<head<<endl;
}
template<> void print<string>(const string& container)
{
cout<<container<<endl;
}
template<typename Head, typename... Tail>
void print(const Head& head, const Tail&... tail)
{
cout<<head<<" ";
print(tail...);
}
template<typename... Args>
void printd(const Args&... args)
{
#ifdef DEBUG
print(args...);
#endif
}
template<typename Head>
void input(Head& head)
{
cin>>head;
}
template<typename Head, typename... Tail>
void input(Head& head, Tail&... tail)
{
cin>>head;
input(tail...);
}
void io_speedup()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
}
template<typename T>
istream& operator >> (istream& is, vector<T>& vec)
{
for(T& x: vec) is >> x;
return is;
}
template<typename T, typename U>
istream& operator >> (istream& is, pair<T, U>& t)
{
is>>t.first>>t.second;
return is;
}
template<int N, typename... Ts, typename enable_if<N == sizeof...(Ts)-1>::type*& = enabler>
void tuple_in(istream &is, tuple<Ts...> &t)
{
is>>get<N>(t);
}
template<int N, typename... Ts, typename enable_if<N < sizeof...(Ts)-1>::type*& = enabler>
void tuple_in(istream &is, tuple<Ts...> &t)
{
is>>get<N>(t);
tuple_in<N+1, Ts...>(is, t);
}
template<typename... Ts>
istream& operator >> (istream& is, tuple<Ts...>& t)
{
tuple_in<0, Ts...>(is, t);
return is;
}
template<typename T, typename U>
ostream& operator << (ostream& os, const pair<T, U>& t)
{
os<<'('<<t.first<<", "<<t.second<<')';
return os;
}
template<int N, typename... Ts, typename enable_if<N == sizeof...(Ts)-1>::type*& = enabler>
void tuple_out(ostream &os,const tuple<Ts...> &t)
{
os<<get<N>(t);
}
template<int N, typename... Ts, typename enable_if<N < sizeof...(Ts)-1>::type*& = enabler>
void tuple_out(ostream &os,const tuple<Ts...> &t)
{
os<<get<N>(t)<<", ";
tuple_out<N+1, Ts...>(os, t);
}
template<typename... Ts>
ostream& operator << (ostream& os, const tuple<Ts...>& t)
{
os<<'(';
tuple_out<0, Ts...>(os, t);
os<<')';
return os;
}
template<typename T>
vector<T> read(int n)
{
vector<T> t(n);
cin>>t;
return t;
}
template<typename T>
T read()
{
T t;
cin>>t;
return t;
}
///
/// Dice
///
enum Face
{
Front,Up,Back,Down,Left,Right,
};
template<typename T>
struct Dice
{
T pip[6];
T& operator[] (const Face id)
{
return pip[id];
}
const T& operator[] (const Face id) const
{
return pip[id];
}
void rotate(Face f1, Face f2, Face f3, Face f4)
{
int tmp=pip[f1];
pip[f1]=pip[f2];
pip[f2]=pip[f3];
pip[f3]=pip[f4];
pip[f4]=tmp;
}
void rollx() { rotate(Up, Front, Down, Back); }
void rollxi() { rotate(Up, Back, Down, Front); }
void rolly() { rotate(Up, Left, Down, Right); }
void rollyi() { rotate(Up, Right, Down, Left); }
void rollz() { rotate(Back, Left, Front, Right); }
void rollzi() { rotate(Back, Right, Front, Left); }
};
///
///Union Find
///
class UnionFind
{
private:
vector<int> parent;
public:
UnionFind(const int n):parent(vector<int>(n,-1))
{}
const int Find(const int p)
{
return parent[p] < 0 ? p : parent[p] = Find(parent[p]);
}
const void Merge(int p, int q);
const bool Belong(const int p, const int q)
{
return Find(p) == Find(q);
}
const int GetSize(const int p)
{
return -parent[Find(p)];
}
};
const void UnionFind::Merge(int p, int q)
{
p=Find(p);
q=Find(q);
if(p==q) return;
if(parent[p] < parent[q])
{
parent[p] += parent[q];
parent[q]=p;
}
else
{
parent[q] += parent[p];
parent[p]=q;
}
}
//
// Kruskal algorithm
//
struct Edge{
int start,end,dis;
bool operator >(const Edge &b)const{return dis > b.dis;}
};
using EdgeQueue=priority_queue<Edge,vector<Edge> ,greater<Edge> >;
const EdgeQueue convertGraph(const vector<vector<Edge> > &g)
{
EdgeQueue que;
for(auto&& es:g)
for(auto&& e:es)
que.push(e);
return que;
}
const vector<Edge> Kruskal(const int n, EdgeQueue que)
{
UnionFind belong(n);
Edge edge;
vector<Edge> min_cost_tree;
while(!que.empty())
{
edge=que.top();
if(belong.Find(edge.start)!=belong.Find(edge.end))
{
belong.Merge(edge.start, edge.end);
min_cost_tree.push_back(edge);
}
que.pop();
}
return min_cost_tree;
}
//
// Bellman-Ford algorithm
//
int inf = INT_MAX / 2;
int edge[V] = {inf};
int V,E;//V is the number of edges. E is that of vertexes.
vector<PII> vertex[V];
void BF()
{
bool update=true;
int to,dist;
while(update)
{
update = false;
REP(i,V)
if(edge[i]!=inf)
for(int j = 0;j < vertex[i].size();j++)
{
to = vertex[i][j].first;
dist = vertex[i][j].second;
if(edge[to] > edge[i] + dist)
{
edge[to] = edge[i] + dist;
update = true;
}
}
}
}
bool BF_negative()
{
bool update=true;
int to,dist,count;
while(update)
{
update = false;
REP(i,V)
if(edge[i]!=inf)
for(int j = 0;j < vertex[i].size();j++)
{
to = vertex[i][j].first;
dist = vertex[i][j].second;
if(edge[to] > edge[i] + dist)
{
edge[to] = edge[i] + dist;
update = true;
}
}
count ++;
if(count > V * E)
return false;
}
return true;
}
/* Dijkstra
使い方
Distを好きな辺のコストの型に置換する
Graph型変数vに辺の情報を与える
(v[a].push_back(PID(b,c))でaからbへコストcの有向辺を張る)
dijkstra(i,v):iから任意の点への最小コストのベクター
dijkstra(i,j,v):iからjへの最小コスト
dijkstra(i,j,v,path):iからjへの最小コスト+pathにiからjへの最小パス
dijkstra(i,v,preVec):iから任意の点への最小コストのベクター
get_path(i,j,preVec)でiからjへの最小パスが求まる
*/
typedef int Dist;
struct Edge;
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
const int INF = 1e9;
struct Edge
{
int to;
Dist dist;
Edge(int to_,Dist dist_):to(to_),dist(dist_)
{}
bool operator >(const Edge &ed)const
{
return dist > ed.dist;
}
};
//Shortest cost from i to j.
vector<Dist> dijkstra(int i, const Graph &vertex, vector<int> &preVector)
{
vector<int> pVector(vertex.size(), INF);
vector<Dist> shortest(vertex.size(), INF);
priority_queue<Edge, vector<Edge>, greater<Edge> > que;
que.push(Edge(i, 0));
shortest[i]=0;
Edge state(0,0), tmp(0,0), e(0,0);
while(!que.empty())
{
state = que.top();
que.pop();
if(shortest[state.to] < state.dist) continue;
for(Edge e : vertex[state.to])
{
if(shortest[e.to] > shortest[state.to] + e.dist)
{
shortest[e.to] = shortest[state.to] + e.dist;
pVector[e.to] = state.to;
tmp = Edge(e.to, shortest[e.to]);
que.push(tmp);
}
}
}
preVector = pVector;
return shortest;
}
vector<int> get_path(int i, int j, const vector<int> &preVector)
{
vector<int> rev;
rev.push_back(j);
int p=j;
while(p!=i)
{
p = preVector[p];
rev.push_back(p);
if(p==INF)
return vector<int>();
}
reverse(rev.begin(),rev.end());
return rev;
}
vector<Dist> dijkstra(int i, const Graph &vertex)
{
vector<int> preVector(vertex.size());
return dijkstra(i, vertex, preVector);
}
Dist dijkstra(int i,int j, const Graph &vertex)
{
return dijkstra(i, vertex)[j];
}
Dist dijkstra(int i,int j, const Graph &vertex, vector<int> &path)
{
vector<int> preVector(vertex.size());
vector<Dist> shortest(dijkstra(i, vertex, preVector));
path = get_path(i, j, preVector);
return shortest[j];
}
///
/// Ford-Fulkerson
///
const int INF=1e9;
struct Edge
{
int from,to,cost,rev;
};
class FlowNetwork
{
public:
FlowNetwork(int n):graph(vector<vector<Edge>>(n)),used(vector<char>(n))
{}
void add_Edge(int from, int to, int cap);
int ford_fulkerson(int from, int to);
private:
vector<vector<Edge>> graph;
vector<char> used;
int dfs(int from, int to, int cap);
};
void FlowNetwork::add_Edge(int from, int to, int cap)
{
graph[from].push_back((Edge){from,to,cap,(int)graph[to].size()});
graph[to].push_back((Edge){to,from,0,(int)graph[from].size()-1});
}
int FlowNetwork::dfs(int from, int to, int f)
{
if(from == to) return f;
int d;
used[from]=true;
for(int i=0;i<graph[from].size();i++)
{
Edge &e=graph[from][i];
if(used[e.to] || e.cost<=0) continue;
d = dfs(e.to, to, min(e.cost, f));
if(d>0)
{
e.cost -= d;
graph[e.to][e.rev].cost += d;
return d;
}
}
return 0;
}
int FlowNetwork::ford_fulkerson(int from, int to)
{
int flow=0;
while(1)
{
fill(used.begin(),used.end(),0);
int f=dfs(from,to,INF);
if(f==0)break;
flow += f;
}
return flow;
}
///
/// Dinic
///
//set INF
using FLOW = long long;
struct Edge
{
int to;
FLOW cap;
int rev;
};
class FlowNetwork
{
public:
FlowNetwork(int n):graph(vector<vector<Edge>>(n)),iter(vector<int>(n)),level(vector<int>(n))
{}
void add_Edge(int from, int to, FLOW cap);
FLOW dinic(int from, int to);
private:
vector<vector<Edge>> graph;
vector<int> iter, level;
FLOW dfs(int from, int to, FLOW cap);
void bfs(int from);
};
void FlowNetwork::add_Edge(int from, int to, FLOW cap)
{
//cout<<from<<":"<<to<<" "<<cap<<endl;
graph[from].push_back((Edge){to,cap,(int)graph[to].size()});
graph[to].push_back((Edge){from,0,(int)graph[from].size()-1});
}
void FlowNetwork::bfs(int from)
{
fill(level.begin(),level.end(),-1);
queue<int> que;
level[from]=0;
que.push(from);
while(!que.empty())
{
int v=que.front();
que.pop();
for(int i=0;i<graph[v].size();i++)
{
Edge &e=graph[v][i];
if(e.cap>0 && level[e.to]<0)
{
level[e.to] = level[v]+1;
que.push(e.to);
}
}
}
}
FLOW FlowNetwork::dfs(int from, int to, FLOW f)
{
if(from == to) return f;
for(int &i=iter[from];i<graph[from].size();i++)
{
Edge &e=graph[from][i];
if(e.cap > 0 && level[from] < level[e.to])
{
FLOW d = dfs(e.to, to, min(e.cap, f));
if(d>0)
{
e.cap -= d;
graph[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
FLOW FlowNetwork::dinic(int from, int to)
{
FLOW flow=0;
while(1)
{
bfs(from);
if(level[to]<0) return flow;
fill(iter.begin(),iter.end(),0);
FLOW f;
while((f=dfs(from,to,INF))>0)
{
flow+=f;
}
}
}
///
/// Primal-Dual
///
//O(FElogV)
using Flow=int;
using Cost=int;
struct Edge{
int to, rev;
Flow cap;
Cost cost;
};
using Edges=vector<Edge>;
using Graph=vector<Edges>;
class MinCostFlow
{
public:
vector<vector<Edge>> g;
MinCostFlow(const int n):g(vector<vector<Edge>>(n))
{}
const void add_Edge(const int from, const int to, const Flow cap, const Cost cost)
{
g[from].push_back((Edge){to, (int)g[to].size(), cap, cost});
g[to].push_back((Edge){from, (int)g[from].size()-1, 0, -cost});
}
const Cost primal_dual(const int s, const int t, Flow f);
private:
const Cost INF=1e9;
};
const Cost MinCostFlow::primal_dual(const int s, const int t, Flow f)
{
int n=g.size();
vector<Cost> h(n), dist(n);
vector<int> prevv(n), preve(n);
using P=pair<Cost, int>;
Cost res=0;
fill(ALL(h), 0);
while(f>0)
{
priority_queue<P, vector<P>, greater<P>> que;
fill(ALL(dist), INF);
dist[s]=0;
que.push(P(0, s));
while(!que.empty())
{
P p=que.top();
que.pop();
int v=p.second;
if(dist[v] < p.first) continue;
for(int i=0;i<g[v].size();i++)
{
Edge &e=g[v][i];
if(e.cap>0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to])
{
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push(P(dist[e.to], e.to));
}
}
}
if(dist[t]==INF)
return -1;
for(int v=0;v<n;v++) h[v] += dist[v];
Flow d=f;
for(int v=t;v!=s;v=prevv[v])
{
d=min(d, g[prevv[v]][preve[v]].cap);
}
f-=d;
res+=d*h[t];
for(int v=t;v!=s;v=prevv[v])
{
Edge &e=g[prevv[v]][preve[v]];
e.cap-=d;
g[v][e.rev].cap+=d;
}
}
return res;
}
/*
usage:
SegmentTree<T> tree(array, PLUS) で(array, +, 1)のセグツリーができる
update: 更新
fold: 畳み込み
*/
template<class Monoid>
struct SegmentTree
{
using M = Monoid;
using T = typename M::type;
const int SIZE;
int tree_size, leaf_number;
vector<T> node;
SegmentTree(const vector<T> &ary);
const int child_l(const int k) const
{
return k*2+1;
}
const int child_r(const int k) const
{
return k*2+2;
}
const int parent(const int k) const
{
return (k-1)/2;
}
const int least_square(const int k) const
{
if(k==0) return 0;
int tmp=k-1;
for(int i=1; 64>i; i<<=1)
tmp |= (tmp >> i);
return tmp+1;
}
const T init(const int k);
const void update(const int k, const T x);
const T fold(const int a, const int b, const int k, const int l, const int r) const;
const T fold(const int a,const int b) const
{
return fold(a, b, 0, 0, leaf_number);
}
const void print() const
{
for(int i=0;i<tree_size;i++)
cout<<node[i]<<" \n"[i==tree_size-1];
}
};
template<class M>
SegmentTree<M>::SegmentTree(const vector<T> &ary):SIZE(ary.size())
{
tree_size=SIZE;
leaf_number=least_square(tree_size);
tree_size=leaf_number*2;
node=vector<T>(tree_size);
tree_size--;
for(int i=0;i<leaf_number;i++)
if(i<SIZE)
node[i+leaf_number-1]=ary[i];
else
node[i+leaf_number-1]=M::id();
init(0);
}
template<class M>
const typename SegmentTree<M>::T SegmentTree<M>::init(const int k)
{
if(k>=leaf_number-1) return node[k];
return node[k]=M::op(init(child_l(k)),init(child_r(k)));
}
template<class M>
const void SegmentTree<M>::update(const int k, const T x)
{
int tmp = k+leaf_number-1;
node[tmp]=x;
while(tmp > 0)
{
tmp=parent(tmp);
node[tmp]=M::op(node[child_l(tmp)], node[child_r(tmp)]);
}
}
template<class M>
const typename SegmentTree<M>::T SegmentTree<M>::fold(const int a, const int b, const int k, const int l, const int r) const
{
if(r <= a || b <= l)
return M::id(); //[a,b)と[l,r)が交わらない
if(a <= l && r <= b)
return node[k]; //[a,b)が[l,r)を含む
return M::op(fold(a,b,child_l(k),l,(l+r)/2), fold(a,b,child_r(k),(l+r)/2, r));
}
template <typename T>
struct MinMonoid
{
using type = T;
static constexpr T id() { return numeric_limits<T>::max(); }
static T op(const T &a, const T &b) { return min(a, b); }
};
template <typename T>
struct MaxMonoid
{
using type = T;
static constexpr T id() { return numeric_limits<T>::min(); }
static T op(const T &a, const T &b) { return max(a, b); }
};
template <typename T>
struct AddMonoid
{
using type = T;
static constexpr T id() { return 0; }
static T op(const T &a, const T &b) { return a + b; }
};
template <typename T>
struct MultMonoid
{
using type = T;
static constexpr T id() { return 1; }
static T op(const T &a, const T &b) { return a * b; }
};
template <typename T>
using RMQ = SegmentTree<MinMonoid<T>>;
template <typename T>
using RSM = SegmentTree<AddMonoid<T>>;
///
/// BIT
///
template<typename T>
class BIT
{
private:
const int tree_size;
const function<T(T,T)> op;
const T e;
const function<T(T)> inv;
vector<T> node;
const int least_square(const int k) const
{
int tmp=k;
for(int i=1; 64>i; i<<=1)
tmp |= (tmp >> i);
return tmp+1;
}
public:
const int SIZE;
BIT(const vector<T> &ary, const function<T(T,T)> f, const T e_, const function<T(T)> inv_);
const void add(const int k, const T x);
const T fold(const int k) const;
const T fold(const int a,const int b) const
{
return op(fold(b), inv(a==0?e:fold(a-1)));
}
const void print() const
{
REP(i, tree_size)
cout<<node[i]<<" \n"[i==tree_size-1];
}
};
template<typename T>
BIT<T>::BIT(const vector<T> &ary, const function<T(T,T)> f, const T e_, const function<T(T)> inv_)
:tree_size(least_square(ary.size()+1)),op(f),e(e_),inv(inv_),SIZE(ary.size())
{
node=vector<T>(tree_size, e);
for(int i=1;i<=SIZE;i++)
add(i, ary[i-1]);
}
template<typename T>
const void BIT<T>::add(const int k, const T x) //k...0 origin
{
for(int j=k+1;j<=tree_size;j+=(j&-j))
node[j]=op(node[j], x);
}
template<typename T>
const T BIT<T>::fold(const int k) const
{
T tmp=e;
for(int j=k+1;j>0;j=j&(j-1))
tmp=op(tmp, node[j]);
return tmp;
}
#define PLUS [](int p,int q){return p+q;},0,[](int x){return -x;}
template<typename T>
class RARS
{
public:
BIT<T> bit0, bit1;
RARS(const vector<T> &ary, const function<T(T,T)> f, const T e_, const function<T(T)> inv_)
:bit0(ary, f, e_, inv_), bit1(vector<T>(ary.size()), f, e_, inv_)
{}
const void add(const int l, const int r, const int x)
{
bit0.add(l, -x*l);
bit1.add(l,x);
bit0.add(r,x*r);
bit1.add(r,-x);
}
const T sum(const int k) const
{
return bit0.fold(k)+bit1.fold(k)*k;
}
const T get(const int k) const
{
return sum(k+1)-sum(k);
}
const void print() const
{
REP(i, bit0.SIZE)
cout<<get(i)<<" \n"[i==bit0.SIZE-1];
}
};
///
/// LazySegmentTree
///
/*
OperatorMonoid:作用素モノイド
act:T x U (x {size})->T
*/
template<class Monoid, class OperatorMonoid>
struct LazySegmentTree
{
using M = Monoid;
using OM = OperatorMonoid;
static_assert(is_same<typename M::type, typename OM::type>::value, "");
using T = typename M::type;
using U = typename OM::act_type;
const int SIZE;
int tree_size, leaf_number;
vector<T> node;
vector<U> lazy_node;
LazySegmentTree(const vector<T> &ary);
const int child_l(const int k) const
{
return k*2+1;
}
const int child_r(const int k) const
{
return k*2+2;
}
const int parent(const int k) const
{
return (k-1)/2;
}