头像

wugensheng




离线:4天前



线段树

重点

  1. 两种操作合并成一种
  2. 合并后的操作需要注意蓝标记向下传递的时候,的更新的情况
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
typedef long long ll;
int n, m, p;
int a[N];
struct node {
    int l, r;
    ll sum, mul, add;
} tr[N * 4];

void eval(node &root, int mul, int add) {
    root.sum = (root.sum * mul % p + (ll)(root.r - root.l + 1) * add) % p;
    root.mul = root.mul * mul % p;
    root.add = (root.add * mul % p + add) % p;
}

void pushup(int u) {
    tr[u].sum = (tr[u << 1].sum + tr[u << 1 | 1].sum) % p;
}

void pushdown(int u) {
    eval(tr[u << 1], tr[u].mul, tr[u].add);
    eval(tr[u << 1 | 1], tr[u].mul, tr[u].add);
    tr[u].add = 0;
    tr[u].mul = 1;
}

void build (int u, int l, int r) {
    if (l == r) tr[u] = {l, r, a[l] % p, 1, 0};
    else {
        tr[u] = {l, r, 0, 1, 0};  // 初始化,太坑了
        int mid = (l + r) >> 1;
        build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
        pushup(u);
    }
}

void modify(int u, int l, int r, int mul, int add) {
    if (tr[u].l >= l && tr[u].r <= r) {
        eval(tr[u], mul, add);
    } else {
        pushdown(u);
        int mid = (tr[u].l + tr[u].r) >> 1;
        if (l <= mid) modify(u << 1, l, r, mul, add);
        if (r > mid) modify(u << 1 | 1, l, r, mul, add);
        pushup(u);
    }
}

ll query(int u, int l, int r) {
    if (tr[u].l >= l && tr[u].r <= r) return tr[u].sum;
    pushdown(u);
    int mid = (tr[u].l + tr[u].r) >> 1;
    ll res = 0;
    if (l <= mid) res += query(u << 1, l, r) % p;
    if (r > mid) res = (res + query(u << 1 | 1, l, r)) % p;
    return res;
}

int main() {
    cin >> n >> p;
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    build(1, 1, n);
    int k, t, g, c;
    cin >> m;
    while (m--) {
        scanf("%d%d%d", &k, &t, &g);
        if (k == 1 || k == 2) {
            scanf("%d", &c);
            if (k == 1) modify(1, t, g, c, 0);
            else modify(1, t, g, 1, c);
        } else if (k == 3) {
            printf("%lld\n", query(1, t, g));
        }
    }
    return 0;
}


活动打卡代码 AcWing 1277. 维护序列

线段树

重点

  1. 两种操作合并成一种
  2. 合并后的操作需要注意蓝标记向下传递的时候,的更新的情况
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
typedef long long ll;
int n, m, p;
int a[N];
struct node {
    int l, r;
    ll sum, mul, add;
} tr[N * 4];

void eval(node &root, int mul, int add) {
    root.sum = (root.sum * mul % p + (ll)(root.r - root.l + 1) * add) % p;
    root.mul = root.mul * mul % p;
    root.add = (root.add * mul % p + add) % p;
}

void pushup(int u) {
    tr[u].sum = (tr[u << 1].sum + tr[u << 1 | 1].sum) % p;
}

void pushdown(int u) {
    eval(tr[u << 1], tr[u].mul, tr[u].add);
    eval(tr[u << 1 | 1], tr[u].mul, tr[u].add);
    tr[u].add = 0;
    tr[u].mul = 1;
}

void build (int u, int l, int r) {
    if (l == r) tr[u] = {l, r, a[l] % p, 1, 0};
    else {
        tr[u] = {l, r, 0, 1, 0};  // 初始化,太坑了
        int mid = (l + r) >> 1;
        build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
        pushup(u);
    }
}

void modify(int u, int l, int r, int mul, int add) {
    if (tr[u].l >= l && tr[u].r <= r) {
        eval(tr[u], mul, add);
    } else {
        pushdown(u);
        int mid = (tr[u].l + tr[u].r) >> 1;
        if (l <= mid) modify(u << 1, l, r, mul, add);
        if (r > mid) modify(u << 1 | 1, l, r, mul, add);
        pushup(u);
    }
}

