模板

[置顶] 缺省源

#include<bits/stdc++.h>
//#define int long long
#define Debug puts("Oops!")
using namespace std;

const int N = 1e5 + 5, M = 5e5 + 5;

inline int read() {
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)) {if(c == '-') f = -1; c = getchar();}
	while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
	return x * f;
}

signed main() {
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);

	return 0;
}

算法

二分

整数域:

int binary(int L, int R) {
	int l = L, r = R, res;
	while(l <= r) {
		int mid = (l + r) >> 1;
		if(check(mid)) l = mid + 1, res = mid;
		else r = mid - 1;
	}
   return res;
}

实数域:

const double eps = 1e-6;

int binary(int L, int R) {
	int l = L, r = R, res;
	while(r - l >= eps) {
		int mid = (l + r) / 2;
		if(check(mid)) l = mid, res = mid;
		else r = mid;
	}
	return res;
}

三分

整数域:

int trisection(int L, int R) {
	int l = L, r = R;
	while(l <= r) {
		int p = l + (r - l) / 3, q = r - (r - l) / 3;
		int w1 = calc(p), w2 = calc(q);
		if(w1 < w2) r = q - 1;
		else l = p + 1;
	}
	return min(calc(l), calc(r));
} 

实数域:

const double eps = 1e-6;

int trisection(int L, int R) {
	int l = L, r = R, res;
	while(r - l >= eps) {
    	int p = l + (r - l) / 3, q = r - (r - l) / 3;
    	int w1 = calc(p), w2 = calc(q);
    	if(w1 < w2) r = q;
    	else l = p;
	}
	return min(calc(l), calc(r));
}

数据结构

树状数组

struct Binary_Indexed_Tree {
	int t[N];
	int lowbit(int x) {return x & -x;}
	void add(int x, int c) {for(; x <= n; x += lowbit(x)) t[x] += c;}
	int ask(int x) {int res = 0; for(; x; x -= lowbit(x)) res += t[x]; return res;}
}bit;

线段树

const int MOD = 998244353;
struct Node {
	int l, r, s;
	int lazys, lazym;
	#define l(p) p << 1
	#define r(p) p << 1 | 1
};

struct Segment_tree {
	Node t[N << 2];
	
	void pushup(int p) {
		t[p].s = (t[l(p)].s + t[r(p)].s) % MOD;
	}
	
	void pushdown(int p) {
		(t[l(p)].s = t[l(p)].s * t[p].lazym % MOD + t[p].lazys * (t[l(p)].r - t[l(p)].l + 1)) %= MOD;
		(t[l(p)].lazym *= t[p].lazym) %= MOD, (t[l(p)].lazys *= t[p].lazym) %= MOD;
		(t[l(p)].lazys += t[p].lazys) %= MOD;
		(t[r(p)].s = t[r(p)].s * t[p].lazym % MOD + t[p].lazys * (t[r(p)].r - t[r(p)].l + 1)) %= MOD;
		(t[r(p)].lazym *= t[p].lazym) %= MOD, (t[r(p)].lazys *= t[p].lazym) %= MOD;
		(t[r(p)].lazys += t[p].lazys) %= MOD;
		t[p].lazym = 1, t[p].lazys = 0;
	}
	
	void build(int p, int l, int r) {
		t[p].l = l, t[p].r = r;
		t[p].lazym = 1;
		if(l == r) {
			t[p].s = a[l];
			return ;
		}
		int mid = l + r >> 1;
		build(l(p), l, mid);
		build(r(p), mid + 1, r);
		pushup(p);
	}
	
	void modify_add(int p, int l, int r, int s) {
		if(l <= t[p].l && t[p].r <= r) {
			t[p].s += (t[p].r - t[p].l + 1) * s, t[p].s %= MOD;
			t[p].lazys += s, t[p].lazys %= MOD;
			return ;
		}
		pushdown(p);
		int mid = t[p].l + t[p].r >> 1;
		if(l <= mid) modify_add(l(p), l, r, s);
		if(r > mid) modify_add(r(p), l, r, s);
		pushup(p);
	}
	
	void modify_mul(int p, int l, int r, int s) {
		if(l <= t[p].l && t[p].r <= r) {
			t[p].s *= s, t[p].s %= MOD;
			(t[p].lazys *= s) %= MOD, (t[p].lazym *= s) %= MOD;
			return ;
		}
		pushdown(p);
		int mid = t[p].l + t[p].r >> 1;
		if(l <= mid) modify_mul(l(p), l, r, s);
		if(r > mid) modify_mul(r(p), l, r, s);
		pushup(p);
	}
	
	int query(int p, int l, int r) {
		if(l <= t[p].l && t[p].r <= r) return t[p].s;
		pushdown(p);
		int tmp = 0, mid = t[p].l + t[p].r >> 1;
		if(l <= mid) tmp += query(l(p), l, r), tmp %= MOD;
		if(r > mid) tmp += query(r(p), l, r), tmp %= MOD;
		return tmp;
	}
}st;

平衡树

FHQ Treap

struct Node {
	int sz, ls, rs;
	int val, pri;
};

struct Treap{
	int root, cnt;
	Node t[N];
	void pushup(int p) { 
		t[p].sz = t[t[p].ls].sz + t[t[p].rs].sz + 1; return ; 
	}
	void newnode(int x) {
		cnt++;
		t[cnt].sz = 1;
		t[cnt].val = x;
		t[cnt].ls = t[cnt].rs = 0;
		t[cnt].pri = rand();
	}
	void split(int p, int x, int &L, int &R) {
		if(p == 0) {L = R = 0; return ;}
		if(x >= t[p].val) {
			L = p;
			split(t[p].rs, x, t[p].rs, R);
		}
		else {
			R = p;
			split(t[p].ls, x, L, t[p].ls);
		}
		pushup(p);
	}
	int merge(int L, int R) {
		if(L == 0 || R == 0) return L + R;
		if(t[L].pri > t[R].pri) {
			t[L].rs = merge(t[L].rs, R);
			pushup(L);
			return L;
		}
		else {
			t[R].ls = merge(L, t[R].ls);
			pushup(R);
			return R;
		}
	}
	void insert(int x) {
		int L, R;
		split(root, x, L, R);
		newnode(x);
		root = merge(merge(L, cnt), R);
	}
	void del(int x) {
		int L, R, p;
		split(root, x, L, R);
		split(L, x - 1, L, p);
		root = merge(merge(L, merge(t[p].ls, t[p].rs)), R);
	}
	int rank(int x) {
		int L, R;
		split(root, x - 1, L, R);
		int res = t[L].sz + 1;
		root = merge(L, R);
		return res;
	}
	int kth(int now, int x) {
		if(x == t[t[now].ls].sz + 1) return t[now].val;
		if(x <= t[t[now].ls].sz) return kth(t[now].ls, x);
		if(x > t[t[now].ls].sz) return kth(t[now].rs, x - t[t[now].ls].sz - 1);
	}
	int lst(int x) {
		int L, R;
		split(root, x - 1, L, R);
		int res = kth(L, t[L].sz);
		root = merge(L, R);
		return res;
	}
	int nxt(int x) {
		int L, R;
		split(root, x, L, R);
		int res = kth(R, 1);
		root = merge(L, R);
		return res;
	}
}tr;
posted @ 2024-08-07 14:36  zeta炀  阅读(43)  评论(0)    收藏  举报