-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.cpp
127 lines (104 loc) · 2.55 KB
/
temp.cpp
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
/**************************Pair Hashing fast *********************************/
struct HASH {
size_t operator()(const pair<int, int>&x)const {
return (size_t) x.first * 37U + (size_t) x.second;
}
};
/************************* DSU implimentation ********************************/
vi dad;
int find(int n) {
if (dad[n] == 0 or dad[n] == n) return dad[n] = n;
return dad[n] = find(dad[n]);
}
/********************************** Power ***************************************/
ll power( ll a, ll b, ll c) {
if (!b) return 1;
ll res = 1;
a %= c;
while (b) {
if (b & 1) res = (res * a) % c;
b >>= 1;
a = (a * a) % c;
}
return res;
}
/********************************** nCr ***************************************/
vi fact, inv;
void preprocess(int n) {
fact.resize(n + 1);
inv.resize(n + 1);
fact[0] = inv[0] = 1;
for (int i = 1; i <= n; ++i) {
fact[i] = (fact[i - 1] * i) % M;
inv[i] = power(fact[i], M - 1, M);
}
}
/********************************** Seive ***************************************/
vi SOE(int n)
{
vector<bool> prime(n + 1, true);
for (int p = 2; p * p <= n; p++)
if (prime[p] == true)
for (int i = p * p; i <= n; i += p)
prime[i] = false;
vi res;
for (int p = 2; p <= n; p++)
if (prime[p])
res.pb(p);
return res;
}
/********************************** SEGMENT TREE *********************************/
struct segTree {
int sz;
ll noOp = -1;
vll ops, vals;
void init(int n) {
sz = 1;
while (sz < n) sz *= 2;
vals.assign(2 * sz, 0);
ops.assign(2 * sz, noOp);
}
void lazyP(int x, int lx, int rx) {
if (ops[x] != noOp)
{
if (rx - lx > 1) {
int lc = 2 * x + 1, rc = lc + 1;
ops[lc] = ops[rc] = ops[x];
}
vals[x] = (rx - lx) * ops[x];
ops[x] = noOp;
}
}
void combine(int x, int lc, int rc) {
vals[x] = vals[lc] + vals[rc];
}
void modify(int l, int r, ll v, int x, int lx, int rx) {
lazyP(x, lx, rx);
if (l >= rx or lx >= r) return;
if (lx >= l and rx <= r) {
ops[x] = v;
lazyP(x, lx, rx);
return;
}
int m = (lx + rx) >> 1, lc = 2 * x + 1, rc = lc + 1;
modify(l, r, v, lc, lx, m);
modify(l, r, v, rc, m, rx);
combine(x, lc, rc);
}
void modify(int l, int r, int v) {
modify(l, r, v, 0, 0, sz);
}
ll find(int l, int r, int x, int lx, int rx) {
lazyP(x, lx, rx);
if (l >= rx or lx >= r) return 0;
if (lx >= l and rx <= r) return vals[x];
int m = (lx + rx) >> 1, lc = 2 * x + 1, rc = lx + 1;
ll s1 = find(l, r, lc, lx, m);
ll s2 = find(l, r, rc, m, rx);
combine(x, lc, rc);
return s1 + s2;
}
ll find(int l, int r) {
return find( l, r, 0, 0, sz);
}
};