ll query(int u, int l, int r) {
    if (tr[u].l >= l && tr[u].r <= r) return tr[u].sum;
    pushdown(u);
    int mid = (tr[u].l + tr[u].r) >> 1;
    ll res = 0;
    if (l <= mid) res += query(u << 1, l, r) % p;
    if (r > mid) res = (res + query(u << 1 | 1, l, r)) % p;
    return res;
}

int main() {
    cin >> n >> p;
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    build(1, 1, n);
    int k, t, g, c;
    cin >> m;
    while (m--) {
        scanf("%d%d%d", &k, &t, &g);
        if (k == 1 || k == 2) {
            scanf("%d", &c);
            if (k == 1) modify(1, t, g, c, 0);
            else modify(1, t, g, 1, c);
        } else if (k == 3) {
            printf("%lld\n", query(1, t, g));
        }
    }
    return 0;
}



字典树

重点
1. 区间修改用懒标记
2. 在每次分裂之前,都需要进行pushdown
3. pushdown操作,每次将本层都add向下传递,下一层的add和sum都增加,但是最开始从上向下传递终止时,本层的sum时已经累加过的,因为sum是属性需要提前维护

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
typedef long long ll;
int a[N];
int n, m;
struct node {
    int l, r;
    ll sum, add;
} tr[N * 4];

void pushup(int u) {
    tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum;
}

void pushdown(int u) {
    auto &l = tr[u << 1], &r = tr[u << 1 | 1];
    l.add += tr[u].add, l.sum += tr[u].add * (l.r - l.l + 1);
    r.add += tr[u].add, r.sum += tr[u].add * (r.r - r.l + 1);
    tr[u].add = 0;
}

void build(int u, int l, int r) {
    if (l == r) tr[u] = {l, r, a[l], 0};
    else {
        tr[u] = {l, r};
        int mid = l + r >> 1;
        build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
        pushup(u);
    }
}

void modify(int u, int l, int r, int d) {
    if (tr[u].l >= l && tr[u].r <= r) tr[u].add += d, tr[u].sum += (ll)(tr[u].r - tr[u].l + 1) * d;
    else {
        pushdown(u);  //这里不可以省略,每次分裂之前都要向下传递
        int mid = tr[u].l + tr[u].r >> 1;
        if (l <= mid) modify(u << 1, l, r, d);
        if (r > mid) modify(u << 1 | 1, l, r, d);
        pushup(u);
    }
}

ll query(int u, int l, int r) {
    if (tr[u].l >= l && tr[u].r <= r) return tr[u].sum;
    else {
        pushdown(u);
        int mid = tr[u].l + tr[u].r >> 1;
        ll sum = 0;
        if (l <= mid) sum += query(u << 1, l, r);
        if (r > mid) sum += query(u << 1 | 1, l, r);
        //pushup(u);
        return sum;
    }
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);

    build(1, 1, n);

    char s[2];
    int l, r, d;
    while (m--) {
        scanf("%s%d%d", s, &l, &r);
        if (*s == 'C') {
            scanf("%d", &d);
            modify(1, l, r, d);
        } else {
            printf("%lld\n", query(1, l, r));   
        }
    }

    return 0;
}



字典树

重点
1. 区间修改用懒标记
2. 在每次分裂之前,都需要进行pushdown
3. pushdown操作,每次将本层都add向下传递,下一层的add和sum都增加,但是最开始从上向下传递终止时,本层的sum时已经累加过的,因为sum是属性需要提前维护

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
typedef long long ll;
int a[N];
int n, m;
struct node {
    int l, r;
    ll sum, add;
} tr[N * 4];

void pushup(int u) {
    tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum;
}

void pushdown(int u) {
    auto &l = tr[u << 1], &r = tr[u << 1 | 1];
    l.add += tr[u].add, l.sum += tr[u].add * (l.r - l.l + 1);
    r.add += tr[u].add, r.sum += tr[u].add * (r.r - r.l + 1);
    tr[u].add = 0;
}

