-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from skku-npc/user/Jaemin
Reformatting Style
- Loading branch information
Showing
9 changed files
with
168 additions
and
195 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,58 @@ | ||
struct convexhull_trick | ||
{ | ||
//일단 binary search와 linear둘다 구현해둠 | ||
struct line | ||
{ | ||
long long a, b; | ||
pair<long long, long long> start_x;// first/second | ||
long long y(long long x){return a*x + b;} | ||
static bool compare_x(line& l, const line& l1, const line& l2)//intersect_x(l, l1) < intersect_x(l, l2) | ||
{ | ||
long long t1 = (l1.b - l.b) * (l.a - l2.a); | ||
long long t2 = (l2.b - l.b) * (l.a - l1.a); | ||
typedef long long ll; | ||
struct convexhull_trick { | ||
//일단 binary search와 linear 둘 다 구현해둠 | ||
struct line { | ||
ll a, b; | ||
pair<ll, ll> start_x; // first/second | ||
ll y(ll x) { return a * x + b; } | ||
static bool compare_x(line &l, const line &l1, const line &l2) { | ||
// intersect_x(l, l1) < intersect_x(l, l2) | ||
ll t1 = (l1.b - l.b) * (l.a - l2.a); | ||
ll t2 = (l2.b - l.b) * (l.a - l1.a); | ||
bool op = (l.a - l2.a < 0) ^ (l.a - l1.a < 0); | ||
return op? t1>t2:t1<t2; | ||
return op ? t1 > t2 : t1 < t2; | ||
} | ||
bool not_start(long long x)//start_x > x | ||
{ | ||
if (start_x.second < 0) return start_x.first < x*start_x.second; | ||
bool not_start(ll x) { // start_x > x | ||
if (start_x.second < 0) | ||
return start_x.first < x * start_x.second; | ||
return start_x.first > x * start_x.second; | ||
} | ||
}; | ||
vector<line> stk; | ||
int qptr; | ||
convexhull_trick(int capacity = 100'010) : qptr(0) | ||
{ | ||
convexhull_trick(int capacity = 100'010) : qptr(0) { | ||
stk.reserve(capacity); | ||
} | ||
void init(int capacity = 100'010) | ||
{ | ||
void init(int capacity = 100'010) { | ||
stk.clear(); | ||
stk.reserve(capacity); | ||
qptr = 0; | ||
} | ||
void push_line(line l) | ||
{ | ||
while(stk.size() > 1) | ||
{ | ||
line& l1 = stk[stk.size() - 1]; | ||
line& l2 = stk[stk.size() - 2]; | ||
void push_line(line l) { | ||
while (stk.size() > 1) { | ||
line &l1 = stk[stk.size() - 1]; | ||
line &l2 = stk[stk.size() - 2]; | ||
if (line::compare_x(l, l2, l1)) break; | ||
stk.pop_back(); | ||
} | ||
if (qptr >= stk.size()) qptr = stk.size() - 1; | ||
if(!stk.empty()) l.start_x = make_pair(stk.back().b - l.b, l.a - stk.back().a); | ||
if (qptr >= stk.size()) | ||
qptr = stk.size() - 1; | ||
if (!stk.empty()) l.start_x = make_pair(stk.back().b - l.b, l.a - stk.back().a); | ||
else l.start_x = make_pair(0LL, 0LL); | ||
stk.push_back(l); | ||
} | ||
void push_line(long long a, long long b) | ||
{ | ||
push_line({a, b}); | ||
} | ||
long long query_bs(long long x) | ||
{ | ||
void push_line(ll a, ll b) { push_line({a, b}); } | ||
ll query_bs(ll x) { | ||
int lf = 0, rg = stk.size() - 1; | ||
while(lf < rg) | ||
{ | ||
while (lf < rg) { | ||
int mid = (lf + rg + 1) / 2; | ||
if (stk[mid].not_start(x)) | ||
{ | ||
rg = mid - 1; | ||
}else lf = mid; | ||
if (stk[mid].not_start(x)) rg = mid - 1; | ||
else lf = mid; | ||
} | ||
return stk[lf].y(x); | ||
} | ||
long long query_linear(long long x)//x값이 증가하는 순서대로 들어와야함 | ||
{ | ||
while(qptr + 1 < stk.size() && !stk[qptr+1].not_start(x)) | ||
++qptr; | ||
ll query_linear(ll x) { // x값이 증가하는 순서대로 들어와야함 | ||
while (qptr + 1 < stk.size() && !stk[qptr + 1].not_start(x)) ++qptr; | ||
return stk[qptr].y(x); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
int phi(int n) | ||
{ | ||
int phi(int n) { | ||
int ret = n; | ||
for (int i=2;i*i<=n;++i) | ||
{ | ||
if (n%i==0) | ||
{ | ||
while(n%i==0) n/=i; | ||
ret -= ret/i; | ||
for (int i = 2; i * i <= n; ++i) { | ||
if (n % i == 0) { | ||
while (n % i == 0) | ||
n /= i; | ||
ret -= ret / i; | ||
} | ||
} | ||
if (n>1) ret -= ret/n; | ||
if (n > 1) ret -= ret / n; | ||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,23 @@ | ||
//문자열 길이 >= 1 | ||
void get_fail(const char* p, int len, vector<int>& fail)//xxxa..xxx f[end of x] => a, //0based | ||
{ | ||
// 문자열 길이 >= 1, xxxa..xxx fail[end of x] => a | ||
// 0-based | ||
void get_fail(const char* p, int len, vector<int>& fail) { | ||
fail.resize(len); | ||
fail[0] = 0; | ||
for (int i = 1, j = 0; i < len; ++i) | ||
{ | ||
while(j && p[i] != p[j]) j = fail[j-1]; | ||
for (int i = 1, j = 0; i < len; ++i) { | ||
while (j && p[i] != p[j]) j = fail[j - 1]; | ||
if (p[i] == p[j]) fail[i] = ++j; | ||
} | ||
} | ||
void KMP(const char* text, int tlen, const char* pattern, int plen, vector<int>& fail, vector<int>& ans) //0based | ||
{ | ||
void KMP(const char* text, int tlen, const char* pattern, int plen, vector<int>& fail, vector<int>& ans) { | ||
ans.clear(); | ||
for (int i = 0, j = 0; i < tlen; ++i) | ||
{ | ||
while(j && text[i] != pattern[j]) j = fail[j-1]; | ||
if (text[i] == pattern[j]) | ||
{ | ||
if (j == plen - 1) | ||
{ | ||
for (int i = 0, j = 0; i < tlen; ++i) { | ||
while (j && text[i] != pattern[j]) j = fail[j - 1]; | ||
if (text[i] == pattern[j]) { | ||
if (j == plen - 1) { | ||
ans.push_back(i - j); | ||
j = fail[j]; | ||
} else ++j; | ||
} else | ||
++j; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
void manachers(const char* str, int len, vector<int>& A) // 사용 전 #을 앞뒤로 꼼꼼히 추가하는 것을 잊지 말자 | ||
{ | ||
A.resize(len); | ||
void manacher(const char* str, int len, vector<int>& a) { // 사용 전 문자 앞뒤로 # 추가하기 | ||
a.resize(len); | ||
int p = 0; | ||
for (int i = 0; i < len; ++i) | ||
{ | ||
if (i <= p + A[p]) | ||
A[i] = min(A[2*p - i], p + A[p] - i); // 점 p를 기준으로 대칭 | ||
else A[i] = 0; | ||
while(i - A[i] - 1 >= 0 && i + A[i] + 1 < len && str[i - A[i] - 1] == str[i + A[i] + 1]) // 확장 | ||
++A[i]; | ||
if(p + A[p] < i + A[i]) p = i; //p = 최대 커버 | ||
for (int i = 0; i < len; ++i) { | ||
if (i <= p + a[p]) | ||
a[i] = min(a[2 * p - i], p + a[p] - i); // 점 p를 기준으로 대칭 | ||
else | ||
a[i] = 0; | ||
while (i - a[i] - 1 >= 0 && i + a[i] + 1 < len && str[i - a[i] - 1] == str[i + a[i] + 1]) // 확장 | ||
++a[i]; | ||
if (p + a[p] < i + a[i]) p = i; // p = 최대 커버 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct SegTree { | ||
vector<int> tree, lazy; | ||
int base; | ||
|
||
SegTree(int a) { | ||
base = 1; | ||
while (base < a) base <<= 1; | ||
tree.resize((base + 1) << 1); | ||
lazy.resize((base + 1) << 1); // FOR LAZY-PROPAGATION | ||
base--; | ||
} | ||
|
||
void update(int idx, int val) { | ||
idx += base; | ||
tree[idx] = val; | ||
idx >>= 1; | ||
while (idx != 0) { | ||
tree[idx] = tree[idx * 2] + tree[idx * 2 + 1]; | ||
idx >>= 1; | ||
} | ||
} | ||
|
||
int query(int l, int r, int s = 1, int e = -1, int node = 1) { | ||
if (e == -1) e = base + 1; | ||
propagate(s, e, node); // FOR LAZY-PROPAGATION | ||
if (l > e || r < s) return 0; | ||
if (l <= s && e <= r) return tree[node]; | ||
return query(l, r, s, (s + e) / 2, node * 2) + | ||
query(l, r, (s + e) / 2 + 1, e, node * 2 + 1); | ||
} | ||
|
||
////////// UNDERNEATH HERE IS FOR LAZY-PROPAGATION ////////// | ||
|
||
void propagate(int s, int e, int node) { | ||
if (lazy[node] == 0) return; | ||
if (s < e) { | ||
lazy[node * 2] += lazy[node]; | ||
lazy[node * 2 + 1] += lazy[node]; | ||
} | ||
tree[node] += lazy[node] * (e - s + 1); | ||
lazy[node] = 0; | ||
} | ||
|
||
void add_val(int val, int l, int r, int s = 1, int e = -1, int node = 1) { | ||
if (e == -1) e = base + 1; | ||
propagate(s, e, node); | ||
if (l > e || r < s) return; | ||
if (l <= s && e <= r) { | ||
lazy[node] += val; | ||
propagate(s, e, node); | ||
} else { | ||
add_val(val, l, r, s, (s + e) / 2, node * 2); | ||
add_val(val, l, r, (s + e) / 2 + 1, e, node * 2 + 1); | ||
tree[node] = tree[node * 2] + tree[node * 2 + 1]; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,37 @@ | ||
//0-based | ||
int conv(char c){ | ||
return c - 'A'; | ||
} | ||
void get_sa(const char* s, int n, vector<int>& sa){ | ||
// 0-based, Capitalized | ||
#define forn(i, n) for(int i = 0; i < n; i++) | ||
int conv(char c) { return c - 'A'; } | ||
void get_sa(const char *s, int n, vector<int> &sa) { | ||
sa.resize(n); | ||
int m = 26;//문자의 갯수 | ||
vector<int> cnt(max(n, m), 0), x(n, 0), y(n, 0); | ||
forn(i, n) | ||
cnt[x[i] = conv(s[i])]++; | ||
forn(i, m-1) cnt[i+1] += cnt[i]; | ||
for(int i=n-1;i>=0;--i) sa[--cnt[x[i]]] = i; | ||
for (int len = 1, p = 0; p + 1 < n; len<<=1, m = p+1){ | ||
constexpr int m = 26; // 문자의 갯수 | ||
vector<int> cnt(max(n, m)), x(n), y(n); | ||
forn(i, n) cnt[x[i] = conv(s[i])]++; | ||
forn(i, m - 1) cnt[i + 1] += cnt[i]; | ||
for (int i = n - 1; i >= 0; --i) | ||
sa[--cnt[x[i]]] = i; | ||
for (int len = 1, p = 0; p + 1 < n; len <<= 1, m = p + 1) { | ||
p = 0; | ||
for (int i = n - len;i<n;++i) y[p++] = i; | ||
for (int i = n - len; i < n; ++i) y[p++] = i; | ||
forn(i, n) if (sa[i] >= len) y[p++] = sa[i] - len; | ||
|
||
forn(i, m) cnt[i] = 0; | ||
forn(i, n) cnt[x[i]]++; | ||
forn(i, m-1) cnt[i+1] += cnt[i]; | ||
for (int i=n-1;i>=0;--i) sa[--cnt[x[y[i]]]] = y[i]; | ||
y = x; p = 0; x[sa[0]] = 0; | ||
forn(i, n-1) | ||
x[sa[i+1]] = sa[i]+len<n && sa[i+1]+len<n && y[sa[i]] == y[sa[i+1]] && y[sa[i]+len] == y[sa[i+1]+len]?p:++p; | ||
forn(i, m - 1) cnt[i + 1] += cnt[i]; | ||
for (int i = n - 1; i >= 0; --i) sa[--cnt[x[y[i]]]] = y[i]; | ||
y = x; | ||
p = 0; | ||
x[sa[0]] = 0; | ||
forn(i, n - 1) | ||
x[sa[i + 1]] = sa[i] + len < n && sa[i + 1] + len < n && y[sa[i]] == y[sa[i + 1]] && y[sa[i] + len] == y[sa[i + 1] + len] ? p : ++p; | ||
} | ||
|
||
} | ||
void get_lcp(const char* s, int n, vector<int>& sa, vector<int>& lcp){ | ||
void get_lcp(const char *s, int n, vector<int> &sa, vector<int> &lcp) { | ||
lcp.resize(n); | ||
vector<int> rank(sa.size()); //상황에 따라 rank도 인자로 받아야 함 | ||
forn(i, n) rank[sa[i]]=i; | ||
forn(i, n) rank[sa[i]] = i; | ||
int k = 0, j; | ||
for(int i=0;i<n;lcp[rank[i++]]=k){ | ||
if (rank[i]-1>=0) | ||
for(k?k--:0, j=sa[rank[i]-1];s[i+k]==s[j+k];++k); | ||
for (int i = 0; i < n; lcp[rank[i++]] = k) { | ||
if (rank[i] - 1 >= 0) | ||
for (k ? k-- : 0, j = sa[rank[i] - 1]; s[i + k] == s[j + k]; ++k); | ||
} | ||
} |
Oops, something went wrong.