Brine
A certain person trying to make his way in the universe.
22527 鄭竣陽
Brine
BrineTW#7355
Index
Sparse Table
struct SparseTable {
vector< vector<int> > v;
SparseTable(int N): v(__lg(N) + 1, vector<int>(N)) {
for (auto& n: v[0]) cin >> n;
for (int i = 0, p = 1; i < __lg(N); i++, p <<= 1) {
for (int j = 0; j + p < N; j++) {
v[i + 1][j] = max(v[i][j], v[i][j + p]);
}
}
}
int query(int l, int r) {
int d = __lg(r - l);
return max(v[d][l], v[d][r - (1 << d)]);
}
};
Segment Tree
#define lc(i) (i << 1)
#define rc(i) (i << 1 ^ 1)
typedef long long ll;
struct SegmentTree {
vector<ll> v;
SegmentTree(int N): v(4 << __lg(N)) {
int L = 2 << __lg(N);
for (int i = L; i < L + N; i++) cin >> v[i];
for (int i = L - 1; i > 0; i--) {
v[i] = v[lc(i)] + v[rc(i)];
}
}
void modify(int mi, ll d, int l, int r, int i = 1) {
if (mi <= l && r <= mi) {
v[i] += d;
return;
}
int m = l + r >> 1;
if (mi < m) modify(mi, d, l, m, lc(i));
if (m < mi) modify(mi, d, m, r, rc(i));
v[i] = v[lc(i)] + v[rc(i)];
}
ll query(int ql, int qr, int l, int r, int i = 1) {
if (ql <= l && r <= qr) return v[i];
int m = l + r >> 1;
ll ans = 0;
if (ql < m) ans += query(ql, qr, l, m, lc(i));
if (m < qr) ans += query(ql, qr, m, r, rc(i));
return ans;
}
};
#define lc(i) (i<<1)
#define rc(i) (i<<1^1)
typedef long long ll;
struct SegmentTree {
int L;
vector<ll> v;
SegmentTree(int N): L(N), v(L << 1) {
for (int i = N; i < (N << 1); i++) cin >> v[i];
for (int i = N - 1; i > 0; i--) {
v[i] = v[lc(i)] + v[rc(i)];
}
}
void modify(int i, ll d) {
for (i += L; i; i >>= 1) v[i] += d;
}
ll query(int l, int r) {
ll sum = 0;
for (l += L, r += L; l < r; l >>= 1, r >>= 1) {
if (l & 1) sum += v[l++];
if (r & 1) sum += v[--r];
}
return sum;
}
};
Lazy Propagation
#define lc(i) (i << 1)
#define rc(i) (i << 1 ^ 1)
using namespace std;
typedef long long ll;
struct SegmentTree {
int L;
vector<ll> v, tag;
SegmentTree(int N): L(2 << __lg(N)), v(L << 1), tag(L << 1) {
for (int i = L; i < L + N; i++) cin >> v[i];
for (int i = L - 1; i > 0; i--) {
v[i] = v[lc(i)] + v[rc(i)];
}
}
void push(int l, int r, int i) {
int m = l + r >> 1;
tag[lc(i)] += tag[i];
tag[rc(i)] += tag[i];
v[lc(i)] += (m - l) * tag[i];
v[rc(i)] += (r - m) * tag[i];
tag[i] = 0;
}
void pull(int i) {
v[i] = v[lc(i)] + v[rc(i)];
}
void modify(int ml, int mr, ll d, int l, int r, int i = 1) {
if (ml <= l && r <= mr) {
v[i] += (r - l) * d;
tag[i] += d;
return;
}
int m = l + r >> 1;
push(l, r, i);
if (ml < m) modify(ml, mr, d, l, m, lc(i));
if (m < mr) modify(ml, mr, d, m, r, rc(i));
pull(i);
}
ll query(int ql, int qr, int l, int r, int i = 1) {
if (ql <= l && r <= qr) return v[i];
int m = l + r >> 1;
ll ans = 0;
push(l, r, i);
if (ql < m) ans += query(ql, qr, l, m, lc(i));
if (m < qr) ans += query(ql, qr, m, r, rc(i));
pull(i);
return ans;
}
};
#define lc(i) (i<<1)
#define rc(i) (i<<1^1)
typedef long long ll;
struct SegmentTree {
int L;
vector<ll> v, sz, tag;
SegmentTree(int N): L(N), v(L << 1), sz(L << 1, 1), tag(L << 1) {
for (int i = N; i < (N << 1); i++) cin >> v[i];
for (int i = N - 1; i > 0; i--) {
v[i] = v[lc(i)] + v[rc(i)];
sz[i] = sz[lc(i)] + sz[rc(i)];
}
}
void update(int i, ll d) {
v[i] += d * sz[i];
tag[i] += d;
}
void pull(int i) {
for (int h = __lg(L) + 1; h; h--) {
int p = i >> h;
if (tag[p]) {
update(lc(p), tag[p]);
update(rc(p), tag[p]);
tag[p] = 0;
}
}
}
void push(int i) {
for (i >>= 1; i; i >>= 1) {
v[i] = v[lc(i)] + v[rc(i)] + tag[i] * sz[i];
}
}
void modify(int l, int r, ll d) {
int ll = l + L, rr = r + L - 1;
pull(ll), pull(rr);
for (l += L, r += L; l < r; l >>= 1, r >>= 1) {
if (l & 1) update(l++, d);
if (r & 1) update(--r, d);
}
push(ll), push(rr);
}
ll query(int l, int r) {
ll sum = 0;
pull(l + L), pull(r + L - 1);
for (l += L, r += L; l < r; l >>= 1, r >>= 1) {
if (l & 1) sum += v[l++];
if (r & 1) sum += v[--r];
}
return sum;
}
};
沒了
By Brine
演算法[15] 稀疏表、懶標線段樹