├── AhoCorasick.cpp ├── Convolution Modulo Any Prime( e.g. 10^9 + 7) ├── Example_Centroid.cpp ├── Example_Centroid_Struct.cpp ├── Fast_Walsh_Hadamard_Transform_for_Xor.cpp ├── Geometry_Template.cpp ├── Heavy_Light_and_Segtree_example.cpp ├── LCA_binary_jumping_on_tree.cpp ├── Lazy_Segtree.cpp ├── Matrix_struct.cpp ├── Max_flow_codearchive_example.cpp ├── Max_flow_min_cost.cpp ├── Maximum Flow from codearchive example new version (flow bounded by 1e9) ├── MinSegtreeWithRangeAddition.cpp ├── Node.cpp ├── Ntt.cpp ├── O(1)_dsu_by_kartikkukreja.cpp ├── O(nloglogn)_FastPrime_Sieve.cpp ├── O_1_range_max_query ├── O_1_range_minimum.cpp ├── Perfect Elimination Ordering in Chordal Graphs, **runtime can be improved**.cpp ├── Persistent_Segtree_Point_Updates.cpp ├── Splay_Tree_Example.cpp ├── binary_exponentiation_factorials.cpp ├── centroid_decomposition.cpp ├── dijkstra.cpp ├── hash.cpp ├── matrix_binary.cpp ├── nlogndsu.cpp ├── sketchyFastFFt.cpp └── treap_usage_example.cpp /AhoCorasick.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)< 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)< 20 | #define sz(x) (int)(x).size() 21 | #define rep(i, a, b) for(int i = a; i < (b); ++i) 22 | const ll INF = 0x3f3f3f3f; 23 | const ll inf = pow(10,9); 24 | ll modulo = pow(10,9)+7; 25 | ld eps = 1e-13; 26 | 27 | 28 | 29 | #define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i)) 30 | #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i)) 31 | #define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i)) 32 | #define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i)) 33 | #define ALL(x) std::begin(x), std::end(x) 34 | 35 | constexpr int32_t modpow(int64_t x, uint64_t k, int32_t MOD) { 36 | assert (0 <= x and x < MOD); 37 | int64_t y = 1; 38 | for (; k; k >>= 1) { 39 | if (k & 1) (y *= x) %= MOD; 40 | (x *= x) %= MOD; 41 | } 42 | assert (0 <= y and y < MOD); 43 | return y; 44 | } 45 | int32_t modinv_nocheck(int32_t value, int32_t MOD) { 46 | assert (0 <= value and value < MOD); 47 | if (value == 0) return -1; 48 | int64_t a = value, b = MOD; 49 | int64_t x = 0, y = 1; 50 | for (int64_t u = 1, v = 0; a; ) { 51 | int64_t q = b / a; 52 | x -= q * u; std::swap(x, u); 53 | y -= q * v; std::swap(y, v); 54 | b -= q * a; std::swap(b, a); 55 | } 56 | if (not (value * x + MOD * y == b and b == 1)) return -1; 57 | if (x < 0) x += MOD; 58 | assert (0 <= x and x < MOD); 59 | return x; 60 | } 61 | inline int32_t modinv(int32_t x, int32_t MOD) { 62 | int32_t y = modinv_nocheck(x, MOD); 63 | assert (y != -1); 64 | return y; 65 | } 66 | 67 | template 68 | struct mint { 69 | int32_t value; 70 | mint() : value() {} 71 | mint(int64_t value_) : value(value_ < 0 ? value_ % MOD + MOD : value_ >= MOD ? value_ % MOD : value_) {} 72 | mint(int32_t value_, std::nullptr_t) : value(value_) {} 73 | explicit operator bool() const { return value; } 74 | inline constexpr mint operator + (mint other) const { return mint(*this) += other; } 75 | inline constexpr mint operator - (mint other) const { return mint(*this) -= other; } 76 | inline constexpr mint operator * (mint other) const { return mint(*this) *= other; } 77 | inline constexpr mint & operator += (mint other) { this->value += other.value; if (this->value >= MOD) this->value -= MOD; return *this; } 78 | inline constexpr mint & operator -= (mint other) { this->value -= other.value; if (this->value < 0) this->value += MOD; return *this; } 79 | inline constexpr mint & operator *= (mint other) { this->value = (uint_fast64_t)this->value * other.value % MOD; return *this; } 80 | inline constexpr mint operator - () const { return mint(this->value ? MOD - this->value : 0, nullptr); } 81 | inline constexpr mint pow(uint64_t k) const { return mint(modpow(value, k, MOD), nullptr); } 82 | inline mint inv() const { return mint(modinv(value, MOD), nullptr); } 83 | inline constexpr mint operator / (mint other) const { return *this * other.inv(); } 84 | inline constexpr mint operator /= (mint other) { return *this *= other.inv(); } 85 | inline constexpr bool operator == (mint other) const { return value == other.value; } 86 | inline constexpr bool operator != (mint other) const { return value != other.value; } 87 | }; 88 | template mint operator * (int64_t value, mint n) { return mint(value) * n; } 89 | template std::istream & operator >> (std::istream & in, mint & n) { int64_t value; in >> value; n = value; return in; } 90 | template std::ostream & operator << (std::ostream & out, mint n) { return out << n.value; } 91 | 92 | template struct proth_prime {}; 93 | template <> struct proth_prime<1224736769> { static constexpr int a = 73, b = 24, g = 3; }; 94 | template <> struct proth_prime<1053818881> { static constexpr int a = 3 * 5 * 67, b = 20, g = 7; }; 95 | template <> struct proth_prime<1051721729> { static constexpr int a = 17 * 59, b = 20, g = 6; }; 96 | template <> struct proth_prime<1045430273> { static constexpr int a = 997, b = 20, g = 3; }; 97 | template <> struct proth_prime<1012924417> { static constexpr int a = 3 * 7 * 23, b = 21, g = 5; }; 98 | template <> struct proth_prime<1007681537> { static constexpr int a = 31 * 31, b = 20, g = 3; }; 99 | template <> struct proth_prime<1004535809> { static constexpr int a = 479, b = 21, g = 3; }; 100 | template <> struct proth_prime< 998244353> { static constexpr int a = 7 * 17, b = 23, g = 3; }; 101 | template <> struct proth_prime< 985661441> { static constexpr int a = 5 * 47, b = 22, g = 3; }; 102 | template <> struct proth_prime< 976224257> { static constexpr int a = 7 * 7 * 19, b = 20, g = 3; }; 103 | template <> struct proth_prime< 975175681> { static constexpr int a = 3 * 5 * 31, b = 21, g = 17; }; 104 | template <> struct proth_prime< 962592769> { static constexpr int a = 3 * 3 * 3 * 17, b = 21, g = 7; }; 105 | template <> struct proth_prime< 950009857> { static constexpr int a = 4 * 151, b = 21, g = 7; }; 106 | template <> struct proth_prime< 943718401> { static constexpr int a = 3 * 3 * 5 * 5, b = 22, g = 7; }; 107 | template <> struct proth_prime< 935329793> { static constexpr int a = 223, b = 22, g = 3; }; 108 | template <> struct proth_prime< 924844033> { static constexpr int a = 3 * 3 * 7 * 7, b = 21, g = 5; }; 109 | template <> struct proth_prime< 469762049> { static constexpr int a = 7, b = 26, g = 3; }; 110 | template <> struct proth_prime< 167772161> { static constexpr int a = 5, b = 25, g = 3; }; 111 | 112 | struct is_proth_prime_impl { 113 | template static auto check(T *) -> decltype(proth_prime::g, std::true_type()); 114 | template static auto check(...) -> std::false_type; 115 | }; 116 | template 117 | struct is_proth_prime : decltype(is_proth_prime_impl::check(nullptr)) { 118 | }; 119 | 120 | /** 121 | * @brief Number Theoretic Transformation (NTT) for Proth primes 122 | * @note O(N log N) 123 | * @note radix-2, decimation-in-frequency, Cooley-Tukey 124 | * @note cache std::polar (~ 2x faster) 125 | */ 126 | template 127 | void ntt_inplace(std::vector > & a, bool inverse) { 128 | const int n = a.size(); 129 | const int log2_n = __builtin_ctz(n); 130 | assert (n == 1 << log2_n); 131 | assert (log2_n <= proth_prime::b); 132 | 133 | // prepare rotors 134 | std::vector > ep, iep; 135 | while ((int)ep.size() <= log2_n) { 136 | ep.push_back(mint(proth_prime::g).pow(mint(-1).value / (1 << ep.size()))); 137 | iep.push_back(ep.back().inv()); 138 | } 139 | 140 | // divide and conquer 141 | std::vector > b(n); 142 | REP3 (i, 1, log2_n + 1) { 143 | int w = 1 << (log2_n - i); 144 | mint base = (inverse ? iep : ep)[i]; 145 | mint now = 1; 146 | for (int y = 0; y < n / 2; y += w) { 147 | REP (x, w) { 148 | auto l = a[y << 1 | x]; 149 | auto r = now * a[y << 1 | x | w]; 150 | b[y | x] = l + r; 151 | b[y | x | n >> 1] = l - r; 152 | } 153 | now *= base; 154 | } 155 | std::swap(a, b); 156 | } 157 | 158 | // div by n if inverse 159 | if (inverse) { 160 | auto n_inv = mint(n).inv(); 161 | REP (i, n) { 162 | a[i] *= n_inv; 163 | } 164 | } 165 | } 166 | 167 | /** 168 | * @brief multiprecation on $\mathbb{F}_p[x]$ for Proth primes 169 | * @note O(N log N) 170 | * @note (f \ast g)(i) = \sum_{0 \le j \lt i + 1} f(j) g(i - j) 171 | */ 172 | template 173 | typename std::enable_if::value, std::vector > >::type ntt_convolution(const std::vector > & a_, const std::vector > & b_) { 174 | if (a_.size() <= 32 or b_.size() <= 32) { 175 | std::vector > c(a_.size() + b_.size() - 1); 176 | REP (i, a_.size()) REP (j, b_.size()) c[i + j] += a_[i] * b_[j]; 177 | // return c; 178 | } 179 | int m = a_.size() + b_.size() - 1; 180 | int n = (m == 1 ? 1 : 1 << (32 - __builtin_clz(m - 1))); 181 | auto a = a_; 182 | auto b = b_; 183 | a.resize(n); 184 | b.resize(n); 185 | ntt_inplace(a, false); 186 | ntt_inplace(b, false); 187 | REP (i, n) { 188 | a[i] *= b[i]; 189 | } 190 | ntt_inplace(a, true); 191 | a.resize(m); 192 | return a; 193 | } 194 | 195 | template 196 | mint garner_algorithm_template(mint a1, mint a2, mint a3) { 197 | static const auto r12 = mint(MOD1).inv(); 198 | static const auto r13 = mint(MOD1).inv(); 199 | static const auto r23 = mint(MOD2).inv(); 200 | a2 = (a2 - a1.value) * r12; 201 | a3 = (a3 - a1.value) * r13; 202 | a3 = (a3 - a2.value) * r23; 203 | return mint(a1.value) + a2.value * mint(MOD1) + a3.value * (mint(MOD1) * mint(MOD2)); 204 | } 205 | 206 | /** 207 | * @brief multiprecation on $\mathbb{Z}/n\mathbb{Z}[x]$ 208 | */ 209 | template 210 | typename std::enable_if::value, std::vector > >::type ntt_convolution(const std::vector > & a, const std::vector > & b) { 211 | if (a.size() <= 32 or b.size() <= 32) { 212 | std::vector > c(a.size() + b.size() - 1); 213 | REP (i, a.size()) REP (j, b.size()) c[i + j] += a[i] * b[j]; 214 | // return c; 215 | } 216 | constexpr int PRIMES[3] = { 1004535809, 998244353, 985661441 }; 217 | std::vector > x0(a.size()); 218 | std::vector > x1(a.size()); 219 | std::vector > x2(a.size()); 220 | REP (i, a.size()) { 221 | x0[i] = a[i].value; 222 | x1[i] = a[i].value; 223 | x2[i] = a[i].value; 224 | } 225 | std::vector > y0(b.size()); 226 | std::vector > y1(b.size()); 227 | std::vector > y2(b.size()); 228 | REP (j, b.size()) { 229 | y0[j] = b[j].value; 230 | y1[j] = b[j].value; 231 | y2[j] = b[j].value; 232 | } 233 | std::vector > z0 = ntt_convolution(x0, y0); 234 | std::vector > z1 = ntt_convolution(x1, y1); 235 | std::vector > z2 = ntt_convolution(x2, y2); 236 | std::vector > c(z0.size()); 237 | REP (k, z0.size()) { 238 | c[k] = garner_algorithm_template(z0[k], z1[k], z2[k]); 239 | } 240 | return c; 241 | } 242 | 243 | 244 | constexpr int MOD = 1'000'000'007; 245 | 246 | // taken from Kimiyuki Onaka github: kmyk 247 | 248 | 249 | void deal(){ 250 | 251 | 252 | ll n, m ; 253 | cin>>n>>m; 254 | 255 | vector > a(n); 256 | fori(n){ 257 | cin>>a[i]; 258 | } 259 | 260 | vector > b(m); 261 | fori(m) { 262 | cin>>b[i]; 263 | } 264 | vector > c = ntt_convolution(a, b); 265 | fori((ll)c.size()){ 266 | cout< 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp setprecision(12)< g[MAX]; // graph 28 | vector > roots[MAX]; // all the parents (in centroid tree) of node i 29 | vector > dist[MAX]; // number of dudes at distance "k" from centre "i", with type "j" 30 | ll sz[MAX]; // sizes of subtrees 31 | void dfs(ll hd, ll pr){ // dfs to update sizes 32 | sz[hd] = 1; 33 | for(auto& hr : g[hd]){ 34 | if(hr==pr || dlt[hr]) 35 | continue; 36 | dfs(hr,hd); 37 | sz[hd]+=sz[hr]; 38 | } 39 | } 40 | ll centre(ll hd,ll pr, ll siz){ // find the centre of the given tree 41 | for(auto& hr : g[hd]){ 42 | if(hr == pr || dlt[hr]) 43 | continue; 44 | if(sz[hr] > siz/2 ) 45 | return centre(hr,hd,siz); 46 | } 47 | return hd; 48 | } 49 | void push_root(ll hd, ll pr, ll root, ll dt, ll& ty, vector& track, ll& mx){ // push the chosen root to all the descendants in centroid tree 50 | roots[hd].push_back(vector({root, ty, dt})); 51 | track.pb(dt); 52 | mx = max(mx,dt); 53 | for(auto hr : g[hd]){ 54 | if(hr==pr || dlt[hr]) 55 | continue; 56 | push_root(hr,hd,root,dt+1, ty, track, mx); 57 | } 58 | } 59 | void create(ll st){ // create the centroid tree 60 | dfs(st,-1); 61 | // cout<<"we started "< > (g[cnt].size() + 2); 66 | ll overall = 1; 67 | fori(g[cnt].size()){ 68 | ll hr = g[cnt][i]; 69 | if(!dlt[hr]){ 70 | vector track; 71 | ll mx = 0; 72 | push_root(hr, cnt, cnt, 1, i, track, mx); 73 | ++mx; 74 | dist[cnt][i].resize(mx, 0); 75 | overall = max(overall, mx); 76 | for(auto el : track) 77 | dist[cnt][i][el]++; 78 | for(ll j =1; j({cnt, (ll)g[cnt].size(), 0})); 88 | dist[cnt][g[cnt].size()].resize(1); 89 | dist[cnt][g[cnt].size()][0] = 1; 90 | ll last = dist[cnt].size()-1; 91 | dist[cnt][last].resize(overall,0); 92 | dist[cnt][last][0] = 1; 93 | fori(dist[cnt].size()-1) 94 | for(ll j = 1; j>n>>k; 141 | fori(n-1){ 142 | ll a, b; 143 | cin>>a>>b; 144 | --a, --b; 145 | centroid::g[a].pb(b); 146 | centroid::g[b].pb(a); 147 | } 148 | if(k==1){ 149 | cout<<2; 150 | return; 151 | } 152 | centroid::create(0); 153 | ll mx = 0; 154 | fori(n){ 155 | ll ans = centroid::find_up_to(i, k/2) + 1; 156 | cout<<"up to for "< 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp setprecision(12)< >* g; // graph 29 | vector >* roots; // all the parents (in centroid tree) of node i 30 | ll* sz; // sizes of subtrees 31 | 32 | centroid( ll n){ 33 | dlt = new bool[n]; 34 | fori(n) dlt[i] = 0; 35 | g = new vector > [n]; 36 | roots = new vector > [n]; 37 | sz = new ll [n]; 38 | } 39 | 40 | void dfs(ll hd, ll pr){ // dfs to update sizes 41 | sz[hd] = 1; 42 | for(auto el : g[hd]){ 43 | auto hr = el.ff; 44 | if(hr==pr || dlt[hr]) 45 | continue; 46 | dfs(hr,hd); 47 | sz[hd]+=sz[hr]; 48 | } 49 | } 50 | ll centre(ll hd,ll pr, ll siz){ // find the centre of the given tree 51 | for(auto el : g[hd]){ 52 | auto hr = el.ff; 53 | if(hr == pr || dlt[hr]) 54 | continue; 55 | if(sz[hr] > siz/2 ) 56 | return centre(hr,hd,siz); 57 | } 58 | return hd; 59 | } 60 | void push_root(ll hd, ll pr, ll root, ll dt){ // push the chosen root to all the descendants in centroid tree 61 | roots[hd].push_back(mp(root,dt)); 62 | for(auto el : g[hd]){ 63 | auto hr = el.ff; 64 | if(hr==pr || dlt[hr]) 65 | continue; 66 | push_root(hr,hd,root,dt+el.ss); 67 | } 68 | } 69 | void create(ll st){ // create the centroid tree 70 | dfs(st,-1); 71 | // cout<<"we started "< mins[MAX]; 92 | int last[MAX]; 93 | #define MAX2 (int)(5*pow(10,6)+10) 94 | vector batch[MAX2]; 95 | ll idx = 0; 96 | bool observe = 0; 97 | 98 | void push_root2(centroid& c2, int hd, int pr, int root2, ll dt, centroid& c1){ // push the chosen root to all the descendants in centroid tree 99 | c2.roots[hd].push_back(mp(root2,dt)); 100 | // cout<<"we here ballin with root "<({hd, root, sum})); 123 | last[root] = hd; 124 | } 125 | } 126 | 127 | 128 | void create2(ll st, centroid& c2, centroid& c1){ 129 | c2.dfs(st,-1); 130 | // cout<<"we started "<>n; 187 | centroid c1(n),c2(n); 188 | fori(n-1){ 189 | ll a , b, w; 190 | cin>>a>>b>>w; --a, --b; 191 | c1.g[a].pb(mp(b,w)); 192 | c1.g[b].pb(mp(a,w)); 193 | } 194 | fori(n-1){ 195 | ll a, b, w; 196 | cin>>a>>b>>w; --a, --b; 197 | c2.g[a].pb(mp(b,w)); 198 | c2.g[b].pb(mp(a,w)); 199 | } 200 | c1.create(0); 201 | create2(0, c2, c1); 202 | /* cout<<"for tree 1 "< 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)<-1; i--){ 28 | has*=has; 29 | has%=modulo; 30 | if(((ll)1<& p, bool inv = false) { 45 | for (ll len=1; 2*len<=N; len*=2) { 46 | for (ll i=0; i all(MAX,0); 65 | ll n; 66 | 67 | void pre(){ 68 | fori(MAX) 69 | prime[i] = 1; 70 | prime[0] = 0, prime[1] = 0; 71 | for(ll i = 2; i>n; 90 | pre(); 91 | vector all1; 92 | all1 = all; 93 | // cout<<"we before "< 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)< pt; 29 | typedef vector pol; 30 | typedef vector vi; 31 | typedef vector vvi; 32 | typedef vector vd; 33 | typedef vector vvd; 34 | typedef pair pii; 35 | typedef vector vpii; 36 | namespace std { 37 | templateinline bool operator<(const complex& a,const complex& b){ 38 | return a.real() struct cplx { 53 | T x, y; cplx() {x = 0.0; y = 0.0;} 54 | cplx(T nx, T ny=0) {x = nx; y = ny;} 55 | cplx operator+(const cplx &c) const {return {x + c.x, y + c.y};} 56 | cplx operator-(const cplx &c) const {return {x - c.x, y - c.y};} 57 | cplx operator*(const cplx &c) const {return {x*c.x - y*c.y, x*c.y + y*c.x};} 58 | cplx& operator*=(const cplx &c) { return *this={x*c.x-y*c.y, x*c.y+y*c.x}; } 59 | // Only supports right scalar multiplication like p*c 60 | template cplx operator*(const U &c) const {return {x*c,y*c};} 61 | template cplx operator/(const U &c) const {return {x/c,y/c};} }; 62 | #define polar(r,a) (pt){r*cos(a),r*sin(a)} 63 | typedef cplx pt; 64 | typedef vector pol; 65 | pt operator*(ld c, const pt p) { return {p.x*c,p.y*c};} // for left mult. c*p 66 | // useful for debugging 67 | ostream&operator<<(ostream &o,const pt &p){o<<"("<inline bool operator<(const cplx& a,const cplx& b){ 75 | return a.x abs(a-b) 89 | inline bool eq(const pt &a, const pt &b) { return abs(a-b) < EPS; } 90 | inline ld sgn(const ld& x) { return abs(x) < EPS ? 0 : x/abs(x); } 91 | inline bool cmp_lex(const pt& a, const pt& b) { 92 | return a.real()EPS?1/za:0; zb=zb>EPS?1/zb:0; 99 | int s1 = sgn(cp(a2-a1, b1-a1)*za), s2 = sgn(cp(a2-a1, b2-a1)*za); 100 | int s3 = sgn(cp(b2-b1, a1-b1)*zb), s4 = sgn(cp(b2-b1, a2-b1)*zb); 101 | if(!s1 && !s2 && !s3) { // collinear 102 | if (cmp_lex(a2, a1)) swap(a1, a2); if (cmp_lex(b2, b1)) swap(b1, b2); 103 | //return cmp_lex(a1, b2) && cmp_lex(b1, a2);//uncomment to exclude endpoints 104 | return !cmp_lex(b2, a1) && !cmp_lex(a2, b1); 105 | } return s1*s2 <= 0 && s3*s4 <= 0; } //change to < to exclude endpoints 106 | 107 | inline pt line_inter(const pt &a, const pt &b, const pt &c, const pt &d) { 108 | return a + cp(c - a, d - c) / cp(b - a, d - c) * (b - a); } 109 | 110 | // Projection of (a -> p) to vector (a -> b), SIGNED - positive in front 111 | inline ld proj_dist(const pt &a, const pt &b, const pt &p) { 112 | return dp(b - a, p - a) / abs(b - a); } 113 | 114 | // SIGNED distance. Pt on the right of vector (a -> b) will be NEGATIVE. 115 | inline ld lp_dist(const pt &a, const pt &b, const pt &p) { 116 | return cp(b - a, p - a) / abs(b - a); } 117 | 118 | // Line segment (a, b) to pt p distance. 119 | inline ld lsp_dist(const pt &a, const pt &b, const pt &p) { 120 | return dp(b - a, p - a) > 0 && dp(a - b, p - b) > 0 ? 121 | abs(cp(b - a, p - a) / abs(b - a)) : min(abs(a - p), abs(b - p)); } 122 | 123 | // Closest pt on line segment (a, b) to pt p. 124 | inline pt lsp_closest(const pt &a, const pt &b, const pt &p) { 125 | if (dp(b - a, p - a) > 0 && dp(a - b, p - b) > 0) 126 | return abs(cp(b-a, p-a)) < EPS ? p : line_inter(a, b, p, p+(a-b)*pt(0,1)); 127 | return abs(a - p) < abs(b - p) ? a : b; } 128 | 129 | // Area of a polygon (convex or concave). Always non-negative. 130 | ld area(const pol &v) { ld s = 0; int n = v.size(); 131 | for(int i = n-1, j = 0; j < n; i = j++) s += cp(v[i], v[j]); return abs(s)/2;} 132 | 133 | // orientation does not matter 134 | pt centroid(const pol &v) { 135 | pt res; ld A = 0; int n = v.size(); 136 | for(int i=n-1,j=0;j b) will be cut off. Convex polygons tested UVa 10117 140 | // Simple polygon tested KTH Challenge 2013 G 141 | pol cut_polygon(const pol &v, const pt &a, const pt &b) { pol out; 142 | for(int i = v.size() - 1, j = 0; j < v.size(); i = j++) { 143 | if(cp(b-a, v[i]-a) < EPS) out.push_back(v[i]); 144 | if(sgn(cp(b-a, v[i]-a)) * sgn(cp(b-a, v[j]-a)) < 0) { 145 | pt p = line_inter(a, b, v[i], v[j]); 146 | if(!out.size() || abs(out.back() - p) > EPS) out.push_back(p); } } 147 | while(out.size() && abs(out[0] - out.back()) < EPS) out.pop_back(); 148 | return out; } 149 | 150 | //////////////////////////////////////////////////////////////////////////////// 151 | // Circle-circle intersection (TESTED UVa 453) 152 | //////////////////////////////////////////////////////////////////////////////// 153 | // Return number of intersections. Circles must not be identical. 154 | int cc_inter(pt p1, ld r1, pt p2, ld r2, pt &i1, pt &i2) { 155 | ld dq=norm(p1-p2), rq=r1*r1-r2*r2; pt c=(p1+p2)*0.5L + (p2-p1)*rq*0.5L/dq; 156 | ld dt=2.0*dq*(r1*r1+r2*r2)-dq*dq-rq*rq; 157 | if(dt < -EPS) return 0; if(dt < EPS) { i1=i2=c; return 1; } 158 | dt=sqrt(dt)*0.5/dq; i1=c+(p2-p1)*pt(0,1)*dt; i2=c-(p2-p1)*pt(0,1)*dt; 159 | return 2; } 160 | 161 | // Does NOT include points on the ends of the segment. 162 | inline bool on_segment(const pt &a, const pt &b, const pt &p) { 163 | return abs(cp(b-a, p-a)) < EPS && dp(b-a, p-a) > 0 && dp(a-b, p-b) > 0; } 164 | 165 | // Checks if p lies on the boundary of a polygon v. 166 | inline bool on_boundary(const pol &v, const pt &p) { bool res = false; 167 | for(int i=v.size()-1,j=0;j 1; } // will be either 2*PI or 0 175 | 176 | 177 | // points outside polygon 178 | // minimum radius to cover whole Polygon 179 | 180 | void test(){ 181 | ll n , m ; 182 | cin>>n; 183 | pol pl; 184 | fori(n){ 185 | ld x,y; 186 | cin>>x>>y; 187 | pl.pb(pt(x,y)); 188 | } 189 | pol crc; 190 | cin>>m; 191 | fori(m){ 192 | ld x,y; 193 | cin>>x>>y; 194 | crc.pb(pt(x,y)); 195 | } 196 | ld lo = 0, hi = 1e9; 197 | while((hi-lo)>eps){ 198 | ld mid = (lo+hi)/2; 199 | pol check; 200 | fori(m){ 201 | for(ll j = i+1; j 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)< b.r || lazy[num].r < b.l) 77 | return; 78 | if(lazy[num].l >= b.l && lazy[num].r<=b.r){ 79 | lazy[num] += b; 80 | push(num); 81 | return; 82 | } 83 | upd(num<<1, b), upd((num<<1)|1, b); 84 | pull(num); 85 | } 86 | 87 | void que(ll num, node& b){ 88 | if(lazy[num].l > b.r || lazy[num].r < b.l) return ; 89 | if(lazy[num].tag) push(num); 90 | if(lazy[num].l >= b.l && lazy[num].r<=b.r){ b *= lazy[num]; return; } 91 | que(num<<1, b) , que((num<<1)|1, b); 92 | } 93 | 94 | ~Segtree(){ delete[] lazy; } 95 | }; 96 | 97 | // u ll have to modify "node" struct accordingly 98 | // usage : 99 | // Segtree lz 100 | // lz.init(1,1,n); 101 | // Updates: 102 | // Segtree::node query(l,r,0,tagval), lz.upd(1, query). !!! tagval is the update ( so initialize it ) !!! 103 | // Queries: 104 | // Segtree::node query(l,r,val, 0), lz.que(1, query). !!! val will be answer to query ( so initialize it ) !! 105 | #define N 200200 106 | vector adj[N];// Heavy Light Decomposition (Paths > Trees) -- henryx 107 | // TESTED: cf1023/F, cf101908/L, cf102059/A, SPOJ/QTREE, SPOJ/QTREE3, cf101669/L 108 | //////////////////////////////////////////////////////////////////////////////// 109 | //usage: 0) 1-INDEXED. 1) build tree in vector adj[N]; 2) implement SegTree 110 | //dealing with the lca: path is lca-exclusive! for lca-inclusive, remove 111 | // if(pos[a]!=pos[b]), and change update(pos[b]+1,...) to update(pos[b],...) 112 | //editing insert_path: 1) for query_path, change segt[]->update to segt[]->query 113 | // these queries should be accumulated into some variable to be returned 114 | // 2) insert_node/query_node should look similar to segt[ch[a]]->query(pos[a]); 115 | //NOTE: if SegTree operation is not commutative, then require b to be an 116 | // ancestor of a, and split each path uv into u->lca and v->lca before using HLD 117 | 118 | /* implement the rest of the segment tree */ 119 | namespace HLD { Segtree* segt[N]; 120 | ll d[N], par[N], sub[N], sz[N], root[N], ch[N], pos[N]; ll dn, cn; 121 | ll dfs(ll u, ll p) { d[u] = d[par[u]=p]+1; sub[u] = 1; 122 | for (ll x:adj[u]) if (x!=p) sub[u] += dfs(x,u); return sub[u]; } 123 | void hld(ll u, ll p) { if (dn == cn) { root[cn]=u; sz[cn++]=0; } 124 | pos[u] = sz[ch[u]=dn]++; ll b=-1, c=-1; 125 | for (ll x:adj[u]) if (x!=p && sub[x]>b) b=sub[c=x]; if (c!=-1) hld(c,u); 126 | for (ll y:adj[u]) if (y!=p && y!=c) { dn++; hld(y,u); } } 127 | void build(ll r) { d[0] = dn = cn = 0; dfs(r,0); hld(r,0); 128 | for (ll i=0; iupd(1, query); a = par[root[ch[a]]]; } 134 | if (pos[a] != pos[b]) { if (d[a] < d[b]) swap(a,b); 135 | Segtree::node query(pos[b]+1, pos[a], 0, v); 136 | segt[ch[a]]->upd(1, query); } return b; } 137 | ll query_path(ll a, ll b, ll v) { // please expand this when typing 138 | Segtree::node ans(-1, -1, 0, 0); 139 | while (ch[a] != ch[b]) { if (d[root[ch[a]]] < d[root[ch[b]]]) swap(a,b); 140 | Segtree::node query(0, pos[a], 0, v); 141 | segt[ch[a]]->que(1, query); a = par[root[ch[a]]]; 142 | ans *= query; 143 | } 144 | if (pos[a] != pos[b]) { if (d[a] < d[b]) swap(a,b); 145 | Segtree::node query(pos[b]+1, pos[a], 0, v); 146 | segt[ch[a]]->que(1, query); 147 | ans *= query;} return ans.val; } 148 | } // returns lca 149 | 150 | void test(){ 151 | ll n; 152 | cin>>n; 153 | fori(n-1){ 154 | ll a, b; 155 | cin>>a>>b; 156 | adj[a].pb(b); 157 | adj[b].pb(a); 158 | } 159 | HLD::build(1); 160 | ll q; 161 | cin>>q; 162 | forl(q){ 163 | ll ty; 164 | cin>>ty; 165 | if(ty == 1){ 166 | ll a, b, vl; 167 | cin>>a>>b>>vl; 168 | HLD::insert_path(a,b,vl); 169 | } 170 | else{ 171 | ll a, b; 172 | cin>>a>>b; 173 | cout< 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp setprecision(12)< g[MAX]; 28 | ll lca[MAX][MAX2]; 29 | ll dep[MAX]; 30 | 31 | 32 | void dfs(ll hd, ll pr){ 33 | lca[hd][0] = pr; 34 | for(auto hr : g[hd]){ 35 | if(hr == pr) 36 | continue; 37 | dep[hr ] = dep[hd]+ 1; 38 | dfs(hr, hd); 39 | } 40 | } 41 | 42 | void pre(ll n){ 43 | for(ll j = 1; j-1; j--) 53 | if(dep[lca[a][j]] > dep[b]) 54 | a = lca[a][j]; 55 | if(dep[a] != dep[b]) 56 | a = lca[a][0]; 57 | for(ll j=MAX2-1; j>-1; j--) 58 | if(lca[a][j]!=lca[b][j]) 59 | a = lca[a][j], b= lca[b][j]; 60 | if(a!=b) 61 | a = lca[a][0], b = lca[b][0]; 62 | return a; 63 | } 64 | 65 | void test(){ 66 | ll n , q; 67 | cin>>n>>q; 68 | vector dfs_; 69 | fori(n){ 70 | ll ed; 71 | cin>>ed; 72 | if(ed == -1) 73 | dfs_.pb(i); 74 | else 75 | g[ed].pb(i); 76 | } 77 | for(auto el : dfs_) 78 | dfs(el, el); 79 | pre(n); 80 | forx(q){ 81 | ll a, b; 82 | cin>>a>>b; 83 | if(lca[a][19] != lca[b][19]) 84 | cout<<"No\n"; 85 | else{ 86 | ll lc = LCA(a,b); 87 | if(lc==b) 88 | cout<<"Yes\n"; 89 | else 90 | cout<<"No\n"; 91 | } 92 | } 93 | } 94 | 95 | void deal(){ 96 | // !!! call dfs(root, root) , dont put -1 as the parent of the root !!! 97 | // nodes must be numbered as [ 0 .. n-1 ] 98 | // and then call pre() 99 | } 100 | 101 | int main(){ 102 | cin.tie(0); 103 | ios_base::sync_with_stdio(0); 104 | test(); // check whether lca of two nodes is the same 105 | } 106 | -------------------------------------------------------------------------------- /Lazy_Segtree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)< b.r || lazy[num].r < b.l) 81 | return; 82 | if(lazy[num].l >= b.l && lazy[num].r<=b.r){ 83 | lazy[num] += b; 84 | push(num); 85 | return; 86 | } 87 | upd(num<<1, b), upd((num<<1)|1, b); 88 | pull(num); 89 | } 90 | 91 | void que(ll num, node& b){ 92 | if(lazy[num].l > b.r || lazy[num].r < b.l) return ; 93 | if(lazy[num].tag) push(num); 94 | if(lazy[num].l >= b.l && lazy[num].r<=b.r){ b *= lazy[num]; return; } 95 | que(num<<1, b) , que((num<<1)|1, b); 96 | } 97 | 98 | ~Segtree(){ delete[] lazy; } 99 | }; 100 | 101 | void deal(){ 102 | // u ll have to modify "node" struct accordingly 103 | // usage : 104 | // Segtree lz 105 | // lz.init(1,1,n); 106 | // Updates: 107 | // Segtree::node query(l,r,0,tagval), lz.upd(1, query). !!! tagval is the update ( so initialize it ) !!! 108 | // Queries: 109 | // Segtree::node query(l,r,val, 0), lz.que(1, query). !!! val will be answer to query ( so initialize it ) !! 110 | Segtree lz; 111 | 112 | } 113 | int main(){ 114 | cin.tie(0); 115 | ios_base::sync_with_stdio(0); 116 | deal(); 117 | } 118 | -------------------------------------------------------------------------------- /Matrix_struct.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)< pt; 23 | typedef vector pol; 24 | typedef vector vi; 25 | typedef vector vvi; 26 | typedef vector vd; 27 | typedef vector vvd; 28 | typedef pair pii; 29 | typedef vector vpii; 30 | const ll INF = 0x3f3f3f3f; 31 | const ll inf = 2*pow(10,9); 32 | ll modulo = pow(10,9) + 7; 33 | 34 | // matrix structure for classic Matrix exponentiation type of problems 35 | 36 | 37 | struct Matrix{ // !!! everything is an integer , and we take modulo while exponentiating (is available up to 2^30) / multiplying 38 | // n should be nonzero 39 | vector > arr; 40 | int n, m; 41 | Matrix(){} 42 | Matrix(int n, int m, bool id){ // if id is true (it must be a square matrix!!) identity matrix is created 43 | this->n = n , this->m = m; 44 | arr.resize(n, vector (m, 0)); 45 | if(id) 46 | fori(n) 47 | arr[i][i] = 1; 48 | } 49 | Matrix(vector > arr){ 50 | this->arr = arr; 51 | n = arr.size(); 52 | m = arr[0].size(); 53 | } 54 | Matrix Multiply(Matrix& A){ 55 | int n1 = A.n , m1 = A.m; 56 | assert(m==n1); 57 | vector > lz; 58 | lz.resize(n); 59 | fori(n){ 60 | lz[i].resize(m1); 61 | forj(m1) lz[i][j] = 0; 62 | } 63 | fori(n) 64 | for(int j1=0; j1& operator [] (ll i){ 70 | return arr[i]; 71 | } 72 | }; 73 | void display(Matrix& A){ 74 | cout<<"number of rows "<-1; i--){ 85 | start = start.Multiply(start); 86 | if(((ll)1< 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)< e^1 29 | //////////////////////////////////////////////////////////////////////////////// 30 | // int first[N]; // first[u] is the first edge in the adjacency list of u. 31 | // int nxt[2*M]; // nxt[e] is the next edge in the same adjacency list as e. 32 | // int ep[2*M]; // ep[e] is the vertex whose adjacency list e is in. e^1 is 33 | // the reverse twin of e, so ep[e^1] is its other endpoint. 34 | // 35 | // memset(first, -1, sizeof first); // m edges are input as pairs of endpoints 36 | // for(int e=0; e<2*m; ++e) cin>>ep[e], nxt[e] = first[ep[e]], first[ep[e]] = e; 37 | // 38 | // for(int e=first[u]; e!=-1; e=nxt[e]) int v = ep[e^1]; // scan neighbours of u 39 | //////////////////////////////////////////////////////////////////////////////// 40 | 41 | //////////////////////////////////////////////////////////////////////////////// 42 | // Maximum flow 43 | //////////////////////////////////////////////////////////////////////////////// 44 | //*! 45 | namespace n5 { 46 | //*/ 47 | // data structures and helper functions common to all flow routines 48 | int par[N], first[N], nxt[2*M], ep[2*M], m; 49 | ll flo[2*M], cap[2*M], cost[2*M]; 50 | void init() { m = 0; memset(first,-1,sizeof first); memset(flo,0,sizeof flo); } 51 | void add_edge(int a, int b, ll c=1, ll p=0) { //a,b - nodes, c, p - cap, price 52 | nxt[m] = first[ep[m]=a], first[ep[m]] = m, cap[m] = c, cost[m] = p, ++m; 53 | nxt[m] = first[ep[m]=b], first[ep[m]] = m, cap[m] = 0, cost[m] = -p, ++m; } 54 | 55 | // Max flow without costs, USAGE: 1) init(); 2) add edges, 3) ll flow=0; and 4) 56 | // for (ll del=INF; del; del/=2) while (ll df=mf_update(s, t, del)) flow+=df; 57 | // Then if you want to extract a min cut: for (int e = 0; e < m; ++e) 58 | // if (par[ep[e]] != -1 && par[ep[e^1]] == -1) cut.push_back(e); 59 | // If you want to recover flow paths: get set of edges with flo[e] > 0. 60 | // E^2logC in general, E*flow on integer capacity graphs 61 | ll mf_update(int s, int t, ll del=1) { 62 | ll df[N]; memset(df, 0, sizeof df); memset(par, -1, sizeof par); 63 | queue q; q.push(s); par[s] = -2; df[s] = INF; 64 | while (!q.empty()) { ll cf; int u = q.front(); q.pop(); 65 | for (int v, e = first[u]; e != -1; e = nxt[e]) 66 | if (par[v=ep[e^1]] == -1 && (cf = cap[e]-flo[e]) >= del) 67 | q.push(v), par[v] = e, df[v] = min(df[u], cf); } 68 | if (par[t] == -1) return 0; 69 | for (int e, i = t; i != s; i = ep[e]) 70 | e = par[i], flo[e] += df[t], flo[e^1] -= df[t]; 71 | return df[t]; } 72 | 73 | // Dinic's fast maximum flow: tested on NAIPC 2015 F but not theoretically 74 | // proven to be floating-point-safe. 75 | // USAGE: 1) init(); 2) add edges 76 | // 3) ll flow=0; while (bfs(S, T)) flow += dfs(S, T, INF); 77 | // V^2E in general, min(V^(2/3),sqrt(E))E on unit caps, sqrt(V)E on bipartite 78 | int L[N], cur[N]; 79 | bool bfs(int s, int t) { 80 | memset(L, INF, sizeof L); 81 | queue q; q.push(s); L[s] = 0; 82 | while (!q.empty()) { int u = q.front(); q.pop(); 83 | for (int v, e = cur[u] = first[u]; e != -1; e = nxt[e]) 84 | if (L[v=ep[e^1]] == INF && flo[e] < cap[e]) 85 | q.push(v), L[v] = L[u]+1; 86 | } return L[t] < INF; } 87 | ll dfs(int u, int t, ll f) { 88 | if (u == t) return f; 89 | ll cf, df = 0; 90 | for (int v, e = cur[u]; e != -1 && df < f; cur[u] = e = nxt[e]) 91 | if (flo[e] < cap[e] && L[v=ep[e^1]] == L[u]+1) { 92 | cf = dfs(v, t, min(cap[e]-flo[e], f-df)); 93 | flo[e] += cf; flo[e^1] -= cf; df += cf; 94 | } return df; } 95 | 96 | // Minimum cost maximum flow, assuming there are no negative cost cycles 97 | // USAGE: 1) init(); 2) add edges, 3) mcf_pot_init(numV) and 4) 98 | // ll price=0,flow=0; while (ll df = mcf_update(s, t, price, numV)) flow += df; 99 | //! for sparse graphs, may help to change Dijkstra from O(N^2) to O(M*lgN) 100 | //! code is provided in comments 101 | /*! 102 | int q[N], inq[N]; 103 | #define BUBL { \ 104 | t = q[i]; q[i] = q[j]; q[j] = t; \ 105 | t = inq[q[i]]; inq[q[i]] = inq[q[j]]; inq[q[j]] = t; } 106 | */ 107 | bool vis[N]; ll pot[N], dist[N]; 108 | void mcf_pot_init(int n) { memset(pot, 0, sizeof pot); 109 | // if all edge costs >= 0, we can safely return before the Bellman-Ford here 110 | for (int k = 1; k < n; ++k) for (int e = 0; e < m; ++e) 111 | if (cap[e]) pot[ep[e^1]] = min(pot[ep[e^1]], pot[ep[e]] + cost[e]); } 112 | ll mcf_update(int s, int t, ll& price, int n) { 113 | memset(vis, 0, sizeof vis); memset(dist, INF, sizeof dist); dist[s] = 0; 114 | /*! 115 | memset(inq, -1, sizeof inq); 116 | int qs = 0; inq[q[qs++] = s] = 0; 117 | while (qs) { 118 | int u = q[0]; inq[u] = -1; 119 | q[0] = q[--qs]; 120 | if( qs ) inq[q[0]] = 0; 121 | for( int i = 0, j = 2*i + 1, t; j < qs; i = j, j = 2*i + 1 ) { 122 | if( j + 1 < qs && dist[q[j + 1]] < dist[q[j]] ) j++; 123 | if( dist[q[j]] >= dist[q[i]] ) break; 124 | BUBL; } 125 | if (vis[u] == 1) continue; vis[u] = 1; 126 | for (int e = first[u]; e != -1; e = nxt[e]) { int v = ep[e^1]; 127 | if (flo[e] < cap[e] && dist[v] > dist[u] + pot[u] - pot[v] + cost[e]) { 128 | dist[v] = dist[u] + pot[u] - pot[v] + cost[e], par[v] = e; 129 | if( inq[v] < 0 ) { inq[q[qs] = v] = qs; qs++; } 130 | for( int i = inq[v], j = ( i - 1 )/2, t; 131 | dist[q[i]] < dist[q[j]]; i = j, j = ( i - 1 )/2 ) 132 | BUBL; 133 | } 134 | } 135 | } 136 | */ 137 | //! replaces the below section 138 | for (int u = s; u != -1; ) { vis[u] = true; // do Dijkstra 139 | for (int e = first[u]; e != -1; e = nxt[e]) { int v = ep[e^1]; 140 | if (flo[e] < cap[e] && dist[v] > dist[u] + pot[u] - pot[v] + cost[e]) 141 | dist[v] = dist[u] + pot[u] - pot[v] + cost[e], par[v] = e; } 142 | u = -1; ll best = INF; 143 | for (int i = 0; i < n; ++i) if (!vis[i] && best>dist[i]) best=dist[u=i]; } 144 | //! 145 | if (dist[t] >= INF) return 0; ll df = INF; 146 | for (int e, i = t; i != s; i = ep[e]) 147 | e = par[i], df = min(df, cap[e] - flo[e]); 148 | for (int e, i = t; i != s; i = ep[e]) 149 | e = par[i], flo[e] += df, flo[e^1] -= df, price += df*cost[e]; 150 | for (int i = 0; i < n; ++i) pot[i] = min(INF, dist[i] + pot[i]); 151 | return df; } 152 | //*! 153 | } 154 | //*/ 155 | //////////////////////////////////////////////////////////////////////////////// 156 | // Maximum flow with lower bounds/demands 157 | //////////////////////////////////////////////////////////////////////////////// 158 | // Create a new graph G' with source s' and sink t', where 159 | // -all nodes and edges from the original graph G are retained 160 | // -cap of s' to v: sum(demands into v) 161 | // -cap of v to t': sum(demands from v) 162 | // -cap of edge: (cap)-(demand) in original graph G 163 | // -cap of t to s: infinity 164 | // Run max flow on G' from s' to t'. 165 | // A flow for G is feasible if the max flow in G' is saturated. 166 | // To find the flow, run max flow on G, from the sink to the source, where 167 | // -cap of v to u: (flow of u to v) 168 | // Now subtract the edge values from this flow from the sum of (original max 169 | // flow on G) and (demands on edges in G). 170 | //////////////////////////////////////////////////////////////////////////////// 171 | // ifstream in; 172 | // ofstream out; 173 | // #define cin in 174 | // #define cout out 175 | 176 | void test(){ 177 | ll n, m , p; 178 | cin>>n>>m>>p; 179 | n5::init(); 180 | // 1 to n are children 181 | // n+1 to n+m are toys so bijection [1 m] - [n+1 n+m] 182 | ll index = n+m+1; 183 | ll S = index++, T = index++; 184 | fori(n){ 185 | ll k; 186 | cin>>k; 187 | // child i is willing to play 188 | forj(k){ 189 | ll ty; 190 | cin>>ty; 191 | n5::add_edge(n+ty, i+1); 192 | } 193 | n5::add_edge(i+1, T); 194 | } 195 | vector seen(m+1, 0); 196 | fori(p){ 197 | ll l; 198 | cin>>l; 199 | vector all(l); 200 | forj(l) 201 | cin>>all[j], seen[all[j]] = 1; 202 | ll r; 203 | cin>>r; 204 | ll cur = index++; 205 | n5::add_edge(S, cur, r); 206 | forj(l) 207 | n5::add_edge(cur, n+all[j]); 208 | } 209 | for(ll i =1; i<=m; i++) 210 | if(!seen[i]) 211 | n5::add_edge(S, n+i); 212 | ll flow=0; 213 | while (n5::bfs(S, T)) flow += n5::dfs(S, T, INF); 214 | cout< 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp setprecision(12)< edges; 43 | void add_edge(int src, int dst, flow_type cap, cost_type cost) { 44 | adj[src].push_back({src, dst, cap, 0, cost, adj[dst].size()}); 45 | adj[dst].push_back({dst, src, 0, 0, -cost, adj[src].size()-1}); 46 | } 47 | int n; 48 | vector> adj; 49 | graph(int n) : n(n), adj(n) { } 50 | 51 | pair min_cost_max_flow(int s, int t) { 52 | flow_type flow = 0; 53 | cost_type cost = 0; 54 | 55 | for (int u = 0; u < n; ++u) // initialize 56 | for (auto &e: adj[u]) e.flow = 0; 57 | 58 | vector p(n, 0); 59 | 60 | auto rcost = [&](edge e) { return e.cost + p[e.src] - p[e.dst]; }; 61 | for (int iter = 0; ; ++iter) { 62 | vector prev(n, -1); prev[s] = 0; 63 | vector dist(n, INF); dist[s] = 0; 64 | if (iter == 0) { // use Bellman-Ford to remove negative cost edges 65 | vector count(n); count[s] = 1; 66 | queue que; 67 | for (que.push(s); !que.empty(); ) { 68 | int u = que.front(); que.pop(); 69 | count[u] = -count[u]; 70 | for (auto &e: adj[u]) { 71 | if (e.capacity > e.flow && dist[e.dst] > dist[e.src] + rcost(e)) { 72 | dist[e.dst] = dist[e.src] + rcost(e); 73 | prev[e.dst] = e.rev; 74 | if (count[e.dst] <= 0) { 75 | count[e.dst] = -count[e.dst] + 1; 76 | que.push(e.dst); 77 | } 78 | } 79 | } 80 | } 81 | for(int i=0;i node; 85 | priority_queue, greater> que; 86 | que.push({0, s}); 87 | while (!que.empty()) { 88 | node a = que.top(); que.pop(); 89 | if (a.ss == t) break; 90 | if (dist[a.ss] > a.ff) continue; 91 | for (auto e: adj[a.ss]) { 92 | if (e.capacity > e.flow && dist[e.dst] > a.ff + rcost(e)) { 93 | dist[e.dst] = dist[e.src] + rcost(e); 94 | prev[e.dst] = e.rev; 95 | que.push({dist[e.dst], e.dst}); 96 | } 97 | } 98 | } 99 | } 100 | if (prev[t] == -1) break; 101 | 102 | for (int u = 0; u < n; ++u) 103 | if (dist[u] < dist[t]) p[u] += dist[u] - dist[t]; 104 | 105 | function augment = [&](int u, flow_type cur) { 106 | if (u == s) return cur; 107 | edge &r = adj[u][prev[u]], &e = adj[r.dst][r.rev]; 108 | flow_type f = augment(e.src, min(e.capacity - e.flow, cur)); 109 | e.flow += f; r.flow -= f; 110 | return f; 111 | }; 112 | flow_type f = augment(t, INF); 113 | flow += f; 114 | cost += f * (p[t] - p[s]); 115 | } 116 | return {flow, cost}; 117 | } 118 | }; 119 | void deal(){ 120 | // usage 121 | // declare: graph n5 (number of vertices) 122 | // add edges: n5.add_edge(a,b, cost,price) 123 | // .ff to outpu max flow, .ss to output min cost : n5.min_cost_max_flow().ff 124 | } 125 | 126 | int main(){ 127 | cin.tie(0); 128 | ios_base::sync_with_stdio(0); 129 | deal(); 130 | } 131 | -------------------------------------------------------------------------------- /Maximum Flow from codearchive example new version (flow bounded by 1e9): -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)< adj[N]; int sz[N]; ll mc; 31 | vector mcf_edges; // for mcf on large graphs with negative costs 32 | void init(int n=N) { 33 | mc=0; fill(sz,sz+n,0); fill(adj,adj+n,vector()); } 34 | void add_edge(int a, int b, ll c=1, ll p=0) { mc = max(mc,c); // scaling 35 | // mcf_edges.push_back({b,a,0,c,p}); 36 | adj[a].push_back({b,sz[b]++,0,c,p}); 37 | adj[b].push_back({a,sz[a]++,0,0,-p}); } 38 | 39 | 40 | int L[N], cur[N], Q[N]; 41 | bool bfs(int s, int t, ll lim=1) { 42 | memset(cur, 0, sizeof cur); 43 | fori(N){ 44 | L[i] = INF; 45 | } 46 | int f,b; Q[f=b=0] = s; L[s] = 0; 47 | while(f<=b && L[t]==INF) { int u = Q[f++]; 48 | for(const Edge& e:adj[u]) if(L[e.v] == INF && e.c-e.f >= lim) { 49 | Q[++b] = e.v; L[e.v] = L[u]+1; } } return L[t] < INF; } 50 | ll dfs(int u, int t, ll f) { if (u == t || !f) return f; 51 | for (int i = cur[u]; i < sz[u]; cur[u] = ++i) { Edge& e = adj[u][i]; 52 | if(e.f::max(); 56 | ll res = 0; for(ll lim=(scaling?mc:1); lim; lim/=2) { 57 | while(bfs(s,t,lim))while(ll cf=dfs(s,t,inf))res += cf;} return res;} 58 | 59 | } 60 | 61 | void deal(){ 62 | ll n; 63 | while(cin>> n){ 64 | if(n == 0){ 65 | return; 66 | } 67 | 68 | 69 | n5::init(10*n); 70 | 71 | ll s = 5*n - 2 , t = 5*n - 3; 72 | for(ll i = 1; i<=n; i++){ 73 | n5::add_edge(s, i, 1); 74 | n5::add_edge(i+n, t, 1); 75 | 76 | ll xl, xr, yl, yr; 77 | cin>>xl>>yl>>xr>>yr; 78 | 79 | for(ll j = xl; j<=xr; j++){ 80 | n5::add_edge(j, 2*n + i, 1); 81 | } 82 | 83 | for(ll j = yl; j<=yr; j++){ 84 | n5::add_edge(3*n+i, n+j, 1); 85 | } 86 | 87 | n5::add_edge(2*n + i, 3*n + i, 1); 88 | } 89 | 90 | 91 | ll ans = n5::flow(s, t); 92 | 93 | if(ans != n){ 94 | cout<<"IMPOSSIBLE\n"; 95 | } 96 | else{ 97 | for(ll i = 1; i<=n; i++){ 98 | ll r = -1,c = -1; 99 | for(auto& el : n5::adj[2*n+i]){ 100 | if(el.v >= 1 && el.v <= n && el.f < 0){ 101 | assert(r == -1); 102 | r = el.v; 103 | } 104 | } 105 | for(auto& el : n5::adj[3*n+i]){ 106 | if(el.v >=n+1 && el.v <= 2*n && el.f > 0){ 107 | assert(c == -1); 108 | c = el.v - n; 109 | } 110 | } 111 | cout< 7 | using namespace std; 8 | 9 | #define mp(a,b) make_pair(a,b) 10 | #define ff first 11 | #define setp(a) setprecision(a)<>n>>q; 106 | init(1, 0, n-1); 107 | for(ll i = 1; i>ty; 110 | if(ty == 1){ 111 | // range add 112 | ll l, r, val; // 1-indexed l,r 113 | cin>>l>>r>>val; 114 | --l, --r; 115 | upd(1, 0, n-1, l, r, val); 116 | } 117 | else{ 118 | ll l, r; 119 | cin>>l>>r; 120 | ll bst = inf, id; 121 | --l, --r; 122 | que(1, 0, n-1, l, r, bst, id); 123 | cout<<"best index is "< 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)<-1){ 32 | if(eded & (1<rightsay++; 36 | if(Root->right == NULL) 37 | Root->right = new Node(); 38 | Root = Root->right; 39 | } 40 | else{ 41 | // cout<<"llllllll \n"; 42 | Root->leftsay++; 43 | if(Root->left == NULL) 44 | Root->left = new Node(); 45 | Root = Root->left; 46 | } 47 | --necenci; 48 | } 49 | } 50 | void checker(Node* root, int eded, int nece, int& cons){ 51 | while(nece>-1){ 52 | // cout<<"we are at level "<leftsay<<" the rightsay: "<rightsay<rightsay>0){ 55 | root->rightsay--; 56 | root = root->right; 57 | } 58 | else{ 59 | root->leftsay--; 60 | cons^=(1<left; 62 | } 63 | } 64 | else{ 65 | if(root->leftsay>0){ 66 | root->leftsay--; 67 | root = root->left; 68 | } 69 | else{ 70 | root->rightsay--; 71 | cons^=(1<right; 73 | } 74 | } 75 | --nece; 76 | } 77 | } 78 | 79 | void deal(){ 80 | // Node struct for typical Xor Maximization Problems 81 | } 82 | 83 | 84 | 85 | int main() 86 | { 87 | cin.tie(0); 88 | ios_base::sync_with_stdio(0); 89 | deal(); 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 | -------------------------------------------------------------------------------- /Ntt.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)< pt; 19 | typedef vector pol; 20 | typedef vector vi; 21 | typedef vector vvi; 22 | typedef vector vd; 23 | typedef vector vvd; 24 | typedef pair pii; 25 | typedef vector vpii; 26 | const ll INF = 0x3f3f3f3f; 27 | const ll inf = pow(10,18); 28 | ll modulo = 163577857; 29 | 30 | // taken from https://www.codechef.com/viewsolution/18397100 31 | 32 | long long powmod(long long a, int p) { 33 | long long r = 1; 34 | while (p) { 35 | if (p & 1) r = r * a % modulo; 36 | p >>= 1; a = a * a % modulo; 37 | } 38 | return r; 39 | } 40 | 41 | 42 | namespace NTT { 43 | const long long P = 163577857; 44 | const int K = 20; 45 | const int ROOT = 23; 46 | const int N = 1 << K; 47 | vector W, invW; 48 | vector rev; 49 | 50 | void init() { 51 | int n2 = N >> 1; 52 | W.resize(n2 + 1); W[0] = 1; 53 | W[1] = powmod(ROOT, (P - 1) / N); 54 | for (int i = 2; i <= n2; i++) 55 | W[i] = 1LL * W[i - 1] * W[1] % P; 56 | invW.resize(n2 + 1); invW[n2] = W[n2]; 57 | for (int i = n2 - 1; i >= 0; i--) 58 | invW[i] = 1LL * invW[i + 1] * W[1] % P; 59 | rev.resize(N); 60 | } 61 | 62 | void transform(vector &a, bool inv=false) { 63 | int k = 0; while ((1 << k) < a.size()) k++; 64 | int n = 1 << k; 65 | a.resize(n, 0); 66 | rev[0] = 0; 67 | for (int i = 1; i < n; i++) { 68 | rev[i] = rev[i >> 1] >> 1 | ((i & 1) << (k - 1)); 69 | if (i < rev[i]) swap(a[i], a[rev[i]]); 70 | } 71 | 72 | vector &twiddle = inv ? invW : W; 73 | for (int len = 2; len <= n; len <<= 1) { 74 | int half = len >> 1, diff = N / len; 75 | for (int i = 0; i < n; i += len) { 76 | int pw = 0; 77 | for (int j = i; j < i + half; j++) { 78 | int v = 1LL * a[j + half] * twiddle[pw] % P; 79 | a[j + half] = a[j] - v; 80 | if (a[j + half] < 0) a[j + half] += P; 81 | a[j] += v; 82 | if (a[j] >= P) a[j] -= P; 83 | pw += diff; 84 | } 85 | } 86 | } 87 | 88 | if (inv) { 89 | long long inv_n = powmod(n, P - 2); 90 | for (int &x : a) x = x * inv_n % P; 91 | } 92 | } 93 | 94 | void convolve(vector &a, vector &b) { 95 | int m = a.size() + b.size() - 1; 96 | a.resize(m, 0); transform(a); 97 | b.resize(m, 0); transform(b); 98 | for (int i = 0; i < a.size(); i++) 99 | a[i] = 1LL * a[i] * b[i] % P; 100 | transform(a, true); 101 | a.resize(m); 102 | } 103 | }; 104 | 105 | 106 | 107 | void deal(){ 108 | // change the root of unity accordingly if u want to change modulo and P 109 | 110 | 111 | 112 | } 113 | 114 | int main(){ 115 | cin.tie(0); 116 | ios_base::sync_with_stdio(0); 117 | deal(); 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 | -------------------------------------------------------------------------------- /O(1)_dsu_by_kartikkukreja.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)< 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)< isp; 29 | void pre() { 30 | isp.set(); 31 | isp[0] = isp[1] = 0; 32 | for (int i = 2; i*i < N; i++) { 33 | if (isp[i]) { 34 | for (int j = i*i; j < N; j += i) { 35 | isp[j] = 0; 36 | } 37 | } 38 | } 39 | } 40 | 41 | void deal(){ 42 | pre(); 43 | } 44 | 45 | int main(){ 46 | cin.tie(0); 47 | ios_base::sync_with_stdio(0); 48 | // test(); 49 | deal(); 50 | } 51 | 52 | 53 | 54 | /* 55 | 6 8 56 | 1 2 57 | 2 3 58 | 3 4 59 | 4 1 60 | 2 5 61 | 3 5 62 | 1 6 63 | 4 6 64 | */ 65 | -------------------------------------------------------------------------------- /O_1_range_max_query: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)< 31 | class range_maximum_query{ 32 | public: 33 | std::vector data; 34 | std::vector > block; 35 | std::vector sblock; 36 | std::vector lookup; 37 | int N; 38 | int lg; 39 | int max(int a,int b){ 40 | return data[b]>data[a]?b:a; 41 | } 42 | range_maximum_query(){} 43 | range_maximum_query(std::vector &t){ 44 | data=t; 45 | N=data.size(); 46 | lg=32-__builtin_clz(N); 47 | block.assign((N+lg-1)/lg,std::vector(lg,0)); 48 | for(int i=0;i st; 58 | for(int i=0;idata[st.back()]) 62 | st.pop_back(); 63 | sblock[j]=1<<(i*lg+lg-j-1); 64 | if(!st.empty()) 65 | sblock[j]|=sblock[st.back()]; 66 | st.push_back(j); 67 | } 68 | } 69 | lookup.assign(1<r1*lg+lg?l:r1*lg+lg; 91 | ans=max(ans,lookup[sblock[t]&(~(((1<<(l-(r1*lg+lg)))-1)<<(r1*lg+(lg<<1)-l)))]+r1*lg+lg); 92 | return ans; 93 | } 94 | }; 95 | void test(){ 96 | int n; 97 | cin>>n; 98 | vector all(n); 99 | fori(n) cin>>all[i]; 100 | range_maximum_query shit(all); 101 | int q; 102 | cin>>q; 103 | fori(q){ 104 | int l,r; 105 | cin>>l>>r; 106 | cout< 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(x) setprecision(x)< 19 | class range_minimum_query{ 20 | public: 21 | std::vector data; 22 | std::vector > block; 23 | std::vector sblock; 24 | std::vector lookup; 25 | int N; 26 | int lg; 27 | int min(int a,int b){ 28 | return data[b] &t){ 32 | data=t; 33 | N=data.size(); 34 | lg=32-__builtin_clz(N); 35 | block.assign((N+lg-1)/lg,std::vector(lg,0)); 36 | for(int i=0;i st; 46 | for(int i=0;ir1*lg+lg?l:r1*lg+lg; 79 | ans=min(ans,lookup[sblock[t]&(~(((1<<(l-(r1*lg+lg)))-1)<<(r1*lg+(lg<<1)-l)))]+r1*lg+lg); 80 | return ans; 81 | } 82 | }; 83 | void test(){ 84 | int n; 85 | cin>>n; 86 | vector all(n); 87 | fori(n) cin>>all[i]; 88 | range_minimum_query shit(all); 89 | int q; 90 | cin>>q; 91 | fori(q){ 92 | int l,r; 93 | cin>>l>>r; 94 | cout< 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)<>t; 28 | while(t--){ 29 | ll n , m; 30 | cin>>n>>m; 31 | vector > edg; 32 | vector > g(n); 33 | map > edges; 34 | 35 | 36 | 37 | 38 | fori(m){ 39 | ll a, b; 40 | cin>>a>>b; 41 | --a, --b; 42 | edg.pb(mp(a,b)); 43 | g[a].insert(b); 44 | g[b].insert(a); 45 | } 46 | 47 | 48 | vector when(n, inf); 49 | vector row; 50 | 51 | 52 | vector tot(n, 0); 53 | vector cur(n); // cur bucket of ith item 54 | vector how_many(n, 0); // how many from each bucket will advance 55 | vector yeni(n); // new bucket of ith item 56 | set > all; // all elements paired by (bucket_number, i) 57 | 58 | fori(n){ 59 | cur[i] = n-1; 60 | tot[cur[i]]++; 61 | all.insert(mp(cur[i], i)); 62 | } 63 | 64 | while(all.size()){ 65 | auto hd = (*all.begin()).ss; 66 | when[hd] = n-row.size(); 67 | row.pb(hd); 68 | all.erase(all.begin()); 69 | 70 | for(auto hr : g[hd]){ 71 | if(when[hr]!=inf){ 72 | continue; 73 | } 74 | how_many[cur[hr]]++; 75 | } 76 | for(auto hr : g[hd]){ 77 | if(when[hr]!=inf){ 78 | continue; 79 | } 80 | yeni[hr] = cur[hr] - (tot[cur[hr]] - how_many[cur[hr]]); 81 | } 82 | for(auto hr : g[hd]){ 83 | if(when[hr]!=inf){ 84 | continue; 85 | } 86 | all.erase(mp(cur[hr], hr)); 87 | how_many[cur[hr]] = 0; 88 | --tot[cur[hr]]; 89 | 90 | cur[hr] = yeni[hr]; 91 | ++tot[cur[hr]]; 92 | all.insert(mp(cur[hr], hr)); 93 | } 94 | } 95 | 96 | 97 | 98 | 99 | reverse(row.begin(), row.end()); 100 | /* cout<<"\n\nthe row \n\n"; 101 | fori(n){ 102 | cout< > cur; 109 | for(auto& el : g[hd]){ 110 | if(when[el] > when[hd]){ 111 | edges[hd][el] = 1; 112 | cur.pb(mp(when[el], el)); 113 | } 114 | } 115 | sort(cur.begin(), cur.end()); 116 | for(ll i = 1; i 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)< pt; 19 | typedef vector pol; 20 | typedef vector vi; 21 | typedef vector vvi; 22 | typedef vector vd; 23 | typedef vector vvd; 24 | typedef pair pii; 25 | typedef vector vpii; 26 | const ll INF = 0x3f3f3f3f; 27 | const ll inf = pow(10,18); 28 | ll modulo = pow(10,9) + 7; 29 | #define MAX (int)(2502 + 10) 30 | 31 | 32 | // Persistent Segment Tree with point updates and range queries 33 | struct per_seg{ 34 | struct Node{ 35 | int data = 0; // TODO add additional types if needed and update the constructors 36 | int l, r; // bounds of this node 37 | int left, right; // index of left right child 38 | Node(){} 39 | Node(int data, int l, int r, int left, int right) : data(data), l(l), r(r), left(left), right(right) {} 40 | Node(Node& other) : l(other.l), r(other.r), data(other.data){} 41 | void operator*=(Node& other){ // merge node value to query value 42 | data += other.data; 43 | } 44 | }; 45 | int indices[MAX]; 46 | Node roots[MAX]; 47 | int count_ = 0; 48 | void pull(int idx){ // TODO update the pull if needed 49 | if(roots[idx].l == roots[idx].r){ 50 | return; 51 | } 52 | ll lef = roots[idx].left, rig = roots[idx].right; 53 | roots[idx].data = roots[lef].data + roots[rig].data; 54 | } 55 | int create_root(int l, int r){ // return the index of the newly created root 56 | int cur = count_; 57 | ++count_; 58 | roots[cur] = Node(0, l, r,0,0); // TODO if u need to initialize data put it here 59 | if(l!=r) 60 | roots[cur].left = create_root(l,(l+r)/2), 61 | roots[cur].right = create_root((l+r)/2+1,r); 62 | return cur; 63 | } 64 | int create_update(int index, int base, int val){ // index : the point of update, base : previous root 65 | if(indexroots[base].r) 66 | return base; 67 | int cur = count_; 68 | ++count_; 69 | roots[cur] = Node(roots[base]); 70 | // cout<<"We wanna update "< query.r || roots[hd].r < query.l) return ; 83 | if(roots[hd].l >= query.l && roots[hd].r <=query.r){ query*=roots[hd]; return; } 84 | que(roots[hd].left, query); 85 | que(roots[hd].right, query); 86 | } 87 | }; 88 | 89 | void test(){ 90 | // Modify Node struct accordingly to use 91 | // usage: 92 | // per_seg Seg; 93 | // Seg.indices[0] = Seg.create_root(1,n) 1-based indexing 94 | // Seg.indices[i] = Seg.create_update( index of update, Seg.indices[i-1] ) 95 | // per_seg::Node query( data initialized to 0, left end , right end , -1 , -1) 96 | // Seg.que( Seg.indices[ "the query root" ] , query ); 97 | // cout< 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)<r; } 29 | inline void setc(pn c, bool right) { (right?r:l) = c; c->p = this; } 30 | } dat[N]; 31 | pn nil = [](){ pn x=dat; x->l=x->r=x->p=x; /*x->acc=INF;*/ return x; }(); //RMQ 32 | node::node(ll k, ll v): 33 | l(nil), r(nil), p(nil), key(k), val(v), cnt(1),rev(0) {} 34 | int nodeid = 0; template pn new_node(T&& ...args) { 35 | return &(dat[++nodeid] = node(forward(args)...)); } 36 | 37 | namespace SplayTree { 38 | //%%== Code for upwards propagating property (eg. count, min) 39 | inline void pull(pn x) { if (x != nil) { x->cnt = 1 + x->l->cnt + x->r->cnt; 40 | /*x->acc = min(x->l->acc, min(x->val, x->r->acc));*/ } } //RMQ 41 | //%%== Code for downward lazy propagating property (eg. reverse subtree, add) 42 | void rev(pn x) { if (x != nil) { x->rev ^= 1; swap(x->l, x->r); } } 43 | inline void modifyNode(pn x, ll v) { if (x != nil) { 44 | /*x->val += v; x->acc += v; x->lazy += v;*/ } } //RMQ 45 | inline void push(pn x) {if (x->rev) { rev(x->l); rev(x->r); x->rev=0; } //REV 46 | /*modifyNode(x->l, x->lazy); modifyNode(x->r, x->lazy); x->lazy=0;*/ } //RMQ 47 | //%%== call pushTo() before using node if an ancestor may have un-pushed lazy 48 | void pushTo(pn c) { if (c->p != nil) pushTo(c->p); push(c); } 49 | //%%== Core splay tree code (rotate, splay, delete) 50 | inline void rot(pn x) { pn p = x->p; push(p); push(x); bool d = x->dir(); 51 | p->setc(d ? x->l : x->r, d); p->p->setc(x, p->dir()); x->setc(p, !d); 52 | pull(p); pull(x); } 53 | // splays node x until it is a child of node to 54 | pn splay(pn x, pn to=nil) { if (x!=nil) { while (x->p!=to) { if (x->p->p!=to) 55 | rot(x->dir() == x->p->dir() ? x->p : x); rot(x); } } return x; } 56 | pn del(pn y) { pn x=splay(y)->r; if (x==nil) x=y->l; else { 57 | while (push(x), x->l != nil) x = x->l; splay(x,y)->setc(y->l,0); } 58 | x->p=nil; pull(x); return x; } 59 | //%%== BST methods (lb, find, insert) (conflicts with other features, see above) 60 | pn lb(pn c, ll k) { if (c == nil) return c; push(c); // c MUST BE THE ROOT 61 | if (c->key>=k) { pn l=lb(c->l,k); return l==nil?c:l; } return lb(c->r,k); } 62 | pn lowerBound(pn ref, ll k) { return lb(splay(ref), k); } 63 | pn find(pn c, ll k) { c=lb(splay(c),k); return c->key!=k?nil:splay(c); } 64 | pn insert(pn c, pn x) { if (splay(c) == nil) return x; 65 | ll k=x->key; pn p; while(c!=nil) { push(c); p=c; c=(p->key>k?p->l:p->r); } 66 | p->setc(x,p->key<=k); return splay(x); } 67 | //%%== Utility code (rank, nth order statistic) (requires count maintained) 68 | int idx(pn x) { return splay(x)->l->cnt; } // rank; for size, remove '->l' 69 | pn nth(pn c, int n) { splay(c); while (c!=nil) { push(c); int l=c->l->cnt; 70 | if (n==l) break; if (n < l) c = c->l; else n = n-l-1, c = c->r; } return c; } 71 | //%%== Iterator-based access, returns nil when iterator exhausted 72 | pn first(pn c) { if (splay(c)!=nil) while (push(c),c->l!=nil)c=c->l; return c; } 73 | pn last(pn c) { if (splay(c)!=nil) while (push(c),c->r!=nil)c=c->r; return c; } 74 | pn prv(pn x) { if (splay(x)->l == nil) return nil; x = x->l; 75 | while (push(x), x->r != nil) x = x->r; return x; } 76 | pn nxt(pn x) { if (splay(x)->r == nil) return nil; x = x->r; 77 | while (push(x), x->l != nil) x = x->l; return x; } 78 | //%%== Iterator-based insert, does NOT work with BST code unless key sorted 79 | pn insertBefore(pn c, pn at, pn x) { // to insert "last", use at=nil 80 | if (at==nil) { if (splay(c)!=nil) last(c)->setc(x,1); } else { pn p=prv(at); 81 | if (p==nil) at->setc(x,0); else p->setc(x,1); } return splay(x); } 82 | //%%== Range query and update operations by iterator, range is EXCLUSIVE! 83 | pn range(pn c, pn l, pn r) { if (l == nil) { if (r == nil) return splay(c); 84 | return splay(r)->l; } if (l == r) return nil; splay(l); 85 | if (r == nil) return l->r; return splay(r,l)->l;} 86 | ll pQuery(pn x) { return splay(x)->val; } 87 | void pUpdate(pn x, ll v) { splay(x)->val += v; pull(x); } //RMQ 88 | void rUpdate(pn c, pn l, pn r, ll v) { 89 | pn u = range(c, l, r); modifyNode(u, v); splay(u); } 90 | //%%== Rope operations: split and merge, nil = right end 91 | pn splitBefore(pn x) { if (splay(x)==nil) return nil; push(x); if (x->l!=nil) 92 | push(x->l); pn ret=x->l; x->l=x->l->p=nil; pull(ret); pull(x);return ret; } 93 | pn append(pn l, pn r) { if (splay(l) == nil) return r; 94 | if (splay(r) == nil) return l; pn x = splay(last(l)); push(x); 95 | push(r); x->setc(r,1); pull(x->r); pull(x); return x; } } 96 | 97 | using namespace SplayTree; 98 | 99 | void test(){ 100 | 101 | pn root = new node(-1, 0); 102 | 103 | /* for(ll i = 1; i<=10;i++){ 104 | pn z = new node(-1, i*100); 105 | insertBefore(root, nth(root, i), z); // x is now rank 15 106 | 107 | } 108 | rev(range(root, nth(root, 4), nth(root, 11))); 109 | 110 | for(ll i = 1; i<=10; i++){ 111 | pn z = nth(root, i); 112 | cout<val<>n>>l>>r; 119 | fori(n){ 120 | char f; 121 | cin>>f; 122 | pn z = new node(-1, f-'a'); 123 | insertBefore(root, nth(root, i+1), z); 124 | } 125 | 126 | ll m; 127 | cin>>m; 128 | fori(m){ 129 | char f; 130 | cin>>f; 131 | // cout<<"query "<>f; 137 | pn z; 138 | if(f == 'L'){ 139 | z = nth(root, l); 140 | } 141 | else{ 142 | z = nth(root, r); 143 | } 144 | cout<<(char)('a' + (z->val)); 145 | } 146 | else{ 147 | cin>>f; 148 | char f1; 149 | cin>>f1; 150 | ll inc = 1; 151 | if(f1 == 'L'){ 152 | inc = -1; 153 | } 154 | if(f == 'L'){ 155 | l+=inc; 156 | } 157 | else{ 158 | r+=inc; 159 | } 160 | } 161 | } 162 | } 163 | 164 | 165 | int main(){ 166 | cin.tie(0); 167 | ios_base::sync_with_stdio(0); 168 | test(); 169 | } 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | -------------------------------------------------------------------------------- /binary_exponentiation_factorials.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)<-1; i--){ 32 | has*=has; 33 | has%=modulo; 34 | if(((ll)1<-1; i--){ 56 | invfkt[i] = invfkt[i+1] * (i+1) % modulo; 57 | } 58 | } 59 | 60 | ll choose(ll n, ll k){ 61 | return fkt[n] * invfkt[k] % modulo * invfkt[n-k] % modulo; 62 | } 63 | 64 | void deal(){ 65 | 66 | } 67 | 68 | int main(){ 69 | cin.tie(0); 70 | ios_base::sync_with_stdio(0); 71 | deal(); 72 | 73 | return 0; 74 | } 75 | 76 | 77 | -------------------------------------------------------------------------------- /centroid_decomposition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp setprecision(12)< g[MAX]; // graph 28 | vector > roots[MAX]; // all the parents (in centroid tree) of node i 29 | ll sz[MAX]; // sizes of subtrees 30 | void dfs(ll hd, ll pr){ // dfs to update sizes 31 | sz[hd] = 1; 32 | for(auto hr : g[hd]){ 33 | if(hr==pr || dlt[hr]) 34 | continue; 35 | dfs(hr,hd); 36 | sz[hd]+=sz[hr]; 37 | } 38 | } 39 | ll centre(ll hd,ll pr, ll siz){ // find the centre of the given tree 40 | for(auto hr : g[hd]){ 41 | if(hr == pr || dlt[hr]) 42 | continue; 43 | if(sz[hr] > siz/2 ) 44 | return centre(hr,hd,siz); 45 | } 46 | return hd; 47 | } 48 | void push_root(ll hd, ll pr, ll root, ll dt){ // push the chosen root to all the descendants in centroid tree 49 | roots[hd].push_back(mp(root,dt)); 50 | for(auto hr : g[hd]){ 51 | if(hr==pr || dlt[hr]) 52 | continue; 53 | push_root(hr,hd,root,dt+1); 54 | } 55 | } 56 | void create(ll st){ // create the centroid tree 57 | dfs(st,-1); 58 | // cout<<"we started "< 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)< > g[MAX]; 25 | 26 | vector dijkstra(ll st, ll n){ 27 | vector dt(n+10, inf); 28 | dt[st] = 0; 29 | priority_queue< pair , vector > , greater > > pq; 30 | pq.push(mp(0, st)); 31 | 32 | while(pq.size()){ 33 | auto tp = pq.top(); 34 | pq.pop(); 35 | auto hardan = tp.ss; 36 | auto val = tp.ff; 37 | 38 | if(val != dt[hardan]){ 39 | continue; 40 | } 41 | 42 | cout<<"minimum distance "<>n>>m; 60 | 61 | fori(m){ 62 | ll a, b, d; 63 | cin>>a>>b>>d; 64 | g[a].pb(mp(b,d)); 65 | g[b].pb(mp(a,d)); 66 | } 67 | 68 | dijkstra(1, n); 69 | 70 | } 71 | 72 | int main(){ 73 | cin.tie(0); 74 | ios_base::sync_with_stdio(0); 75 | // test(); 76 | deal(); 77 | } 78 | 79 | 80 | 81 | /* 82 | 6 8 83 | 1 2 84 | 2 3 85 | 3 4 86 | 4 1 87 | 2 5 88 | 3 5 89 | 1 6 90 | 4 6 91 | */ 92 | -------------------------------------------------------------------------------- /hash.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)< mods({(ll)pow(10,9) + 7, (ll)pow(10,9) + 9}); 24 | vector inv[MAX]; // inverse of alpha^(i) 25 | 26 | ll pow_mod(ll a, ll b, ll modulo){ 27 | a = a%modulo; 28 | if(!a){ 29 | return 0; 30 | } 31 | ll has = 1; 32 | for(ll i = 62; i>-1; i--){ 33 | has*=has; 34 | has%=modulo; 35 | if(((ll)1< >& all ,string& a){ 59 | ll n = a.length(); 60 | fori((ll)mods.size()){ 61 | ll curmod = mods[i]; 62 | vector cur(n+1, 0); 63 | ll pv = alpha; 64 | for(ll j = 1; j<=n; j++){ 65 | cur[j] = cur[j-1] + (a[j-1]-'A')*pv; // TODO change the hash function according to alphabet 66 | cur[j] %= curmod; 67 | pv *= alpha; 68 | pv %= curmod; 69 | } 70 | all.pb(cur); 71 | } 72 | } 73 | 74 | vector hash_interval(ll a, ll b, vector >& all){ 75 | vector ans; 76 | fori((ll)mods.size()){ 77 | ll curmod = mods[i]; 78 | ll cur = (all[i][b] - all[i][a-1] + curmod) % curmod * inv[a][i] % curmod; 79 | ans.pb(cur); 80 | } 81 | return ans; 82 | } 83 | 84 | void disp(vector& all){ 85 | fori((ll)all.size()){ 86 | cout<>a>>b; 95 | 96 | vector > hash1, hash2; 97 | getHashes(hash1, a); 98 | getHashes(hash2, b); 99 | 100 | 101 | 102 | 103 | } 104 | 105 | int main(){ 106 | cin.tie(0); 107 | ios_base::sync_with_stdio(0); 108 | // test(); ECNA 2016 I 109 | deal(); 110 | } 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | -------------------------------------------------------------------------------- /matrix_binary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)< pt; 23 | typedef vector pol; 24 | typedef vector vi; 25 | typedef vector vvi; 26 | typedef vector vd; 27 | typedef vector vvd; 28 | typedef pair pii; 29 | typedef vector vpii; 30 | const ll INF = 0x3f3f3f3f; 31 | const ll inf = 2*pow(10,9); 32 | ll modulo = pow(10,9) + 7; 33 | 34 | // matrix structure for classic Matrix exponentiation type of problems 35 | 36 | 37 | struct Matrix{ // !!! everything is an integer , and we take modulo while exponentiating (is available up to 2^30) / multiplying 38 | // n should be nonzero 39 | bitset* rows; 40 | bitset* cols; 41 | int n, m; 42 | Matrix(){} 43 | Matrix(int n, int m, bool id){ // if id is true (it must be a square matrix!!) identity matrix is created 44 | this->n = n , this->m = m; 45 | rows = new bitset[n]; 46 | cols = new bitset[m]; 47 | if(id) 48 | fori(n) 49 | rows[i].set(i), cols[i].set(i); 50 | } 51 | 52 | void set(ll i, ll j){ 53 | rows[i].set(j); 54 | cols[j].set(i); 55 | } 56 | 57 | void Multiply(Matrix& A){ 58 | int n1 = A.n , m1 = A.m; 59 | assert(m==n1); 60 | 61 | bitset* r1 = new bitset[n]; 62 | bitset* c1 = new bitset[m1]; 63 | 64 | for(int i = 0; i& operator [] (ll i){ 76 | return rows[i]; 77 | } 78 | }; 79 | 80 | void display(Matrix& A){ 81 | cout<<"number of rows "<-1; i--){ 93 | start.Multiply(start); 94 | if(((ll)1< 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)< pt; 19 | typedef vector pol; 20 | typedef vector vi; 21 | typedef vector vvi; 22 | typedef vector vd; 23 | typedef vector vvd; 24 | typedef pair pii; 25 | typedef vector vpii; 26 | const ll INF = 0x3f3f3f3f; 27 | const ll inf = pow(10,18); 28 | ll modulo = pow(10,9) + 7; 29 | #define MAX (int)(pow(10,6) + 10) 30 | 31 | 32 | namespace dsu{ 33 | int aid[MAX]; 34 | vector dsu[MAX]; 35 | void ini(int n){ 36 | fori(n) 37 | aid[i] = i, dsu[i].clear(), dsu[i].push_back(i); 38 | } 39 | bool join(int a,int b){ // 1 - if joined, 0 - if they were already joined 40 | int aid1 = aid[a], aid2 = aid[b]; 41 | if(dsu[aid2].size() > dsu[aid1].size()) 42 | swap(aid1,aid2); 43 | if(aid1==aid2) 44 | return 0; 45 | for(auto hd : dsu[aid2]){ 46 | aid[hd] = aid1; 47 | dsu[aid1].push_back(hd); 48 | } 49 | dsu[aid2].clear(); 50 | return 1; 51 | } 52 | }; 53 | 54 | void deal(){ 55 | 56 | } 57 | 58 | int main() { 59 | cin.tie(0); 60 | ios_base::sync_with_stdio(0); 61 | deal(); 62 | } 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /sketchyFastFFt.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define fi first 3 | #define se second 4 | #define pb push_back 5 | #define REP(i,a,b) for(ll i=a;i ; 14 | 15 | // by PR_0202 16 | 17 | const int N = 2500005; 18 | const int MOD = 998244353; 19 | const int root = 961777435; 20 | const int root_1 = 246144584; 21 | const int root_pw = 1ll << 23; 22 | /***************************code begins here*****************************/ 23 | int power(int x, unsigned int y) { 24 | int res = 1; 25 | x = x % MOD; 26 | if (x == 0) return 0; 27 | 28 | while (y > 0) 29 | { 30 | if (y & 1) 31 | res = (1LL*res*x) % MOD; 32 | y = y>>1; 33 | x = (1LL*x*x) % MOD; 34 | } 35 | return res; 36 | } 37 | 38 | void fft(vector & a, bool invert) { 39 | int n = a.size(); 40 | 41 | for (int i = 1, j = 0; i < n; i++) { 42 | int bit = n >> 1; 43 | for (; j & bit; bit >>= 1) 44 | j ^= bit; 45 | j ^= bit; 46 | 47 | if (i < j) 48 | swap(a[i], a[j]); 49 | } 50 | 51 | for (int len = 2; len <= n; len <<= 1) { 52 | int wlen = invert ? root_1 : root; 53 | for (int i = len; i < root_pw; i <<= 1) 54 | wlen = (int)(1LL * wlen * wlen % MOD); 55 | 56 | for (int i = 0; i < n; i += len) { 57 | int w = 1; 58 | for (int j = 0; j < len / 2; j++) { 59 | int u = a[i+j], v = (int)(1LL * a[i+j+len/2] * w % MOD); 60 | a[i+j] = u + v < MOD ? u + v : u + v - MOD; 61 | a[i+j+len/2] = u - v >= 0 ? u - v : u - v + MOD; 62 | w = (int)(1LL * w * wlen % MOD); 63 | } 64 | } 65 | } 66 | 67 | if (invert) { 68 | int n_1 = power(n,MOD-2); 69 | for (int & x : a) 70 | x = (int)(1LL * x * n_1 % MOD); 71 | } 72 | } 73 | void PolyMult(vi &a,vi &v) { 74 | ll n=1; 75 | while(n<((ll)a.size())+((ll)a.size())) 76 | { 77 | n<<=1; 78 | } 79 | vi fa(a.begin(),a.end()); 80 | fa.resize(n,0); 81 | 82 | fft(fa,false); 83 | v.resize(n,0); 84 | REP(i,0,n) 85 | { 86 | v[i]=((1LL*fa[i]*fa[i])%MOD); 87 | } 88 | fft(v,true); 89 | return; 90 | } 91 | 92 | void deal(){ 93 | } 94 | 95 | int main(){ 96 | cin.tie(0); 97 | ios_base::sync_with_stdio(0); 98 | deal(); 99 | } 100 | 101 | -------------------------------------------------------------------------------- /treap_usage_example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define mp(a,b) make_pair(a,b) 4 | #define ff first 5 | #define setp(a) setprecision(a)<count; } // regular&implicit 50 | //change Lazy and Update for lazy-propagation (only use it in implicit) 51 | 52 | void lazy(node *&x){ 53 | if(x == NULL) return; 54 | x->lazy = x->v; 55 | if(x->l){ 56 | x->lazy = min(x->lazy, x->l->lazy); 57 | } 58 | 59 | if(x->r){ 60 | x->lazy = min(x->lazy, x->r->lazy); 61 | } 62 | 63 | } 64 | 65 | void update(node *&curr){ if (!curr) return; // regular & implicit 66 | curr->count = count(curr->l) + count(curr->r) + 1; lazy(curr); } 67 | 68 | void merge(node *&curr, node *l, node *r){ // regular & implicit 69 | lazy(l); lazy(r); if(l == NULL || r == NULL) curr = (l == NULL)?r:l; 70 | else if( l->p < r->p ){ merge(r->l, l, r->l); curr = r; 71 | }else{ merge(l->r, l->r, r); curr = l; } update(curr); } 72 | 73 | 74 | node* kth_element(node *&treap, int k){ // 1-indexed. Kth element from L to R 75 | lazy(treap); if(k - count(treap->l) == 1) return treap; 76 | if(k <= count(treap->l)) return kth_element(treap->l, k); 77 | return kth_element(treap->r, k - count(treap->l) - 1); } 78 | 79 | // FUNCTIONS ONLY FOR REGULAR TREAP ------------------------------------ 80 | void split(node *curr, node *&l, node *&r, int key){ // regular 81 | if(curr == NULL){ l = r = NULL; return; } 82 | if(curr->v <= key){ split(curr->r, curr->r, r, key); l=curr; 83 | }else{ split(curr->l, l, curr->l, key); r=curr; } update(curr); } 84 | 85 | void insert(node *&treap, node *x){ node *l=0,*r=0,*aux=0; // regular 86 | split(treap, l, r, x->v); merge(aux, l, x); merge(treap, aux, r); aux = NULL;} 87 | 88 | void erase(node *&treap, int v){ node *l=0,*r=0,*l2=0,*r2=0;// regular 89 | split(treap, l, r, v); split(l, l2, r2, v-1); if(count(r2) > 1){ 90 | merge(r2, r2->l, r2->r); merge(l2, l2, r2); } merge(treap, l2, r); } 91 | 92 | bool isInTreap(node *treap, int x){ // regular | checks if x is in treap 93 | if(treap == NULL) return false; if(treap->v == x) return true; 94 | if(treap->v < x) return isInTreap(treap->r,x); 95 | return isInTreap(treap->l,x); } 96 | 97 | 98 | // FUNCTIONS ONLY FOR IMPLICIT TREAP ----------------------------------- 99 | void splitImplicit(node *curr, node *&l, node *&r, int key){ // implicit 100 | lazy(curr); if(curr == NULL){ l = r = NULL; return; } 101 | if(count(curr->l) + 1 <= key){ 102 | splitImplicit(curr->r,curr->r,r,key-count(curr->l)-1); l=curr; 103 | }else{splitImplicit(curr->l,l,curr->l,key); r=curr;}update(curr);} 104 | 105 | // implicit | inserts node at pos (1-indexed) 106 | void insertImplicit(node *&treap, node *x, int pos){ 107 | lazy(treap); node *l=0,*r=0,*aux=0; splitImplicit(treap, l, r, pos-1); 108 | merge(aux, l, x); merge(treap, aux, r); aux = 0; } 109 | 110 | void eraseImplicit(node *&treap, int pos){ // deletes node at pos 111 | lazy(treap); node *l=0,*r=0,*l2=0,*r2=0; 112 | splitImplicit(treap, l, r, pos); splitImplicit(l, l2, r2, pos-1); 113 | merge(treap, l2, r); l2 = r2 = l = r = NULL; } 114 | 115 | 116 | void test(){ 117 | node* root = 0; 118 | 119 | for(ll i = 1; i<=5; i++){ 120 | ll v = rand() % 10; 121 | cout<<"value "<lazy<>n; 145 | 146 | 147 | node* root = 0; 148 | fori(n){ 149 | char f; 150 | cin>>f; 151 | if(f == '+'){ 152 | ll ps, v; 153 | cin>>ps>>v; 154 | ++ps; 155 | node* x = new node( v, rand()); 156 | insertImplicit(root, x, ps); 157 | } 158 | else{ 159 | ll l, r; 160 | cin>>l>>r; 161 | node *t1 = 0, *t2 = 0, *t3 = 0; 162 | splitImplicit(root, t1, t2, l-1); 163 | splitImplicit(t2, t2, t3, r-l+1); 164 | cout<lazy<<'\n'; 165 | merge(root, t1, t2); 166 | merge(root, root, t3); 167 | } 168 | } 169 | 170 | } 171 | 172 | 173 | 174 | int main(){ 175 | srand(time(NULL)); 176 | cin.tie(0); 177 | ios_base::sync_with_stdio(0); 178 | deal(); 179 | } 180 | 181 | 182 | 183 | 184 | 185 | --------------------------------------------------------------------------------