void build(int u, int l, int r) {
    if (l == r) tr[u] = {l, r, a[l], 0};
    else {
        tr[u] = {l, r};
        int mid = l + r >> 1;
        build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
        pushup(u);
    }
}

void modify(int u, int l, int r, int d) {
    if (tr[u].l >= l && tr[u].r <= r) tr[u].add += d, tr[u].sum += (ll)(tr[u].r - tr[u].l + 1) * d;
    else {
        pushdown(u);  //这里不可以省略,每次分裂之前都要向下传递
        int mid = tr[u].l + tr[u].r >> 1;
        if (l <= mid) modify(u << 1, l, r, d);
        if (r > mid) modify(u << 1 | 1, l, r, d);
        pushup(u);
    }
}

ll query(int u, int l, int r) {
    if (tr[u].l >= l && tr[u].r <= r) return tr[u].sum;
    else {
        pushdown(u);
        int mid = tr[u].l + tr[u].r >> 1;
        ll sum = 0;
        if (l <= mid) sum += query(u << 1, l, r);
        if (r > mid) sum += query(u << 1 | 1, l, r);
        //pushup(u);
        return sum;
    }
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);

    build(1, 1, n);

    char s[2];
    int l, r, d;
    while (m--) {
        scanf("%s%d%d", s, &l, &r);
        if (*s == 'C') {
            scanf("%d", &d);
            modify(1, l, r, d);
        } else {
            printf("%lld\n", query(1, l, r));   
        }
    }

    return 0;
}



线段树

重点
1. 根据题目分析,节点需要添加的新的信息,新信息如何维护,继续思考,直到全部信息可知
2. query的时候,需要分情况,因为查询的东西比较特殊,需要利用返回值

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 500010, M = 100010;
int a[N];
int n, m;
struct Node {
    int l, r;
    int sum, lmax, rmax, tmax;
} tr[N * 4];

void pushup(Node &u, Node &l, Node &r){
    u.sum = l.sum + r.sum;
    u.lmax = max(l.lmax, l.sum + r.lmax);
    u.rmax = max(r.rmax, r.sum + l.rmax);
    u.tmax = max(max(l.tmax, r.tmax), l.rmax + r.lmax);
}

void pushup(int u) {
    pushup(tr[u], tr[u << 1], tr[u << 1 | 1]);
}

void build(int u, int l, int r) {
    if (l == r) tr[u] = {l, r, a[l], a[l], a[l], a[l]};
    else {
        tr[u] = {l, r};
        int mid = l + r >> 1;
        build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
        pushup(u);
    }
}

void modify(int u, int x, int v) {
    if (tr[u].l == x && tr[u].r == x) tr[u] = {x, x, v, v, v, v};
    else {
        int mid = tr[u].l + tr[u].r >> 1;
        if (x <= mid) modify(u << 1, x, v);
        else modify(u << 1 | 1, x, v);
        pushup(u);
    }
}

Node query(int u, int l, int r) {
    if (tr[u].l >= l && tr[u].r <= r) return tr[u];
    else {
        int mid = tr[u].l + tr[u].r >> 1;
        if (r <= mid) return query(u << 1, l, r);
        else if (l > mid) return query(u << 1 | 1, l, r);
        else {
            auto lnode = query(u << 1, l, r);
            auto rnode = query(u << 1 | 1, l, r);
            Node res;
            pushup(res, lnode, rnode);
            return res;
        }
    }
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
    }
    build(1, 1, n);

    int x, y, k;
    while (m--) {
        scanf("%d%d%d", &k, &x, &y);
        if (k == 1) {
            if (x > y) swap(x, y);
            auto t = query(1, x, y);
            printf("%d\n", t.tmax);
        } else {
            modify(1, x, y);
        }
    }
    return 0;
}



线段树

