Luon
Luon
**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;
}
**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;
// 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
// 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
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;
}
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;