0% found this document useful (0 votes)
22 views11 pages

Luon

template

Uploaded by

phamducluongwww
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views11 pages

Luon

template

Uploaded by

phamducluongwww
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

**BridgeAndJoint

void DFS(int u, int pre)


{
int child = 0;
num[u] = low[u] = ++Time;
for (int v : e[u])
{
if (v == pre) continue;
if (!num[v])
{
DFS(v, u);
low[u] = min(low[u], low[v]);
if (low[v] == num[v]) ++bridge;
++child;
if (!pre)
{
if (child > 1) joint[u] = true;
}
else if (low[v] >= num[u]) joint[u] = true;
}
else low[u] = min(low[u], num[v]);
}
}

**Default
#include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define F first
#define S second
#define all(p) p.begin(),p.end()
using namespace std;
const int mod = 1e9+7;
const int N = 1e5 + 5;

void file()
{
#define task ""
if (fopen(task ".inp", "r"))
{
freopen(task ".inp", "r", stdin);
freopen(task ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}

void Solve()
{

signed main()
{
file();
Solve();
return 0;
}
**EfficientAndEasySegmentTree
struct SegmentTree
{
vector<int> t;
void init(int _n)
{
t.resize(2*_n+5,0);
}
void build()
{
for (int i = n - 1; i > 0; --i) t[i] = t[i<<1] + t[i<<1|1];
}
void update(int p, int value)
{
for (t[p += n] = value; p > 1; p >>= 1) t[p>>1] = t[p] + t[p^1];
}
int query(int l, int r)
{ // sum on interval [l, r)
int res = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1)
{
if (l&1) res += t[l++];
if (r&1) res += t[--r];
}
return res;
}
}

**Geometric
struct Point
{
long double x, y;
Point() {}
Point(long double x, long double y): x(x), y(y) {}
Point operator + (const Point &a) const {return Point(x + a.x, y + a.y);}
Point operator - (const Point &a) const {return Point(x - a.x, y - a.y);}
long double operator * (const Point &a) const {return x*a.y-a.x*y;}
bool operator == (const Point &a) {return a.x==x && a.y==y;}
bool operator < (const Point &a)
{
if(x!=a.x) return x<a.x;
return y<a.y;
}
};
bool ccw(Point A, Point B, Point C)
{
return (B-A)*(C-B)>0;
}
void ConvexHull(vector<Point> &a)
{
sort(all(a));
vector<Point> hull;
hull.push_back(a.front());
for(int i=1; i<n; ++i)
{
while(hull.size()>1 and ccw(hull[hull.size()-2],hull.back(),a[i]))
hull.pop_back();
hull.push_back(a[i]);
}
for(int i=n-2; i>=0; --i)
{
while(hull.size()>1 and ccw(hull[hull.size()-2],hull.back(),a[i]))
hull.pop_back();
hull.push_back(a[i]);
}
if(n>1) hull.pop_back();
}
Point findIntersection(double a1, double b1, double c1, double a2, double b2,
double c2)
{
long double determinant = a1 * b2 - a2 * b1;
if (abs(determinant) < 1e-9) return Point(-1e18,-1e18);
long double x = (b1 * c2 - b2 * c1) / determinant;
long double y = (a2 * c1 - a1 * c2) / determinant;
return Point(x, y);
}
Point rotate_point(Point A, Point C, long double theta) //quay A quanh C mot goc
theta
{
A = A - C;
long double x = A.x * cos(theta) - A.y * sin(theta);
long double y = A.x * sin(theta) + A.y * cos(theta);
return Point(x + C.x, y + C.y);
}

**HLD
void DFS(int u)
{
cnt[u]=1;
for(int v: e[u])
{
if(v==par[u]) continue;
par[v]=u;
DFS(v);
cnt[u]+=cnt[v];
}
}
void HLD(int u)
{
if(head[id]==0) head[id]=u;
Index[u]=id;
a[u]=pos++;
int nxt=0;
for(int v: e[u]) if(v!=par[u] and cnt[v]>cnt[nxt]) nxt=v;
if(nxt) HLD(nxt);
for(int v: e[u])
{
if(v==par[u] or v==nxt) continue;
++id;
HLD(v);
}
}
int lca(int u, int v)
{
while(Index[u]!=Index[v])
{
if(Index[u]>Index[v]) u=par[head[Index[u]]];
else v=par[head[Index[v]]];
}
if(a[u]<a[v]) return u;
return v;
}
int query(int u, int v)
{
int p=lca(u,v), res=0;
while(Index[u]!=Index[p])
{
res+=st.get(1,1,n,a[head[Index[u]]],a[u]);
u=par[head[Index[u]]];
}
while(Index[v]!=Index[p])
{
res+=st.get(1,1,n,a[head[Index[v]]],a[v]);
v=par[head[Index[v]]];
}
if(a[u]<a[v]) res+=st.get(1,1,n,a[u],a[v]);
else res+=st.get(1,1,n,a[v],a[u]);
return res;
}

**KMP
void KMP(string s)
{
for (int i = 1; i < n; i++)
{
int j = pi[i - 1];
while (j > 0 && s[i] != s[j]) j = pi[j - 1];
if (s[i] == s[j]) ++j;
pi[i] = j;
}
}

**LazyUpdate
struct SegmentTree
{
struct node
{
int val, lazy=2e9;
node() {}
node(int val, int lazy): val(val), lazy(lazy) {}
node operator = (const node &A)
{
this->val=A.val;
this->lazy=A.lazy;
return *this;
}
node operator + (const node &A) const
{
return node(val+A.val,2e9);
}
};
vector<node> tree;
void init(int _n)
{
tree.resize(4*_n+5);
}
void Build(int id, int l, int r)
{
if(l>r) return;
if(l==r)
{
tree[id].val=a[l];
return;
}
int mid=(l+r)>>1;
Build(id<<1,l,mid);
Build(id<<1|1,mid+1,r);
tree[id].val=tree[id<<1].val+tree[id<<1|1].val;
}
void down(int id, int l, int r)
{
if(tree[id].lazy==2e9) return;
tree[id].val=(r-l+1)*tree[id].lazy;
if(l!=r)
{
tree[id<<1].lazy=tree[id].lazy;
tree[id<<1|1].lazy=tree[id].lazy;
}
tree[id].lazy=2e9;
}
void update(int id, int l, int r, int u, int v, int val)
{
down(id,l,r);
if(v<l or r<u) return;
if(u<=l and r<=v)
{
tree[id].lazy=val;
down(id,l,r);
return;
}
int mid=(l+r)>>1;
update(id<<1,l,mid,u,v,val);
update(id<<1|1,mid+1,r,u,v,val);
tree[id].val=tree[id<<1].val+tree[id<<1|1].val;
}
node get(int id, int l, int r, int u, int v)
{
if(v<l or r<u) return node(0,0);
down(id,l,r);
if(u<=l and r<=v) return tree[id];
int mid=(l+r)>>1;
return get(id<<1,l,mid,u,v)+get(id<<1|1,mid+1,r,u,v);
}
};

**Lca
void DFS(int u)
{
for(int v : e[u])
{
if(v == up[u][0]) continue;
h[v] = h[u] + 1;
up[v][0] = u;
for(int j = 1; j < 20; ++j) up[v][j] = up[up[v][j - 1]][j - 1];
DFS(v);
}
}
int lca(int u, int v)
{
if(h[u] != h[v])
{
if(h[u] < h[v]) swap(u, v);
int k = h[u] - h[v];
for(int j = 0; (1 << j) <= k; ++j) if(k >> j & 1) u = up[u][j];
}
if(u == v) return u;
int k = __lg(h[u]);
for(int j = k; j >= 0; --j) if(up[u][j] != up[v][j]) {u = up[u][j]; v = up[v]
[j];}
return up[u][0];
}

**Manacher
string convertToNewString(const string &s)
{
string newString = "@";
for (int i = 0; i < s.size(); i++) newString += "#" + s.substr(i, 1);
newString += "#$";
return newString;
}

string Manacher(const string &s)


{
string Q = convertToNewString(s);
int c = 0, r = 0;
for (int i = 1; i < Q.size() - 1; i++) {
int iMirror = c - (i - c);
if(r > i) P[i] = min(r - i, P[iMirror]);
while (Q[i + 1 + P[i]] == Q[i - 1 - P[i]]) P[i]++;
if (i + P[i] > r)
{
c = i;
r = i + P[i];
}
}
int maxPalindrome = 0;
int centerIndex = 0;
for (int i = 1; i < Q.size() - 1; i++)
{
if (P[i] > maxPalindrome) {
maxPalindrome = P[i];
centerIndex = i;
}
}
cout << maxPalindrome << "\n";
return s.substr( (centerIndex - 1 - maxPalindrome) / 2, maxPalindrome);
}

**Tarjan
void DFS(int u)
{
num[u] = low[u] = ++Time;
st.push(u);
for (int v : e[u])
{
if (deleted[v]) continue;
if (!num[v])
{
dfs(v);
low[u] = min(low[u], low[v]);
}
else low[u] = min(low[u], num[v]);
}
if (low[u] == num[u])
{
++scc;
int v;
do
{
v = st.top();
st.pop();
deleted[v] = true;
}
while (v != u);
}
}

**Trie
struct Trie
{
struct node
{
node *child[26];
int en, cnt;
node()
{
for(int i=0; i<26; ++i) child[i]=NULL;
en=cnt=0;
}
};
node *root=new node();
void Add(string s)
{
node *p = root;
for(auto i: s)
{
int c=i-'a';
if(p->child[c]==NULL) p->child[c]=new node();
p=p->child[c];
++p->cnt;
}
++p->en;
}
bool del(node *p, string &s, int i)
{
if(i!=(int)s.size())
{
int c=s[i]-'a';
bool ok =del(p->child[c],s,i+1);
if(ok) p->child[c] = NULL;
}
else --p->en;
if(p!=root)
{
--p->cnt;
if(!p->cnt)
{
delete(p);
return true;
}
}
return false;
}
void Del(string s)
{
if (!Find(s)) return;
del(root,s,0);
}
bool Find(string s)
{
node *p=root;
for (auto i : s)
{
int c=i-'a';
if(p->child[c]==NULL) return false;
p=p->child[c];
}
return p->en;
}
};

**TrinhCham
#include <bits/stdc++.h>
#define int long long
using namespace std;

// Tên chương trình


const string NAME = "Code";
// Số test kiểm tra
const int NTEST = 1000;

// Sử dụng mt19937 để sinh số ngẫu nhiên


mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());

// Hàm sinh ngẫu nhiên số trong phạm vi long long, số sinh ra >= l và <= h
long long Rand(long long l, long long h) {
return rd() % (h - l + 1) + l;
}

signed main() {
// Sinh test cho 1000 lần kiểm tra
for (int iTest = 1; iTest <= NTEST; iTest++) {
ofstream inp((NAME + ".inp").c_str()); // Tạo file input
set<pair<int, int>> edges; // Set để lưu các cạnh và tránh sinh cạnh trùng

// Sinh ngẫu nhiên số đỉnh và số cạnh


int n = Rand(1,5), m = Rand(2,5), q =10;
inp << n << ' ' << m <<'\n';
for(int i = 1; i <= m; i++)
{
inp << Rand(0,n-1) <<' '<<Rand(1,5) <<'\n';
}
inp.close(); // Đóng file input

// Nếu dùng Linux thì "./" + Tên chương trình, với Windows không cần "./"
system((NAME + "_cham.exe").c_str()); // Chạy chương trình chuẩn
system((NAME + "_trau.exe").c_str()); // Chạy chương trình đối chứng

// So sánh kết quả (Windows dùng fc, Linux dùng diff)


// Thay đổi tùy thuộc hệ điều hành bạn sử dụng
#ifdef _WIN32
if (system(("fc " + NAME + ".out " + NAME + ".ans").c_str()) != 0) {
cout << "Test " << iTest << ": WRONG!\n";
return 0;
}
#else
if (system(("diff " + NAME + ".out " + NAME + ".ans").c_str()) != 0) {
cout << "Test " << iTest << ": WRONG!\n";
return 0;
}
#endif

cout << "Test " << iTest << ": CORRECT!\n"; // Nếu đúng in ra CORRECT
}

return 0;
}

**ConvexHullTrick
-dp[i]=max(dp[j]+(h[i]-h[j])*(h[i]-h[j])+C)
struct line
{
int a, b, p;
int value(int x) {return a*x+b;}
};
deque<line> hull;
int division(int a, int b)
{
return a/b-((a<0)!=(b<0) and a%b);
}
bool isect(line &x, const line &y)
{
if(x.a==y.a) x.p=x.a>y.a? 1e18: -1e18;
else x.p=division(y.b-x.b,x.a-y.a);
return x.p>=y.p;
}
void add(int a, int b)
{
line l={a,b,0};
while(hull.sz>1 and (isect(l,hull.back()),isect(hull.back(),hull[hull.sz-
2]),l.p<hull.back().p)) hull.pop_back();
hull.pb(l);
}
int query(int x)
{
while(hull.sz>1 and hull[0].value(x)>=hull[1].value(x)) hull.pop_front();
return hull[0].value(x);
}
void ins(int i)
{
add(-2*h[i],dp[i]+h[i]*h[i]);
}
void Solve()
{
cin>>n>>c;
For(i,1,n) cin>>h[i];
ins(1);
For(i,2,n)
{
int tmp=query(h[i]);
dp[i]=c+h[i]*h[i]+tmp;
ins(i);
}
cout<<dp[n];
}

****Mo
struct query{
// t là số lượng truy vấn cập nhật đứng trước truy vấn này
int l, r, t;
int id;
}

bool cmp(const query &a, const query &b){


if(a.l / S != b.l / S)
return a.l < b.l;
else if(a.r / S != b.r / S){
if((a.l / S) % 2 == 1)
return a.r < b.r;
else
return a.r > b.r
}
return a.t < b.t;
}
for(query &q : queries){
// Truy vấn cập nhật
while(t < q.t) t++, run_update(update_queries[t].index,
update_queries[t].next);

// Đảo ngược truy vấn cập nhật


while(t > q.t) run_update(update_queries[t].index, update_queries[t].old),
t--;

while (l > q.l) sum += a[--l];


while (r < q.r) sum += a[++r];
while (l < q.l) sum -= a[l++];
while (r > q.r) sum -= a[r--];

ans[q.id] = sum;
}

****SPFA
bool spfa() {
for(int i = 1 ; i <= N ; i++) {
Dist[i] = Inf;
Cnt[i] = 0;
inqueue[i] = false;
}
Dist[S] = 0;
q.push(S);
inqueue[S] = true;
while(!q.empty()) {
int u = q.front();
q.pop();
inqueue[u] = false;

for (ii tmp: AdjList[u]) {


int v = tmp.first;
int w = tmp.second;

if (Dist[u] + w < Dist[v]) {


Dist[v] = Dist[u] + w;
if (!inqueue[v]) {
q.push(v);
inqueue[v] = true;
Cnt[v]++;
if (Cnt[v] > N)
return false; // Chu trình âm
}
}
}
}
return true;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy