├── docs ├── .nojekyll ├── CNAME ├── favicon.ico ├── asset-manifest.json ├── manifest.json ├── index.html └── service-worker.js ├── README.md ├── Data Structure ├── RadixSort.cpp ├── Sparse Table.cpp ├── RMQ Point(Iterative).cpp ├── Running Median.cpp ├── Mo's Algorithm.cpp ├── Matrix Expo.cpp └── BIT Multi-type.cpp ├── String ├── KMP.cpp ├── Palindromic Substring.cpp ├── Suffix Array.cpp ├── Aho Corasick.cpp ├── Suffix Automata.cpp └── Suffix Array DC3.cpp ├── Math ├── Chinese Remainder Theorem.cpp ├── Gaussian Elimination.cpp └── Linear Diophantine Equation.cpp ├── Graph ├── Least Common Ancestor(LCA).cpp ├── Articulation Point and Bridge.cpp ├── Articulation Point.cpp └── Centroid Decomposition.cpp ├── Geometry ├── Closest Pair.cpp ├── Convex Diameter.cpp ├── Kattis Dogs.cpp ├── Rectangle Union.cpp ├── Minimum Bounding Rectangle.cpp └── Geometry.cpp ├── Start Template.cpp ├── Helper ├── FastIO.cpp └── VectorCompressor.cpp └── Flow ├── MCMF.cpp └── Dinic.cpp /docs/.nojekyll: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/CNAME: -------------------------------------------------------------------------------- 1 | codelibrary.backslash.one -------------------------------------------------------------------------------- /docs/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oneshadab/cp-codelibrary/HEAD/docs/favicon.ico -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CP Code Library 2 | A collection of algorithms and data structures used in competitive programming 3 | -------------------------------------------------------------------------------- /docs/asset-manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "main.css": "static/css/main.1f8ac975.css", 3 | "main.css.map": "static/css/main.1f8ac975.css.map", 4 | "main.js": "static/js/main.b832e41e.js", 5 | "main.js.map": "static/js/main.b832e41e.js.map" 6 | } -------------------------------------------------------------------------------- /docs/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | Code Library
-------------------------------------------------------------------------------- /Data Structure/RadixSort.cpp: -------------------------------------------------------------------------------- 1 | 2 | namespace RadixSort { 3 | const int NMAX = 100000; 4 | const int Width = 32, CHUNK = 8; 5 | const int bitmask = (1 << CHUNK) - 1; 6 | 7 | unsigned int bucket[1 << CHUNK], temp[NMAX + 10]; 8 | 9 | void sort(unsigned int *ar, int n) { 10 | int i, j, x; 11 | for (i = 0; i < Width; i += CHUNK) { 12 | clr(bucket); 13 | for (j = 0; j < n; j++) bucket[(ar[j] >> i) & bitmask]++; 14 | for (j = 1; j <= bitmask; j++) { 15 | bucket[j] += bucket[j - 1]; 16 | } 17 | for (j=n-1; j>=0; j--)temp[--bucket[(ar[j]>>i) & bitmask]] = ar[j]; 18 | for (j = 0; j < n; j++) ar[j] = temp[j]; 19 | } 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /String/KMP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector kmp(string &str) { 6 | vector fail; 7 | fail.push_back(0); 8 | int pos = 0; 9 | for (int i = 1; i < str.length(); i++) { 10 | fail.push_back(pos); 11 | while (pos > 0 && str[i] != str[pos]) 12 | pos = fail[pos]; 13 | if (str[i] == str[pos]) 14 | pos += 1; 15 | } 16 | fail.push_back(pos); 17 | return fail; 18 | } 19 | 20 | int main() { 21 | for (;;) { 22 | string str; 23 | cin >> str; 24 | if (str == ".") break; 25 | int len = str.length(); 26 | vector fail = kmp(str); 27 | int cnt = fail.back(); 28 | int left = len - cnt; 29 | int ans = 1; 30 | if (len % left == 0) ans = len / left; 31 | cout << ans << "\n"; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Data Structure/Sparse Table.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | //Sparse Table 6 | struct Sparse{ 7 | static const int NMAX = 200000; 8 | static const int WIDTH = 20; 9 | int A[NMAX+10], idx[NMAX+10][WIDTH+2]; 10 | 11 | void build(int tmp[]){ 12 | for(int i=0; i y) swap(x, y); 21 | int h = bitWidth(y - x); 22 | return argmin(idx[x][h], idx[y - (1< 2 | 3 | using namespace std; 4 | 5 | /* 6 | Chinese Remainder Theorem Finds X % M 7 | Where, 8 | M = mod[0] * mod[1] * mod[2] ... 9 | And, 10 | r[0] = X % mod[0] 11 | r[1] = X % mod[1] 12 | r[2] = X % mod[2] 13 | .... 14 | Where mod[0], mod[1], mod[2] ... are co-prime 15 | 16 | NOTE: 17 | if there is a possibility of k being very big, then 18 | prime factorize m[i], find modular inverse of 'tmp' 19 | of each of the factors 'k' equals to the multiplication 20 | (modular mods[i]) of modular inverses 21 | */ 22 | 23 | 24 | typedef long long ll; 25 | ll CRT (vector &r, vector &mods) { 26 | ll M = 1, ret = 0, n = mods.size(); 27 | for (int i = 0; i < n; i++) M *= mods[i]; 28 | for (int i = 0; i < n; i++) { 29 | ll m = M / mods[i], tmp = m % mods[i], k = 0; 30 | while((k * tmp) % mods[i] != 1) k++; 31 | ret += ((m * k) % M *r[i]) % M; 32 | ret %= M; 33 | } 34 | return ret; 35 | } 36 | -------------------------------------------------------------------------------- /Data Structure/RMQ Point(Iterative).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct RMQ { 6 | vector t; 7 | int n; 8 | 9 | void build(int *A, int n) { 10 | this->n = n; 11 | t.assign(2 * n, 0); 12 | for (int p = n - 1; p >= 0; p--) t[p + n] = A[p]; 13 | for (int p = n - 1; p >= 0; p--) t[p] = min(t[p << 1], t[p << 1 | 1]); 14 | } 15 | 16 | void update(int p, int val) { 17 | for (t[p += n] = val; p >>= 1;) { 18 | t[p] = min(t[p << 1], t[p << 1 | 1]); 19 | } 20 | } 21 | 22 | int query(int L, int R) { 23 | int res = t[n + L]; 24 | //n+1 for exclusivity [L, R) 25 | for (L += n, R += n + 1; L < R; L >>= 1, R >>= 1) { 26 | if (L & 1) res = min(res, t[L++]); 27 | if (R & 1) res = min(t[--R], res); 28 | } 29 | return res; 30 | } 31 | }; 32 | 33 | int main() { 34 | int A[] = {10, 3, 4, 5, 2}; 35 | RMQ t; 36 | t.build(A, 5); 37 | cout << t.query(0, 4); 38 | } 39 | 40 | 41 | -------------------------------------------------------------------------------- /Math/Gaussian Elimination.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int N, M; 6 | int GS[110][110]; 7 | bool gauss(int GS[110][110], int N, int M){ 8 | for(int k = 1; k <= N; k++){ 9 | int mx = k; 10 | for(int i = k + 1; i <= N; i++){ 11 | if(GS[i][k] > GS[mx][k]) mx = i; 12 | } 13 | swap(GS[k], GS[mx]); 14 | double x = GS[k][k]; 15 | if(x == 0) return false; 16 | for(int i = k + 1; i <= N; i++){ 17 | double y = (GS[i][k]); 18 | for(int j = 1; j <= N + 1; j++){ 19 | GS[i][j] -= (y / x) * GS[k][j]; 20 | } 21 | } 22 | } 23 | for(int k = N; k >= 1; k--){ 24 | double x = GS[k][k]; 25 | if(x == 0) return false; 26 | for(int i = k - 1; i >= 1; i--){ 27 | double y = (GS[i][k]); 28 | for(int j = 1; j <= N + 1; j++){ 29 | GS[i][j] -= (y / x) * GS[k][j]; 30 | } 31 | } 32 | } 33 | for(int k = 1; k <= N; k++){ 34 | double x = GS[k][k]; 35 | GS[k][k] = 1; 36 | GS[k][N + 1] /= x; 37 | } 38 | } 39 | 40 | int main() 41 | { 42 | bool possible = gauss(GS, N, M); 43 | } 44 | -------------------------------------------------------------------------------- /String/Palindromic Substring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define cpp_io() {ios_base::sync_with_stdio(false);cin.tie(NULL);} 4 | #define FOR(i, N) FORR(i, 0, N) 5 | #define FORR(i, a, b) FOTR(i, a, b, 1) 6 | #define FOTR(i, a, b, c) for(int i=(a);i<(b);i+=(c)) 7 | 8 | using namespace std; 9 | 10 | string preprocess(const string &s) { 11 | int len = s.length(); 12 | string t = "^"; 13 | FOR(i, len) { t += "#", t += s[i]; } 14 | return t + "#$"; 15 | } 16 | 17 | vector PS_Array(const string &s) { 18 | string T = preprocess(s); 19 | int n = T.length(), C = 0, R = 0, LN; 20 | vector ps(n); 21 | FORR(i, 1, n) { 22 | LN = (R > i) ? min(R - i, ps[2 * C - i]) : 0; 23 | while (T[i + 1 + LN] == T[i - 1 - LN])LN++; 24 | if (i + LN > R) { C = i, R = i + LN; } 25 | ps[i] = LN; 26 | } 27 | return ps; 28 | /* Print Palindromic Substrings: 29 | * FOR(i, ps.size()) 30 | * if(ps[i]>0) 31 | * cout<>1, ps[i])<<"\n"; 32 | */ 33 | } 34 | 35 | int main() { 36 | cpp_io(); 37 | int n; 38 | string s; 39 | cin >> n; 40 | while (n--) { 41 | cin >> s; 42 | vector lps = PS_Array(s); 43 | 44 | int mx = 0, c = 0; 45 | for (auto v: lps) if (v > mx)mx = v; 46 | for (auto v: lps) if (v == mx)c++; 47 | 48 | cout << mx << " " << c << "\n"; 49 | } 50 | } 51 | 52 | // SOLVED 53 | /* SPOJ MSUBSTR */ 54 | // _SOLVED -------------------------------------------------------------------------------- /Graph/Least Common Ancestor(LCA).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | //LCA with Sparse Table 7 | struct LCA{ 8 | static const int NMAX = 200100; // Must be atleast twice the number of nodes 9 | static const int WIDTH = 20; 10 | int F[NMAX+10], L[NMAX+10], D[NMAX+10], lst; 11 | int idx [NMAX+10][WIDTH+2]; 12 | vector *G; 13 | void build(vector *G, int u){ 14 | memset(F, -1, sizeof(F)); 15 | this->G=G; 16 | D[u]=lst=0; 17 | dfs(u); 18 | for(int h=0; h y) swap(x, y); 38 | int h = bitWidth(y - x); 39 | return argmin(idx[x][h], idx[y - (1< 2 | 3 | using namespace std; 4 | 5 | #define FOR(i,N) FORR(i, 0, N) 6 | #define FORR(i,a,b) FOTR(i, a, b, 1) 7 | #define FOTR(i,a,b,c) for(int i=(a);i<(b);i+=(c)) 8 | 9 | #define cpp_io() {ios_base::sync_with_stdio(false);cin.tie(NULL);} 10 | #define ll long long 11 | #define MOD 1000000007 12 | 13 | struct running_median{ 14 | 15 | priority_queue, less >lh; //left-half of sorted values 16 | priority_queue,greater >rh; //right-half of sorted values 17 | 18 | void INSERT(ll val){ 19 | if(lh.empty() || (!rh.empty() && val < rh.top())) lh.push(val); 20 | else rh.push(val); 21 | 22 | //maintain size(lh) == size(rh) OR size(lh)+1 == size(rh) 23 | if(lh.size() < rh.size())lh.push(rh.top()), rh.pop(); 24 | if(lh.size() -1 > rh.size())rh.push(lh.top()), lh.pop(); 25 | } 26 | ll GET(){ 27 | return lh.empty()?-1:lh.top(); 28 | } 29 | void INIT(){ 30 | lh = priority_queue, less >(); 31 | rh = priority_queue, greater >(); 32 | } 33 | }RM; 34 | 35 | //SPOJ WEIRDFN 36 | int main(){ 37 | cpp_io(); 38 | 39 | int T; 40 | cin>> T; 41 | FOR(cs, T){ 42 | ll a, b, c, n, ans=1, num=1, mid; 43 | cin>>a>>b>>c>>n; 44 | RM.INIT(); 45 | RM.INSERT(1); 46 | FORR(i, 2, n+1){ 47 | mid = RM.GET(); 48 | num = ((a*mid) + (b*i) + c)%MOD; 49 | ans += num; 50 | RM.INSERT(num); 51 | } 52 | cout< 2 | 3 | using namespace std; 4 | 5 | #define FOR(i, N) FORR(i, 0, N) 6 | #define FORR(i, a, b) FOTR(i, a, b, 1) 7 | #define FOTR(i, a, b, c) for(int i=(a);i<(b);i+=(c)) 8 | #define cpp_io() {ios_base::sync_with_stdio(false);cin.tie(NULL);} 9 | #define MAX 10010 10 | #define EPS 1e-9 11 | 12 | struct point { 13 | double x, y; 14 | 15 | const bool operator<(const point &B) const { 16 | if (fabs(x - B.x) < EPS) return y < B.y; 17 | return x < B.x; 18 | } 19 | 20 | } P[MAX]; 21 | 22 | double dist(const point &A, const point &B) { 23 | return hypot(A.x - B.x, A.y - B.y); 24 | } 25 | 26 | double closest(int l, int r) { 27 | if (l >= r) return 1e10; 28 | if (l + 1 == r) 29 | return dist(P[l], P[r]); 30 | 31 | int mid = (l + r) >> 1; 32 | double mn = min(closest(l, mid), closest(mid + 1, r)); 33 | 34 | vector LB, RB; 35 | 36 | for (int i = mid; i >= l && (P[mid].x - P[i].x) < mn; --i) 37 | LB.push_back(P[i]); 38 | for (int i = mid + 1; i <= r && (P[i].x - P[mid].x) < mn; ++i) 39 | RB.push_back(P[i]); 40 | 41 | FORR(i, 0, LB.size()) FORR(j, 0, RB.size()) mn = min(mn, dist(LB[i], RB[j])); 42 | 43 | return mn; 44 | } 45 | 46 | int main() { 47 | cpp_io(); 48 | int N; 49 | while (cin >> N) { 50 | if (N == 0)break; 51 | FOR(i, N)cin >> P[i].x >> P[i].y; 52 | sort(P, P + N); 53 | double ans = closest(0, N - 1); 54 | if (ans < 10000.0) { 55 | cout << fixed << setprecision(4) << ans << "\n"; 56 | } else cout << "INFINITY\n"; 57 | } 58 | } 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /Start Template.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define EPS 1e-9 6 | #define For(i,N) FOR(i,N) 7 | #define Forr(i,a,b) FORR(i,a,b) 8 | #define Fotr(i,a,b,c) FOTR(i,a,b,c) 9 | #define FOR(i,N) FORR(i, 0, N) 10 | #define FORR(i,a,b) FOTR(i, a, b, 1) 11 | #define FOTR(i,a,b,c) for(int i=(a);i<(b);i+=(c)) 12 | #define FOREACH(i,x) for(__typeof__((x).begin())i=(x).begin();i!=(x).end();i++) 13 | #define MEM(a,x) memset(a,x,sizeof(a)) 14 | #define BCHK(a,x) (((a)>>(x))&1) 15 | #define BSET(a,x) ((a)|(1<<(x))) 16 | #define BCLR(a,x) ((a)&(~(1<<(x)))) 17 | #define BTGL(a,x) ((a)^(1<<(x))) 18 | #define FMT(...) (sprintf(CRTBUFF, __VA_ARGS__)?CRTBUFF:0) 19 | #define read() freopen("input.txt","r",stdin) 20 | #define write() freopen("output.txt","w",stdout) 21 | #define cpp_io() {ios_base::sync_with_stdio(false);cin.tie(NULL);} 22 | #define BUFFSIZE 30000 23 | #define INF 1000000000 24 | #define MAX 300010 25 | #define MOD 1000000007 26 | #define pb push_back 27 | #define mkpr make_pair 28 | #define pii pair 29 | #define PI acos(-1) 30 | #define ll long long 31 | #define fi first 32 | #define si second 33 | #define all(x) x.begin(), x.end() 34 | 35 | char CRTBUFF[BUFFSIZE]; 36 | #define dbg(args...) {cerr<<"-->";debugger::call(#args,args);cerr<<"\n";} 37 | struct debugger { 38 | static void call(const char* it) {} 39 | template 40 | static void call(const char* it, T a, aT... rest) { 41 | string b; 42 | for (; *it&&*it != ','; ++it) if (*it != ' ')b += *it; 43 | cerr << b << "=" << a << " "; 44 | call(++it, rest...); 45 | } 46 | }; 47 | 48 | int main(){ 49 | cpp_io(); 50 | #ifdef LOCAL 51 | read(); 52 | //write(); 53 | #endif 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Math/Linear Diophantine Equation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define ll long long 6 | 7 | ll egcd(ll a, ll b, ll &x, ll &y) { 8 | if (a == 0) { 9 | x = 0; y = 1; 10 | return b; 11 | } 12 | ll x1, y1; 13 | ll d = egcd (b%a, a, x1, y1); 14 | x = y1 - (b / a) * x1; 15 | y = x1; 16 | return d; 17 | } 18 | 19 | //Ax + By = C; Doesn't handle A = B = 0 20 | //More solutions: x' = x + (k*b) / g, y' = y - (k*a) / g 21 | bool LDE_One(ll a, ll b, ll c, ll &x0, ll &y0, ll &g) { 22 | g = egcd (abs(a), abs(b), x0, y0); 23 | if (c % g != 0) 24 | return false; 25 | x0 *= c / g; 26 | y0 *= c / g; 27 | if (a < 0) x0 *= -1; 28 | if (b < 0) y0 *= -1; 29 | return true; 30 | } 31 | 32 | //Ax + By + C = 0 33 | ll LDE_All(int a, int b, int c, ll x1, ll x2, ll y1, ll y2) { 34 | ll x, y, g; 35 | g = egcd(a, b, x, y); 36 | if (!a && !b) { 37 | if (c) return 0; 38 | return (x2 - x1 + 1) * (ll)(y2 - y1 + 1); 39 | } 40 | else if (!a) { 41 | if (c % b) return 0; 42 | c = -c / b; 43 | if (y1 <= c && c <= y2) return (x2 - x1 + 1); 44 | return 0; 45 | } 46 | else if (!b) { 47 | if (c % a) return 0; 48 | c = -c / a; 49 | if (x1 <= c && c <= x2) return (y2 - y1 + 1); 50 | return 0; 51 | } 52 | if (c % g == 0) { 53 | x *= (-c / g); y *= (-c / g); 54 | b /= g; a /= g; 55 | swap(a, b); 56 | double p = (x1 - x) / (double)a, q = (x2 - x) / (double)a; 57 | if (p > q) swap(p, q); 58 | x1 = (ll)ceil(p); 59 | x2 = (ll)floor(q); 60 | 61 | p = (y - y1) / (double)b; q = (y - y2) / (double)b; 62 | if (p > q) swap(p, q); 63 | y1 = (ll)ceil(p); 64 | y2 = (ll)floor(q); 65 | 66 | x1 = max(x1, y1); x2 = min(x2, y2); 67 | return max(0LL, x2 - x1 + 1); 68 | } 69 | return 0; 70 | } 71 | 72 | int main(){ 73 | ll T, a,b,c,x1,y1,x2,y2; 74 | cin>>T; 75 | for(int cs = 1; cs<=T; cs++){ 76 | cin>>a>>b>>c>>x1>>x2>>y1>>y2; 77 | cout<<"Case "< 2 | 3 | using namespace std; 4 | 5 | #define MAX 105000 6 | #define read() freopen("input.txt", "r", stdin) 7 | #define MEM(a, x) memset(a, x, sizeof(a)) 8 | 9 | struct query_t { 10 | int x, y, block, idx, ans; 11 | 12 | static bool sortByBlock(const query_t &a, const query_t &b) { 13 | if (a.block == b.block) { 14 | return a.y < b.y; 15 | } 16 | return a.block < b.block; 17 | } 18 | 19 | static bool sortByIdx(const query_t &a, const query_t &b) { 20 | return a.idx < b.idx; 21 | } 22 | }; 23 | 24 | int N, M; 25 | int A[MAX + 100]; 26 | query_t Q[MAX + 100]; 27 | int MP[MAX + 100]; 28 | int L, R; 29 | int ANS; 30 | 31 | void process(int i) { 32 | Q[i].ans = 0; 33 | while (L > Q[i].x) { 34 | L -= 1; 35 | MP[A[L]] += 1; 36 | if (MP[A[L]] == 1) { 37 | ANS += 1; 38 | } 39 | } 40 | while (R <= Q[i].y) { 41 | MP[A[R]] += 1; 42 | if (MP[A[R]] == 1) { 43 | ANS += 1; 44 | } 45 | R += 1; 46 | } 47 | while (L < Q[i].x) { 48 | MP[A[L]] -= 1; 49 | if (MP[A[L]] == 0) { 50 | ANS -= 1; 51 | } 52 | L += 1; 53 | } 54 | while (R > Q[i].y + 1) { 55 | R -= 1; 56 | MP[A[R]] -= 1; 57 | if (MP[A[R]] == 0) { 58 | ANS -= 1; 59 | } 60 | } 61 | Q[i].ans = ANS; 62 | } 63 | 64 | int main() { 65 | //read(); 66 | int TC; 67 | scanf("%d", &TC); 68 | for (int cs = 1; cs <= TC; cs++) { 69 | ANS = L = R = 0; 70 | MEM(MP, 0); 71 | scanf("%d%d", &N, &M); 72 | for (int i = 1; i <= N; i++) { 73 | scanf("%d", &A[i]); 74 | } 75 | int sqN = sqrt(N); 76 | for (int i = 1; i <= M; i++) { 77 | scanf("%d%d", &Q[i].x, &Q[i].y); 78 | Q[i].idx = i; 79 | Q[i].block = Q[i].x / sqN; 80 | } 81 | sort(Q + 1, Q + M + 1, query_t::sortByBlock); 82 | for (int i = 1; i <= M; i++) { 83 | process(i); 84 | } 85 | sort(Q + 1, Q + M + 1, query_t::sortByIdx); 86 | printf("Case %d:\n", cs); 87 | for (int i = 1; i <= M; i++) { 88 | printf("%d\n", Q[i].ans); 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /Geometry/Convex Diameter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define FOR(i,N) FORR(i, 0, N) 6 | #define FORR(i,a,b) FOTR(i, a, b, 1) 7 | #define FOTR(i,a,b,c) for(int i=(a);i<(b);i+=(c)) 8 | #define cpp_io() {ios_base::sync_with_stdio(false);cin.tie(NULL);} 9 | #define ll long long 10 | #define NXT(a, n) ((a)+1<(n)?(a)+1:(a)+1-(n)) 11 | 12 | struct point { 13 | ll x, y; 14 | bool operator <(const point &p) const { 15 | return (x < p.x || (x == p.x && y < p.y)); 16 | } 17 | point operator-(const point &p) const { 18 | return {x - p.x, y - p.y}; 19 | } 20 | }; 21 | 22 | vector CH, P; 23 | 24 | ll cross(point p1, point p2, point p) { 25 | return (p2.x - p1.x) * (p.y - p1.y) - (p2.y - p1.y) * (p.x - p1.x); 26 | } 27 | 28 | double dist(const point &a, const point &b){ 29 | return hypot(a.x - b.x, a.y - b.y); 30 | } 31 | double det(const point&a, const point &b){ 32 | return a.x * b.y - a.y * b.x; 33 | } 34 | void convexHull() { 35 | sort(P.begin(), P.end()); 36 | int n = P.size(), k = 0; 37 | vector H(2 * n); 38 | for (int i = 0; i= 2 && cross(H[k - 2], H[k - 1], P[i]) <= 0) k--; 40 | H[k++] = P[i]; 41 | } 42 | for (int i = n - 2, t = k + 1; i >= 0; i--) { 43 | while (k >= t && cross(H[k - 2], H[k - 1], P[i]) <= 0)k--; 44 | H[k++] = P[i]; 45 | } 46 | H.resize(k); 47 | CH = H; 48 | } 49 | 50 | double convexDiameter(const vector&ps) { 51 | int n = ps.size()-1; //ps[0] == ps[last] 52 | int is = 0, js = 0; 53 | for (int i = 1; i < n; ++i) { 54 | if (ps[i].x > ps[is].x) 55 | is = i; 56 | if (ps[i].x < ps[js].x) 57 | js = i; 58 | } 59 | double maxd = dist(ps[is],ps[js]); 60 | int i = is, j = js; 61 | do { 62 | if (det(ps[NXT(i,n)] - ps[i], ps[NXT(j,n)] - ps[j]) >= 0){ 63 | ++j; 64 | if(j>=n) j-=n; 65 | } 66 | else{ 67 | ++i; 68 | if(i>=n) i-=n; 69 | } 70 | maxd = max(maxd, dist(ps[i],ps[j])); 71 | } while (i != is || j != js); 72 | return maxd; 73 | } 74 | 75 | int main() { 76 | cpp_io(); 77 | int n; 78 | cin>>n; 79 | P.resize(n); 80 | FOR(i, n)cin>>P[i].x >> P[i].y; 81 | convexHull(); 82 | cout< 2 | 3 | using namespace std; 4 | 5 | typedef pair pii; 6 | 7 | namespace ArticulationPoint{ 8 | const int NMAX = 110; 9 | vector > bridges; 10 | int low[NMAX], num[NMAX], parent[NMAX], counter, root, rc; 11 | bool visited[NMAX], aVertex[NMAX]; 12 | 13 | void clear(){ 14 | memset(visited, 0, sizeof(visited)); 15 | memset(low, -1, sizeof(low)); 16 | memset(num, -1, sizeof(num)); 17 | memset(parent, 0, sizeof(parent)); 18 | counter = 0; 19 | root = -1; 20 | bridges.clear(); 21 | } 22 | void find(vector G[], int u){ 23 | if(num[u] != -1 ) return; 24 | if(root == -1) root = u, rc = 0; 25 | low[u] = num[u] = counter++; 26 | for(auto v: G[u]){ 27 | if (num[v] == -1){ 28 | parent[v] = u; 29 | if (u == root) rc++; 30 | find(G, v); 31 | if (low[v] > num[u]) { 32 | if(u != root) aVertex[u] = true; 33 | bridges.push_back({min(u, v), max(u, v)}); 34 | } 35 | low[u] = min(low[u], low[v]); 36 | } 37 | else if (v != parent[u]){ 38 | low[u] = min(low[u], num[v]); 39 | } 40 | } 41 | if (rc >= 2) aVertex[root] = true; 42 | if (root == u) root = -1, rc = 0; 43 | } 44 | } 45 | 46 | 47 | 48 | bool fnc(const pii &a, const pii &b) 49 | { 50 | return (a.first < b.first); 51 | } 52 | int main(){ 53 | //freopen("796.txt", "r", stdin); 54 | //ofstream cot("o.txt"); 55 | stringstream ss; 56 | string s; 57 | int x, y, m, i, cs = 1; 58 | int n; 59 | while (cin >> n) { 60 | getline (cin, s); 61 | vector grph[110]; 62 | for (i=0; i>x; 67 | ss>>s; 68 | while(ss>>y) { 69 | grph[x].push_back(y); 70 | } 71 | } 72 | ArticulationPoint::clear(); 73 | for (i=0; i 2 | 3 | using namespace std; 4 | 5 | #define FOR(i, N) FORR(i, 0, N) 6 | #define FORR(i, a, b) FOTR(i, a, b, 1) 7 | #define FOTR(i, a, b, c) for(int i=(a);i<(b);i+=(c)) 8 | 9 | #define MEM(a, x) memset(a,x,sizeof(a)) 10 | #define pii pair 11 | #define MAX 100010 //Keep MAX as small as possible 12 | #define MAXLG 17 //ceil(log2(MAX)) 13 | 14 | struct SuffixArray { 15 | pair L[MAX]; 16 | int P[MAXLG + 1][MAX], n, stp, cnt, sa[MAX], lcp[MAX]; 17 | char str[MAX]; 18 | 19 | void Generate() { 20 | MEM(L, 0); 21 | 22 | n = strlen(str); 23 | FOR(i, n) P[0][i] = str[i] - 1; 24 | sa[0] = 0; 25 | for (stp = 1, cnt = 1; cnt < n; stp++, cnt <<= 1) { 26 | FOR(i, n) L[i] = 27 | { {P[stp - 1][i], (i + cnt < n)? P[stp - 1][i + cnt] : -1}, i}; 28 | sort(L, L + n); 29 | FOR(i, n)P[stp][L[i].second] = 30 | i > 0 && L[i].first == L[i - 1].first ? 31 | P[stp][L[i - 1].second] : i; 32 | } 33 | FOR(i, n)sa[i] = L[i].second; 34 | } 35 | 36 | int LcpXY(int x, int y) { // LcpXY(i, j) = lcp of suffixes [i...j] 37 | x = sa[x], y = sa[y]; 38 | int k, ret = 0; 39 | if (x == y)return n - x; 40 | for (k = stp - 1; k >= 0 && x < n && y < n; k--) { 41 | if (P[k][x] == P[k][y]) 42 | x += 1 << k, y += 1 << k, ret += 1 << k; 43 | } 44 | return ret; 45 | } 46 | 47 | void LcpArray() { 48 | int i, j, k; 49 | int *ar = new int[MAX]; 50 | 51 | FOR(i, n) ar[sa[i]] = i; 52 | 53 | for (k = 0, i = 0; i < n; i++, k ? k-- : 0) { 54 | if (ar[i] == (n - 1))k = 0; 55 | else { 56 | j = sa[ar[i] + 1]; 57 | while ( ((i + k) < n) && ((j + k) < n) && 58 | (str[i + k] == str[j + k]) ) 59 | k++; 60 | } 61 | lcp[ar[i]] = k; 62 | } 63 | delete ar; 64 | } 65 | } sa; 66 | 67 | int main() { 68 | sprintf(sa.str, "%s", "abcabc"); 69 | sa.Generate(); 70 | sa.LcpArray(); 71 | 72 | FOR(i, 6)cout << sa.sa[i] << " "; //3 0 4 1 5 2 73 | cout << "\n\n"; 74 | 75 | FOR(i, 5)cout << sa.lcp[i] << " "; //3 0 2 0 1 76 | cout << "\n\n"; 77 | 78 | cout << sa.LcpXY(0,2) << "\n"; // 0 79 | cout << sa.LcpXY(2,3) << "\n"; // 2 80 | cout << sa.LcpXY(0,1) << "\n"; // 3 81 | cout << sa.LcpXY(0,0) << "\n\n"; // 3 82 | } 83 | 84 | // SOLVED 85 | /* SPOJ SARRAY - Suffix Array (80) 86 | /* SPOJ SUBLEX - Lexicographical Substring Search 87 | * SPOJ DISUBSTR - Distinct Substrings */ 88 | // _SOLVED -------------------------------------------------------------------------------- /Geometry/Kattis Dogs.cpp: -------------------------------------------------------------------------------- 1 | /* Kattis /problems/dogs 2 | Two dogs are moving along two sequence of line segments 3 | Have to find the minimum distance between the dogs 4 | Smaller problem - Given 2 line segments of same length, 5 | AB and CD. A dog is moving from A to B, another from C to D. 6 | what will be their minimum distance? 7 | */ 8 | 9 | #include 10 | 11 | using namespace std; 12 | 13 | #define EPS 1e-9 14 | #define FOR(i,N) FORR(i, 0, N) 15 | #define FORR(i,a,b) FOTR(i, a, b, 1) 16 | #define FOTR(i,a,b,c) for(int i=(a);i<(b);i+=(c)) 17 | #define cpp_io() {ios_base::sync_with_stdio(false);cin.tie(NULL);} 18 | 19 | double ans; 20 | 21 | struct point{ 22 | double x, y; 23 | }; 24 | 25 | double dist(const point &a, const point &b){ 26 | return hypot(a.x-b.x, a.y-b.y); 27 | } 28 | 29 | point nextPoint(point a, point b, double T){ 30 | return { (a.x+T*b.x)/(1+T), (a.y+T*b.y)/(1+T) }; 31 | } 32 | 33 | void calc(point a, point b, point c, point d, double rt){ 34 | 35 | double lf = 0, m1, m2, t1, t2, ds, d1, d2; 36 | point p1, p2, p3, p4; 37 | FOR(i, 100){ 38 | ds = rt - lf; 39 | m1 = lf+ds/3, m2 = rt-ds/3; 40 | 41 | t1 = ds - m1; 42 | if(t1>n){ 65 | vector d1, d2; 66 | d1.resize(n); 67 | FOR(i, n)cin>>d1[i].x>>d1[i].y; 68 | cin>>m; 69 | d2.resize(m); 70 | FOR(i, m)cin>>d2[i].x>>d2[i].y; 71 | 72 | it1=it2=1; 73 | point c1=d1[0], c2=d2[0], pr1, pr2; 74 | double dst1, dst2; 75 | ans = 1e15; 76 | while(it1 2 | 3 | using namespace std; 4 | 5 | /* Template Begins */ 6 | 7 | #define For(i,N) Forr(i, 0, N) 8 | #define Forr(i,a,b) Fotr(i, a, b, 1) 9 | #define Fotr(i,a,b,c) for(int i=(a);i<(b);i+=(c)) 10 | #define FOREACH(it, x) for(__typeof__((x).begin()) it=(x).begin();it!=(x).end();it++) 11 | #define MEM(a,x) memset(a,x,sizeof(a)) 12 | #define BCHK(a,x) (((a)>>(x))&1) 13 | #define BSET(a,x) ((a)|(1<<(x))) 14 | #define BCLR(a,x) ((a)&(~(1<<(x)))) 15 | #define BTGL(a,x) ((a)^(1<<(x))) 16 | #define FMT(...) (sprintf(CRTBUFF, __VA_ARGS__)?CRTBUFF:0) 17 | #define read() freopen("input.txt","r",stdin) 18 | #define write() freopen("output.txt","w",stdout) 19 | #define cpp_io() {ios_base::sync_with_stdio(false);cin.tie(NULL);} 20 | #define BUFFSIZE 30000 21 | #define INF 1000000000 22 | #define MOD 1000000007 23 | #define MAX 200000 24 | #define pb push_back 25 | #define mkpr make_pair 26 | #define pii pair 27 | #define fi first 28 | #define si second 29 | typedef long long ll; 30 | 31 | char CRTBUFF[BUFFSIZE]; 32 | 33 | #define dbg(args...) {cout<<"-->";debugger::call(#args,args);cout<<"\n";} 34 | struct debugger{ 35 | static void call(const char* it){} 36 | template 37 | static void call(const char* it, T a, aT... rest){ 38 | string b; 39 | for(;*it&&*it!=',';++it) if(*it!=' ')b+=*it; 40 | cout< vec; 49 | int R, C; 50 | 51 | inline Matrix(){} 52 | inline Matrix(int RR, int CC){ 53 | R = RR, C = CC; 54 | vec.resize(R * C + 1); 55 | fill(vec.begin(), vec.end(), 0); 56 | } 57 | 58 | inline ll* operator[](int x){ 59 | return &vec[x * C]; 60 | } 61 | 62 | inline Matrix operator*(Matrix &B){ 63 | Matrix &A = *this; 64 | Matrix C(A.R, B.C); 65 | for(int i = 0; i < C.R; i++){ 66 | for(int k = 0; k < A.C; k++){ 67 | for(int j = 0; j < C.C; j++){ 68 | C[i][j] += (MOD + A[i][k] * B[k][j]) % MOD; 69 | C[i][j] %= MOD; 70 | } 71 | } 72 | } 73 | return C; 74 | } 75 | 76 | inline static Matrix Identity(int R, int C){ 77 | Matrix M(R, C); 78 | for(int i=0; i>=1; 89 | } 90 | return ret; 91 | } 92 | }; 93 | 94 | 95 | int main() 96 | { 97 | return 0; 98 | } 99 | 100 | 101 | -------------------------------------------------------------------------------- /Graph/Articulation Point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MEM(a,x) memset(a,x,sizeof(a)) 4 | #define cpp_io() {ios_base::sync_with_stdio(false);cin.tie(NULL);} 5 | #define pii pair 6 | #define pb push_back 7 | 8 | using namespace std; 9 | 10 | //namespace ArticulationPoint 11 | namespace AP { 12 | const int NMAX = 110; 13 | vector > bridges; 14 | int low[NMAX], num[NMAX], parent[NMAX], counter, root, rootc; 15 | 16 | bool visited[NMAX]; 17 | int ap[NMAX]; //ap[i] stores the number of vertex will disconnect 18 | //if i is removed 19 | 20 | void clear() { 21 | MEM(visited, 0); MEM(ap, 0); MEM(low, -1); MEM(num, -1); MEM(parent, 0); 22 | counter = 0; rootc = 0; 23 | bridges.clear(); 24 | } 25 | 26 | void find(vectorconst (&G)[NMAX], int u) { 27 | visited[u] = true; 28 | if(u==root)rootc = 0; 29 | low[u] = num[u] = counter++; 30 | for (auto v: G[u]) { 31 | if (!visited[v]) { 32 | parent[v] = u; 33 | if (u == root)rootc++; 34 | find(G, v); 35 | 36 | //set Articulation Point 37 | if (low[v] >= num[u])ap[u]++; 38 | 39 | //set Bridge 40 | if (low[v] > num[u]) { 41 | int k = u, l = v; 42 | if (k > l)swap(k, l); 43 | bridges.pb({k, l}); 44 | } 45 | low[u] = min(low[u], low[v]); 46 | } else if (v != parent[u]) { 47 | low[u] = min(low[u], num[v]); 48 | } 49 | } 50 | if(u==root && rootc < 2) ap[root] = 0; 51 | } 52 | } 53 | 54 | //UVa 796 - Critical Links 55 | int main() { 56 | cpp_io(); 57 | stringstream ss; 58 | string s; 59 | int x, y, m, i, cs = 1; 60 | int n; 61 | while (cin >> n) { 62 | getline(cin, s); 63 | vector G[110]; 64 | for (i = 0; i < n; i++) { 65 | getline(cin, s); 66 | ss.clear(); 67 | ss.str(s); 68 | ss >> x; 69 | ss >> s; 70 | while (ss >> y) { 71 | G[x].pb(y); 72 | } 73 | } 74 | AP::clear(); 75 | for (i = 0; i < n; i++) { 76 | if (AP::visited[i])continue; 77 | AP::root = i; 78 | AP::find(G, i); 79 | } 80 | auto &bridges = AP::bridges; 81 | y = 0; 82 | sort(bridges.begin(), bridges.end()); 83 | cout << bridges.size() << " critical links\n"; 84 | for (i = 0; i < bridges.size(); i++) { 85 | cout << bridges[i].first << " - " << bridges[i].second << "\n"; 86 | } 87 | cout << "\n"; 88 | } 89 | } 90 | 91 | // SOLVED 92 | /* UVa 796 - Critical Links 93 | * UVa 10765 - Doves and bombs 94 | * hackerearth tutorial: https://goo.gl/rD1Z1q */ 95 | // _SOLVED -------------------------------------------------------------------------------- /docs/service-worker.js: -------------------------------------------------------------------------------- 1 | "use strict";var precacheConfig=[["/index.html","893e388b2099565b4311dc79e09e7906"],["/static/css/main.1f8ac975.css","44b9f4c3b48361118225bc6e6a1a2b9c"],["/static/js/main.b832e41e.js","6679fdae3d9e5c392115eebe1d3d1ee8"]],cacheName="sw-precache-v3-sw-precache-webpack-plugin-"+(self.registration?self.registration.scope:""),ignoreUrlParametersMatching=[/^utm_/],addDirectoryIndex=function(e,t){var n=new URL(e);return"/"===n.pathname.slice(-1)&&(n.pathname+=t),n.toString()},cleanResponse=function(t){return t.redirected?("body"in t?Promise.resolve(t.body):t.blob()).then(function(e){return new Response(e,{headers:t.headers,status:t.status,statusText:t.statusText})}):Promise.resolve(t)},createCacheKey=function(e,t,n,r){var a=new URL(e);return r&&a.pathname.match(r)||(a.search+=(a.search?"&":"")+encodeURIComponent(t)+"="+encodeURIComponent(n)),a.toString()},isPathWhitelisted=function(e,t){if(0===e.length)return!0;var n=new URL(t).pathname;return e.some(function(e){return n.match(e)})},stripIgnoredUrlParameters=function(e,n){var t=new URL(e);return t.hash="",t.search=t.search.slice(1).split("&").map(function(e){return e.split("=")}).filter(function(t){return n.every(function(e){return!e.test(t[0])})}).map(function(e){return e.join("=")}).join("&"),t.toString()},hashParamName="_sw-precache",urlsToCacheKeys=new Map(precacheConfig.map(function(e){var t=e[0],n=e[1],r=new URL(t,self.location),a=createCacheKey(r,hashParamName,n,/\.\w{8}\./);return[r.toString(),a]}));function setOfCachedUrls(e){return e.keys().then(function(e){return e.map(function(e){return e.url})}).then(function(e){return new Set(e)})}self.addEventListener("install",function(e){e.waitUntil(caches.open(cacheName).then(function(r){return setOfCachedUrls(r).then(function(n){return Promise.all(Array.from(urlsToCacheKeys.values()).map(function(t){if(!n.has(t)){var e=new Request(t,{credentials:"same-origin"});return fetch(e).then(function(e){if(!e.ok)throw new Error("Request for "+t+" returned a response with status "+e.status);return cleanResponse(e).then(function(e){return r.put(t,e)})})}}))})}).then(function(){return self.skipWaiting()}))}),self.addEventListener("activate",function(e){var n=new Set(urlsToCacheKeys.values());e.waitUntil(caches.open(cacheName).then(function(t){return t.keys().then(function(e){return Promise.all(e.map(function(e){if(!n.has(e.url))return t.delete(e)}))})}).then(function(){return self.clients.claim()}))}),self.addEventListener("fetch",function(t){if("GET"===t.request.method){var e,n=stripIgnoredUrlParameters(t.request.url,ignoreUrlParametersMatching),r="index.html";(e=urlsToCacheKeys.has(n))||(n=addDirectoryIndex(n,r),e=urlsToCacheKeys.has(n));var a="/index.html";!e&&"navigate"===t.request.mode&&isPathWhitelisted(["^(?!\\/__).*"],t.request.url)&&(n=new URL(a,self.location).toString(),e=urlsToCacheKeys.has(n)),e&&t.respondWith(caches.open(cacheName).then(function(e){return e.match(urlsToCacheKeys.get(n)).then(function(e){if(e)return e;throw Error("The cached response that was expected is missing.")})}).catch(function(e){return console.warn('Couldn\'t serve response for "%s" from cache: %O',t.request.url,e),fetch(t.request)}))}}); -------------------------------------------------------------------------------- /Geometry/Rectangle Union.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define FOR(i, N) FORR(i, 0, N) 6 | #define FORR(i, a, b) FOTR(i, a, b, 1) 7 | #define FOTR(i, a, b, c) for(int i=(a);i<(b);i+=(c)) 8 | #define MEM(a, x) memset(a,x,sizeof(a)) 9 | #define cpp_io() {ios_base::sync_with_stdio(false);cin.tie(NULL);} 10 | #define INF 1000000000 11 | #define MAX 70000 12 | #define MOD 1000000007 13 | typedef long long ll; 14 | 15 | struct item { 16 | int x, y1, y2, type; 17 | 18 | bool operator<(const item &a) const { 19 | if (x == a.x)return type > a.type; 20 | return x < a.x; 21 | } 22 | } items[MAX]; 23 | 24 | struct NODE{ 25 | int len, val; 26 | }tree[MAX*2]; 27 | 28 | int A[MAX]; 29 | 30 | void update(int node, int first, int last, int l, int r, int v) { 31 | int mid = (first + last) >> 1, ch1 = node << 1, ch2 = ch1 | 1; 32 | NODE &cnd = tree[node]; 33 | if (first >= l && last <= r) { 34 | cnd.val += v; 35 | if (cnd.val) { 36 | cnd.len = A[last - 1] - A[first - 2]; 37 | } else { 38 | if (first != last)cnd.len = tree[ch1].len + tree[ch2].len; 39 | else cnd.len = 0; 40 | } 41 | return; 42 | } 43 | if (r <= mid) update(ch1, first, mid, l, r, v); 44 | else if (l > mid) update(ch2, mid + 1, last, l, r, v); 45 | else { 46 | update(ch1, first, mid, l, r, v); 47 | update(ch2, mid + 1, last, l, r, v); 48 | } 49 | if (cnd.val)cnd.len = A[last - 1] - A[first - 2]; 50 | else cnd.len = tree[ch1].len + tree[ch2].len; 51 | } 52 | 53 | int main() { 54 | int T, n, x1, y1, x2, y2; 55 | scanf("%d", &T); 56 | FOR(cs, T) { 57 | MEM(tree, 0); 58 | map mp; 59 | scanf("%d", &n); 60 | FOR(i, n) { 61 | scanf("%d%d%d%d", &x1, &y1, &x2, &y2); 62 | int ch1 = (i << 1), ch2 = ch1 | 1; 63 | items[ch1].x = x1, items[ch1].y1 = y1, items[ch1].y2 = y2, items[ch1].type = 0; 64 | items[ch2].x = x2, items[ch2].y1 = y1, items[ch2].y2 = y2, items[ch2].type = 1; 65 | A[ch1] = y1; 66 | A[ch2] = y2; 67 | } 68 | 69 | sort(items, items + n * 2); 70 | sort(A, A + n * 2); 71 | int j = 0; 72 | FOR(i, n * 2) { 73 | if (mp[A[i]] == 0) { 74 | mp[A[i]] = j + 1; 75 | A[j] = A[i]; 76 | j++; 77 | } 78 | } 79 | ll ans = 0, p = 0; 80 | FOR(i, n * 2) { 81 | x1 = items[i].x; 82 | ans += tree[1].len * (x1 - p) * 1LL; 83 | do { 84 | if (items[i].y1 != items[i].y2) { 85 | if (items[i].type) 86 | update(1, 1, j, mp[items[i].y1] + 1, mp[items[i].y2], -1); 87 | else update(1, 1, j, mp[items[i].y1] + 1, mp[items[i].y2], 1); 88 | } 89 | i++; 90 | } while (i < n * 2 && items[i].x == x1); 91 | i--; 92 | p = x1; 93 | } 94 | printf("Case %d: %lld\n", cs + 1, ans); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /Helper/FastIO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /* 6 | Enables reading 128-bit integers 7 | */ 8 | #ifdef __SIZEOF_INT128__ 9 | namespace std { template<> struct is_integral<__int128_t>: true_type {}; } 10 | #endif 11 | 12 | struct FastIO { 13 | static const int MaxSize = 8192; 14 | bool isEOF; 15 | 16 | char in[MaxSize+10], out[MaxSize+10], buffer[MaxSize+10]; 17 | int inPos, inSize, outSize; 18 | 19 | char nextChar() { 20 | if (inPos == inSize) inSize = fread(in, sizeof(char), MaxSize, stdin), inPos = 0; 21 | if (inPos == inSize) return isEOF = true, EOF; 22 | return in[inPos++]; 23 | } 24 | 25 | void printChar(char ch) { 26 | if (outSize == MaxSize) flush(); 27 | out[outSize++] = ch; 28 | } 29 | 30 | void flush() { 31 | if (outSize) fwrite(out, sizeof(char), outSize, stdout); 32 | outSize = 0; 33 | } 34 | 35 | ~FastIO() { 36 | flush(); 37 | } 38 | 39 | /* For EOF */ 40 | explicit operator bool() const { 41 | return !isEOF; 42 | } 43 | 44 | /* For Numbers */ 45 | template< 46 | typename T, 47 | typename = typename enable_if::value, T>::type> 48 | FastIO& operator>>(T &num) { 49 | num = 0; 50 | char ch = nextChar(), neg = false; 51 | while (ch != EOF && !isdigit(ch) && ch != '-' && ch != '+') ch = nextChar(); 52 | if (ch != EOF && ch == '-' || ch == '+') neg = (ch == '-'), ch = nextChar(); 53 | while (ch != EOF && isdigit(ch)) num = num * 10 + ch - '0', ch = nextChar(); 54 | if (neg) num = -num; 55 | return *this; 56 | } 57 | 58 | template< 59 | typename T, 60 | typename = typename enable_if::value, T>::type> 61 | FastIO& operator<<(T num) { 62 | int n = 0, neg = false; 63 | if (num < 0) neg = true, num = -num; 64 | while (num || !n) buffer[n++] = num % 10 + '0', num = num / 10; 65 | if (neg) printChar('-'); 66 | while (n) printChar(buffer[--n]); 67 | return *this; 68 | } 69 | 70 | /* For Characters */ 71 | FastIO& operator>>(char &ch) { 72 | ch = nextChar(); 73 | return *this; 74 | } 75 | 76 | FastIO& operator<<(char ch) { 77 | printChar(ch); 78 | return *this; 79 | } 80 | 81 | /*For Strings */ 82 | FastIO& operator>>(string &s) { 83 | s.clear(); 84 | char ch = nextChar(); 85 | while (ch != EOF && ch == ' ' || ch == '\n') ch = nextChar(); 86 | while (ch != EOF && ch != ' ' && ch != '\n') s.push_back(ch), ch = nextChar(); 87 | return *this; 88 | } 89 | 90 | FastIO& operator<<(string s) { 91 | for (auto ch: s) printChar(ch); 92 | return *this; 93 | } 94 | 95 | /*For Doubles */ 96 | 97 | } fio; 98 | 99 | int main() { 100 | //SPOJ INOUTTEST 101 | int n; 102 | fio >> n; 103 | for (int i = 0; i < n; i++) { 104 | int a, b; 105 | fio >> a >> b; 106 | fio << (a * b) << "\n"; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /String/Aho Corasick.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define MAX 1010101 6 | #define STT 300000 7 | #define CLR(a, x) (memset(a, x, sizeof(a))) 8 | char mStr[MAX + 7]; 9 | char tempStr[MAX + 7]; 10 | int jump[STT][30]; 11 | int fail[STT]; 12 | int visited[STT]; 13 | int term[STT]; 14 | vector rev[STT]; 15 | 16 | struct AhoCorasick{ 17 | static const int MAXN = 300000; 18 | int jump[MAXN][300], fail[MAXN], visited[MAXN]; 19 | vector rev[STT], term; 20 | int lst; 21 | 22 | void add(string str){ //Returns the final state of a string 23 | int j = 0; 24 | for(int i = 0; str[i]; i++) { 25 | char ch = str[i]; 26 | if(jump[j][ch] == -1) jump[j][ch] = ++lst; 27 | j = jump[j][ch]; 28 | } 29 | term.push_back(j); 30 | } 31 | 32 | void build(){ 33 | queue que; 34 | fail[0] = -1; 35 | que.push(0); 36 | while(!que.empty()) { 37 | int v = que.front(); 38 | que.pop(); 39 | for(int i = 0; i <= 26; i++) { 40 | if(jump[v][i] != -1) { 41 | int u = jump[v][i]; 42 | int j = fail[v]; 43 | while(j != -1 && jump[j][i] == -1) j = fail[j]; 44 | if(j != -1) { 45 | fail[u] = jump[j][i]; 46 | rev[jump[j][i]].push_back(u); 47 | } 48 | que.push(u); 49 | } 50 | } 51 | } 52 | } 53 | 54 | void process(string s){ 55 | memset(visited, 0, sizeof(visited)); 56 | int j = 0; 57 | for(auto ch: s){ 58 | while(j != -1 && jump[j][ch] == -1) { 59 | j = fail[j]; 60 | } 61 | if(j != -1 && jump[j][ch] != -1) 62 | j = jump[j][ch]; 63 | if(j == -1) j = 0; 64 | visited[j] += 1;; 65 | } 66 | } 67 | 68 | int countMatched(int i){ // Number of times the i'th string was matched 69 | return dfs(term[i]); 70 | } 71 | 72 | int dfs(int u){ 73 | if(u == 0) return 0; 74 | int cnt = visited[u]; 75 | for(auto v: rev[u]) cnt+=dfs(v); 76 | return cnt; 77 | } 78 | 79 | void clear(){ 80 | lst=0; 81 | memset(jump, -1, sizeof(jump)); 82 | memset(fail, 0, sizeof(fail)); 83 | for(int i=0;i> t; 94 | for(int cs = 1; cs <= t; cs++) { 95 | CLR(term, -1); 96 | aho.clear(); 97 | int n; 98 | scanf("%d", &n); 99 | scanf("%s", mStr); 100 | for(int i = 0; i < n; i++) { 101 | scanf("%s", tempStr); 102 | aho.add(tempStr); 103 | } 104 | aho.build(); 105 | aho.process(mStr); 106 | printf("Case %d:\n", cs); 107 | for(int i = 0; i < n; i++) { 108 | int cnt = aho.countMatched(i); 109 | printf("%d\n", cnt); 110 | } 111 | 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /Geometry/Minimum Bounding Rectangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define FOR(i, N) FORR(i, 0, N) 6 | #define FORR(i, a, b) FOTR(i, a, b, 1) 7 | #define FOTR(i, a, b, c) for(int i=(a);i<(b);i+=(c)) 8 | #define cpp_io() {ios_base::sync_with_stdio(false);cin.tie(NULL);} 9 | #define INF 1000000000 10 | 11 | #define NXT(a, n) ((a)+1<(n)?(a)+1:(a)+1-(n)) 12 | #define LMD(a, n) ((a)<(n)?(a):(a)-(n)) 13 | #define double long double 14 | 15 | struct point { 16 | double x, y; 17 | 18 | bool operator<(const point &p) const { 19 | return (x < p.x || (x == p.x && y < p.y)); 20 | } 21 | 22 | point operator-(const point &a) const { 23 | return {x - a.x, y - a.y}; 24 | } 25 | 26 | point operator/(const double val) const { 27 | return {x / val, y / val}; 28 | } 29 | }; 30 | 31 | vector CH, P; 32 | 33 | double cross(point p1, point p2, point p) { 34 | return (p2.x - p1.x) * (p.y - p1.y) - (p2.y - p1.y) * (p.x - p1.x); 35 | } 36 | 37 | double cross2(point A, point B) { 38 | return A.x * B.y - A.y * B.x; 39 | } 40 | 41 | double dot(point A, point B) { 42 | return A.x * B.x + A.y * B.y; 43 | } 44 | 45 | double dist(point A, point B) { 46 | return hypot(A.x - B.x, A.y - B.y); 47 | } 48 | 49 | void convexHull() { 50 | sort(P.begin(), P.end()); 51 | int n = P.size(), k = 0; 52 | vector H(2 * n); 53 | for (int i = 0; i < n; i++) { 54 | while (k >= 2 && cross(H[k - 2], H[k - 1], P[i]) <= 0) k--; 55 | H[k++] = P[i]; 56 | } 57 | for (int i = n - 2, t = k + 1; i >= 0; i--) { 58 | while (k >= t && cross(H[k - 2], H[k - 1], P[i]) <= 0)k--; 59 | H[k++] = P[i]; 60 | } 61 | H.resize(k); 62 | CH = H; 63 | } 64 | 65 | void smallRect() { 66 | int n = CH.size() - 1; // if CH[0] == CH[LAST] 67 | if (n < 3) { 68 | //cout << "0.00 0.00\n"; 69 | //return; 70 | } 71 | int l = 1, r = 1, u = 1; 72 | double area = INF, per = INF; 73 | FOR(i, n) { 74 | point edge = (CH[NXT(i, n)] - CH[i]) / dist(CH[NXT(i, n)], CH[i]); 75 | 76 | while (dot(edge, CH[LMD(r, n)] - CH[i]) 77 | < dot(edge, CH[NXT(r, n)] - CH[i])) r++; 78 | 79 | while (u < r || cross2(edge, CH[LMD(u, n)] - CH[i]) 80 | < cross2(edge, CH[NXT(u, n)] - CH[i])) u++; 81 | 82 | while (l < u || dot(edge, CH[LMD(l, n)] - CH[i]) 83 | > dot(edge, CH[NXT(l, n)] - CH[i])) l++; 84 | 85 | double w = dot(edge, CH[LMD(r, n)] - CH[i]) - dot(edge, CH[LMD(l, n)] - CH[i]); 86 | double h = fabs(cross2(CH[i] - CH[LMD(u, n)], CH[NXT(i, n)] - CH[LMD(u, n)]) / 87 | dist(CH[i], CH[NXT(i, n)])); 88 | 89 | area = min(area, w * h); 90 | per = min(per, (w + h) * 2); 91 | } 92 | cout << fixed << setprecision(2) << area << " " << per << "\n"; 93 | } 94 | 95 | int main() { 96 | cpp_io(); 97 | int N, cs = 1; 98 | while (cin >> N) { 99 | if (N == 0)break; 100 | P.resize(N); 101 | FOR(i, N)cin >> P[i].x >> P[i].y; 102 | convexHull(); 103 | smallRect(); 104 | } 105 | } 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /Flow/MCMF.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct _mcmf { 6 | 7 | struct edge_t { 8 | int u, v, w, cap, prev; 9 | }; 10 | 11 | static const int NMAX = 300; 12 | static const int NINF = 1000000; 13 | vector edges; 14 | int last[NMAX]; 15 | int D[NMAX]; // Distance from source 16 | int E[NMAX]; // Edge used to get to u 17 | bool expand[NMAX]; // Edge used to get to u 18 | 19 | void reset() { 20 | edges.clear(); 21 | memset(last, -1, sizeof(last)); 22 | } 23 | 24 | void add_edge(int u, int v, int w, int cap) { 25 | edges.push_back({u, v, w, cap, last[u]}); 26 | last[u] = edges.size() - 1; 27 | edges.push_back({v, u, -w, 0, last[v]}); 28 | last[v] = edges.size() - 1; 29 | } 30 | 31 | bool spfa(int src, int sink) { 32 | for (int u = 0; u < NMAX; u++) 33 | D[u] = NINF, E[u] = -1, expand[u] = false; 34 | queue que; 35 | que.push(src); 36 | D[src] = 0, E[src] = -2, expand[src] = true; 37 | while(!que.empty()){ 38 | int u = que.front(); 39 | que.pop(); 40 | if(expand[u] == false) continue; 41 | expand[u] = false; 42 | for (int i = last[u]; i != -1; i = edges[i].prev) { 43 | int v = edges[i].v, w = edges[i].w; 44 | if (E[u] == -1) continue; 45 | if (edges[i].cap && (E[v]==-1 || D[u]+w < D[v])){ 46 | D[v] = D[u] + w; 47 | E[v] = i; 48 | expand[v] = true; 49 | que.push(v); 50 | } 51 | } 52 | } 53 | return (E[sink] != -1); 54 | } 55 | 56 | pair max_flow(int src, int sink) { 57 | int mflow = 0; 58 | int mcost = 0; 59 | while (spfa(src, sink)) { 60 | int flow = NINF; 61 | for (int i = E[sink]; i >= 0; i = E[edges[i].u]) { 62 | flow = min(flow, edges[i].cap); 63 | } 64 | for (int i = E[sink]; i >= 0; i = E[edges[i].u]) { 65 | edges[i].cap -= flow; 66 | edges[i ^ 1].cap += flow; 67 | mcost += edges[i].w * flow; 68 | } 69 | mflow += flow; 70 | } 71 | return {mcost, mflow}; 72 | } 73 | 74 | } mcmf; 75 | 76 | //Sample Problem SPOJ-SCITIES 77 | 78 | int main() { 79 | //freopen("input.txt", "r", stdin); 80 | int TC; 81 | scanf("%d", &TC); 82 | for (int cs = 1; cs <= TC; cs++) { 83 | mcmf.reset(); 84 | int n, m; 85 | scanf("%d%d", &n, &m); 86 | for (;;) { 87 | int u, v, w; 88 | scanf("%d%d%d", &u, &v, &w); 89 | if (u == 0 && v == 0 && w == 0) break; 90 | mcmf.add_edge(u, n + v, -w, 1); 91 | } 92 | for (int i = 1; i <= n; i++) { 93 | mcmf.add_edge(0, i, 0, 1); 94 | } 95 | for (int i = 1; i <= m; i++) { 96 | mcmf.add_edge(n + i, n + m + 1, 0, 1); 97 | } 98 | int mcost, mflow; 99 | tie(mcost, mflow) = mcmf.max_flow(0, n + m + 1); 100 | printf("%d\n", -mcost); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /String/Suffix Automata.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct SuffixAutomata { 6 | struct state { 7 | int len, link; 8 | map next; 9 | }; 10 | 11 | const int MAXLEN = 100000; 12 | vector st; 13 | int sz; 14 | int last; 15 | 16 | ///For finding number of occurrences 17 | set < pair > base; 18 | vector cnt; 19 | bool isPreProcessed; 20 | ///For finding number of occurrences 21 | 22 | SuffixAutomata() { 23 | st = vector (MAXLEN * 2); 24 | last = 0; 25 | st[0].len = 0; 26 | st[0].link = -1; 27 | sz = 1; 28 | 29 | ///For finding number of occurrences 30 | cnt = vector (MAXLEN * 2); 31 | isPreProcessed = false; 32 | ///For finding number of occurrences 33 | } 34 | 35 | void BuildAutomata(string& s) { 36 | for (auto &x: s) Insert(x); 37 | } 38 | 39 | void Insert(char c) { 40 | int cur = sz++; 41 | st[cur].len = st[last].len + 1; 42 | 43 | ///For finding number of occurrences 44 | cnt[cur] = 1; 45 | base.insert(make_pair(st[cur].len, cur)); 46 | ///For finding number of occurrences 47 | 48 | int p; 49 | for (p = last; p != -1 && !st[p].next.count(c); p = st[p].link) 50 | st[p].next[c] = cur; 51 | if (p == -1) 52 | st[cur].link = 0; 53 | else { 54 | int q = st[p].next[c]; 55 | if (st[p].len + 1 == st[q].len) 56 | st[cur].link = q; 57 | else { 58 | int clone = sz++; 59 | st[clone].len = st[p].len + 1; 60 | st[clone].next = st[q].next; 61 | st[clone].link = st[q].link; 62 | for (; p !=- 1 && st[p].next[c] == q; p = st[p].link) 63 | st[p].next[c] = clone; 64 | st[q].link = st[cur].link = clone; 65 | 66 | ///For finding number of occurrences 67 | cnt[clone] = 0; 68 | base.insert({st[clone].len, clone}); 69 | ///For finding number of occurrences 70 | } 71 | } 72 | last = cur; 73 | } 74 | 75 | /** 76 | Finds the lexicographically earliest longest common substring between the String s 77 | and string on which automata is built 78 | Tested Problem: SSTORY(CodeChef) 79 | **/ 80 | string LongestCommonSubstring(string &s) { 81 | int v = 0, l = 0, best = 0, bestpos = 0; 82 | for (int i = 0; i < s.size(); i++) { 83 | while (v && !st[v].next.count(s[i])) { 84 | v = st[v].link; 85 | l = st[v].len; 86 | } 87 | if (st[v].next.count(s[i])) { 88 | v = st[v].next[s[i]]; 89 | l++; 90 | } 91 | if (l > best) 92 | best = l, bestpos = i; 93 | } 94 | return s.substr(bestpos - best + 1, best); 95 | } 96 | 97 | /** 98 | Finds the number of time(s) String s occurs in the String on which 99 | automata is built 100 | Tested Problem: 432D(CF) 101 | **/ 102 | 103 | int OccurrencesPreprocess() { 104 | for (auto it = base.rbegin(); it != base.rend(); it++) 105 | cnt[st[it->second].link] += cnt[it->second]; 106 | } 107 | 108 | int Occurrences(string& s) { 109 | if (isPreProcessed == false) OccurrencesPreprocess(); 110 | isPreProcessed = true; 111 | 112 | int v = 0; 113 | for (auto &x: s) v = st[v].next[x]; 114 | 115 | return cnt[v]; 116 | } 117 | }; 118 | 119 | 120 | int main() { 121 | /** 122 | builds automata on String s1 and finds out the Longest common substring between s1 and s2 123 | string s1, s2; 124 | cin >> s1 >> s2; 125 | SuffixAutomata SA; 126 | SA.BuildAutomata(s1); 127 | cout << SA.LongestCommonSubstring(s2) << ; 128 | **/ 129 | 130 | return 0; 131 | } -------------------------------------------------------------------------------- /Data Structure/BIT Multi-type.cpp: -------------------------------------------------------------------------------- 1 | // COMMENTS 2 | /* ****** 0 indexed BIT ****** 3 | * Source: http://code-library.herokuapp.com/fenwick-tree-extended/java 4 | * There are 3 kinds of BIT functions based on query and update: 5 | * 1. Point Update Range Query (PURQ) 6 | * 2. Range Update Point Query (RUPQ) 7 | * 3. Range Update Range Query (RURQ) 8 | */ 9 | // _COMMENTS 10 | 11 | #include 12 | 13 | using namespace std; 14 | 15 | #define MEM(a, x) memset(a,x,sizeof(a)) 16 | #define MAX 100010 17 | #define ll long long 18 | 19 | struct BIT_PURQ { 20 | ll bit[MAX]; 21 | int N; 22 | 23 | void init(int n) { 24 | MEM(bit, 0); 25 | N = n; 26 | } 27 | // add value to bit[i] 28 | void add(int i, ll value) { 29 | for (; i < N; i |= i + 1) bit[i] += value; 30 | } 31 | // sum[0..i] 32 | ll sum(int i) { 33 | ll res = 0; 34 | for (; i >= 0; i = (i & (i + 1)) - 1) res += bit[i]; 35 | return res; 36 | } 37 | // sum[a..b] 38 | ll sum(int a, int b) { 39 | return sum(b) - ((a < 1) ? 0 : sum(a-1)); 40 | } 41 | // bit[i] = value 42 | void assign(int i, ll value) { 43 | add(i, value -sum(i, i)); 44 | } 45 | }; 46 | 47 | struct BIT_RUPQ { 48 | ll bit[MAX]; 49 | int N; 50 | 51 | void init(int n) { 52 | MEM(bit, 0); 53 | N = n; 54 | } 55 | // add 'value' to each element in range [i..N] 56 | void add(int i, ll value) { 57 | for (; i < N; i |= i + 1) bit[i] += value; 58 | } 59 | // add 'value' to each element in range [a..b] 60 | void add(int a, int b, ll value) { 61 | add(a, value); 62 | add(b + 1, -value); 63 | } 64 | // return value of bit[i] 65 | ll get(int i) { 66 | ll res = 0; 67 | for (; i >= 0; i = (i & (i + 1)) - 1) res += bit[i]; 68 | return res; 69 | } 70 | }; 71 | 72 | struct BIT_RURQ { 73 | ll bit1[MAX], bit2[MAX]; 74 | int N; 75 | 76 | void init(int n) { 77 | MEM(bit1, 0); 78 | MEM(bit2, 0); 79 | N = n; 80 | } 81 | void _add(ll *bit, int i, ll value) { 82 | for (; i < N; i |= i + 1) 83 | bit[i] += value; 84 | } 85 | ll _sum(ll *bit, int i) { 86 | ll res = 0; 87 | for (; i >= 0; i = (i & (i + 1)) - 1) 88 | res += bit[i]; 89 | return res; 90 | } 91 | // add 'value' to each element in range [a..b] 92 | void add(int a, int b, ll value) { 93 | _add(bit1, a, value); 94 | _add(bit1, b, -value); 95 | _add(bit2, a, -value * (a - 1)); 96 | _add(bit2, b, value * b); 97 | } 98 | // sum[0...i] 99 | ll sum(int i) { 100 | return (_sum(bit1, i) * i + _sum(bit2, i)); 101 | } 102 | // sum[a...b] 103 | ll sum(int a, int b) { 104 | return sum(b) - ((a < 1) ? 0 : sum(a-1)); 105 | } 106 | }; 107 | 108 | // Find the smallest index for which _sum[0...index]>=value 109 | // Works for BIT_PURQ & BIT_RURQ 110 | int bs_bit(BIT_PURQ bit, ll value) { 111 | int lf = 0, rt = bit.N - 1, mid; 112 | while (lf <= rt) { 113 | mid = (lf + rt) >> 1; 114 | ll res = bit.sum(mid); 115 | if (res == value)return mid; 116 | if (res > value) rt = mid - 1; 117 | else lf = mid + 1; 118 | } 119 | return -1; 120 | } 121 | 122 | BIT_PURQ bit_purq; 123 | BIT_RUPQ bit_rupq; 124 | BIT_RURQ bit_rurq; 125 | 126 | int main() { 127 | 128 | bit_purq.init(10); 129 | bit_purq.assign(5, 1); 130 | bit_purq.add(9, -2); 131 | cout << bit_purq.sum(0, 10) << "\n"; // -1 132 | 133 | bit_purq.init(6); 134 | vector v = {1, 2, 3, 4, 5, 6}; 135 | for(int i=0; i<6; i++) bit_purq.add(i, v[i]); 136 | for (int i = 0; i < 6; i++) 137 | cout << bit_purq.sum(i, i) << " "; 138 | cout << endl; // 1 2 3 4 5 6 139 | 140 | bit_purq.init(8); 141 | v = {0, 0, 1, 0, 0, 1, 0, 0}; 142 | for(int i=0; i<6; i++) bit_purq.add(i, v[i]); 143 | cout << bs_bit(bit_purq, 2) << "\n"; // 5 144 | cout << bs_bit(bit_purq, 3) << "\n"; // -1 145 | 146 | bit_rupq.init(10); 147 | bit_rupq.add(0, 2, 5); 148 | bit_rupq.add(2, 4, 5); 149 | cout << bit_rupq.get(1) << " " << bit_rupq.get(2) 150 | << " " << bit_rupq.get(3) << "\n"; // 5 10 5 151 | 152 | bit_rurq.init(10); 153 | bit_rurq.add(0, 9, 1); 154 | bit_rurq.add(0, 0, -2); 155 | cout << bit_rurq.sum(0, 9) << endl; // 8 156 | } 157 | 158 | // SOLVED 159 | /* LightOJ 1112 - Curious Robin Hood (PURQ) 160 | * SPOJ UPDATEIT (RUPQ) 161 | * SPOJ HORRIBLE (RURQ) */ 162 | // _SOLVED 163 | -------------------------------------------------------------------------------- /String/Suffix Array DC3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define FOR(i, N) FORR(i, 0, N) 4 | #define FORR(i, a, b) FOTR(i, a, b, 1) 5 | #define FOTR(i, a, b, c) for(int i=(a);i<(b);i+=(c)) 6 | #define MEM(a, x) memset(a,x,sizeof(a)) 7 | 8 | #define MAX 100010 9 | #define clr(ar) memset(ar, 0, sizeof(ar)) 10 | #define read() freopen("input.txt", "r", stdin) 11 | 12 | 13 | struct SuffixArray { 14 | char str[MAX]; 15 | int s0[(MAX / 3) + 10], sa0[(MAX / 3) + 10]; 16 | int n, ar[MAX], sa[MAX], lcp[MAX], bucket[MAX], mem[MAX << 2]; 17 | 18 | /* sa[i] is starting index of (i)th smallest suffix in str. 19 | * i is in range[0 .... n) 20 | * lcp[j] is longest common prefix of (j)th and (j-1)th suffix 21 | * j is in range[0 .... n-1) */ 22 | 23 | void radixsort(int *source, int *dest, int *val, int n, int lim) { 24 | int i, s = 0, x; 25 | MEM(bucket, 0); 26 | FOR(i, n) bucket[ val[source[i]] ]++; 27 | FOR(i, lim) { 28 | x = bucket[i]; 29 | bucket[i] = s, s += x; 30 | } 31 | FOR(i, n) dest[bucket[ val[source[i]] ]++] = source[i]; 32 | } 33 | 34 | void DC3(int *ar, int *sa, int n, int lim, int ptr) { 35 | int *s12, *sa12; 36 | int allc = (n / 3) << 1, n0 = (n + 2) / 3; 37 | int i, j, k, l, c, d, p, t, m, r, counter; 38 | 39 | s12 = &mem[ptr], ptr += (allc + 5), sa12 = &mem[ptr], ptr += (allc + 5); 40 | 41 | c = 0, m = 0, r = n + ((n % 3) == 1); 42 | FOR(i, r) { 43 | if (m == 3) m = 0; 44 | if (m) s12[c++] = i; 45 | m++; 46 | } 47 | s12[c] = sa12[c] = s12[c + 1] = sa12[c + 1] = 0; 48 | s12[c + 2] = sa12[c + 2] = 0; 49 | 50 | radixsort(s12, sa12, ar + 2, c, lim + 1); 51 | radixsort(sa12, s12, ar + 1, c, lim + 1); 52 | radixsort(s12, sa12, ar, c, lim + 1); 53 | 54 | counter = 0, j = -1; 55 | FOR(i, c) { 56 | if ( (ar[sa12[i]] != j) || (ar[sa12[i] + 1] != k) || 57 | (ar[sa12[i] + 2] != l) ) { 58 | 59 | counter++; 60 | j = ar[sa12[i]], k = ar[sa12[i] + 1], l = ar[sa12[i] + 2]; 61 | } 62 | if ( (sa12[i] % 3) == 1 ) s12[sa12[i] / 3] = counter; 63 | else s12[ (sa12[i] / 3) + n0 ] = counter; 64 | } 65 | 66 | if (counter == c) { 67 | FOR(i, c) sa12[s12[i] - 1] = i; 68 | } else { 69 | DC3(s12, sa12, c, counter, ptr); 70 | FOR(i, c) s12[sa12[i]] = i + 1; 71 | } 72 | d = 0; 73 | FOR(i, c) if (sa12[i] < n0) s0[d++] = (sa12[i] * 3); 74 | 75 | radixsort(s0, sa0, ar, d, lim + 1); 76 | 77 | k = 0; 78 | l = ((n % 3) == 1); 79 | 80 | FOR(r, n) { 81 | j = sa0[k]; 82 | i = ((sa12[l] < n0) ? (sa12[l] * 3) + 1 : ((sa12[l] - n0) * 3) + 2); 83 | if (l == c) sa[r] = sa0[k++]; 84 | else if (k == d) sa[r] = i, l++; 85 | else { 86 | if (sa12[l] < n0) { 87 | if ((ar[i] < ar[j]) || (ar[i] == ar[j] && 88 | s12[sa12[l] + n0] <= s12[j / 3])) 89 | sa[r] = i, l++; 90 | else sa[r] = j, k++; 91 | } else { 92 | if ( (ar[i] < ar[j]) || 93 | (ar[i] == ar[j] && ar[i + 1] < ar[j + 1]) || 94 | (ar[i] == ar[j] && ar[i + 1] == ar[j + 1] && 95 | s12[sa12[l] - n0 + 1] <= s12[(j / 3) + n0]) ) 96 | sa[r] = i, l++; 97 | else sa[r] = j, k++; 98 | } 99 | } 100 | } 101 | } 102 | 103 | void LcpArray() { 104 | int i, j, k; 105 | FOR(i, n) ar[sa[i]] = i; 106 | 107 | for (k = 0, i = 0; i < n; i++, k ? k-- : 0) { 108 | if (ar[i] == (n - 1)) k = 0; 109 | else { 110 | j = sa[ar[i] + 1]; 111 | while ( ((i + k) < n) && ((j + k) < n) && 112 | (str[i + k] == str[j + k]) ) 113 | k++; 114 | } 115 | lcp[ar[i]] = k; 116 | } 117 | } 118 | 119 | void Generate() { 120 | n = strlen(str); 121 | int lim = 0; 122 | FOR(i, n) { 123 | ar[i] = str[i]; 124 | if (ar[i] > lim) lim = ar[i]; 125 | } 126 | ar[n] = ar[n + 1] = ar[n + 2] = 0; 127 | DC3(ar, sa, n, lim, 0); 128 | } 129 | 130 | } sa; 131 | 132 | int main() { 133 | scanf("%s", sa.str); 134 | sa.Generate(); 135 | int n = strlen(sa.str); 136 | FOR(i, n) { 137 | printf("%d\n", sa.sa[i]); 138 | } 139 | return 0; 140 | } 141 | 142 | // SOLVED 143 | /* SPOJ SARRAY - Suffix Array (100) 144 | /* SPOJ SUBST1 - New Distinct Substrings 145 | /* SPOJ SUBLEX - Lexicographical Substring Search */ 146 | // _SOLVED 147 | -------------------------------------------------------------------------------- /Flow/Dinic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct _dinic { 6 | struct edge_t { 7 | int u, v, flow, cap, prev; 8 | int def_flow, def_cap; 9 | //default flow and capacity (necessary for resetting flow) 10 | 11 | edge_t(int uu, int vv, int cc, int ff, int pp) { 12 | u = uu, v = vv, def_cap = cap = cc, def_flow = flow = ff, prev = pp; 13 | } 14 | }; 15 | 16 | const static int NMAX = 500; 17 | const static int NINF = 1000000000; 18 | vector edges; 19 | 20 | int last[NMAX + 10]; 21 | int lvl[NMAX + 10]; 22 | int ptr[NMAX + 10]; 23 | int cover[NMAX + 10]; 24 | 25 | void reset() { 26 | memset(last, -1, sizeof(last)); 27 | edges.clear(); 28 | } 29 | 30 | void reset_flow() { 31 | for (int i = 0; i < edges.size(); i++) { 32 | edges[i].flow = edges[i].def_flow; 33 | edges[i].cap = edges[i].def_cap; 34 | } 35 | } 36 | 37 | void add_edge(int u, int v, int cap) { 38 | edges.push_back(edge_t(u, v, cap, 0, last[u])); 39 | last[u] = edges.size() - 1; 40 | edges.push_back(edge_t(v, u, cap, cap, last[v])); 41 | last[v] = edges.size() - 1; 42 | } 43 | 44 | int bfs(int src, int sink) { 45 | memset(lvl, -1, sizeof(lvl)); 46 | queue que; 47 | que.push(src); 48 | lvl[src] = 0; 49 | while (!que.empty()) { 50 | int u = que.front(); 51 | que.pop(); 52 | for (int i = last[u]; i != -1; i = edges[i].prev) { 53 | int v = edges[i].v; 54 | int cap = edges[i].cap; 55 | int flow = edges[i].flow; 56 | if (lvl[v] == -1 && cap > flow) { 57 | lvl[v] = lvl[u] + 1; 58 | que.push(v); 59 | } 60 | } 61 | } 62 | return lvl[sink] != -1; 63 | } 64 | 65 | int dfs(int u, int sink, int res) { 66 | if (res == 0) return 0; 67 | if (u == sink) return res; 68 | for (int &idx = ptr[u]; idx != -1; idx = edges[idx].prev) { 69 | int v = edges[idx].v; 70 | int cap = edges[idx].cap; 71 | int flow = edges[idx].flow; 72 | if (lvl[v] == lvl[u] + 1 && cap > flow) { 73 | int pushed = dfs(v, sink, min(res, cap - flow)); 74 | if (pushed) { 75 | edges[idx].flow += pushed; 76 | edges[idx ^ 1].flow -= pushed; 77 | return pushed; 78 | } 79 | } 80 | } 81 | return 0; 82 | } 83 | 84 | int max_flow(int src, int sink) { 85 | reset_flow(); 86 | int ret = 0; 87 | while (bfs(src, sink)) { 88 | memcpy(ptr, last, sizeof(last)); 89 | while (int pushed = dfs(src, sink, NINF)) { 90 | ret += pushed; 91 | } 92 | } 93 | return ret; 94 | } 95 | 96 | //left and right side(excluding source and sink) 97 | vector vertex_cover 98 | (int src, int sink, int l_st, int l_ed, int r_st, int r_ed) { 99 | 100 | memset(cover, 0, sizeof(cover)); 101 | bfs(src, sink); 102 | for (int i = l_st; i <= l_ed; i++) { 103 | if (lvl[i] == -1) cover[i] = 1;; 104 | } 105 | for (int i = r_st; i <= r_ed; i++) { 106 | if (lvl[i] != -1) cover[i] = 1; 107 | } 108 | vector vec; 109 | for (int i = l_st; i <= l_ed; i++) { 110 | if (!cover[i] && !cover[r_st + (i - l_st)]) { 111 | vec.push_back(i); 112 | } 113 | } 114 | return vec; 115 | } 116 | 117 | } dinic; 118 | 119 | int A[500]; 120 | 121 | //Sample Problem SPOJ-DIVREL 122 | 123 | int main() { 124 | //freopen("input.txt", "r", stdin); 125 | dinic.reset(); 126 | int n; 127 | scanf("%d", &n); 128 | for (int i = 1; i <= n; i++) { 129 | scanf("%d", &A[i]); 130 | } 131 | sort(A + 1, A + n + 1); 132 | int j = 1; 133 | for (int i = 1; i <= n; i++) { 134 | if (A[i] != A[j]) { 135 | A[++j] = A[i]; 136 | } 137 | } 138 | n = j; 139 | for (int i = 1; i <= n; i++) { 140 | dinic.add_edge(0, i, 1); 141 | dinic.add_edge(n + i, n + n + 1, 1); 142 | } 143 | 144 | for (int i = 1; i <= n; i++) { 145 | for (int j = 1; j <= n; j++) { 146 | if (i != j && A[i] % A[j] == 0) { 147 | dinic.add_edge(i, n + j, 1); 148 | } 149 | } 150 | } 151 | int dt = dinic.max_flow(0, n + n + 1); 152 | vector vec = dinic.vertex_cover(0, n + n + 1, 1, n, n + 1, n + n); 153 | printf("%d\n", (int) vec.size()); 154 | for (int i = 0; i < vec.size(); i++) { 155 | int x = vec[i]; 156 | if (i != 0) printf(" "); 157 | printf("%d", A[x]); 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /Graph/Centroid Decomposition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | /* Template Begins */ 8 | 9 | #define FOR(i,N) FORR(i, 0, N) 10 | #define FORR(i,a,b) FOTR(i, a, b, 1) 11 | #define FOTR(i,a,b,c) for(int i=(a);i<(b);i+=(c)) 12 | #define FOREACH(it, x) for(__typeof__((x).begin()) it=(x).begin();it!=(x).end();it++) 13 | #define MEM(a,x) memset(a,x,sizeof(a)) 14 | #define BCHK(a,x) (((a)>>(x))&1) 15 | #define BSET(a,x) ((a)|(1<<(x))) 16 | #define BCLR(a,x) ((a)&(~(1<<(x)))) 17 | #define BTGL(a,x) ((a)^(1<<(x))) 18 | #define FMT(...) (sprintf(CRTBUFF, __VA_ARGS__)?CRTBUFF:0) 19 | #define read() freopen("input.txt","r",stdin) 20 | #define write() freopen("output.txt","w",stdout) 21 | #define cpp_io() {ios_base::sync_with_stdio(false);cin.tie(NULL);} 22 | #define BUFFSIZE 30000 23 | #define INF 1000000000 24 | #define MOD 1000000007 25 | #define MAX 200000 26 | #define pb push_back 27 | 28 | typedef long long ll; 29 | 30 | char CRTBUFF[BUFFSIZE]; 31 | 32 | #define dbg(args...) {string s(#args);s+=',';cout<<"-->";debugger::call(s.begin(), s.end(), args);cout<<"\n";} 33 | #define dbg_A(A, N) {cout<<#A<<"=(";FOR(i,N)cout< 38 | static void call(si it, si ed, T a, aT... rest){ 39 | string b; 40 | for(;*it!=',';++it) if(*it!=' ')b+=*it; 41 | cout << b << "=" << a << " "; 42 | call(++it, ed, rest...); 43 | } 44 | }; 45 | 46 | //LCA with Sparse Table 47 | struct LCA{ 48 | static const int NMAX = 200000; 49 | static const int WIDTH = 20; 50 | int F[NMAX+10], L[NMAX+10], D[NMAX+10], lst; 51 | int idx [NMAX+10][WIDTH+2]; 52 | vector *G; 53 | void build(vector *G, int u){ 54 | memset(F, -1, sizeof(F)); 55 | this->G=G; 56 | D[u]=lst=0; 57 | dfs(u); 58 | for(int h=0; h y) swap(x, y); 78 | int h = bitWidth(y - x); 79 | return argmin(idx[x][h], idx[y - (1< G[NMAX+10], NG[2][NMAX+10]; 94 | 95 | void addEdge(int u, int v){ 96 | G[u].push_back(v); 97 | G[v].push_back(u); 98 | } 99 | 100 | int dfsSUB(int u, int p){ 101 | SUB[u]=1; 102 | for(auto v: G[u]){ 103 | if(v!=p&&!REM[v]){ 104 | SUB[u]+=dfsSUB(v, u); 105 | } 106 | } 107 | return SUB[u]; 108 | } 109 | 110 | int findCentroid(int u, int p, int sz){ 111 | int mx=-1; 112 | for(auto v: G[u]){ 113 | if(v!=p&&!REM[v]){ 114 | if(mx==-1||SUB[v]>SUB[mx]) 115 | mx=v; 116 | } 117 | } 118 | if(mx==-1||SUB[mx]<=(sz/2)) return u; 119 | return findCentroid(mx, u, sz); 120 | } 121 | void decompose(int u){ 122 | root = decompose(u, u); 123 | } 124 | 125 | int decompose(int u, int p){ 126 | dfsSUB(u, u); 127 | int c=findCentroid(u, u, SUB[u]); 128 | REM[c]=true; 129 | for(auto v: G[c]){ 130 | if(!REM[v]){ 131 | int tmp=decompose(v, c); 132 | //NG[0][c].pb(tmp); 133 | P[tmp]=c; 134 | } 135 | } 136 | return c; 137 | } 138 | 139 | void add(int u){ 140 | int v=u; 141 | while(true){ 142 | A[v]=min(A[v], lca.dist(u, v)); 143 | if(v==root) break; 144 | v=P[v]; 145 | } 146 | } 147 | 148 | int ask(int u){ 149 | int ret=NINF, v=u; 150 | while(true){ 151 | ret=min(ret, lca.dist(u, v)+A[v]); 152 | if(v==root) break; 153 | v=P[v]; 154 | } 155 | return ret; 156 | } 157 | }cd; 158 | 159 | 160 | 161 | // Sample Problem: Codeforces Xenia and Tree 162 | int main() 163 | { 164 | //read(); 165 | cpp_io(); 166 | //begin 167 | int n, m; 168 | while(cin >> n >> m){ 169 | FOR(i, n-1){ 170 | int u, v; 171 | cin >> u >> v; 172 | u--, v--; 173 | cd.addEdge(u, v); 174 | } 175 | lca.build(cd.G, 0); 176 | cd.decompose(0); 177 | FOR(i, n) cd.A[i]=INF; 178 | cd.add(0); 179 | FOR(i, m){ 180 | int op, x; 181 | cin >> op >> x; 182 | x--; 183 | if(op==1){ 184 | cd.add(x); 185 | } 186 | else{ 187 | int ans=cd.ask(x); 188 | cout << ans << "\n"; 189 | } 190 | } 191 | } 192 | return 0; 193 | } 194 | -------------------------------------------------------------------------------- /Helper/VectorCompressor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct BitStream { 6 | const int MAX_CAP = 8; 7 | 8 | vector bits; 9 | int start; 10 | 11 | BitStream() : BitStream("") {} 12 | 13 | BitStream(string s) { 14 | start = 0; 15 | for (auto ch: s) { 16 | push(ch, 8); 17 | } 18 | } 19 | 20 | void push(int n, int bit_width) { 21 | for (int i = bit_width - 1; i >= 0; i--) { 22 | bits.push_back(get_nth_bit(n, i)); 23 | } 24 | } 25 | 26 | int pop(int bit_width) { 27 | int n = 0; 28 | for (int i = 0; i < bit_width; i++) { 29 | n = (n << 1) | bits[start]; 30 | start++; 31 | } 32 | return n; 33 | } 34 | 35 | int get_nth_bit(int n, int nth) { 36 | return (n >> nth) & 1; 37 | } 38 | 39 | string str() { 40 | string s; 41 | for(int i = 0; i < bits.size(); i++) { 42 | if (i % 8 == 0) { 43 | s.push_back(0); 44 | } 45 | s[s.size() - 1] <<= 1; 46 | s[s.size() - 1] |= bits[i]; 47 | } 48 | for(int i = bits.size(); i % 8 != 0; i++) { 49 | s[s.size() - 1] <<= 1; 50 | } 51 | return s; 52 | } 53 | }; 54 | 55 | struct Compressor { 56 | string dictionary; 57 | int max_window, max_length; 58 | int window_bitwidth, length_bitwidth, symbol_bitwidth; 59 | 60 | Compressor() {} 61 | 62 | Compressor(string dictionary, int max_window = 1023, int max_length = 255) { 63 | this->dictionary = dictionary; 64 | this->max_window = max_window; 65 | this->max_length = max_length; 66 | 67 | this->window_bitwidth = get_bits_for(max_window); 68 | this->length_bitwidth = get_bits_for(max_length); 69 | this->symbol_bitwidth = get_bits_for(dictionary.length()); 70 | } 71 | 72 | string compress(string original) { 73 | string s = original + dictionary[0]; 74 | 75 | BitStream bs; 76 | for(int i = 0; i < s.length(); i++) { 77 | int max_match = 0, diff = 0; 78 | 79 | for(int j = max(0, i - max_window); j < i; j++) { 80 | int match = 0; 81 | for(int x = i, y = j; x < s.length() && y < i && s[x] == s[y]; x++, y++) { 82 | match++; 83 | } 84 | if (max_match < match) { 85 | max_match = match; 86 | diff = (i - j); 87 | } 88 | } 89 | 90 | max_match = min(max_match, max_length); 91 | 92 | bs.push(diff, window_bitwidth); 93 | bs.push(max_match, length_bitwidth); 94 | bs.push(get_symbol_index(s[i+max_match]), symbol_bitwidth); 95 | 96 | i += max_match; 97 | } 98 | 99 | bs.push(0, window_bitwidth); 100 | bs.push(0, length_bitwidth); 101 | bs.push(0, symbol_bitwidth); 102 | 103 | return bs.str(); 104 | } 105 | 106 | string decompress(string compressed) { 107 | BitStream bs(compressed); 108 | 109 | string uncompressed; 110 | for(int i = 0; ; i++) { 111 | int diff = bs.pop(window_bitwidth); 112 | int match = bs.pop(length_bitwidth); 113 | int symbol_index = bs.pop(symbol_bitwidth); 114 | char symbol = dictionary[symbol_index]; 115 | 116 | for (int i = 0, startIndex = uncompressed.size() - diff; i < match; i++) { 117 | uncompressed.push_back(uncompressed[startIndex + i]); 118 | } 119 | 120 | if (symbol_index != 0) { 121 | uncompressed.push_back(symbol); 122 | } 123 | else { 124 | break; // Null symbol found 125 | } 126 | } 127 | return uncompressed; 128 | } 129 | 130 | int get_symbol_index(char symbol) { 131 | return find(dictionary.begin(), dictionary.end(), symbol) - dictionary.begin(); 132 | } 133 | 134 | int get_bits_for(int n) { 135 | return 32 - __builtin_clz(n); 136 | } 137 | }; 138 | 139 | struct VectorCompress { 140 | Compressor compressor; 141 | 142 | VectorCompress() { 143 | compressor = Compressor("$,0123456789"); 144 | } 145 | 146 | string compress(vector vec) { 147 | return compressor.compress(vec2string(vec)); 148 | } 149 | 150 | vector decompress(string compressed) { 151 | return string2vec(compressor.decompress(compressed)); 152 | } 153 | 154 | string vec2string(vector vec) { 155 | stringstream ss; 156 | for (auto x: vec) { 157 | ss << x << ","; 158 | } 159 | return ss.str(); 160 | } 161 | 162 | vector string2vec(string s) { 163 | vector v; 164 | int x = 0; 165 | for(auto ch: s) { 166 | if (ch == ',') { 167 | v.push_back(x); 168 | x = 0; 169 | } 170 | else { 171 | x = (x * 10) + (ch - '0'); 172 | } 173 | } 174 | return v; 175 | } 176 | 177 | }; 178 | 179 | /*********************************************************************************************************** 180 | EXAMPLE USAGE 181 | ***********************************************************************************************************/ 182 | 183 | vector sieve(int max_num) { 184 | auto is_prime = new bool[max_num + 1]; 185 | fill(is_prime, is_prime + max_num + 1, true); 186 | int lim = sqrt(max_num); 187 | for(int i = 2; i <= lim; i++) { 188 | if (is_prime[i]) { 189 | for (int j = i * i; j <= max_num; j += i) { 190 | is_prime[j] = false; 191 | } 192 | } 193 | } 194 | 195 | vector primes; 196 | for(int i = 2; i <= max_num; i++) { 197 | if (is_prime[i]) { 198 | primes.push_back(i); 199 | } 200 | } 201 | return primes; 202 | } 203 | 204 | int main() { 205 | vector primes = sieve(1000000); 206 | 207 | auto vc = VectorCompress(); 208 | 209 | string uncompressed = vc.vec2string(primes); 210 | string compressed = vc.compress(primes); 211 | 212 | cout << "uncompressed = " << uncompressed.length() << " " << "compressed = " << compressed.length() << "\n"; 213 | cout << "Ratio = " << (compressed.length() / double(uncompressed.length())) << "\n"; 214 | assert (primes == vc.decompress(compressed)); 215 | } -------------------------------------------------------------------------------- /Geometry/Geometry.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define sqr(x) ((x)*(x)) 6 | #define mp make_pair 7 | #define pb push_back 8 | 9 | const double eps = 1e-10; 10 | const double pi = acos(-1.0); 11 | const double inf = 1e20; 12 | const int maxp = 10; 13 | 14 | int dblcmp(double d) { 15 | if (fabs(d) < eps) return 0; 16 | return d > eps ? 1 : -1; 17 | } 18 | 19 | struct point { 20 | double x, y; 21 | point() {} 22 | point(double _x, double _y) : 23 | x(_x), y(_y) { 24 | }; 25 | bool operator==(point a)const { 26 | return dblcmp(a.x - x) == 0 && dblcmp(a.y - y) == 0; 27 | } 28 | bool operator<(point a)const { 29 | return dblcmp(a.x - x) == 0 ? dblcmp(y - a.y) < 0 : x < a.x; 30 | } 31 | double len() { 32 | return hypot(x, y); 33 | } 34 | double len2() { 35 | return x*x + y*y; 36 | } 37 | double distance(point p) { 38 | return hypot(x - p.x, y - p.y); 39 | } 40 | point add(point p) { 41 | return point(x + p.x, y + p.y); 42 | } 43 | point sub(point p) { 44 | return point(x - p.x, y - p.y); 45 | } 46 | point mul(double b) { 47 | return point(x*b, y*b); 48 | } 49 | point div(double b) { 50 | return point(x / b, y / b); 51 | } 52 | double dot(point p) { 53 | return x*p.x + y*p.y; 54 | } 55 | double det(point p) { 56 | return x*p.y - y*p.x; 57 | } 58 | double rad(point a, point b) { 59 | point p = *this; 60 | return fabs(atan2(fabs(a.sub(p).det(b.sub(p))), a.sub(p).dot(b.sub(p)))); 61 | } 62 | point trunc(double r) { 63 | double l = len(); 64 | if (!dblcmp(l))return *this; 65 | r /= l; 66 | return point(x*r, y*r); 67 | } 68 | point rotleft() { 69 | return point(-y, x); 70 | } 71 | point rotright() { 72 | return point(y, -x); 73 | } 74 | point rotate(point p, double angle) { // P counterclockwise rotation angle around the point angle 75 | point v = this->sub(p); 76 | double c = cos(angle), s = sin(angle); 77 | return point(p.x + v.x*c - v.y*s, p.y + v.x*s + v.y*c); 78 | } 79 | }; 80 | 81 | struct line { 82 | point a, b; 83 | line() {} 84 | line(point _a, point _b) { 85 | a = _a; 86 | b = _b; 87 | } 88 | double length() { 89 | return a.distance(b); 90 | } 91 | 92 | /* 93 | Points and line segments between 94 | 1 Counter clockwise 95 | 2 in a clockwise 96 | 3 parallel 97 | */ 98 | int relation(point p) { 99 | int c = dblcmp(p.sub(a).det(b.sub(a))); 100 | if (c < 0)return 1; 101 | if (c > 0)return 2; 102 | return 3; 103 | } 104 | bool parallel(line v) { 105 | return dblcmp(b.sub(a).det(v.b.sub(v.a))) == 0; 106 | } 107 | 108 | /* 0 Parallel 109 | 1 coincides 110 | 2 intersect */ 111 | int linecrossline(line v) { 112 | if ((*this).parallel(v)) { 113 | return v.relation(a) == 3; 114 | } 115 | return 2; 116 | } 117 | point crosspoint(line v) { 118 | double a1 = v.b.sub(v.a).det(a.sub(v.a)); 119 | double a2 = v.b.sub(v.a).det(b.sub(v.a)); 120 | return point((a.x*a2 - b.x*a1) / (a2 - a1), (a.y*a2 - b.y*a1) / (a2 - a1)); 121 | } 122 | double dispointtoline(point p) { 123 | return fabs(p.sub(a).det(b.sub(a))) / length(); 124 | } 125 | point lineprog(point p) { 126 | return a.add(b.sub(a).mul(b.sub(a).dot(p.sub(a)) / b.sub(a).len2())); 127 | } 128 | }; 129 | 130 | struct circle { 131 | point p; 132 | double r; 133 | circle() {} 134 | circle(point _p, double _r) : 135 | p(_p), r(_r) { 136 | }; 137 | circle(double x, double y, double _r) : 138 | p(point(x, y)), r(_r) { 139 | }; 140 | circle(point a, point b, point c){ //Circumcircle of triangle 141 | p = line(a.add(b).div(2),a.add(b).div(2).add(b.sub(a).rotleft())).crosspoint(line(c.add(b).div(2), c.add(b).div(2).add(b.sub(c).rotleft()))); 142 | r = p.distance(a); 143 | } 144 | double area() { 145 | return pi*sqr(r); 146 | } 147 | bool operator==(circle v) { 148 | return ((p == v.p) && dblcmp(r - v.r) == 0); 149 | } 150 | bool operator<(circle v)const { 151 | return ((p < v.p) || (p == v.p) && dblcmp(r - v.r) < 0); 152 | } 153 | int relation(point b) { 154 | double dst = b.distance(p); 155 | if (dblcmp(dst - r) < 0)return 2; 156 | if (dblcmp(dst - r) == 0)return 1; 157 | return 0; 158 | } 159 | int relationcircle(circle v) { 160 | double d = p.distance(v.p); 161 | if (dblcmp(d - r - v.r) > 0)return 5; 162 | if (dblcmp(d - r - v.r) == 0)return 4; 163 | double l = fabs(r - v.r); 164 | if (dblcmp(d - r - v.r) < 0 && dblcmp(d - l) > 0)return 3; 165 | if (dblcmp(d - l) == 0)return 2; 166 | if (dblcmp(d - l) < 0)return 1; 167 | } 168 | int relationline(line v) { 169 | double dst = v.dispointtoline(p); 170 | if (dblcmp(dst - r) < 0)return 2; 171 | if (dblcmp(dst - r) == 0)return 1; 172 | return 0; 173 | } 174 | int pointcrossline(line v, point &p1, point &p2){ 175 | if (!(*this).relationline(v))return 0; 176 | point a = v.lineprog(p); 177 | double d = v.dispointtoline(p); 178 | d = sqrt(r*r - d*d); 179 | if (dblcmp(d) == 0) { 180 | p1 = a; 181 | p2 = a; 182 | return 1; 183 | } 184 | p1 = a.sub(v.b.sub(v.a).trunc(d)); 185 | p2 = a.add(v.b.sub(v.a).trunc(d)); 186 | return 2; 187 | } 188 | double areacircle(circle v) { 189 | int rel = relationcircle(v); 190 | if (rel >= 4)return 0.0; 191 | if (rel <= 2)return min(area(), v.area()); 192 | double d = p.distance(v.p); 193 | double hf = (r + v.r + d) / 2.0; 194 | double ss = 2 * sqrt(hf*(hf - r)*(hf - v.r)*(hf - d)); 195 | double a1 = acos((r*r + d*d - v.r*v.r) / (2.0*r*d)); 196 | a1 = a1*r*r; 197 | double a2 = acos((v.r*v.r + d*d - r*r) / (2.0*v.r*d)); 198 | a2 = a2*v.r*v.r; 199 | return a1 + a2 - ss; 200 | } 201 | double areatriangle(point a, point b) { 202 | if (dblcmp(p.sub(a).det(p.sub(b)) == 0))return 0.0; 203 | point q[5]; 204 | int len = 0; 205 | q[len++] = a; 206 | line l(a, b); 207 | point p1, p2; 208 | if (pointcrossline(l, q[1], q[2]) == 2) { 209 | if (dblcmp(a.sub(q[1]).dot(b.sub(q[1]))) < 0)q[len++] = q[1]; 210 | if (dblcmp(a.sub(q[2]).dot(b.sub(q[2]))) < 0)q[len++] = q[2]; 211 | } 212 | q[len++] = b; 213 | if (len == 4 && (dblcmp(q[0].sub(q[1]).dot(q[2].sub(q[1]))) > 0))swap(q[1], q[2]); 214 | double res = 0; 215 | int i; 216 | for (i = 0; i < len - 1; i++) { 217 | if (relation(q[i]) == 0 || relation(q[i + 1]) == 0) { 218 | double arg = p.rad(q[i], q[i + 1]); 219 | res += r*r*arg / 2.0; 220 | } 221 | else { 222 | res += fabs(q[i].sub(p).det(q[i + 1].sub(p)) / 2.0); 223 | } 224 | } 225 | return res; 226 | } 227 | }; 228 | 229 | struct polygon { 230 | int n; 231 | point p[maxp]; 232 | bool isconvex() { 233 | bool s[3]; 234 | memset(s, 0, sizeof(s)); 235 | int i, j, k; 236 | for (i = 0; i < n; i++) { 237 | j = (i + 1) % n; 238 | k = (j + 1) % n; 239 | s[dblcmp(p[j].sub(p[i]).det(p[k].sub(p[i]))) + 1] = 1; 240 | if (s[0] && s[2])return 0; 241 | } 242 | return 1; 243 | } 244 | double getcircumference() { 245 | double sum = 0; 246 | int i; 247 | for (i = 0; i < n; i++) { 248 | sum += p[i].distance(p[(i + 1) % n]); 249 | } 250 | return sum; 251 | } 252 | double getarea() { 253 | double sum = 0; 254 | int i; 255 | for (i = 0; i < n; i++) { 256 | sum += p[i].det(p[(i + 1) % n]); 257 | } 258 | return fabs(sum) / 2; 259 | } 260 | 261 | double areacircle(circle c) { // area of intersection of polygon and circle 262 | int i, j, k, l, m; 263 | double ans = 0; 264 | for (i = 0; i < n; i++) { 265 | int j = (i + 1) % n; 266 | if (dblcmp(p[j].sub(c.p).det(p[i].sub(c.p))) >= 0) { 267 | ans += c.areatriangle(p[i], p[j]); 268 | } 269 | else { 270 | ans -= c.areatriangle(p[i], p[j]); 271 | } 272 | } 273 | return fabs(ans); 274 | } 275 | void find(int st, point tri[], circle &c) { 276 | if (!st) { 277 | c = circle(point(0, 0), -2); 278 | } 279 | if (st == 1) { 280 | c = circle(tri[0], 0); 281 | } 282 | if (st == 2) { 283 | c = circle(tri[0].add(tri[1]).div(2), tri[0].distance(tri[1]) / 2.0); 284 | } 285 | if (st == 3) { 286 | c = circle(tri[0], tri[1], tri[2]); 287 | } 288 | } 289 | void solve(int cur, int st, point tri[], circle &c) { 290 | find(st, tri, c); 291 | if (st == 3)return; 292 | int i; 293 | for (i = 0; i < cur; i++) { 294 | if (dblcmp(p[i].distance(c.p) - c.r) > 0) { 295 | tri[st] = p[i]; 296 | solve(i, st + 1, tri, c); 297 | } 298 | } 299 | } 300 | circle mincircle() { //Smallest circle covering point set 301 | random_shuffle(p, p + n); 302 | point tri[4]; 303 | circle c; 304 | solve(n, 0, tri, c); 305 | return c; 306 | } 307 | }; 308 | 309 | int main(){ 310 | return 0; 311 | } 312 | --------------------------------------------------------------------------------