重点
1. 根据题目分析,节点需要添加的新的信息,新信息如何维护,继续思考,直到全部信息可知
2. query的时候,需要分情况,因为查询的东西比较特殊,需要利用返回值

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 500010, M = 100010;
int a[N];
int n, m;
struct Node {
    int l, r;
    int sum, lmax, rmax, tmax;
} tr[N * 4];

void pushup(Node &u, Node &l, Node &r){
    u.sum = l.sum + r.sum;
    u.lmax = max(l.lmax, l.sum + r.lmax);
    u.rmax = max(r.rmax, r.sum + l.rmax);
    u.tmax = max(max(l.tmax, r.tmax), l.rmax + r.lmax);
}

void pushup(int u) {
    pushup(tr[u], tr[u << 1], tr[u << 1 | 1]);
}

void build(int u, int l, int r) {
    if (l == r) tr[u] = {l, r, a[l], a[l], a[l], a[l]};
    else {
        tr[u] = {l, r};
        int mid = l + r >> 1;
        build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
        pushup(u);
    }
}

void modify(int u, int x, int v) {
    if (tr[u].l == x && tr[u].r == x) tr[u] = {x, x, v, v, v, v};
    else {
        int mid = tr[u].l + tr[u].r >> 1;
        if (x <= mid) modify(u << 1, x, v);
        else modify(u << 1 | 1, x, v);
        pushup(u);
    }
}

Node query(int u, int l, int r) {
    if (tr[u].l >= l && tr[u].r <= r) return tr[u];
    else {
        int mid = tr[u].l + tr[u].r >> 1;
        if (r <= mid) return query(u << 1, l, r);
        else if (l > mid) return query(u << 1 | 1, l, r);
        else {
            auto lnode = query(u << 1, l, r);
            auto rnode = query(u << 1 | 1, l, r);
            Node res;
            pushup(res, lnode, rnode);
            return res;
        }
    }
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
    }
    build(1, 1, n);

    int x, y, k;
    while (m--) {
        scanf("%d%d%d", &k, &x, &y);
        if (k == 1) {
            if (x > y) swap(x, y);
            auto t = query(1, x, y);
            printf("%d\n", t.tmax);
        } else {
            modify(1, x, y);
        }
    }
    return 0;
}



线段树

重点
1. build,modify,query,pushup,pushdown五种操作
2. 建树的时候,区间大小是确定的,只需要记住区间长度即可

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 200010;
int n, m, p;
struct node {
    int l, r;
    int v;
} tr[N << 2];

void build(int u, int l, int r) {
    tr[u] = {l, r};
    if (l == r) return ;
    int mid = l + r >> 1;
    build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
}

int query(int u, int l, int r) {  // 从u结点开始向下找,在区间l,r中的最大值
    if (tr[u].l >= l && tr[u].r <= r) return tr[u].v;
    int mid = tr[u].l + tr[u].r >> 1;
    int v = 0;
    if (l <= mid) v = query(u << 1, l, r);
    if (r > mid) v = max(v, query(u << 1 | 1, l, r));
    return v;
}

void pushup(int u) {
    tr[u].v = max(tr[u << 1].v, tr[u << 1 | 1].v);
}

void modify(int u, int x, int v) {  //从u节点开始向下找序号为x的叶子结点
    if (tr[u].l == x && tr[u].r == x) tr[u].v = v;
    else {
        int mid = tr[u].l + tr[u].r >> 1;
        if (x <= mid) modify(u << 1, x, v);
        else modify(u << 1 | 1, x, v);
        pushup(u);
    }
}

int main() {
    cin >> m >> p;
    build(1, 1, m);

    char s[2];
    int l, t, last;
    while (m--) {
        scanf("%s", s);
        if (*s == 'Q') {
            scanf("%d", &l);
            last = query(1, n - l + 1, n);
            printf("%d\n", last);
        } else {
            scanf("%d", &t);
            modify(1, n + 1, (t + last) % p);
            n++;
        }
    }


    return 0;
}


活动打卡代码 AcWing 1275. 最大数

线段树

