简单的码风总结

· · 个人记录

  1. 头文件的左括号前空一格,通常使用 <bits/stdc++.h> 库,有时也会使用其它 C++ 标准的头文件。

  2. 对于题目需要多次使用 long long 等较长的类型时, 在用 C++11 及以上使用形为 using ll = long long 的语句声明,而在其它情况下使用 typedef long long ll 。

  3. 一切运算符号(除括号,逗号,分号)的左右需要用一个空格符隔开,括号、逗号后用一个空格符隔开,分号后用换行符。

  4. 代码的层次之间用 tab 区分。

  5. 括号风格使用 java 风格。

  6. 数组范围较大时,会根据常用大写字母声明一个常量。

线段树求区间最值:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
using ll = long long;
const ll inf = 0xcccccccccccccccc;
ll tag[N << 2], val[N << 2], a[N], n, q;
inline ll read() {
    ll x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9') f = ch == '-' ? -1 : 1, ch = getchar();
    while(ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
    return x * f;
}
inline void pushup(ll p) {
    val[p] = max(val[p << 1], val[p << 1 | 1]);
}
inline void build(ll p, ll l, ll r) {
    tag[p] = inf;
    if(l == r) {
        val[p] = a[l];
        return;
    }
    ll mid = l + r >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
    pushup(p);
}
inline void func(ll p, ll k) {
    val[p] = tag[p] = k;
}
inline void pushdown(ll p, ll l, ll r) {
    if(tag[p] == inf) return;
    func(p << 1, tag[p]);
    func(p << 1 | 1, tag[p]);
    tag[p] = inf;
}
inline void modify(ll p, ll l, ll r, ll L, ll R, ll k) {
    if(L <= l && r <= R) {
        val[p] = tag[p] = k;
        return;
    }
    pushdown(p, l, r);
    ll mid = l + r >> 1;
    if(L <= mid) modify(p << 1, l, mid, L, R, k);
    if(R > mid) modify(p << 1 | 1, mid + 1, r, L, R, k);
    pushup(p);
}
inline ll query(ll p, ll l, ll r, ll L, ll R) {
    if(L <= l && r <= R) return val[p];
    pushdown(p, l, r);
    ll mid = l + r >> 1, res = inf;
    if(L <= mid) res = query(p << 1, l, mid, L, R);
    if(R > mid) res = max(res, query(p << 1 | 1, mid + 1, r, L, R));
    return res;
}
int main() {
    n = read(), q = read();
    for(int i = 1; i <= n; i++) a[i] = read();
    build(1, 1, n);
    while(q--) {
        ll op = read(), l = read(), r = read();
        if(op & 1) modify(1, 1, n, l, r, read());
        else printf("%lld\n", query(1, 1, n, l, r));
    }
    return 0;
}