重点
1. build,modify,query,pushup,pushdown五种操作
2. 建树的时候,区间大小是确定的,只需要记住区间长度即可

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 200010;
int n, m, p;
struct node {
    int l, r;
    int v;
} tr[N << 2];

void build(int u, int l, int r) {
    tr[u] = {l, r};
    if (l == r) return ;
    int mid = l + r >> 1;
    build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
}

int query(int u, int l, int r) {  // 从u结点开始向下找,在区间l,r中的最大值
    if (tr[u].l >= l && tr[u].r <= r) return tr[u].v;
    int mid = tr[u].l + tr[u].r >> 1;
    int v = 0;
    if (l <= mid) v = query(u << 1, l, r);
    if (r > mid) v = max(v, query(u << 1 | 1, l, r));
    return v;
}

void pushup(int u) {
    tr[u].v = max(tr[u << 1].v, tr[u << 1 | 1].v);
}

void modify(int u, int x, int v) {  //从u节点开始向下找序号为x的叶子结点
    if (tr[u].l == x && tr[u].r == x) tr[u].v = v;
    else {
        int mid = tr[u].l + tr[u].r >> 1;
        if (x <= mid) modify(u << 1, x, v);
        else modify(u << 1 | 1, x, v);
        pushup(u);
    }
}

int main() {
    cin >> m >> p;
    build(1, 1, m);

    char s[2];
    int l, t, last;
    while (m--) {
        scanf("%s", s);
        if (*s == 'Q') {
            scanf("%d", &l);
            last = query(1, n - l + 1, n);
            printf("%d\n", last);
        } else {
            scanf("%d", &t);
            modify(1, n + 1, (t + last) % p);
            n++;
        }
    }


    return 0;
}



树状数组

重点
1. 两种操作:单点修改,区间求和,维护一个前缀和序列
2. 想到了如何求牛的身高,但是没能看透两种操作的本质

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
using namespace std;
const int N = 100010;
int n;
int tr[N], a[N], b[N];

int lowbit(int x) {
    return x & -x;
}

void add(int x, int c) {
    for (int i = x; i <= n; i += lowbit(i)) tr[i] += c;
}

int sum(int x) {
    int res = 0;
    for (int i = x; i; i -= lowbit(i)) res += tr[i];
    return res;
}

int main() {
    cin >> n;
    a[1] = 0;
    for (int i = 2; i <= n; i++) {
        scanf("%d", &a[i]);
    }
    for (int i = 1; i <= n; i++) {
        add(i, 1);
    }
    for (int i = n; i; i--) {
        int l = 1, r = n; 
        while (l < r) {
            int mid = (l + r) >> 1;
            if (sum(mid) >= a[i] + 1) r = mid;
            else l = mid + 1;
        }
        b[i] = l;
        add(l, -1);
    }
    for (int i = 1; i <= n; i++) printf("%d\n", b[i]);

    return 0;
}


活动打卡代码 AcWing 244. 谜一样的牛

树状数组

重点
1. 两种操作:单点修改,区间求和,维护一个前缀和序列
2. 想到了如何求牛的身高,但是没能看透两种操作的本质

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
using namespace std;
const int N = 100010;
int n;
int tr[N], a[N], b[N];

int lowbit(int x) {
    return x & -x;
}

void add(int x, int c) {
    for (int i = x; i <= n; i += lowbit(i)) tr[i] += c;
}

int sum(int x) {
    int res = 0;
    for (int i = x; i; i -= lowbit(i)) res += tr[i];
    return res;
}

int main() {
    cin >> n;
    a[1] = 0;
    for (int i = 2; i <= n; i++) {
        scanf("%d", &a[i]);
    }
    for (int i = 1; i <= n; i++) {
        add(i, 1);
    }
    for (int i = n; i; i--) {
        int l = 1, r = n; 
        while (l < r) {
            int mid = (l + r) >> 1;
            if (sum(mid) >= a[i] + 1) r = mid;
            else l = mid + 1;
        }
        b[i] = l;
        add(l, -1);
    }
    for (int i = 1; i <= n; i++) printf("%d\n", b[i]);

    return 0;
}